Пример #1
0
func TestSnapshots(t *testing.T) {
	Convey("Given a mocked Vmrun Wrapper", t, func() {
		vmrun := vmware.NewMockVmrun()
		vmrun.TemplateVM = "/tmp/vmlcmverify/test.vmx"
		vmrun.TemplateSnapshots = []string{
			"SetUp",
			"POM 2015",
		}

		config := new(util.LCMConfiguration)
		config.ClonesDirectory = "/tmp/vmlcmverify/clones/"
		config.TemplatePath = "/tmp/vmlcmverify/test.vmx"
		config.Vmrun = "/tmp/vmlcmverify/vmrun"
		config.Prefix = "Pom2015"
		config.Addresses = []string{
			"a1:b1:c1:d1:e1:f1",
			"a2:b2:c2:d2:e2:f2",
		}

		Convey("getTemplateSnapshots should be able to parse the template snapshot names", func() {
			snapshots, err := getTemplateSnapshots(vmrun, config)
			So(err, ShouldBeNil)
			So(snapshots, ShouldNotBeNil)
			So(len(snapshots), ShouldEqual, 2)
			So(snapshots[0], ShouldEqual, "SetUp")
			So(snapshots[1], ShouldEqual, "POM 2015")
		})

		Convey("createTemplateSnapshot should be able to create a snapshot", func() {
			_, err := createTemplateSnapshot(vmrun, config)
			So(err, ShouldBeNil)
		})
	})
}
Пример #2
0
func TestStatus(t *testing.T) {
	Convey("Given a mocked Vmrun Wrapper", t, func() {
		vmrun := vmware.NewMockVmrun()
		runningPaths := []string{
			"/Volumes/VM_SB3/VMware/webbruegge.vmwarevm/webbruegge.vmx",
			"/Volumes/VM_SB3/VMware/dockerbruegge1.vmwarevm/dockerbruegge1.vmx",
			"/Volumes/VM_SB3/VMware/repoarchbruegge.vmwarevm/repoarchbruegge.vmx",
			"/Volumes/VM_SB3/VMware/build-agents-reserve/buildagent-mac-5.vmwarevm/buildagent-mac-5.vmx",
			"/Volumes/VM_SB3/VMware/LS1Cloud.vmwarevm/LS1Cloud.vmx",
			"/Volumes/VM_SB3/VMware/build-agents-reserve/buildagent-mac-6.vmwarevm/buildagent-mac-6.vmx",
			"/Volumes/VM_SB3/VMware/build-agents-reserve/buildagent-mac-4.vmwarevm/buildagent-mac-4.vmx",
			"/Volumes/VM_SB3/VMware/backupbruegge.vmwarevm/backupbruegge.vmx",
			"/Volumes/VM_SB3/VMware/build-agents-reserve/buildagent-mac-8.vmwarevm/buildagent-mac-8.vmx",
			"/Volumes/VM_SB3/VMware/build-agents-reserve/buildagent-mac-7.vmwarevm/buildagent-mac-7.vmx",
			"/Volumes/VM_SB3/VMware/mailbruegge.vmwarevm/mailbruegge.vmx",
			"/Volumes/VM_SB3/VMware/webbruegge_prelive.vmwarevm/webbruegge_prelive.vmx",
			"/Volumes/VM_SB3/VMware/monitorbruegge.vmwarevm/monitorbruegge.vmx",
		}
		for _, runningPath := range runningPaths {
			vmrun.RunningVMs = append(vmrun.RunningVMs, runningPath)
		}
		clonePaths := []string{
			"/tmp/vmlcmstatus/pom2015-a1b1c1d1e1f1.vmwarevm/pom2015-a1b1c1d1e1f1.vmx",
			"/tmp/vmlcmstatus/pom2015-a2b2c2d2e2f2.vmwarevm/pom2015-a2b2c2d2e2f2.vmx",
			"/tmp/vmlcmstatus/pom2015-a3b3c3d3e3f3.vmwarevm/pom2015-a3b3c3d3e3f3.vmx",
		}
		for _, clonePath := range clonePaths {
			vmrun.CloneFolderVMs = append(vmrun.CloneFolderVMs, clonePath)
		}

		Convey("getVmrunVersion should be able to parse the version information", func() {
			result, err := getVmrunVersion(vmrun)
			So(err, ShouldBeNil)
			So(result.version, ShouldEqual, "1.14.2")
			So(result.build, ShouldEqual, "2779224")
		})

		Convey("getRunningVMNumber should be able to parse the number of running vms", func() {
			result, err := getRunningVMNumber(vmrun)
			So(err, ShouldBeNil)
			So(result, ShouldEqual, 13)
		})

		Convey("getVMs should be able to detect all running vms and all vms in the clone directory", func() {
			// Create folders
			createTestStatusFolders()
			defer deleteTestStatusFolders()

			// Create configurations
			config := new(util.LCMConfiguration)
			config.Addresses = []string{
				"a1b1c1d1e1f1",
				"a2b2c2d2e2f2",
				"a3b3c3d3e3f3",
				"a4b4c4d4e4f4",
				"a5b5c5d5e5f5",
				"a6b6c6d6e6f6",
				"a7b7c7d7e7f7",
			}
			config.ClonesDirectory = "/tmp/vmlcmstatus/"
			config.Prefix = "pom2015"
			config.TemplatePath = "/tmp/vmlcmstatus/pom2015-template.vmwarevm/pom2015-template.vmx"

			// Now run getVMs and check for error first
			vms, err := getVMs(vmrun, config)

			So(err, ShouldBeNil)
			So(vms, ShouldNotBeNil)
			So(len(vms), ShouldEqual, 19)

			vmMap := make(map[string]*virtualMachine)

			// Build map with vms
			for _, vm := range vms {
				vmMap[vm.path] = vm
			}

			for _, path := range runningPaths {
				vm, ok := vmMap[path]
				So(ok, ShouldBeTrue)
				So(vm, ShouldNotBeNil)
				So(vm.running, ShouldBeTrue)
				So(vm.template, ShouldBeFalse)
			}

			// Then check all clones
			clonePaths := []string{
				"/tmp/vmlcmstatus/pom2015-a1b1c1d1e1f1.vmwarevm/pom2015-a1b1c1d1e1f1.vmx",
				"/tmp/vmlcmstatus/pom2015-a2b2c2d2e2f2.vmwarevm/pom2015-a2b2c2d2e2f2.vmx",
				"/tmp/vmlcmstatus/pom2015-a3b3c3d3e3f3.vmwarevm/pom2015-a3b3c3d3e3f3.vmx",
				"/tmp/vmlcmstatus/pom2015-a4b4c4d4e4f4.vmwarevm/pom2015-a4b4c4d4e4f4.vmx",
				"/tmp/vmlcmstatus/pom2015-a5b5c5d5e5f5.vmwarevm/pom2015-a5b5c5d5e5f5.vmx",
			}

			for _, path := range clonePaths {
				vm, ok := vmMap[path]
				So(ok, ShouldBeTrue)
				So(vm, ShouldNotBeNil)
				So(vm.clone, ShouldBeTrue)
			}

			template, ok := vmMap["/tmp/vmlcmstatus/pom2015-template.vmwarevm/pom2015-template.vmx"]
			So(ok, ShouldBeTrue)
			So(template, ShouldNotBeNil)
			So(template.clone, ShouldBeFalse)
			So(template.running, ShouldBeFalse)
			So(template.template, ShouldBeTrue)
		})
	})
}
Пример #3
0
func TestClones(t *testing.T) {
	Convey("Given a mocked Vmrun Wrapper", t, func() {
		createTestClonesFolders()
		defer deleteTestClonesFolders()

		// Prepare mock vmrun
		vmrun := vmware.NewMockVmrun()
		vmrun.TemplateVM = "/tmp/vmlcmclones/pom2015-template.vmwarevm/pom2015-template.vmx"
		vm1 := new(virtualMachine)
		vm2 := new(virtualMachine)
		vm3 := new(virtualMachine)
		vm1.path = "/foo/bar/pom2015-A1B1C1D1E1F1.vmwarevm/pom2015-A1B1C1D1E1F1.vmx"
		vm2.path = "/foo/bar/pom2015-A2B2C2D2E2F2.vmwarevm/pom2015-A2B2C2D2E2F2.vmx"
		vm3.path = "/foo/bar/pom2015-A3B3C3D3E3F3.vmwarevm/pom2015-A3B3C3D3E3F3.vmx"
		clones := []*virtualMachine{
			vm1,
			vm2,
			vm3,
		}
		vmrun.CloneFolderVMs = []string{
			"/foo/bar/pom2015-A1B1C1D1E1F1.vmwarevm/pom2015-A1B1C1D1E1F1.vmx",
			"/foo/bar/pom2015-A2B2C2D2E2F2.vmwarevm/pom2015-A2B2C2D2E2F2.vmx",
			"/foo/bar/pom2015-A3B3C3D3E3F3.vmwarevm/pom2015-A3B3C3D3E3F3.vmx",
		}

		// Prepare config
		config := new(util.LCMConfiguration)
		config.Vmrun = "/Applications/VMware Fusion.app/Contents/Library/vmrun"
		config.Prefix = "pom2015"
		config.ClonesDirectory = "/tmp/vmlcmclones/"
		config.TemplatePath = "/tmp/vmlcmclones/pom2015-template.vmwarevm/pom2015-template.vmx"
		config.Vmrun = ""
		config.Addresses = []string{
			"a1:b1:c1:d1:e1:f1",
			"a2:b2:c2:d2:e2:f2",
			"a3:b3:c3:d3:e3:f3",
			"a4:b4:c4:d4:e4:f4",
			"a5:b5:c5:d5:e5:f5",
		}
		for i, address := range config.Addresses {
			config.Addresses[i] = strings.ToUpper(address)
		}

		// Check mac addresses
		available := getAvailableMacAddresses(clones, config)
		So(available, ShouldNotBeNil)
		So(len(available), ShouldEqual, 2)

		// Test getAvailableMacAddresses
		Convey("getAvailableMacAddresses should only return available MAC addresses", func() {
			// 2 available
			available := getAvailableMacAddresses(clones, config)
			So(available, ShouldNotBeNil)
			So(len(available), ShouldEqual, 2)

			// 1 available
			config.Addresses = config.Addresses[0:4]
			available = getAvailableMacAddresses(clones, config)
			So(len(available), ShouldEqual, 1)

			// 0 available
			config.Addresses = config.Addresses[0:3]
			available = getAvailableMacAddresses(clones, config)
			So(len(available), ShouldEqual, 0)

			// -1 available
			config.Addresses = config.Addresses[0:2]
			available = getAvailableMacAddresses(clones, config)
			So(len(available), ShouldEqual, 0)

			// None provided
			config.Addresses = []string{}
			available = getAvailableMacAddresses(clones, config)
			So(len(available), ShouldEqual, 0)
		})

		Convey("cloneUpTo should clone no VM if up < existing", func() {
			created, err := cloneUpTo(vmrun, config, clones, 3)
			So(err, ShouldBeNil)
			So(created, ShouldNotBeNil)
			So(len(created), ShouldEqual, 0)
			created, err = cloneUpTo(vmrun, config, clones, 2)
			So(err, ShouldBeNil)
			So(created, ShouldNotBeNil)
			So(len(created), ShouldEqual, 0)
			created, err = cloneUpTo(vmrun, config, clones, 1)
			So(err, ShouldBeNil)
			So(created, ShouldNotBeNil)
			So(len(created), ShouldEqual, 0)
			created, err = cloneUpTo(vmrun, config, clones, 0)
			So(err, ShouldBeNil)
			So(created, ShouldNotBeNil)
			So(len(created), ShouldEqual, 0)
			created, err = cloneUpTo(vmrun, config, clones, -10)
			So(err, ShouldBeNil)
			So(created, ShouldNotBeNil)
			So(len(created), ShouldEqual, 0)
		})

		Convey("cloneUpTo should correctly create 1 clone if allowed", func() {
			So(len(vmrun.TemplateSnapshots), ShouldEqual, 0)
			created, err := cloneUpTo(vmrun, config, clones, 4)
			So(err, ShouldBeNil)

			// Check cloneUpTo result array
			So(created, ShouldNotBeNil)
			So(len(created), ShouldEqual, 1)
			So(created[0], ShouldEqual, fmt.Sprintf(
				"%s-%s", config.Prefix, "A4B4C4D4E4F4"))

			// Check vmrun CloneFolder
			So(len(vmrun.CloneFolderVMs), ShouldEqual, 4)
			path := fmt.Sprintf("/tmp/vmlcmclones/%s-%s.vmwarevm/%s-%s.vmx",
				config.Prefix, "A4B4C4D4E4F4", config.Prefix, "A4B4C4D4E4F4")
			So(vmrun.CloneFolderVMs[3], ShouldEqual, path)

			// Check created snapshot
			So(vmrun.TemplateSnapshots, ShouldNotBeNil)
			So(len(vmrun.TemplateSnapshots), ShouldEqual, 1)
			So(vmrun.TemplateSnapshots[0], ShouldContainSubstring, config.Prefix)
		})

		Convey("cloneUpTo should correctly create 2 clones if allowed", func() {
			created, err := cloneUpTo(vmrun, config, clones, 5)
			So(err, ShouldBeNil)

			// Check cloneUpTo result array
			So(created, ShouldNotBeNil)
			So(len(created), ShouldEqual, 2)
			So(created[0], ShouldEqual, fmt.Sprintf(
				"%s-%s", config.Prefix, "A4B4C4D4E4F4"))
			So(created[1], ShouldEqual, fmt.Sprintf(
				"%s-%s", config.Prefix, "A5B5C5D5E5F5"))

			// Check vmrun CloneFolder
			path := fmt.Sprintf("/tmp/vmlcmclones/%s-%s.vmwarevm/%s-%s.vmx",
				config.Prefix, "A4B4C4D4E4F4", config.Prefix, "A4B4C4D4E4F4")
			So(vmrun.CloneFolderVMs[3], ShouldEqual, path)
			path = fmt.Sprintf("/tmp/vmlcmclones/%s-%s.vmwarevm/%s-%s.vmx",
				config.Prefix, "A5B5C5D5E5F5", config.Prefix, "A5B5C5D5E5F5")
			So(vmrun.CloneFolderVMs[4], ShouldEqual, path)

			// Check created snapshot
			So(vmrun.TemplateSnapshots, ShouldNotBeNil)
			So(len(vmrun.TemplateSnapshots), ShouldEqual, 1)
			So(vmrun.TemplateSnapshots[0], ShouldContainSubstring, config.Prefix)
		})

		Convey("cloneUpTo should create only 2 clones even though 3 are demanded", func() {
			created, err := cloneUpTo(vmrun, config, clones, 6)
			So(err, ShouldBeNil)

			// Check cloneUpTo result array
			So(created, ShouldNotBeNil)
			So(len(created), ShouldEqual, 2)
			So(created[0], ShouldEqual, fmt.Sprintf(
				"%s-%s", config.Prefix, "A4B4C4D4E4F4"))
			So(created[1], ShouldEqual, fmt.Sprintf(
				"%s-%s", config.Prefix, "A5B5C5D5E5F5"))

			// Check vmrun CloneFolder
			path := fmt.Sprintf("/tmp/vmlcmclones/%s-%s.vmwarevm/%s-%s.vmx",
				config.Prefix, "A4B4C4D4E4F4", config.Prefix, "A4B4C4D4E4F4")
			So(vmrun.CloneFolderVMs[3], ShouldEqual, path)
			path = fmt.Sprintf("/tmp/vmlcmclones/%s-%s.vmwarevm/%s-%s.vmx",
				config.Prefix, "A5B5C5D5E5F5", config.Prefix, "A5B5C5D5E5F5")
			So(vmrun.CloneFolderVMs[4], ShouldEqual, path)

			// Check created snapshot
			So(vmrun.TemplateSnapshots, ShouldNotBeNil)
			So(len(vmrun.TemplateSnapshots), ShouldEqual, 1)
			So(vmrun.TemplateSnapshots[0], ShouldContainSubstring, config.Prefix)
		})

		Convey("cloneUpTo should create a snapshot if only invalid snapshots are existing", func() {
			vmrun.TemplateSnapshots = []string{
				"existing-but-wrong-snapshot-1",
				"existing-but-wrong-snapshot-2",
			}

			_, err := cloneUpTo(vmrun, config, clones, 5)
			So(err, ShouldBeNil)

			// Check created snapshot
			So(vmrun.TemplateSnapshots, ShouldNotBeNil)
			So(len(vmrun.TemplateSnapshots), ShouldEqual, 3)
			So(vmrun.TemplateSnapshots[2], ShouldContainSubstring, config.Prefix)
		})

		Convey("cloneUpTo should not create a snapshot if a valid exists", func() {
			vmrun.TemplateSnapshots = []string{
				"existing-but-wrong-snapshot-1",
				"existing-but-wrong-snapshot-2",
				fmt.Sprintf("%s-1234567", config.Prefix),
			}
			_, err := cloneUpTo(vmrun, config, clones, 5)
			So(err, ShouldBeNil)
			So(vmrun.TemplateSnapshots, ShouldNotBeNil)
			So(len(vmrun.TemplateSnapshots), ShouldEqual, 3)
		})
	})
}
Пример #4
0
func TestVerification(t *testing.T) {
	Convey("Given a verification controller", t, func(context C) {
		// $REPO/samples/config

		Convey("isValidMacAddress should identify valid mac addresses", func() {
			So(isValidMacAddress("a1:b1:c1:d1:e1:f1"), ShouldBeTrue)
			So(isValidMacAddress("11:22:33:44:55:66"), ShouldBeTrue)
		})

		Convey("isValidMacAddress should detect invalid mac addresses", func() {
			So(isValidMacAddress(""), ShouldBeFalse)
			So(isValidMacAddress(":"), ShouldBeFalse)
			So(isValidMacAddress("a1:b1:c1:d1:e1"), ShouldBeFalse)
		})

		Convey("isAbsolutePath should identify valid absolute paths", func() {
			So(isAbsolutePath("/"), ShouldBeTrue)
			So(isAbsolutePath("/blabla/foo/32/bar.xyz/.4"), ShouldBeTrue)
		})

		Convey("isAbsolutePath should detect invalid absolute paths", func() {
			So(isAbsolutePath("./"), ShouldBeFalse)
			So(isAbsolutePath("foo"), ShouldBeFalse)
			So(isAbsolutePath(".foo"), ShouldBeFalse)
			So(isAbsolutePath("./blabla/foo/32/bar.xyz/.4"), ShouldBeFalse)
		})

		Convey("isValidPath should identify valid absolute paths", func() {
			So(isValidPath("/tmp"), ShouldBeTrue)
			So(isValidPath("/tmp/"), ShouldBeTrue)
		})

		Convey("isValidPath should detect invalid absolute paths", func() {
			So(isValidPath("./"), ShouldBeFalse)
			So(isValidPath("foo"), ShouldBeFalse)
			So(isValidPath("/not/existing/path"), ShouldBeFalse)
			So(isValidPath("../samples/config/valid1.json"), ShouldBeFalse)
		})
	})

	Convey("Verify should successfully verify various configurations", t, func() {
		createTestVerifyFolders()
		createTestVerifyTemplate()
		createTestVerifyVmrun()
		defer deleteTestVerifyFolders()

		vmrun := vmware.NewMockVmrun()
		config := new(util.LCMConfiguration)
		config.ClonesDirectory = "/tmp/vmlcmverify/clones/"
		config.TemplatePath = "/tmp/vmlcmverify/test.vmx"
		config.Vmrun = "/tmp/vmlcmverify/vmrun"
		config.Prefix = "Pom2015"
		config.Addresses = []string{
			"a1:b1:c1:d1:e1:f1",
			"a2:b2:c2:d2:e2:f2",
		}

		// Success
		err := Verify(nil, vmrun, config)
		So(err, ShouldBeNil)

		// Adding invalid MAC address
		config.Addresses = append(config.Addresses, "keine_valide_mac")
		err = Verify(nil, vmrun, config)
		So(err, ShouldNotBeNil)

		// Vmrun deletion
		config.Addresses = config.Addresses[:len(config.Addresses)-1]
		os.Remove("/tmp/vmlcmverify/vmrun")
		err = Verify(nil, vmrun, config)
		So(err, ShouldNotBeNil)

		// Template deletion
		createTestVerifyVmrun()
		os.Remove("/tmp/vmlcmverify/test.vmx")
		err = Verify(nil, vmrun, config)
		So(err, ShouldNotBeNil)

		// Clones directory deletion
		createTestVerifyTemplate()
		os.Remove("/tmp/vmlcmverify/clones")
		err = Verify(nil, vmrun, config)
		So(err, ShouldNotBeNil)

		// Invalid template file extension
		createTestVerifyFolders()
		os.Remove("/tmp/vmlcmverify/test.vmx")
		ioutil.WriteFile("/tmp/vmlcmverify/test", []byte(""), 0644)
		config.TemplatePath = "/tmp/vmlcmverify/test"
		err = Verify(nil, vmrun, config)
		So(err, ShouldNotBeNil)

		// File as clone folder
		os.Remove("/tmp/vmlcmverify/test")
		createTestVerifyTemplate()
		config.TemplatePath = "/tmp/vmlcmverify/test.vmx"
		os.Remove("/tmp/vmlcmverify/clones")
		ioutil.WriteFile("/tmp/vmlcmverify/clones", []byte(""), 0644)
		err = Verify(nil, vmrun, config)
		So(err, ShouldNotBeNil)
	})
}