Skip to content

Commit c0530a2

Browse files
committed
stembuild: cleanup go code in specs
1 parent 277c451 commit c0530a2

File tree

1 file changed

+13
-35
lines changed

1 file changed

+13
-35
lines changed

stembuild/iaas_cli/iaas_clients/vcenter_govmomi_contract_test.go

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,26 @@ func envMustExist(variableName string) string {
2525
}
2626

2727
var _ = Describe("VcenterClient", func() {
28-
Describe("StartProgram", func() {
28+
const powershell = "C:\\Windows\\System32\\WindowsPowerShell\\V1.0\\powershell.exe"
2929

30-
var (
31-
managerFactory *vcenterclientfactory.ManagerFactory
32-
factoryConfig *vcenterclientfactory.FactoryConfig
33-
)
30+
var (
31+
ctx = context.TODO()
32+
managerFactory = &vcenterclientfactory.ManagerFactory{}
33+
factoryConfig *vcenterclientfactory.FactoryConfig
34+
)
3435

36+
Describe("StartProgram", func() {
3537
BeforeEach(func() {
36-
3738
factoryConfig = &vcenterclientfactory.FactoryConfig{
3839
VCenterServer: envMustExist(VcenterUrl),
3940
Username: envMustExist(VcenterUsername),
4041
Password: envMustExist(VcenterPassword),
4142
ClientCreator: &vcenterclientfactory.ClientCreator{},
4243
FinderCreator: &vcenterclientfactory.GovmomiFinderCreator{},
4344
}
44-
45-
managerFactory = &vcenterclientfactory.ManagerFactory{}
4645
})
4746

4847
ExpectProgramToStartAndExitSuccessfully := func() {
49-
50-
ctx := context.TODO()
51-
5248
vCenterManager, err := managerFactory.VCenterManager(ctx)
5349
Expect(err).ToNot(HaveOccurred())
5450

@@ -64,7 +60,6 @@ var _ = Describe("VcenterClient", func() {
6460

6561
time.Sleep(10 * time.Second)
6662

67-
powershell := "C:\\Windows\\System32\\WindowsPowerShell\\V1.0\\powershell.exe"
6863
pid, err := guestManager.StartProgramInGuest(ctx, powershell, "Exit 59")
6964
Expect(err).ToNot(HaveOccurred())
7065

@@ -77,6 +72,7 @@ var _ = Describe("VcenterClient", func() {
7772
It("Starts a program and returns its exit code", func() {
7873
factoryConfig.RootCACertPath = ""
7974
managerFactory.SetConfig(*factoryConfig)
75+
8076
ExpectProgramToStartAndExitSuccessfully()
8177
})
8278
})
@@ -114,7 +110,6 @@ var _ = Describe("VcenterClient", func() {
114110
factoryConfig.RootCACertPath = fakeCertPath
115111
managerFactory.SetConfig(*factoryConfig)
116112

117-
ctx := context.TODO()
118113
_, err = managerFactory.VCenterManager(ctx)
119114
Expect(err).To(HaveOccurred())
120115
Expect(err.Error()).To(ContainSubstring("cannot be used as a trusted CA certificate"))
@@ -123,13 +118,7 @@ var _ = Describe("VcenterClient", func() {
123118
})
124119

125120
Describe("DownloadFileFromGuest", func() {
126-
var (
127-
managerFactory *vcenterclientfactory.ManagerFactory
128-
factoryConfig *vcenterclientfactory.FactoryConfig
129-
fileToDownload string
130-
expectedContents string
131-
guestManager *guest_manager.GuestManager
132-
)
121+
var guestManager *guest_manager.GuestManager
133122

134123
BeforeEach(func() {
135124
factoryConfig = &vcenterclientfactory.FactoryConfig{
@@ -139,14 +128,9 @@ var _ = Describe("VcenterClient", func() {
139128
ClientCreator: &vcenterclientfactory.ClientCreator{},
140129
FinderCreator: &vcenterclientfactory.GovmomiFinderCreator{},
141130
}
142-
managerFactory = &vcenterclientfactory.ManagerFactory{}
143131
factoryConfig.RootCACertPath = ""
144132
managerFactory.SetConfig(*factoryConfig)
145133

146-
fileToDownload = "C:\\Windows\\dummy.txt"
147-
expectedContents = "infinite content"
148-
149-
ctx := context.TODO()
150134
vCenterManager, err := managerFactory.VCenterManager(ctx)
151135
Expect(err).ToNot(HaveOccurred())
152136

@@ -164,10 +148,10 @@ var _ = Describe("VcenterClient", func() {
164148
})
165149

166150
Context("specified file exists", func() {
167-
BeforeEach(func() {
168-
ctx := context.TODO()
151+
var fileToDownload = "C:\\Windows\\dummy.txt"
152+
var expectedContents = "infinite content"
169153

170-
powershell := "C:\\Windows\\System32\\WindowsPowerShell\\V1.0\\powershell.exe"
154+
BeforeEach(func() {
171155
pid, err := guestManager.StartProgramInGuest(ctx, powershell, fmt.Sprintf("'%s' | Set-Content %s", expectedContents, fileToDownload))
172156
Expect(err).ToNot(HaveOccurred())
173157

@@ -177,18 +161,13 @@ var _ = Describe("VcenterClient", func() {
177161
})
178162

179163
It("downloads the file", func() {
180-
ctx := context.TODO()
181164
fileContents, _, err := guestManager.DownloadFileInGuest(ctx, fileToDownload)
182165
Expect(err).NotTo(HaveOccurred())
183166

184167
Eventually(BufferReader(fileContents)).Should(Say(expectedContents))
185-
186168
})
187169

188170
AfterEach(func() {
189-
ctx := context.TODO()
190-
191-
powershell := "C:\\Windows\\System32\\WindowsPowerShell\\V1.0\\powershell.exe"
192171
pid, err := guestManager.StartProgramInGuest(ctx, powershell, fmt.Sprintf("rm %s", fileToDownload))
193172
Expect(err).ToNot(HaveOccurred())
194173

@@ -200,8 +179,7 @@ var _ = Describe("VcenterClient", func() {
200179

201180
Context("specified file does not exist", func() {
202181
It("returns an error", func() {
203-
ctx := context.TODO()
204-
_, _, err := guestManager.DownloadFileInGuest(ctx, fileToDownload)
182+
_, _, err := guestManager.DownloadFileInGuest(ctx, "C:\\Windows\\non-existent-file.txt")
205183
Expect(err.Error()).To(ContainSubstring("vcenter_client - unable to download file"))
206184
})
207185
})

0 commit comments

Comments
 (0)