packer-cn/builder/vsphere/common/testing/utility.go

70 lines
1.4 KiB
Go

package testing
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"testing"
"time"
"github.com/hashicorp/packer/builder/vsphere/common"
"github.com/hashicorp/packer/builder/vsphere/driver"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
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 []packersdk.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
}