2017-06-14 14:16:57 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-10-25 16:54:13 -04:00
|
|
|
"encoding/json"
|
2017-06-14 14:16:57 -04:00
|
|
|
builderT "github.com/hashicorp/packer/helper/builder/testing"
|
2017-08-17 19:24:59 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-08-23 15:40:57 -04:00
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
2017-10-25 16:20:59 -04:00
|
|
|
driverT "github.com/jetbrains-infra/packer-builder-vsphere/driver/testing"
|
2017-10-25 16:54:13 -04:00
|
|
|
"testing"
|
2017-06-14 14:16:57 -04:00
|
|
|
)
|
|
|
|
|
2017-08-17 19:24:59 -04:00
|
|
|
func TestBuilderAcc_default(t *testing.T) {
|
|
|
|
config := defaultConfig()
|
2017-06-14 14:16:57 -04:00
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
2017-08-17 19:24:59 -04:00
|
|
|
Template: renderConfig(config),
|
2017-10-25 16:54:13 -04:00
|
|
|
Check: func(artifacts []packer.Artifact) error {
|
|
|
|
d := driverT.NewTestDriver(t)
|
|
|
|
driverT.VMCheckDefault(t, d, getVM(t, d, artifacts), config["vm_name"].(string),
|
|
|
|
config["host"].(string), driverT.DefaultDatastore)
|
|
|
|
return nil
|
|
|
|
},
|
2017-06-14 14:16:57 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-17 19:24:59 -04:00
|
|
|
func defaultConfig() map[string]interface{} {
|
|
|
|
config := map[string]interface{}{
|
2017-10-25 16:20:59 -04:00
|
|
|
"vcenter_server": driverT.DefaultVCenterServer,
|
|
|
|
"username": driverT.DefaultVCenterUsername,
|
|
|
|
"password": driverT.DefaultVCenterPassword,
|
2017-07-01 11:59:36 -04:00
|
|
|
"insecure_connection": true,
|
2017-06-14 14:16:57 -04:00
|
|
|
|
2017-10-25 16:20:59 -04:00
|
|
|
"template": driverT.DefaultTemplate,
|
|
|
|
"host": driverT.DefaultHost,
|
2017-06-14 14:16:57 -04:00
|
|
|
|
2017-09-03 11:34:12 -04:00
|
|
|
"ssh_username": "root",
|
2017-08-17 19:24:59 -04:00
|
|
|
"ssh_password": "jetbrains",
|
|
|
|
}
|
2017-10-25 16:20:59 -04:00
|
|
|
config["vm_name"] = driverT.NewVMName()
|
2017-08-17 19:24:59 -04:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:32:45 -04:00
|
|
|
func TestBuilderAcc_artifact(t *testing.T) {
|
|
|
|
config := defaultConfig()
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: renderConfig(config),
|
|
|
|
Check: checkArtifact(t),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkArtifact(t *testing.T) builderT.TestCheckFunc {
|
|
|
|
return func(artifacts []packer.Artifact) error {
|
|
|
|
if len(artifacts) > 1 {
|
|
|
|
t.Fatal("more than 1 artifact")
|
|
|
|
}
|
|
|
|
|
|
|
|
artifactRaw := artifacts[0]
|
|
|
|
_, ok := artifactRaw.(*Artifact)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unknown artifact: %#v", artifactRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 17:23:29 -04:00
|
|
|
func TestBuilderAcc_folder(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: folderConfig(),
|
2017-08-23 17:54:23 -04:00
|
|
|
Check: checkFolder(t, "folder1/folder2"),
|
2017-08-23 17:23:29 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func folderConfig() string {
|
|
|
|
config := defaultConfig()
|
2017-08-23 17:54:23 -04:00
|
|
|
config["folder"] = "folder1/folder2"
|
2017-08-23 17:23:29 -04:00
|
|
|
config["linked_clone"] = true // speed up
|
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkFolder(t *testing.T, folder string) builderT.TestCheckFunc {
|
|
|
|
return func(artifacts []packer.Artifact) error {
|
2017-10-25 16:20:59 -04:00
|
|
|
d := driverT.NewTestDriver(t)
|
2017-08-23 17:23:29 -04:00
|
|
|
vm := getVM(t, d, artifacts)
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
vmInfo, err := vm.Info("parent")
|
2017-08-23 17:23:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f := d.NewFolder(vmInfo.Parent)
|
2017-08-23 20:06:50 -04:00
|
|
|
path, err := f.Path()
|
2017-08-23 17:23:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read folder name: %v", err)
|
|
|
|
}
|
|
|
|
if path != folder {
|
|
|
|
t.Errorf("Wrong folder. expected: %v, got: %v", folder, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 18:23:48 -04:00
|
|
|
func TestBuilderAcc_resourcePool(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: resourcePoolConfig(),
|
|
|
|
Check: checkResourcePool(t, "pool1/pool2"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePoolConfig() string {
|
|
|
|
config := defaultConfig()
|
|
|
|
config["resource_pool"] = "pool1/pool2"
|
|
|
|
config["linked_clone"] = true // speed up
|
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkResourcePool(t *testing.T, pool string) builderT.TestCheckFunc {
|
|
|
|
return func(artifacts []packer.Artifact) error {
|
2017-10-25 16:20:59 -04:00
|
|
|
d := driverT.NewTestDriver(t)
|
2017-08-23 18:23:48 -04:00
|
|
|
vm := getVM(t, d, artifacts)
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
vmInfo, err := vm.Info("resourcePool")
|
2017-08-23 18:23:48 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := d.NewResourcePool(vmInfo.ResourcePool)
|
2017-08-23 20:06:50 -04:00
|
|
|
path, err := p.Path()
|
2017-08-23 18:23:48 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read resource pool name: %v", err)
|
|
|
|
}
|
|
|
|
if path != pool {
|
|
|
|
t.Errorf("Wrong folder. expected: %v, got: %v", pool, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 16:20:59 -04:00
|
|
|
// FIXME: why do we need this??? Why don't perform these checks in checkDefault?
|
2017-08-24 16:18:14 -04:00
|
|
|
func TestBuilderAcc_datastore(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: datastoreConfig(),
|
2017-10-25 16:54:13 -04:00
|
|
|
Check: func(artifacts []packer.Artifact) error {
|
|
|
|
d := driverT.NewTestDriver(t)
|
|
|
|
driverT.VMCheckDatastore(t, d, getVM(t, d, artifacts), driverT.DefaultDatastore)
|
|
|
|
return nil
|
|
|
|
},
|
2017-08-24 16:18:14 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func datastoreConfig() string {
|
|
|
|
config := defaultConfig()
|
2017-09-03 11:34:12 -04:00
|
|
|
config["template"] = "alpine-host4" // on esxi-4.vsphere55.test
|
2017-08-24 16:18:14 -04:00
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderAcc_multipleDatastores(t *testing.T) {
|
2017-10-25 16:54:13 -04:00
|
|
|
t.Skip("test must fail")
|
2017-08-24 16:18:14 -04:00
|
|
|
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: multipleDatastoresConfig(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func multipleDatastoresConfig() string {
|
|
|
|
config := defaultConfig()
|
|
|
|
config["host"] = "esxi-4.vsphere55.test" // host with 2 datastores
|
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
2017-08-17 19:24:59 -04:00
|
|
|
func TestBuilderAcc_linkedClone(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: linkedCloneConfig(),
|
|
|
|
Check: checkLinkedClone(t),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func linkedCloneConfig() string {
|
|
|
|
config := defaultConfig()
|
|
|
|
config["linked_clone"] = true
|
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkLinkedClone(t *testing.T) builderT.TestCheckFunc {
|
|
|
|
return func(artifacts []packer.Artifact) error {
|
2017-10-25 16:20:59 -04:00
|
|
|
d := driverT.NewTestDriver(t)
|
2017-08-23 16:32:45 -04:00
|
|
|
vm := getVM(t, d, artifacts)
|
2017-08-17 19:24:59 -04:00
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
vmInfo, err := vm.Info("layoutEx.disk")
|
2017-08-17 19:24:59 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-08-31 08:05:26 -04:00
|
|
|
if len(vmInfo.LayoutEx.Disk[0].Chain) != 2 {
|
2017-08-17 19:24:59 -04:00
|
|
|
t.Error("Not a linked clone")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 16:50:25 -04:00
|
|
|
func TestBuilderAcc_hardware(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: hardwareConfig(),
|
2017-10-25 16:54:13 -04:00
|
|
|
Check: func(artifacts []packer.Artifact) error {
|
|
|
|
d := driverT.NewTestDriver(t)
|
|
|
|
driverT.VMCheckHardware(t, d, getVM(t, d, artifacts))
|
|
|
|
return nil
|
|
|
|
},
|
2017-08-24 16:50:25 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func hardwareConfig() string {
|
|
|
|
config := defaultConfig()
|
2017-10-25 16:20:59 -04:00
|
|
|
config["CPUs"] = driverT.DefaultCPUs
|
|
|
|
config["CPU_reservation"] = driverT.DefaultCPUReservation
|
|
|
|
config["CPU_limit"] = driverT.DefaultCPULimit
|
|
|
|
config["RAM"] = driverT.DefaultRAM
|
|
|
|
config["RAM_reservation"] = driverT.DefaultRAMReservation
|
2017-08-24 16:50:25 -04:00
|
|
|
config["linked_clone"] = true // speed up
|
|
|
|
|
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderAcc_RAMReservation(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: RAMReservationConfig(),
|
|
|
|
Check: checkRAMReservation(t),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func RAMReservationConfig() string {
|
|
|
|
config := defaultConfig()
|
|
|
|
config["RAM_reserve_all"] = true
|
|
|
|
config["linked_clone"] = true // speed up
|
|
|
|
|
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkRAMReservation(t *testing.T) builderT.TestCheckFunc {
|
|
|
|
return func(artifacts []packer.Artifact) error {
|
2017-10-25 16:20:59 -04:00
|
|
|
d := driverT.NewTestDriver(t)
|
2017-08-24 16:50:25 -04:00
|
|
|
|
|
|
|
vm := getVM(t, d, artifacts)
|
|
|
|
vmInfo, err := vm.Info("config")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *vmInfo.Config.MemoryReservationLockedToMax != true {
|
|
|
|
t.Errorf("VM should have all RAM reserved")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 08:05:26 -04:00
|
|
|
func TestBuilderAcc_sshKey(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: sshKeyConfig(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func sshKeyConfig() string {
|
|
|
|
config := defaultConfig()
|
|
|
|
config["ssh_password"] = ""
|
|
|
|
config["ssh_private_key_file"] = "test-key.pem"
|
|
|
|
config["linked_clone"] = true // speed up
|
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:15:19 -04:00
|
|
|
func TestBuilderAcc_snapshot(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: snapshotConfig(),
|
2017-10-25 16:54:13 -04:00
|
|
|
Check: func(artifacts []packer.Artifact) error {
|
|
|
|
d := driverT.NewTestDriver(t)
|
|
|
|
driverT.VMCheckSnapshor(t, d, getVM(t, d, artifacts))
|
|
|
|
return nil
|
|
|
|
},
|
2017-08-23 16:15:19 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func snapshotConfig() string {
|
|
|
|
config := defaultConfig()
|
|
|
|
config["create_snapshot"] = true
|
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
2017-08-17 19:24:59 -04:00
|
|
|
func TestBuilderAcc_template(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: templateConfig(),
|
2017-10-25 16:54:13 -04:00
|
|
|
Check: func(artifacts []packer.Artifact) error {
|
|
|
|
d := driverT.NewTestDriver(t)
|
|
|
|
driverT.VMCheckTemplate(t, d, getVM(t, d, artifacts))
|
|
|
|
return nil
|
|
|
|
},
|
2017-08-17 19:24:59 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func templateConfig() string {
|
|
|
|
config := defaultConfig()
|
|
|
|
config["convert_to_template"] = true
|
2017-08-23 17:23:29 -04:00
|
|
|
config["linked_clone"] = true // speed up
|
2017-08-17 19:24:59 -04:00
|
|
|
return renderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderConfig(config map[string]interface{}) string {
|
|
|
|
t := map[string][]map[string]interface{}{
|
|
|
|
"builders": {
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for k, v := range config {
|
|
|
|
t["builders"][0][k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
j, _ := json.Marshal(t)
|
|
|
|
return string(j)
|
|
|
|
}
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
func getVM(t *testing.T, d *driver.Driver, artifacts []packer.Artifact) *driver.VirtualMachine {
|
2017-08-17 19:24:59 -04:00
|
|
|
artifactRaw := artifacts[0]
|
|
|
|
artifact, _ := artifactRaw.(*Artifact)
|
|
|
|
|
2017-08-23 15:40:57 -04:00
|
|
|
vm, err := d.FindVM(artifact.Name)
|
2017-08-17 19:24:59 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot find VM: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm
|
2017-06-14 14:16:57 -04:00
|
|
|
}
|