Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Fix VM create related unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darkowlzz committed May 31, 2020
1 parent e1b9176 commit 7902818
Showing 1 changed file with 20 additions and 40 deletions.
60 changes: 20 additions & 40 deletions cmd/ignite/run/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"reflect"
"testing"

"github.com/weaveworks/gitops-toolkit/pkg/runtime"
api "github.com/weaveworks/ignite/pkg/apis/ignite"
meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1"
)
Expand Down Expand Up @@ -38,26 +37,19 @@ func TestConstructVMFromCLI(t *testing.T) {
{
name: "with VM name and image arg",
createFlag: &CreateFlags{
VM: &api.VM{
ObjectMeta: runtime.ObjectMeta{
Name: "fooVM",
},
},
VMName: "fooVM",
},
args: []string{testImage},
},
{
name: "with invalid image reference",
createFlag: &CreateFlags{
VM: &api.VM{},
},
args: []string{"foo:bar:baz"},
err: true,
name: "with invalid image reference",
createFlag: &CreateFlags{},
args: []string{"foo:bar:baz"},
err: true,
},
{
name: "valid copy files flag",
createFlag: &CreateFlags{
VM: &api.VM{},
CopyFiles: []string{"/tmp/foo:/tmp/bar"},
},
args: []string{testImage},
Expand All @@ -71,7 +63,6 @@ func TestConstructVMFromCLI(t *testing.T) {
{
name: "invalid copy files syntax",
createFlag: &CreateFlags{
VM: &api.VM{},
CopyFiles: []string{"foo:bar:baz"},
},
args: []string{testImage},
Expand All @@ -80,7 +71,6 @@ func TestConstructVMFromCLI(t *testing.T) {
{
name: "invalid copy files paths - not absolute paths",
createFlag: &CreateFlags{
VM: &api.VM{},
CopyFiles: []string{"foo:bar"},
},
args: []string{testImage},
Expand All @@ -89,7 +79,6 @@ func TestConstructVMFromCLI(t *testing.T) {
{
name: "valid port mapping",
createFlag: &CreateFlags{
VM: &api.VM{},
PortMappings: []string{"80:80"},
},
args: []string{testImage},
Expand All @@ -105,7 +94,6 @@ func TestConstructVMFromCLI(t *testing.T) {
{
name: "invalid port mapping",
createFlag: &CreateFlags{
VM: &api.VM{},
PortMappings: []string{"1.1.1.1:foo:bar"},
},
args: []string{testImage},
Expand All @@ -114,7 +102,6 @@ func TestConstructVMFromCLI(t *testing.T) {
{
name: "ssh public key set",
createFlag: &CreateFlags{
VM: &api.VM{},
SSH: api.SSH{
Generate: true,
PublicKey: "some-pub-key",
Expand All @@ -129,7 +116,6 @@ func TestConstructVMFromCLI(t *testing.T) {
{
name: "with no VM name and --require-name flag set",
createFlag: &CreateFlags{
VM: &api.VM{},
RequireName: true,
},
args: []string{testImage},
Expand All @@ -138,13 +124,7 @@ func TestConstructVMFromCLI(t *testing.T) {
{
name: "with sandbox image",
createFlag: &CreateFlags{
VM: &api.VM{
Spec: api.VMSpec{
Sandbox: api.VMSandboxSpec{
OCI: sandboxOCIRef,
},
},
},
SandboxOCI: testSandboxImage,
},
args: []string{testImage},
wantSandboxImage: sandboxOCIRef,
Expand All @@ -153,42 +133,43 @@ func TestConstructVMFromCLI(t *testing.T) {

for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
err := rt.createFlag.constructVMFromCLI(rt.args)
vm := &api.VM{}
err := rt.createFlag.constructVMFromCLI(vm, rt.args)
if (err != nil) != rt.err {
t.Errorf("expected error %t, actual: %v", rt.err, err)
}

if !rt.err {
// Check if the VM image is set as expected.
if rt.createFlag.VM.Spec.Image.OCI != testOCIRef {
t.Errorf("expected VM.Spec.Image.OCI to be %q, actual: %q", testOCIRef.String(), rt.createFlag.VM.Spec.Image.OCI.String())
if vm.Spec.Image.OCI != testOCIRef {
t.Errorf("expected VM.Spec.Image.OCI to be %q, actual: %q", testOCIRef.String(), vm.Spec.Image.OCI.String())
}

// Check if copy files are set as expected.
if len(rt.wantCopyFiles) > 0 {
if !reflect.DeepEqual(rt.createFlag.VM.Spec.CopyFiles, rt.wantCopyFiles) {
t.Errorf("expected VM.Spec.CopyFiles to be %v, actual: %v", rt.wantCopyFiles, rt.createFlag.VM.Spec.CopyFiles)
if !reflect.DeepEqual(vm.Spec.CopyFiles, rt.wantCopyFiles) {
t.Errorf("expected VM.Spec.CopyFiles to be %v, actual: %v", rt.wantCopyFiles, vm.Spec.CopyFiles)
}
} else {
// If the copy files map is empty, compare their sizes.
if len(rt.wantCopyFiles) != len(rt.createFlag.VM.Spec.CopyFiles) {
t.Errorf("expected VM.Spec.CopyFiles to be %v, actual: %v", rt.wantCopyFiles, rt.createFlag.VM.Spec.CopyFiles)
if len(rt.wantCopyFiles) != len(vm.Spec.CopyFiles) {
t.Errorf("expected VM.Spec.CopyFiles to be %v, actual: %v", rt.wantCopyFiles, vm.Spec.CopyFiles)
}
}

// Check if port mappings are set as expected.
if reflect.DeepEqual(rt.createFlag.VM.Spec.Network.Ports, rt.wantPortMapping) {
t.Errorf("expected VM.Spec.Network.Ports to be %v, actual: %v", rt.wantPortMapping, rt.createFlag.VM.Spec.Network.Ports)
if reflect.DeepEqual(vm.Spec.Network.Ports, rt.wantPortMapping) {
t.Errorf("expected VM.Spec.Network.Ports to be %v, actual: %v", rt.wantPortMapping, vm.Spec.Network.Ports)
}

// Check if the SSH values are set as expected.
if reflect.DeepEqual(rt.createFlag.VM.Spec.SSH, rt.wantSSH) {
t.Errorf("expected VM.Spec.SSH to be %v, actual: %v", rt.wantSSH, rt.createFlag.VM.Spec.SSH)
if reflect.DeepEqual(vm.Spec.SSH, rt.wantSSH) {
t.Errorf("expected VM.Spec.SSH to be %v, actual: %v", rt.wantSSH, vm.Spec.SSH)
}

// Check if the sandbox image is set as expected.
if rt.createFlag.VM.Spec.Sandbox.OCI != rt.wantSandboxImage {
t.Errorf("expected VM.Spec.Sandbox to be %v, actual: %v", rt.wantSandboxImage, rt.createFlag.VM.Spec.Sandbox.OCI)
if vm.Spec.Sandbox.OCI != rt.wantSandboxImage {
t.Errorf("expected VM.Spec.Sandbox to be %v, actual: %v", rt.wantSandboxImage, vm.Spec.Sandbox.OCI)
}
}
})
Expand All @@ -204,7 +185,6 @@ func TestNewCreateOptions(t *testing.T) {
{
name: "require-name with no name",
createFlag: &CreateFlags{
VM: &api.VM{},
RequireName: true,
},
err: true,
Expand Down

0 comments on commit 7902818

Please sign in to comment.