-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_docker_test.go
More file actions
256 lines (223 loc) · 8.07 KB
/
generate_docker_test.go
File metadata and controls
256 lines (223 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd_test
import (
"errors"
"path"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/codesphere-cloud/cs-go/api"
openapi_client "github.com/codesphere-cloud/cs-go/api/openapi_client"
"github.com/codesphere-cloud/cs-go/cli/cmd"
"github.com/codesphere-cloud/cs-go/pkg/ci"
"github.com/codesphere-cloud/cs-go/pkg/cs"
"github.com/codesphere-cloud/cs-go/pkg/exporter"
"github.com/codesphere-cloud/cs-go/pkg/git"
)
var _ = Describe("GenerateDocker", func() {
var (
memoryFs *cs.FileSystem
mockEnv *cmd.MockEnv
mockExporter *exporter.MockExporter
mockGit *git.MockGit
mockClient *cmd.MockClient
c *cmd.GenerateDockerCmd
wsId int
)
BeforeEach(func() {
memoryFs = cs.NewMemFileSystem()
mockEnv = cmd.NewMockEnv(GinkgoT())
mockExporter = exporter.NewMockExporter(GinkgoT())
mockGit = git.NewMockGit(GinkgoT())
mockClient = cmd.NewMockClient(GinkgoT())
defaultInput := "ci.yml"
defaultOutput := "./export"
wsId = 1
c = &cmd.GenerateDockerCmd{
Opts: &cmd.GenerateDockerOpts{
GenerateOpts: &cmd.GenerateOpts{
GlobalOptions: &cmd.GlobalOptions{
WorkspaceId: -1,
Env: mockEnv,
},
Input: defaultInput,
Output: defaultOutput,
},
},
}
})
Context("the baseimage is not provided", func() {
It("should return an error", func() {
clientFactory := func() (cmd.Client, error) { return mockClient, nil }
err := c.GenerateDocker(memoryFs, mockExporter, mockGit, clientFactory)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("baseimage is required"))
})
})
Context("a new input file and baseimage is provided", func() {
BeforeEach(func() {
c.Opts.BaseImage = "alpine:latest"
c.Opts.Input = "ci.dev.yml"
})
Context("initial file exists", func() {
var ciYmlPath string
JustBeforeEach(func() {
ciYmlPath = path.Join(c.Opts.RepoRoot, "ci.dev.yml")
_, err := memoryFs.Create(ciYmlPath)
Expect(err).To(Not(HaveOccurred()))
})
It("should not return an error", func() {
mockExporter.EXPECT().ReadYmlFile(ciYmlPath).Return(&ci.CiYml{}, nil)
mockExporter.EXPECT().ExportDockerArtifacts().Return(nil)
clientFactory := func() (cmd.Client, error) { return mockClient, nil }
err := c.GenerateDocker(memoryFs, mockExporter, mockGit, clientFactory)
Expect(err).To(Not(HaveOccurred()))
})
})
})
Context("CloneRepository", func() {
var (
clonedir string
repoUrl string
branch string
)
BeforeEach(func() {
c.Opts.BaseImage = "alpine:latest"
clonedir = "./test-clone"
repoUrl = "https://github.com/test/repo.git"
branch = "main"
})
Context("when workspace ID cannot be retrieved", func() {
It("should return an error", func() {
mockEnv.EXPECT().GetWorkspaceId().Return(0, errors.New("workspace ID not found"))
err := c.CloneRepository(mockClient, memoryFs, mockGit, clonedir)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to get workspace ID"))
})
})
Context("when workspace ID is set in the env var", func() {
BeforeEach(func() {
wsId = 99
})
It("should use the workspace ID from environment", func() {
cmd := &cmd.GenerateDockerCmd{
Opts: &cmd.GenerateDockerOpts{
GenerateOpts: &cmd.GenerateOpts{
GlobalOptions: &cmd.GlobalOptions{
Env: mockEnv,
WorkspaceId: -1,
// WorkspaceId is -1 (default), so it will use env var
},
},
BaseImage: "alpine:latest",
},
}
ws := api.Workspace{
GitUrl: *openapi_client.NewNullableString(&repoUrl),
}
mockEnv.EXPECT().GetWorkspaceId().Return(wsId, nil)
mockClient.EXPECT().GetWorkspace(99).Return(ws, nil)
mockGit.EXPECT().CloneRepository(memoryFs, repoUrl, branch, clonedir).Return(nil, nil)
err := cmd.CloneRepository(mockClient, memoryFs, mockGit, clonedir)
Expect(err).To(Not(HaveOccurred()))
})
})
Context("when workspace ID is set in the -w flag", func() {
It("should use the workspace ID from flag", func() {
flagWsId := 42
cmd := &cmd.GenerateDockerCmd{
Opts: &cmd.GenerateDockerOpts{
GenerateOpts: &cmd.GenerateOpts{
GlobalOptions: &cmd.GlobalOptions{
Env: mockEnv,
WorkspaceId: flagWsId,
},
},
BaseImage: "alpine:latest",
},
}
ws := api.Workspace{
GitUrl: *openapi_client.NewNullableString(&repoUrl),
}
mockClient.EXPECT().GetWorkspace(flagWsId).Return(ws, nil)
mockGit.EXPECT().CloneRepository(memoryFs, repoUrl, branch, clonedir).Return(nil, nil)
err := cmd.CloneRepository(mockClient, memoryFs, mockGit, clonedir)
Expect(err).To(Not(HaveOccurred()))
})
})
Context("when workspace cannot be retrieved", func() {
It("should return an error", func() {
mockEnv.EXPECT().GetWorkspaceId().Return(wsId, nil)
mockClient.EXPECT().GetWorkspace(wsId).Return(api.Workspace{}, errors.New("workspace not found"))
err := c.CloneRepository(mockClient, memoryFs, mockGit, clonedir)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to get workspace"))
})
})
Context("when workspace has no git repository", func() {
It("should return an error", func() {
ws := api.Workspace{}
mockEnv.EXPECT().GetWorkspaceId().Return(wsId, nil)
mockClient.EXPECT().GetWorkspace(wsId).Return(ws, nil)
err := c.CloneRepository(mockClient, memoryFs, mockGit, clonedir)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("workspace does not have a git repository"))
})
})
Context("when git clone fails", func() {
It("should return an error", func() {
ws := api.Workspace{
GitUrl: *openapi_client.NewNullableString(&repoUrl),
}
mockEnv.EXPECT().GetWorkspaceId().Return(wsId, nil)
mockClient.EXPECT().GetWorkspace(wsId).Return(ws, nil)
mockGit.EXPECT().CloneRepository(memoryFs, repoUrl, branch, clonedir).Return(nil, errors.New("clone failed"))
err := c.CloneRepository(mockClient, memoryFs, mockGit, clonedir)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to clone repository"))
})
})
Context("when cloning succeeds with default branch", func() {
It("should clone the repository", func() {
ws := api.Workspace{
GitUrl: *openapi_client.NewNullableString(&repoUrl),
}
mockEnv.EXPECT().GetWorkspaceId().Return(wsId, nil)
mockClient.EXPECT().GetWorkspace(wsId).Return(ws, nil)
mockGit.EXPECT().CloneRepository(memoryFs, repoUrl, branch, clonedir).Return(nil, nil)
err := c.CloneRepository(mockClient, memoryFs, mockGit, clonedir)
Expect(err).To(Not(HaveOccurred()))
})
})
Context("when workspace has initial branch set", func() {
It("should use the workspace's initial branch", func() {
customBranch := "develop"
ws := api.Workspace{
GitUrl: *openapi_client.NewNullableString(&repoUrl),
InitialBranch: *openapi_client.NewNullableString(&customBranch),
}
mockEnv.EXPECT().GetWorkspaceId().Return(wsId, nil)
mockClient.EXPECT().GetWorkspace(wsId).Return(ws, nil)
mockGit.EXPECT().CloneRepository(memoryFs, repoUrl, customBranch, clonedir).Return(nil, nil)
err := c.CloneRepository(mockClient, memoryFs, mockGit, clonedir)
Expect(err).To(Not(HaveOccurred()))
})
})
Context("when branch is explicitly set in options", func() {
It("should use the explicitly set branch", func() {
customBranch := "feature-branch"
workspaceBranch := "develop"
c.Opts.Branch = customBranch
ws := api.Workspace{
GitUrl: *openapi_client.NewNullableString(&repoUrl),
InitialBranch: *openapi_client.NewNullableString(&workspaceBranch),
}
mockEnv.EXPECT().GetWorkspaceId().Return(wsId, nil)
mockClient.EXPECT().GetWorkspace(wsId).Return(ws, nil)
mockGit.EXPECT().CloneRepository(memoryFs, repoUrl, customBranch, clonedir).Return(nil, nil)
err := c.CloneRepository(mockClient, memoryFs, mockGit, clonedir)
Expect(err).To(Not(HaveOccurred()))
})
})
})
})