forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpip_test.go
380 lines (328 loc) · 11.9 KB
/
pip_test.go
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"testing"
gofrogcmd "github.com/jfrog/gofrog/io"
"github.com/jfrog/jfrog-cli-go/artifactory/commands/pip"
piputils "github.com/jfrog/jfrog-cli-go/artifactory/utils/pip"
"github.com/jfrog/jfrog-cli-go/inttestutils"
"github.com/jfrog/jfrog-cli-go/utils/cliutils"
"github.com/jfrog/jfrog-cli-go/utils/tests"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
)
type PipCmd struct {
Command string
Options []string
}
func TestPipInstall(t *testing.T) {
// Init pip.
initPipTest(t)
// Init CLI without credential flags.
artifactoryCli = tests.NewJfrogCli(execMain, "jfrog rt", "")
// Add virtual-environment path to 'PATH' for executing all pip and python commands inside the virtual-environment.
pathValue := setPathEnvForPipInstall(t)
if t.Failed() {
t.FailNow()
}
defer os.Setenv("PATH", pathValue)
// Check pip env is clean.
validateEmptyPipEnv(t)
// Populate cli config with 'default' server.
oldHomeDir, newHomeDir := prepareHomeDir(t)
defer os.Setenv(cliutils.HomeDir, oldHomeDir)
defer os.RemoveAll(newHomeDir)
// Create test cases.
allTests := []struct {
name string
project string
outputFolder string
moduleId string
args []string
expectedDependencies int
cleanAfterExecution bool
}{
{"setuppy", "setuppyproject", "setuppy", "jfrog-python-example", []string{"pip-install", ".", "--no-cache-dir", "--force-reinstall", "--build-name=" + tests.PipBuildName}, 3, true},
{"setuppy-verbose", "setuppyproject", "setuppy-verbose", "jfrog-python-example", []string{"pip-install", ".", "--no-cache-dir", "--force-reinstall", "-v", "--build-name=" + tests.PipBuildName}, 3, true},
{"setuppy-with-module", "setuppyproject", "setuppy-with-module", "setuppy-with-module", []string{"pip-install", ".", "--no-cache-dir", "--force-reinstall", "--build-name=" + tests.PipBuildName, "--module=setuppy-with-module"}, 3, true},
{"requirements", "requirementsproject", "requirements", tests.PipBuildName, []string{"pip-install", "-r", "requirements.txt", "--no-cache-dir", "--force-reinstall", "--build-name=" + tests.PipBuildName}, 5, true},
{"requirements-verbose", "requirementsproject", "requirements-verbose", tests.PipBuildName, []string{"pip-install", "-r", "requirements.txt", "--no-cache-dir", "--force-reinstall", "-v", "--build-name=" + tests.PipBuildName}, 5, false},
{"requirements-use-cache", "requirementsproject", "requirements-verbose", "requirements-verbose-use-cache", []string{"pip-install", "-r", "requirements.txt", "--module=requirements-verbose-use-cache", "--build-name=" + tests.PipBuildName}, 5, true},
}
// Run test cases.
for buildNumber, test := range allTests {
t.Run(test.name, func(t *testing.T) {
testPipCmd(t, test.name, createPipProject(t, test.outputFolder, test.project), strconv.Itoa(buildNumber), test.moduleId, test.expectedDependencies, test.args)
if test.cleanAfterExecution {
// cleanup
inttestutils.DeleteBuild(artifactoryDetails.Url, tests.PipBuildName, artHttpDetails)
cleanPipTest(t, test.name)
}
})
}
cleanPipTest(t, "cleanup")
tests.CleanFileSystem()
}
func testPipCmd(t *testing.T, outputFolder, projectPath, buildNumber, module string, expectedDependencies int, args []string) {
wd, err := os.Getwd()
if err != nil {
t.Error(err)
}
err = os.Chdir(projectPath)
if err != nil {
t.Error(err)
}
defer os.Chdir(wd)
args = append(args, "--build-number="+buildNumber)
err = artifactoryCli.Exec(args...)
if err != nil {
t.Errorf("Failed executing pip-install command: %s", err.Error())
cleanPipTest(t, outputFolder)
return
}
artifactoryCli.Exec("bp", tests.PipBuildName, buildNumber)
buildInfo := inttestutils.GetBuildInfo(artifactoryDetails.Url, tests.PipBuildName, buildNumber, t, artHttpDetails)
if buildInfo.Modules == nil || len(buildInfo.Modules) == 0 {
t.Error("Pip build info was not generated correctly, no modules were created.")
}
if expectedDependencies != len(buildInfo.Modules[0].Dependencies) {
t.Error("Incorrect number of artifacts found in the build-info, expected:", expectedDependencies, " Found:", len(buildInfo.Modules[0].Dependencies))
}
if module != buildInfo.Modules[0].Id {
t.Error(fmt.Errorf("Expected module name %s, got %s", module, buildInfo.Modules[0].Id))
}
}
func cleanPipTest(t *testing.T, outFolder string) {
// Clean pip environment from installed packages.
pipFreezeCmd := &PipCmd{Command: "freeze", Options: []string{"--local"}}
out, err := gofrogcmd.RunCmdOutput(pipFreezeCmd)
if err != nil {
t.Fatal(err)
}
// If no packages to uninstall, return.
if out == "" {
return
}
// Save freeze output to file.
freezeTarget, err := fileutils.CreateFilePath(tests.Temp, outFolder+"-freeze.txt")
if err != nil {
t.Error(err)
}
file, err := os.Create(freezeTarget)
if err != nil {
t.Error(err)
}
defer file.Close()
_, err = file.Write([]byte(out))
if err != nil {
t.Error(err)
}
// Delete freezed packages.
pipUninstallCmd := &PipCmd{Command: "uninstall", Options: []string{"-y", "-r", freezeTarget}}
err = gofrogcmd.RunCmd(pipUninstallCmd)
if err != nil {
t.Fatal(err)
}
}
func createPipProject(t *testing.T, outFolder, projectName string) string {
projectSrc := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "pip", projectName)
projectTarget := filepath.Join(tests.Out, outFolder+"-"+projectName)
err := fileutils.CreateDirIfNotExist(projectTarget)
if err != nil {
t.Error(err)
}
// Copy pip-installation file.
err = fileutils.CopyDir(projectSrc, projectTarget, true)
if err != nil {
t.Error(err)
}
// Copy pip-config file.
configSrc := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "pip", "pip.yaml")
configTarget := filepath.Join(projectTarget, ".jfrog", "projects")
tests.ReplaceTemplateVariables(configSrc, configTarget)
return projectTarget
}
func initPipTest(t *testing.T) {
if !*tests.TestPip {
t.Skip("Skipping Pip test. To run Pip test add the '-test.pip=true' option.")
}
if !isRepoExist(tests.PypiRemoteRepo) {
t.Error("Pypi test remote repository doesn't exist.")
}
if !isRepoExist(tests.PypiVirtualRepo) {
t.Error("Pypi test virtual repository doesn't exist.")
}
if t.Failed() {
t.FailNow()
}
}
func setPathEnvForPipInstall(t *testing.T) string {
// Keep original value of 'PATH'.
pathValue, exists := os.LookupEnv("PATH")
if !exists {
t.Fatal("Couldn't find PATH variable, failing pip tests.")
}
// Append the path.
virtualEnvPath := *tests.PipVirtualEnv
if virtualEnvPath != "" {
var newPathValue string
if cliutils.IsWindows() {
newPathValue = fmt.Sprintf("%s;%s", virtualEnvPath, pathValue)
} else {
newPathValue = fmt.Sprintf("%s:%s", virtualEnvPath, pathValue)
}
err := os.Setenv("PATH", newPathValue)
if err != nil {
t.Fatal(err)
}
}
// Return original PATH value.
return pathValue
}
// Ensure that the provided pip virtual-environment is empty from installed packages.
func validateEmptyPipEnv(t *testing.T) {
//pipFreezeCmd := &PipFreezeCmd{Executable: "pip", Command: "freeze"}
pipFreezeCmd := &PipCmd{Command: "freeze", Options: []string{"--local"}}
out, err := gofrogcmd.RunCmdOutput(pipFreezeCmd)
if err != nil {
t.Fatal(err)
}
if out != "" {
t.Fatalf("Provided pip virtual-environment contains installed packages: %s\n. Please provide a clean environment.", out)
}
}
func (pfc *PipCmd) GetCmd() *exec.Cmd {
var cmd []string
cmd = append(cmd, "pip")
cmd = append(cmd, pfc.Command)
cmd = append(cmd, pfc.Options...)
return exec.Command(cmd[0], cmd[1:]...)
}
func (pfc *PipCmd) GetEnv() map[string]string {
return map[string]string{}
}
func (pfc *PipCmd) GetStdWriter() io.WriteCloser {
return nil
}
func (pfc *PipCmd) GetErrWriter() io.WriteCloser {
return nil
}
func TestPipDepsTree(t *testing.T) {
initPipTest(t)
// Add virtual-environment path to 'PATH' for executing all pip and python commands inside the virtual-environment.
pathValue := setPathEnvForPipInstall(t)
if t.Failed() {
t.FailNow()
}
defer os.Setenv("PATH", pathValue)
// Check pip env is clean.
validateEmptyPipEnv(t)
// Populate cli config with 'default' server.
oldHomeDir, newHomeDir := prepareHomeDir(t)
defer os.Setenv(cliutils.HomeDir, oldHomeDir)
defer os.RemoveAll(newHomeDir)
// Create test cases.
allTests := []struct {
name string
project string
outputFolder string
moduleId string
args []string
expectedDependencies int
cleanAfterExecution bool
}{
{"setuppy", "setuppyproject", "setuppy", "jfrog-python-example", []string{".", "--no-cache-dir", "--force-reinstall"}, 3, true},
{"setuppy-verbose", "setuppyproject", "setuppy-verbose", "jfrog-python-example", []string{".", "--no-cache-dir", "--force-reinstall", "-v"}, 3, true},
{"setuppy-with-module", "setuppyproject", "setuppy-with-module", "setuppy-with-module", []string{".", "--no-cache-dir", "--force-reinstall"}, 3, true},
{"requirements", "requirementsproject", "requirements", tests.PipBuildName, []string{"-r", "requirements.txt", "--no-cache-dir", "--force-reinstall"}, 5, true},
{"requirements-verbose", "requirementsproject", "requirements-verbose", tests.PipBuildName, []string{"-r", "requirements.txt", "--no-cache-dir", "--force-reinstall", "-v"}, 5, false},
{"requirements-use-cache", "requirementsproject", "requirements-verbose", "requirements-verbose-use-cache", []string{"-r", "requirements.txt"}, 5, true},
}
// Run test cases.
for _, test := range allTests {
t.Run(test.name, func(t *testing.T) {
testPipDepsTreeCmd(t, createPipProject(t, test.outputFolder, test.project), test.expectedDependencies, test.args)
if test.cleanAfterExecution {
// cleanup
cleanPipTest(t, test.name)
}
})
}
cleanPipTest(t, "cleanup")
tests.CleanFileSystem()
}
func testPipDepsTreeCmd(t *testing.T, projectPath string, expectedElements int, args []string) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir(projectPath)
if err != nil {
t.Fatal(err)
}
defer os.Chdir(wd)
// Get pip configuration.
pipConfig, err := piputils.GetPipConfiguration()
if err != nil {
t.Fatalf("Error occurred while attempting to read pip-configuration file: %s\n"+
"Please run 'jfrog rt pip-config' command prior to running 'jfrog rt pip-deps-tree'.", err.Error())
}
// Set arg values.
rtDetails, err := pipConfig.RtDetails()
if err != nil {
t.Fatal(err)
}
// Create command.
pipDepsTreeCmd := pip.NewPipDepTreeCommand()
pipDepsTreeCmd.SetRtDetails(rtDetails).SetRepo(pipConfig.TargetRepo()).SetArgs(args)
err = pipDepsTreeCmd.Run()
if err != nil {
t.Fatalf("Failed while executing pip-deps-tree command: %s", err)
}
// Check result elements.
treeJsonData, err := pipDepsTreeCmd.DepsTreeRoot.MarshalJSON()
if err != nil {
t.Fatalf("Failed parsing tree json: %s", err)
}
// Count dependencies.
var depsTreeTest []DependenciesTreeTest
err = json.Unmarshal(treeJsonData, &depsTreeTest)
if err != nil {
t.Error(err)
}
depsCount := countDependencies(depsTreeTest)
if expectedElements != depsCount {
t.Errorf("Incorrect number of dependencies found, expected: %d, found: %d", expectedElements, depsCount)
}
}
type DependenciesTreeTest struct {
Id string `json:"id,omitempty"`
DirectDependencies []DependenciesTreeTest `json:"dependencies,omitempty"`
}
func countDependencies(allDeps []DependenciesTreeTest) int {
depsMap := make(map[string]int)
// Iterate over dependencies, resolve and discover more dependencies.
index := -1
var currentDep string
for {
index++
// Check if should stop.
if len(allDeps) < index+1 {
break
}
currentDep = allDeps[index].Id
// Check if current dependency already resolved.
if _, ok := depsMap[currentDep]; ok {
// Already resolved.
continue
}
// Add currentDep dependencies for cound.
allDeps = append(allDeps, allDeps[index].DirectDependencies...)
}
return index
}