github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/package_stemcell/ovftool/ovftool_windows_test.go (about) 1 package ovftool_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 8 "github.com/cloudfoundry-incubator/stembuild/package_stemcell/ovftool" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "golang.org/x/sys/windows/registry" 12 ) 13 14 var _ = Describe("ovftool", func() { 15 16 Context("vmwareInstallPaths", func() { 17 18 It("returns install paths when given valid set of registry key paths", func() { 19 20 keypaths := []string{ 21 `SOFTWARE\Wow6432Node\VMware, Inc.\VMware Workstation`, 22 `SOFTWARE\Wow6432Node\VMware, Inc.\VMware OVF Tool`, 23 `SOFTWARE\VMware, Inc.\VMware Workstation`, 24 `SOFTWARE\VMware, Inc.\VMware OVF Tool`, 25 } 26 27 searchPaths, err := ovftool.VmwareInstallPaths(keypaths) 28 29 Expect(err).ToNot(HaveOccurred()) 30 Expect(searchPaths).ToNot(BeNil()) 31 }) 32 33 It("fails when given invalid registry key path", func() { 34 keypaths := []string{`\SOFTWARE\faketempkey`} 35 36 _, err := ovftool.VmwareInstallPaths(keypaths) 37 38 Expect(err).To(HaveOccurred()) 39 }) 40 41 It("fails when given empty set of registry keys paths", func() { 42 var keypaths []string 43 44 _, err := ovftool.VmwareInstallPaths(keypaths) 45 46 Expect(err).To(HaveOccurred()) 47 }) 48 49 Context("when given a valid registry keypath that does not have an installPath[64] value", func() { 50 var key registry.Key 51 var keypaths []string 52 53 BeforeEach(func() { 54 var err error 55 key, err = registry.OpenKey(registry.CURRENT_USER, `SOFTWARE`, registry.WRITE) 56 Expect(err).ToNot(HaveOccurred()) 57 _, exists, err := registry.CreateKey(key, `faketempkey`, registry.WRITE) 58 Expect(err).ToNot(HaveOccurred()) 59 Expect(exists).To(BeFalse()) 60 keypaths = []string{`\SOFTWARE\faketempkey`} 61 }) 62 63 AfterEach(func() { 64 Expect(key).ToNot(BeNil()) 65 err := registry.DeleteKey(key, `faketempkey`) 66 Expect(err).ToNot(HaveOccurred()) 67 }) 68 69 It("fails", func() { 70 71 _, err := ovftool.VmwareInstallPaths(keypaths) 72 73 Expect(err).To(HaveOccurred()) 74 }) 75 }) 76 }) 77 78 Context("Ovftool", func() { 79 var oldPath string 80 81 BeforeEach(func() { 82 oldPath = os.Getenv("PATH") 83 os.Unsetenv("PATH") 84 }) 85 86 AfterEach(func() { 87 os.Setenv("PATH", oldPath) 88 }) 89 90 It("when ovftool is on the path, its path is returned", func() { 91 tmpDir, err := ioutil.TempDir(os.TempDir(), "ovftmp") 92 Expect(err).ToNot(HaveOccurred()) 93 err = ioutil.WriteFile(filepath.Join(tmpDir, "ovftool.exe"), []byte{}, 0700) 94 Expect(err).ToNot(HaveOccurred()) 95 os.Setenv("PATH", tmpDir) 96 97 ovfPath, err := ovftool.Ovftool([]string{}) 98 os.RemoveAll(tmpDir) 99 100 Expect(err).ToNot(HaveOccurred()) 101 Expect(ovfPath).To(Equal(filepath.Join(tmpDir, "ovftool.exe"))) 102 }) 103 104 It("fails when given invalid set of install paths", func() { 105 tmpDir, err := ioutil.TempDir(os.TempDir(), "ovftmp") 106 Expect(err).ToNot(HaveOccurred()) 107 108 _, err = ovftool.Ovftool([]string{tmpDir}) 109 os.RemoveAll(tmpDir) 110 111 Expect(err).To(HaveOccurred()) 112 }) 113 114 It("fails when given empty set of install paths", func() { 115 _, err := ovftool.Ovftool([]string{}) 116 Expect(err).To(HaveOccurred()) 117 }) 118 119 Context("when ovftool is installed", func() { 120 var tmpDir, dummyDir string 121 122 BeforeEach(func() { 123 var err error 124 tmpDir, err = ioutil.TempDir(os.TempDir(), "ovftmp") 125 Expect(err).ToNot(HaveOccurred()) 126 dummyDir, err = ioutil.TempDir(os.TempDir(), "trashdir") 127 Expect(err).ToNot(HaveOccurred()) 128 err = ioutil.WriteFile(filepath.Join(tmpDir, "ovftool.exe"), []byte{}, 0644) 129 Expect(err).ToNot(HaveOccurred()) 130 }) 131 132 AfterEach(func() { 133 os.RemoveAll(tmpDir) 134 os.RemoveAll(dummyDir) 135 }) 136 137 It("Returns the path to the ovftool", func() { 138 ovfPath, err := ovftool.Ovftool([]string{"notrealdir", dummyDir, tmpDir}) 139 140 Expect(err).ToNot(HaveOccurred()) 141 Expect(ovfPath).To(Equal(filepath.Join(tmpDir, "ovftool.exe"))) 142 }) 143 }) 144 }) 145 })