Skip to content

Commit

Permalink
Merge pull request #46 from luthermonson/update-int-tests
Browse files Browse the repository at this point in the history
fixing integration tests
  • Loading branch information
luthermonson authored Jan 15, 2023
2 parents 230b7e6 + 8486a24 commit 5631122
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestCov() error {

func TestIntegration() error {
fmt.Println("Running Integration Tests against a PVE Cluster...")
return sh.RunV("go", "test", "./tests/integration", "-tags", "\"nodes containers vms\"")
return sh.RunV("go", "test", "./tests/integration", "-tags", "nodes containers vms")
}

// validate all env vars to run the testing suite
Expand Down
2 changes: 1 addition & 1 deletion storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ func deleteVolume(c *Client, n, s, v, p, t string) (*Task, error) {
v = fmt.Sprintf("%s:%s/%s", s, t, filepath.Base(p))
}

err := c.Delete(fmt.Sprintf("/nodes/%s/storage/%s/content/%s?delay=5", n, s, v), &upid)
err := c.Delete(fmt.Sprintf("/nodes/%s/storage/%s/content/%s", n, s, v), &upid)
return NewTask(upid, c), err
}
6 changes: 3 additions & 3 deletions tests/integration/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
func TestLogin(t *testing.T) {
client := ClientFromEnv()
_, err := client.Version()
assert.Equal(t, err, proxmox.ErrNotAuthorized)
assert.True(t, proxmox.IsNotAuthorized(err))

err = client.Login(td.username, td.password)
assert.Nil(t, err)

version, err := client.Version()
assert.Nil(t, err)
assert.NotEmpty(t, version.Version)
Expand All @@ -23,7 +23,7 @@ func TestLogin(t *testing.T) {
func TestAPIToken(t *testing.T) {
client := ClientFromEnv()
_, err := client.Version()
assert.Equal(t, err, proxmox.ErrNotAuthorized)
assert.True(t, proxmox.IsNotAuthorized(err))

client.APIToken(td.tokenID, td.secret)
version, err := client.Version()
Expand Down
25 changes: 12 additions & 13 deletions tests/integration/proxmox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ type TestingData struct {
storage *proxmox.Storage
appliance *proxmox.Appliance

username string
password string
tokenID string
secret string
otp string
nodeName string
nodeStorage string
appliancePrefix string
isoURL string
username string
password string
tokenID string
secret string
otp string
nodeName string
nodeStorage string
isoURL string
}

var (
Expand All @@ -45,8 +44,9 @@ var (
},
}

tinycoreURL = "https://github.com/luthermonson/go-proxmox/releases/download/tests/tinycore.iso"
ubuntuURL = "https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso"
tinycoreURL = "https://github.com/luthermonson/go-proxmox/releases/download/tests/tinycore.iso"
ubuntuURL = "https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso"
alpineAppliance = "http://download.proxmox.com/images/system/alpine-3.17-default_20221129_amd64.tar.xz"
)

func init() {
Expand All @@ -57,8 +57,7 @@ func init() {
td.secret = os.Getenv("PROXMOX_SECRET")
td.nodeName = os.Getenv("PROXMOX_NODE_NAME")
td.nodeStorage = os.Getenv("PROXMOX_NODE_STORAGE")
td.appliancePrefix = os.Getenv("PROXMOX_APPLIANCE_PREFIX") // alpine-3.14-default_20210623_amd64.tar.xz
td.isoURL = os.Getenv("PROXMOX_ISO_URL") // https://dl-cdn.alpinelinux.org/alpine/v3.14/releases/x86_64/alpine-virt-3.14.1-x86_64.iso
td.isoURL = os.Getenv("PROXMOX_ISO_URL") // https://dl-cdn.alpinelinux.org/alpine/v3.14/releases/x86_64/alpine-virt-3.14.1-x86_64.iso

if td.nodeName == "" {
return
Expand Down
12 changes: 8 additions & 4 deletions tests/integration/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ func TestStorage_VzTmpl(t *testing.T) {
_, err := td.storage.VzTmpl("doesnt-exist")
assert.Contains(t, err.Error(), "unable to parse directory volume name 'vztmpl/doesnt-exist'")

assert.NotNil(t, td.appliance)
vztmpl, err := td.storage.VzTmpl(td.appliance.Template)
name := nameGenerator(12) + ".tar.xz"
task, err := td.storage.DownloadURL("vztmpl", name, alpineAppliance)
assert.Nil(t, err)
assert.True(t, strings.HasSuffix(vztmpl.Path, td.appliance.Template))
task.Wait(1*time.Second, 5*time.Second)

vztmpl, err := td.storage.VzTmpl(name)
assert.Nil(t, err)
assert.True(t, strings.HasSuffix(vztmpl.Path, "tar.xz"))

task, err := vztmpl.Delete()
task, err = vztmpl.Delete()
assert.Nil(t, err)
task.Wait(1*time.Second, 15*time.Second)
}
4 changes: 2 additions & 2 deletions tests/integration/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestTask(t *testing.T) {
// test ping and wait, big iso should take more than 15s
go func() {
timeout := task.Wait(time.Duration(5*time.Second), time.Duration(30*time.Second))
assert.Contains(t, timeout.Error(), "timeout while waiting for task")
assert.True(t, proxmox.IsTimeout(timeout))
assert.Nil(t, task.Stop())
}()

Expand All @@ -83,7 +83,7 @@ func TestTask(t *testing.T) {
watch = nil
break
}
task.client.log.Debugf("%s", ln)
logger.Debugf("%s", ln)
}
if watch == nil {
break
Expand Down
24 changes: 13 additions & 11 deletions tests/integration/virtual_machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package integration

import (
"fmt"
"path/filepath"
"strings"
"testing"
"time"
Expand All @@ -16,7 +17,7 @@ import (
)

// NewVirtualMachine fixture to download a tinycore iso and returns a vm, make sure you defer the cleanup if you use it
func NewVirtualMachine(t *testing.T, name string) (*proxmox.VirtualMachine, error) {
func NewVirtualMachine(t *testing.T, name string) *proxmox.VirtualMachine {
client := ClientFromLogins()
node, err := client.Node(td.nodeName)
require.NoError(t, err)
Expand Down Expand Up @@ -48,34 +49,35 @@ func NewVirtualMachine(t *testing.T, name string) (*proxmox.VirtualMachine, erro

require.NoError(t, err)
require.NoError(t, task.Wait(1*time.Second, 10*time.Second))
return nil, nil

return vm
}

func CleanupVirtualMachine(t *testing.T, vm *proxmox.VirtualMachine) {
if vm.VirtualMachineConfig != nil && vm.VirtualMachineConfig.Ide2 != "" {
s := strings.Split(vm.VirtualMachineConfig.Ide2, ",")
task, err := vm.Stop()
require.NoError(t, err)
require.NoError(t, task.Wait(1*time.Second, 30*time.Second))

if vm.VirtualMachineConfig != nil && vm.VirtualMachineConfig.IDE2 != "" {
s := strings.Split(vm.VirtualMachineConfig.IDE2, ",")
if len(s) > 2 {
iso, err := td.storage.ISO(s[0])
iso, err := td.storage.ISO(filepath.Base(s[0]))
assert.Nil(t, err)
task, err := iso.Delete()
require.NoError(t, err)
require.NoError(t, task.Wait(1*time.Second, 10*time.Second))
}
}

task, err := vm.Stop()
require.NoError(t, err)
require.NoError(t, task.Wait(1*time.Second, 30*time.Second))

task, err = vm.Delete()
require.NoError(t, err)
require.NoError(t, task.Wait(1*time.Second, 30*time.Second))
}

func TestNode_NewVirtualMachine(t *testing.T) {
testname := nameGenerator(12)
vm, err := NewVirtualMachine(t, testname)
require.NoError(t, err)
vm := NewVirtualMachine(t, testname)
require.NotNil(t, vm)
defer CleanupVirtualMachine(t, vm)

// Start
Expand Down

0 comments on commit 5631122

Please sign in to comment.