Skip to content

Commit

Permalink
Merge pull request #258 from rf152/test-cloudinit
Browse files Browse the repository at this point in the history
Test Cloud-init
  • Loading branch information
mleone87 committed May 14, 2023
2 parents 2a02bab + c8054cd commit 62dbebe
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions scripts/vagrant-get-cloudinit-template.sh
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions test/api/CloudInit/cloudinit_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
59 changes: 59 additions & 0 deletions test/api/CloudInit/shared_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package api_test

import (
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
)

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
}
4 changes: 2 additions & 2 deletions test/api/Test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api_test

import (
"crypto/tls"

pxapi "github.com/Telmate/proxmox-api-go/proxmox"
)

Expand All @@ -13,7 +14,7 @@ type Test struct {
HttpHeaders string
RequireSSL bool

_client *pxapi.Client
_client *pxapi.Client
}

func (test *Test) CreateClient() (err error) {
Expand All @@ -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()
Expand Down

0 comments on commit 62dbebe

Please sign in to comment.