From 2447a8748faf749c52c7a609665723b1169c02e3 Mon Sep 17 00:00:00 2001 From: Richard Franks Date: Thu, 4 May 2023 12:05:12 +0100 Subject: [PATCH 1/3] Test Cloud-init At the moment it is very basic testing --- Vagrantfile | 6 +++ scripts/vagrant-get-cloudinit-template.sh | 3 ++ test/api/CloudInit/cloudinit_test.go | 55 +++++++++++++++++++ test/api/CloudInit/shared_test.go | 65 +++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 scripts/vagrant-get-cloudinit-template.sh create mode 100644 test/api/CloudInit/cloudinit_test.go create mode 100644 test/api/CloudInit/shared_test.go diff --git a/Vagrantfile b/Vagrantfile index 017f33f3..b27db063 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -22,6 +22,12 @@ Vagrant.configure("2") do |config| path: './scripts/vagrant-get-container-template.sh', run: "always" + config.vm.provision "Download Cloud-Init Template", + type: "shell", + privileged: true, + path: './scripts/vagrant-get-cloudinit-template.sh', + run: "always" + config.vm.provider :virtualbox do |vb| vb.memory = 2048 vb.cpus = 2 diff --git a/scripts/vagrant-get-cloudinit-template.sh b/scripts/vagrant-get-cloudinit-template.sh new file mode 100644 index 00000000..2e734d08 --- /dev/null +++ b/scripts/vagrant-get-cloudinit-template.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +wget -O /tmp/jammy-server-cloudimg-amd64.img https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img \ No newline at end of file diff --git a/test/api/CloudInit/cloudinit_test.go b/test/api/CloudInit/cloudinit_test.go new file mode 100644 index 00000000..68c5a275 --- /dev/null +++ b/test/api/CloudInit/cloudinit_test.go @@ -0,0 +1,55 @@ +package api_test + +import ( + "testing" + + pxapi "github.com/Telmate/proxmox-api-go/proxmox" + api_test "github.com/Telmate/proxmox-api-go/test/api" + "github.com/stretchr/testify/require" +) + +func Test_Cloud_Init_VM(t *testing.T) { + Test := api_test.Test{} + _ = Test.CreateTest() + config := _create_vm_spec(true) + vmref := _create_vmref() + + // Create network + configNetwork := _create_network_spec() + + err := configNetwork.CreateNetwork(Test.GetClient()) + require.NoError(t, err) + _, err = Test.GetClient().ApplyNetwork("pve") + require.NoError(t, err) + + disk := make(map[string]interface{}) + disk["import-from"] = "/tmp/jammy-server-cloudimg-amd64.img" + disk["type"] = "virtio" + disk["storage"] = "local" + + config.QemuDisks[0] = disk + config.Name = "Base-Image" + + err = config.CreateVm(vmref, Test.GetClient()) + require.NoError(t, err) + + config.Ipconfig = pxapi.IpconfigMap{} + config.Boot = "order=virtio0;ide2;net0" + + config.Ipconfig[0] = "gw=10.0.0.1,ip=10.0.0.2/24" + + err = config.UpdateConfig(vmref, Test.GetClient()) + require.NoError(t, err) + + testConfig, _ := pxapi.NewConfigQemuFromApi(vmref, Test.GetClient()) + + require.Equal(t, testConfig.Ipconfig[0], "gw=10.0.0.1,ip=10.0.0.2/24") + + _, err = Test.GetClient().DeleteVm(vmref) + require.NoError(t, err) + + _, err = Test.GetClient().DeleteNetwork("pve", "vmbr0") + require.NoError(t, err) + _, err = Test.GetClient().ApplyNetwork("pve") + require.NoError(t, err) +} diff --git a/test/api/CloudInit/shared_test.go b/test/api/CloudInit/shared_test.go new file mode 100644 index 00000000..abe34d8c --- /dev/null +++ b/test/api/CloudInit/shared_test.go @@ -0,0 +1,65 @@ +package api_test + +import ( + pxapi "github.com/Telmate/proxmox-api-go/proxmox" +) + +func _create_basevmref() (ref *pxapi.VmRef) { + ref = pxapi.NewVmRef(100) + ref.SetNode("pve") + ref.SetVmType("qemu") + return ref +} +func _create_vmref() (ref *pxapi.VmRef) { + ref = pxapi.NewVmRef(101) + ref.SetNode("pve") + ref.SetVmType("qemu") + return ref +} + +func _create_vm_spec(network bool) pxapi.ConfigQemu { + + disks := make(pxapi.QemuDevices) + + networks := make(pxapi.QemuDevices) + if network { + networks[0] = make(map[string]interface{}) + networks[0]["bridge"] = "vmbr0" + networks[0]["firewall"] = "true" + networks[0]["id"] = "0" + networks[0]["macaddr"] = "B6:8F:9D:7C:8F:BC" + networks[0]["model"] = "virtio" + } + + config := pxapi.ConfigQemu{ + Name: "test-qemu01", + Bios: "seabios", + Tablet: pxapi.PointerBool(true), + Memory: 2048, + QemuOs: "l26", + QemuCores: 1, + QemuSockets: 1, + QemuCpu: "kvm64", + QemuNuma: pxapi.PointerBool(false), + QemuKVM: pxapi.PointerBool(true), + Hotplug: "network,disk,usb", + QemuNetworks: networks, + QemuIso: "none", + Boot: "order=ide2;net0", + Scsihw: "virtio-scsi-pci", + QemuDisks: disks, + } + + return config +} + +func _create_network_spec() pxapi.ConfigNetwork { + config := pxapi.ConfigNetwork{ + Type: "bridge", + Iface: "vmbr0", + Node: "pve", + Autostart: true, + } + + return config +} From 517592649f5de5d65dbae462c8eef50371a3de14 Mon Sep 17 00:00:00 2001 From: Richard Franks Date: Fri, 5 May 2023 09:13:49 +0100 Subject: [PATCH 2/3] Removed unused function --- test/api/Test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/api/Test.go b/test/api/Test.go index d1171a5f..e09b7fcb 100644 --- a/test/api/Test.go +++ b/test/api/Test.go @@ -2,6 +2,7 @@ package api_test import ( "crypto/tls" + pxapi "github.com/Telmate/proxmox-api-go/proxmox" ) @@ -13,12 +14,12 @@ type Test struct { HttpHeaders string RequireSSL bool - _client *pxapi.Client + _client *pxapi.Client } func (test *Test) CreateClient() (err error) { if test.APIurl == "" { - test.APIurl = "https://127.0.0.1:8006/api2/json" + test.APIurl = "https://172.26.8.219:8006/api2/json" } if test.UserID == "" { test.UserID = "root@pam" @@ -40,7 +41,6 @@ func (test *Test) GetClient() (client *pxapi.Client) { return test._client } - func (test *Test) Login() (err error) { if test._client == nil { err = test.CreateClient() From c8054cdacc0925b8c4648b48ec82d5b8f50927e9 Mon Sep 17 00:00:00 2001 From: Richard Franks Date: Fri, 5 May 2023 09:31:07 +0100 Subject: [PATCH 3/3] . --- test/api/CloudInit/shared_test.go | 6 ------ test/api/Test.go | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/test/api/CloudInit/shared_test.go b/test/api/CloudInit/shared_test.go index abe34d8c..ecdebb3a 100644 --- a/test/api/CloudInit/shared_test.go +++ b/test/api/CloudInit/shared_test.go @@ -4,12 +4,6 @@ import ( pxapi "github.com/Telmate/proxmox-api-go/proxmox" ) -func _create_basevmref() (ref *pxapi.VmRef) { - ref = pxapi.NewVmRef(100) - ref.SetNode("pve") - ref.SetVmType("qemu") - return ref -} func _create_vmref() (ref *pxapi.VmRef) { ref = pxapi.NewVmRef(101) ref.SetNode("pve") diff --git a/test/api/Test.go b/test/api/Test.go index e09b7fcb..6a71fa8a 100644 --- a/test/api/Test.go +++ b/test/api/Test.go @@ -19,7 +19,7 @@ type Test struct { func (test *Test) CreateClient() (err error) { if test.APIurl == "" { - test.APIurl = "https://172.26.8.219:8006/api2/json" + test.APIurl = "https://127.0.0.1:8006/api2/json" } if test.UserID == "" { test.UserID = "root@pam"