Add some acceptance tests for iso builder

This commit is contained in:
Andrei Tonkikh 2018-01-30 19:48:32 +03:00
parent 76a09d206e
commit ee6192f1c1
4 changed files with 181 additions and 48 deletions

View File

@ -5,7 +5,6 @@ import (
commonT "github.com/jetbrains-infra/packer-builder-vsphere/common/testing" commonT "github.com/jetbrains-infra/packer-builder-vsphere/common/testing"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"testing" "testing"
"github.com/jetbrains-infra/packer-builder-vsphere/common" "github.com/jetbrains-infra/packer-builder-vsphere/common"
) )
@ -38,8 +37,8 @@ func defaultConfig() map[string]interface{} {
func checkDefault(t *testing.T, name string, host string, datastore string) builderT.TestCheckFunc { func checkDefault(t *testing.T, name string, host string, datastore string) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error { return func(artifacts []packer.Artifact) error {
d := testConn(t) d := commonT.TestConn(t)
vm := getVM(t, d, artifacts) vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("name", "parent", "runtime.host", "resourcePool", "datastore", "layoutEx.disk") vmInfo, err := vm.Info("name", "parent", "runtime.host", "resourcePool", "datastore", "layoutEx.disk")
if err != nil { if err != nil {
@ -137,8 +136,8 @@ func folderConfig() string {
func checkFolder(t *testing.T, folder string) builderT.TestCheckFunc { func checkFolder(t *testing.T, folder string) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error { return func(artifacts []packer.Artifact) error {
d := testConn(t) d := commonT.TestConn(t)
vm := getVM(t, d, artifacts) vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("parent") vmInfo, err := vm.Info("parent")
if err != nil { if err != nil {
@ -175,8 +174,8 @@ func resourcePoolConfig() string {
func checkResourcePool(t *testing.T, pool string) builderT.TestCheckFunc { func checkResourcePool(t *testing.T, pool string) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error { return func(artifacts []packer.Artifact) error {
d := testConn(t) d := commonT.TestConn(t)
vm := getVM(t, d, artifacts) vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("resourcePool") vmInfo, err := vm.Info("resourcePool")
if err != nil { if err != nil {
@ -212,8 +211,8 @@ func datastoreConfig() string {
func checkDatastore(t *testing.T, name string) builderT.TestCheckFunc { func checkDatastore(t *testing.T, name string) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error { return func(artifacts []packer.Artifact) error {
d := testConn(t) d := commonT.TestConn(t)
vm := getVM(t, d, artifacts) vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("datastore") vmInfo, err := vm.Info("datastore")
if err != nil { if err != nil {
@ -269,8 +268,8 @@ func linkedCloneConfig() string {
func checkLinkedClone(t *testing.T) builderT.TestCheckFunc { func checkLinkedClone(t *testing.T) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error { return func(artifacts []packer.Artifact) error {
d := testConn(t) d := commonT.TestConn(t)
vm := getVM(t, d, artifacts) vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("layoutEx.disk") vmInfo, err := vm.Info("layoutEx.disk")
if err != nil { if err != nil {
@ -307,9 +306,9 @@ func hardwareConfig() string {
func checkHardware(t *testing.T) builderT.TestCheckFunc { func checkHardware(t *testing.T) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error { return func(artifacts []packer.Artifact) error {
d := testConn(t) d := commonT.TestConn(t)
vm := getVM(t, d, artifacts) vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("config") vmInfo, err := vm.Info("config")
if err != nil { if err != nil {
t.Fatalf("Cannot read VM properties: %v", err) t.Fatalf("Cannot read VM properties: %v", err)
@ -362,9 +361,9 @@ func RAMReservationConfig() string {
func checkRAMReservation(t *testing.T) builderT.TestCheckFunc { func checkRAMReservation(t *testing.T) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error { return func(artifacts []packer.Artifact) error {
d := testConn(t) d := commonT.TestConn(t)
vm := getVM(t, d, artifacts) vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("config") vmInfo, err := vm.Info("config")
if err != nil { if err != nil {
t.Fatalf("Cannot read VM properties: %v", err) t.Fatalf("Cannot read VM properties: %v", err)
@ -409,9 +408,9 @@ func snapshotConfig() string {
func checkSnapshot(t *testing.T) builderT.TestCheckFunc { func checkSnapshot(t *testing.T) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error { return func(artifacts []packer.Artifact) error {
d := testConn(t) d := commonT.TestConn(t)
vm := getVM(t, d, artifacts) vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("layoutEx.disk") vmInfo, err := vm.Info("layoutEx.disk")
if err != nil { if err != nil {
t.Fatalf("Cannot read VM properties: %v", err) t.Fatalf("Cannot read VM properties: %v", err)
@ -443,9 +442,9 @@ func templateConfig() string {
func checkTemplate(t *testing.T) builderT.TestCheckFunc { func checkTemplate(t *testing.T) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error { return func(artifacts []packer.Artifact) error {
d := testConn(t) d := commonT.TestConn(t)
vm := getVM(t, d, artifacts) vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("config.template") vmInfo, err := vm.Info("config.template")
if err != nil { if err != nil {
t.Fatalf("Cannot read VM properties: %v", err) t.Fatalf("Cannot read VM properties: %v", err)
@ -458,28 +457,3 @@ func checkTemplate(t *testing.T) builderT.TestCheckFunc {
return nil return nil
} }
} }
func testConn(t *testing.T) *driver.Driver {
d, err := driver.NewDriver(&driver.ConnectConfig{
VCenterServer: "vcenter.vsphere65.test",
Username: "root",
Password: "jetbrains",
InsecureConnection: true,
})
if err != nil {
t.Fatal("Cannot connect: ", err)
}
return d
}
func getVM(t *testing.T, d *driver.Driver, artifacts []packer.Artifact) *driver.VirtualMachine {
artifactRaw := artifacts[0]
artifact, _ := artifactRaw.(*common.Artifact)
vm, err := d.FindVM(artifact.Name)
if err != nil {
t.Fatalf("Cannot find VM: %v", err)
}
return vm
}

View File

@ -5,6 +5,10 @@ import (
"math/rand" "math/rand"
"time" "time"
"encoding/json" "encoding/json"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"testing"
"github.com/jetbrains-infra/packer-builder-vsphere/common"
) )
func NewVMName() string { func NewVMName() string {
@ -27,3 +31,30 @@ func RenderConfig(config map[string]interface{}) string {
j, _ := json.Marshal(t) j, _ := json.Marshal(t)
return string(j) return string(j)
} }
func TestConn(t *testing.T) *driver.Driver {
d, err := driver.NewDriver(&driver.ConnectConfig{
VCenterServer: "vcenter.vsphere65.test",
Username: "root",
Password: "jetbrains",
InsecureConnection: true,
})
if err != nil {
t.Fatal("Cannot connect: ", err)
}
return d
}
func GetVM(t *testing.T, d *driver.Driver, artifacts []packer.Artifact) *driver.VirtualMachine {
artifactRaw := artifacts[0]
artifact, _ := artifactRaw.(*common.Artifact)
vm, err := d.FindVM(artifact.Name)
if err != nil {
t.Fatalf("Cannot find VM: %v", err)
}
return vm
}

View File

@ -4,6 +4,7 @@ import (
builderT "github.com/hashicorp/packer/helper/builder/testing" builderT "github.com/hashicorp/packer/helper/builder/testing"
commonT "github.com/jetbrains-infra/packer-builder-vsphere/common/testing" commonT "github.com/jetbrains-infra/packer-builder-vsphere/common/testing"
"testing" "testing"
"github.com/hashicorp/packer/packer"
) )
func TestISOBuilderAcc_default(t *testing.T) { func TestISOBuilderAcc_default(t *testing.T) {
@ -11,6 +12,7 @@ func TestISOBuilderAcc_default(t *testing.T) {
builderT.Test(t, builderT.TestCase{ builderT.Test(t, builderT.TestCase{
Builder: &Builder{}, Builder: &Builder{},
Template: commonT.RenderConfig(config), Template: commonT.RenderConfig(config),
Check: checkDefault(t, config["vm_name"].(string), config["host"].(string), "datastore1"),
}) })
} }
@ -31,3 +33,129 @@ func defaultConfig() map[string]interface{} {
return config return config
} }
func checkDefault(t *testing.T, name string, host string, datastore string) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error {
d := commonT.TestConn(t)
vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("name", "parent", "runtime.host", "resourcePool", "datastore", "layoutEx.disk")
if err != nil {
t.Fatalf("Cannot read VM properties: %v", err)
}
if vmInfo.Name != name {
t.Errorf("Invalid VM name: expected '%v', got '%v'", name, vmInfo.Name)
}
f := d.NewFolder(vmInfo.Parent)
folderPath, err := f.Path()
if err != nil {
t.Fatalf("Cannot read folder name: %v", err)
}
if folderPath != "" {
t.Errorf("Invalid folder: expected '/', got '%v'", folderPath)
}
h := d.NewHost(vmInfo.Runtime.Host)
hostInfo, err := h.Info("name")
if err != nil {
t.Fatal("Cannot read host properties: ", err)
}
if hostInfo.Name != host {
t.Errorf("Invalid host name: expected '%v', got '%v'", host, hostInfo.Name)
}
p := d.NewResourcePool(vmInfo.ResourcePool)
poolPath, err := p.Path()
if err != nil {
t.Fatalf("Cannot read resource pool name: %v", err)
}
if poolPath != "" {
t.Errorf("Invalid resource pool: expected '/', got '%v'", poolPath)
}
dsr := vmInfo.Datastore[0].Reference()
ds := d.NewDatastore(&dsr)
dsInfo, err := ds.Info("name")
if err != nil {
t.Fatal("Cannot read datastore properties: ", err)
}
if dsInfo.Name != datastore {
t.Errorf("Invalid datastore name: expected '%v', got '%v'", datastore, dsInfo.Name)
}
return nil
}
}
func TestISOBuilderAcc_hardware(t *testing.T) {
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: hardwareConfig(),
Check: checkHardware(t),
})
}
func hardwareConfig() string {
config := defaultConfig()
config["CPUs"] = 2
config["CPU_reservation"] = 1000
config["CPU_limit"] = 1500
config["RAM"] = 2048
config["RAM_reservation"] = 1024
return commonT.RenderConfig(config)
}
func checkHardware(t *testing.T) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error {
d := commonT.TestConn(t)
vm := commonT.GetVM(t, d, artifacts)
vmInfo, err := vm.Info("config")
if err != nil {
t.Fatalf("Cannot read VM properties: %v", err)
}
cpuSockets := vmInfo.Config.Hardware.NumCPU
if cpuSockets != 2 {
t.Errorf("VM should have 2 CPU sockets, got %v", cpuSockets)
}
cpuReservation := vmInfo.Config.CpuAllocation.GetResourceAllocationInfo().Reservation
if cpuReservation != 1000 {
t.Errorf("VM should have CPU reservation for 1000 Mhz, got %v", cpuReservation)
}
cpuLimit := vmInfo.Config.CpuAllocation.GetResourceAllocationInfo().Limit
if cpuLimit != 1500 {
t.Errorf("VM should have CPU reservation for 1500 Mhz, got %v", cpuLimit)
}
ram := vmInfo.Config.Hardware.MemoryMB
if ram != 2048 {
t.Errorf("VM should have 2048 MB of RAM, got %v", ram)
}
ramReservation := vmInfo.Config.MemoryAllocation.GetResourceAllocationInfo().Reservation
if ramReservation != 1024 {
t.Errorf("VM should have RAM reservation for 1024 MB, got %v", ramReservation)
}
return nil
}
}
func TestISOBuilderAcc_cdrom(t *testing.T) {
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: cdromConfig(),
})
}
func cdromConfig() string {
config := defaultConfig()
config["iso_path"] = "[datastore1] alpine-standard-3.6.2-x86_64.iso"
return commonT.RenderConfig(config)
}

View File

@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh -e
(cd driver && ./test.sh) (cd driver && ./test.sh "$@")
(cd clone && ./test.sh) (cd clone && ./test.sh "$@")
(cd iso && ./test.sh) (cd iso && ./test.sh "$@")