69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package testing
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/hashicorp/packer/packer"
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/common"
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
|
"math/rand"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func NewVMName() string {
|
|
rand.Seed(time.Now().UnixNano())
|
|
return fmt.Sprintf("test-%v", rand.Intn(1000))
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func TestConn(t *testing.T) *driver.Driver {
|
|
username := os.Getenv("VSPHERE_USERNAME")
|
|
if username == "" {
|
|
username = "root"
|
|
}
|
|
password := os.Getenv("VSPHERE_PASSWORD")
|
|
if password == "" {
|
|
password = "jetbrains"
|
|
}
|
|
|
|
d, err := driver.NewDriver(&driver.ConnectConfig{
|
|
VCenterServer: "vcenter.vsphere65.test",
|
|
Username: username,
|
|
Password: password,
|
|
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
|
|
}
|