Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: test and example tech debt #466

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions builder/vsphere/common/testing/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ import (
"encoding/json"
"fmt"
"math/rand"
"os"
"time"

"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common/utils"
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/driver"
)

func NewVMName() string {
rand.Seed(time.Now().UnixNano())
return fmt.Sprintf("test-%v", rand.Intn(1000))
r := rand.New(rand.NewSource(time.Now().UnixNano()))
tenthirtyam marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf("test-%v", r.Intn(1000))
}

func RenderConfig(builderType string, config map[string]interface{}) string {
t := map[string][]map[string]interface{}{
"builders": {
map[string]interface{}{
"type": builderType,
},
{"type": builderType},
},
}
for k, v := range config {
Expand All @@ -35,23 +33,18 @@ func RenderConfig(builderType string, config map[string]interface{}) string {
}

func TestConn() (driver.Driver, error) {
username := os.Getenv("VSPHERE_USERNAME")
if username == "" {
username = "root"
}
password := os.Getenv("VSPHERE_PASSWORD")
if password == "" {
password = "jetbrains"
}
vcenter := utils.GetenvOrDefault(utils.EnvVcenterServer, utils.DefaultVcenterServer)
username := utils.GetenvOrDefault(utils.EnvVsphereUsername, utils.DefaultVsphereUsername)
password := utils.GetenvOrDefault(utils.EnvVspherePassword, utils.DefaultVspherePassword)

d, err := driver.NewDriver(&driver.ConnectConfig{
VCenterServer: "vcenter.example.com",
VCenterServer: vcenter,
Username: username,
Password: password,
InsecureConnection: true,
})
if err != nil {
return nil, fmt.Errorf("error connecting to vCenter Server instance: %v", err)
return nil, fmt.Errorf("error connecting to endpoint: %v", err)
}
return d, nil
}
Expand All @@ -61,7 +54,6 @@ func GetVM(d driver.Driver, name string) (driver.VirtualMachine, error) {
if err != nil {
return nil, fmt.Errorf("error finding virtual machine: %v", err)
}

return vm, nil
}

Expand Down
27 changes: 27 additions & 0 deletions builder/vsphere/common/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package utils

import (
"os"
)

const (
DefaultVcenterServer = "vcenter.example.com"
DefaultVsphereUsername = "[email protected]"
DefaultVspherePassword = "VMw@re1!"
DefaultVsphereHost = "esxi-01.example.com"

EnvVcenterServer = "VSPHERE_VCENTER_SERVER"
EnvVsphereUsername = "VSPHERE_USERNAME"
EnvVspherePassword = "VSPHERE_PASSWORD"
EnvVsphereHost = "VSPHERE_HOST"
)

func GetenvOrDefault(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
28 changes: 10 additions & 18 deletions builder/vsphere/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"math/rand"
"net/http"
"net/url"
"os"
"testing"
"time"

"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common/utils"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/session"
Expand All @@ -23,21 +23,13 @@ import (
"github.com/vmware/govmomi/vim25/soap"
)

// Defines whether acceptance tests should be run
const TestHostName = "esxi-01.example.com"

func newTestDriver(t *testing.T) Driver {
username := os.Getenv("VSPHERE_USERNAME")
if username == "" {
username = "root"
}
password := os.Getenv("VSPHERE_PASSWORD")
if password == "" {
password = "jetbrains"
}
vcenter := utils.GetenvOrDefault(utils.EnvVcenterServer, utils.DefaultVcenterServer)
username := utils.GetenvOrDefault(utils.EnvVsphereUsername, utils.DefaultVsphereUsername)
password := utils.GetenvOrDefault(utils.EnvVspherePassword, utils.DefaultVspherePassword)

d, err := NewDriver(&ConnectConfig{
VCenterServer: "vcenter.example.com",
VCenterServer: vcenter,
Username: username,
Password: password,
InsecureConnection: true,
Expand All @@ -49,8 +41,8 @@ func newTestDriver(t *testing.T) Driver {
}

func newVMName() string {
rand.Seed(time.Now().UTC().UnixNano())
return fmt.Sprintf("test-%v", rand.Intn(1000))
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
return fmt.Sprintf("test-%v", r.Intn(1000))
}

type VCenterSimulator struct {
Expand Down Expand Up @@ -94,23 +86,23 @@ func (s *VCenterSimulator) Close() {
}
}

// Simulator shortcut to choose any pre created VM.
// Simulator shortcut to choose any pre-created virtual machine.
func (s *VCenterSimulator) ChooseSimulatorPreCreatedVM() (VirtualMachine, *simulator.VirtualMachine) {
machine := simulator.Map.Any("VirtualMachine").(*simulator.VirtualMachine)
ref := machine.Reference()
vm := s.driver.NewVM(&ref)
return vm, machine
}

// Simulator shortcut to choose any pre created Datastore.
// Simulator shortcut to choose any pre-created datastore.
func (s *VCenterSimulator) ChooseSimulatorPreCreatedDatastore() (Datastore, *simulator.Datastore) {
ds := simulator.Map.Any("Datastore").(*simulator.Datastore)
ref := ds.Reference()
datastore := s.driver.NewDatastore(&ref)
return datastore, ds
}

// Simulator shortcut to choose any pre created Host.
// Simulator shortcut to choose any pre-created EXSi host.
func (s *VCenterSimulator) ChooseSimulatorPreCreatedHost() (*Host, *simulator.HostSystem) {
h := simulator.Map.Any("HostSystem").(*simulator.HostSystem)
ref := h.Reference()
Expand Down
8 changes: 5 additions & 3 deletions builder/vsphere/driver/host_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ package driver

import (
"testing"

"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common/utils"
)

func TestHostAcc(t *testing.T) {
t.Skip("Acceptance tests not configured yet.")
d := newTestDriver(t)
host, err := d.FindHost(TestHostName)
host, err := d.FindHost(utils.DefaultVsphereHost)
if err != nil {
t.Fatalf("unexpected error: '%s'", err)
}
Expand All @@ -19,7 +21,7 @@ func TestHostAcc(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: '%s'", err)
}
if info.Name != TestHostName {
t.Errorf("unexpected result: expected '%s', but returned '%s'", TestHostName, info.Name)
if info.Name != utils.DefaultVsphereHost {
t.Errorf("unexpected result: expected '%s', but returned '%s'", utils.DefaultVsphereHost, info.Name)
}
}
8 changes: 5 additions & 3 deletions builder/vsphere/driver/vm_clone_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net"
"testing"
"time"

"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common/utils"
)

func TestVMAcc_clone(t *testing.T) {
Expand All @@ -32,7 +34,7 @@ func TestVMAcc_clone(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.config.Host = TestHostName
tc.config.Host = utils.DefaultVsphereHost
tc.config.Name = newVMName()

templateName := "alpine"
Expand Down Expand Up @@ -88,8 +90,8 @@ func cloneDefaultCheck(t *testing.T, vm VirtualMachine, config *CloneConfig) {
if err != nil {
t.Fatal("Cannot read host properties: ", err)
}
if hostInfo.Name != TestHostName {
t.Errorf("Invalid host name: expected '%v', got '%v'", TestHostName, hostInfo.Name)
if hostInfo.Name != utils.DefaultVsphereHost {
t.Errorf("Invalid host name: expected '%v', got '%v'", utils.DefaultVsphereHost, hostInfo.Name)
}

p := d.NewResourcePool(vmInfo.ResourcePool)
Expand Down
8 changes: 5 additions & 3 deletions builder/vsphere/driver/vm_create_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package driver
import (
"log"
"testing"

"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common/utils"
)

func TestVMAcc_create(t *testing.T) {
Expand All @@ -20,7 +22,7 @@ func TestVMAcc_create(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.config.Host = TestHostName
tc.config.Host = utils.DefaultVsphereHost
tc.config.Name = newVMName()

d := newTestDriver(t)
Expand Down Expand Up @@ -71,8 +73,8 @@ func createDefaultCheck(t *testing.T, vm VirtualMachine, config *CreateConfig) {
if err != nil {
t.Fatal("Cannot read host properties: ", err)
}
if hostInfo.Name != TestHostName {
t.Errorf("Invalid host name: expected '%v', got '%v'", TestHostName, hostInfo.Name)
if hostInfo.Name != utils.DefaultVsphereHost {
t.Errorf("Invalid host name: expected '%v', got '%v'", utils.DefaultVsphereHost, hostInfo.Name)
}

p := d.NewResourcePool(vmInfo.ResourcePool)
Expand Down
47 changes: 15 additions & 32 deletions builder/vsphere/iso/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hashicorp/packer-plugin-sdk/acctest"
commonT "github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common/testing"
"github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common/utils"
"github.com/vmware/govmomi/vim25/types"
)

Expand Down Expand Up @@ -39,24 +40,10 @@ func TestAccISOBuilderAcc_default(t *testing.T) {
}

func defaultConfig() map[string]interface{} {
username := os.Getenv("VSPHERE_USERNAME")
if username == "" {
username = "root"
}
password := os.Getenv("VSPHERE_PASSWORD")
if password == "" {
password = "jetbrains"
}

vcenter := os.Getenv("VSPHERE_VCENTER_SERVER")
if vcenter == "" {
vcenter = "vcenter.example.com"
}

host := os.Getenv("VSPHERE_HOST")
if host == "" {
host = "esxi-01.example.com"
}
vcenter := utils.GetenvOrDefault(utils.EnvVcenterServer, utils.DefaultVcenterServer)
username := utils.GetenvOrDefault(utils.EnvVsphereUsername, utils.DefaultVsphereUsername)
password := utils.GetenvOrDefault(utils.EnvVspherePassword, utils.DefaultVspherePassword)
host := utils.GetenvOrDefault(utils.EnvVsphereHost, utils.DefaultVsphereHost)

config := map[string]interface{}{
"vcenter_server": vcenter,
Expand All @@ -65,8 +52,8 @@ func defaultConfig() map[string]interface{} {
"host": host,
"insecure_connection": true,

"ssh_username": "root",
"ssh_password": "jetbrains",
"ssh_username": "packer",
"ssh_password": "VMw@re1!",

"vm_name": commonT.NewVMName(),
"storage": map[string]interface{}{
Expand Down Expand Up @@ -544,23 +531,19 @@ func TestAccISOBuilderAcc_full(t *testing.T) {
}

func fullConfig() map[string]interface{} {
username := os.Getenv("VSPHERE_USERNAME")
if username == "" {
username = "root"
}
password := os.Getenv("VSPHERE_PASSWORD")
if password == "" {
password = "jetbrains"
}
vcenter := utils.GetenvOrDefault(utils.EnvVcenterServer, utils.DefaultVcenterServer)
username := utils.GetenvOrDefault(utils.EnvVsphereUsername, utils.DefaultVsphereUsername)
password := utils.GetenvOrDefault(utils.EnvVspherePassword, utils.DefaultVspherePassword)
host := utils.GetenvOrDefault(utils.EnvVsphereHost, utils.DefaultVsphereHost)

config := map[string]interface{}{
"vcenter_server": "vcenter.example.com",
"vcenter_server": vcenter,
"username": username,
"password": password,
"host": host,
"insecure_connection": true,

"vm_name": commonT.NewVMName(),
"host": "esxi-01.example.com",

"RAM": 512,
"disk_controller_type": []string{
Expand Down Expand Up @@ -602,8 +585,8 @@ func fullConfig() map[string]interface{} {
"/media/floppy/SETUP.SH<enter>",
},

"ssh_username": "root",
"ssh_password": "jetbrains",
"ssh_username": "packer",
"ssh_password": "VMw@re1!",
}

return config
Expand Down
4 changes: 2 additions & 2 deletions examples/builder/vsphere-clone/alpine/alpine.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"type": "vsphere-clone",

"vcenter_server": "vcenter.example.com",
"username": "root",
"password": "jetbrains",
"username": "[email protected]",
"password": "VMw@re1!",
"insecure_connection": "true",

"template": "alpine",
Expand Down
4 changes: 2 additions & 2 deletions examples/builder/vsphere-clone/alpine/alpine.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ source "vsphere-clone" "example_clone" {
communicator = "none"
host = "esxi-01.example.com"
insecure_connection = "true"
password = "jetbrains"
password = "VMw@re1!"
template = "alpine"
username = "root"
username = "[email protected]"
vcenter_server = "vcenter.example.com"
vm_name = "alpine-clone-${local.timestamp}"
}
Expand Down
8 changes: 4 additions & 4 deletions examples/builder/vsphere-iso/alpine/alpine-3.8.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@
"network_card": "vmxnet3"
}
],
"password": "jetbrains",
"ssh_password": "jetbrains",
"ssh_username": "root",
"password": "VMw@re1!",
"ssh_password": "VMw@re1!",
"ssh_username": "packer",
"storage": [
{
"disk_size": 1024,
"disk_thin_provisioned": true
}
],
"username": "root",
"username": "[email protected]",
"vcenter_server": "vcenter.example.com",
"vm_name": "alpine-{{timestamp}}"
}
Expand Down
Loading