Skip to content

Commit 543d94b

Browse files
committed
Fixing tests for ci
Signed-off-by: Ryan Swanson <ryan.swanson@loft.sh>
1 parent c6f59d2 commit 543d94b

16 files changed

Lines changed: 225 additions & 196 deletions

File tree

dist/npm/index.js

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env node
22
const fs = require("fs");
3+
const https = require("https");
34
const path = require("path");
45
const execSync = require("child_process").execSync;
5-
const fetch = require("node-fetch");
6-
const Spinner = require("cli-spinner").Spinner;
7-
const inquirer = require('inquirer');
8-
const findProcess = require('find-process');
6+
7+
const getFetch = () => require("node-fetch");
8+
const getSpinner = () => require("cli-spinner").Spinner;
9+
const getInquirer = () => require("inquirer");
10+
const getFindProcess = () => require("find-process");
911

1012
const downloadPathTemplate =
1113
"https://github.com/devspace-sh/devspace/releases/download/v{{version}}/devspace-{{platform}}-{{arch}}";
@@ -80,43 +82,27 @@ const sanitizeVersion = function(version) {
8082

8183
const getLatestVersion = function (callback) {
8284
const releasesURL = "https://github.com/devspace-sh/devspace/releases/latest";
85+
https.get(releasesURL, { headers: requestHeaders }, function (res) {
86+
const redirectUrl = res.headers.location;
87+
const matches = redirectUrl && /\/tag\/(.*)$/.exec(redirectUrl);
8388

84-
fetch(releasesURL, { headers: requestHeaders, redirect: false })
85-
.then(function (res) {
86-
if (!res.ok) {
87-
console.error(
88-
"Error requesting URL " +
89-
releasesURL +
90-
" (Status Code: " +
91-
res.status +
92-
")"
93-
);
94-
process.exit(1);
95-
}
96-
97-
const redirectUrl = res.url
98-
if (redirectUrl == null) {
99-
throw new Error('Error fetching latest version')
100-
}
101-
102-
const matches = /\/tag\/(.*)$/.exec(redirectUrl)
103-
if (!matches || matches.length !== 2) {
104-
throw new Error('Error fetching latest version')
105-
}
106-
107-
const latestVersion = matches[1].replace('v', '')
108-
if (latestVersion) {
109-
callback(latestVersion);
110-
} else {
111-
console.error("Unable to identify latest devspace version");
112-
process.exit(2);
113-
}
114-
})
115-
.catch(function (err) {
89+
if (!matches || matches.length !== 2) {
11690
console.error("Error requesting URL " + releasesURL);
117-
console.error(err)
11891
process.exit(1);
119-
})
92+
}
93+
94+
const latestVersion = matches[1].replace('v', '')
95+
if (latestVersion) {
96+
callback(latestVersion);
97+
} else {
98+
console.error("Unable to identify latest devspace version");
99+
process.exit(2);
100+
}
101+
}).on("error", function (err) {
102+
console.error("Error requesting URL " + releasesURL);
103+
console.error(err)
104+
process.exit(1);
105+
})
120106
};
121107

122108
if (action === "update-version") {
@@ -311,6 +297,7 @@ let continueProcess = function (askRemoveGlobalFolder) {
311297
removeScripts(true);
312298

313299
if (askRemoveGlobalFolder && process.stdout.isTTY) {
300+
const inquirer = getInquirer();
314301
let removeGlobalFolder = function () {
315302
try {
316303
let homedir = require('os').homedir();
@@ -366,6 +353,7 @@ let continueProcess = function (askRemoveGlobalFolder) {
366353
};
367354

368355
const downloadRelease = function (version) {
356+
const fetch = getFetch();
369357
let downloadPath = downloadPathTemplate
370358
.replace("{{version}}", version)
371359
.replace("{{platform}}", platform)
@@ -377,6 +365,7 @@ let continueProcess = function (askRemoveGlobalFolder) {
377365

378366
console.log("Download DevSpace CLI release: " + downloadPath + "\n");
379367

368+
const Spinner = getSpinner();
380369
const spinner = new Spinner(
381370
"%s Downloading DevSpace CLI... (this may take a minute)"
382371
);
@@ -466,25 +455,30 @@ let continueProcess = function (askRemoveGlobalFolder) {
466455
}
467456

468457
if (process.ppid > 1) {
469-
findProcess('pid', process.ppid)
470-
.then(function (list) {
471-
if (list.length === 1 && list[0].ppid > 1) {
472-
findProcess('pid', list[0].ppid)
473-
.then(function (list) {
474-
if (list.length === 1 && /((npm-cli.js("|')\s+up(date)?)|(yarn.js("|')\s+(global\s+)?upgrade))\s+.*((\/)|(\\)|(\s))devspace((\/)|(\\)|(\s)|$)/.test(list[0].cmd)) {
475-
continueProcess(false);
476-
} else {
458+
try {
459+
const findProcess = getFindProcess();
460+
findProcess('pid', process.ppid)
461+
.then(function (list) {
462+
if (list.length === 1 && list[0].ppid > 1) {
463+
findProcess('pid', list[0].ppid)
464+
.then(function (list) {
465+
if (list.length === 1 && /((npm-cli.js("|')\s+up(date)?)|(yarn.js("|')\s+(global\s+)?upgrade))\s+.*((\/)|(\\)|(\s))devspace((\/)|(\\)|(\s)|$)/.test(list[0].cmd)) {
466+
continueProcess(false);
467+
} else {
468+
continueProcess(true);
469+
}
470+
}, function () {
477471
continueProcess(true);
478-
}
479-
}, function () {
480-
continueProcess(true);
481-
})
482-
} else {
472+
})
473+
} else {
474+
continueProcess(true);
475+
}
476+
}, function () {
483477
continueProcess(true);
484-
}
485-
}, function () {
486-
continueProcess(true);
487-
})
478+
})
479+
} catch (e) {
480+
continueProcess(true);
481+
}
488482
} else {
489483
continueProcess(true);
490484
}

e2e/framework/env.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package framework
2+
3+
import "os"
4+
5+
func Setenv(key, value string) {
6+
ExpectNoError(os.Setenv(key, value))
7+
}
8+
9+
func Unsetenv(key string) {
10+
ExpectNoError(os.Unsetenv(key))
11+
}

e2e/framework/helper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ func ExpectRemoteFileContents(imageSelector string, namespace string, filePath s
9292
ExpectNoErrorWithOffset(1, err)
9393
}
9494

95-
func ExpectLocalCurlContents(urlString string, contents string) {
95+
func ExpectLocalCurlContains(urlString string, contents string) {
9696
client := resty.New()
9797
err := wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
9898
resp, _ := client.R().
9999
EnableTrace().
100100
Get(urlString)
101-
return strings.TrimSpace(string(resp.Body())) == strings.TrimSpace(contents), nil
101+
return strings.Contains(strings.TrimSpace(string(resp.Body())), strings.TrimSpace(contents)), nil
102102
})
103103
ExpectNoErrorWithOffset(1, err)
104104
}
@@ -118,15 +118,15 @@ func ExpectContainerNameAndImageEqual(namespace, deploymentName, containerImage,
118118
ExpectNoErrorWithOffset(1, err)
119119
}
120120

121-
func ExpectRemoteCurlContents(imageSelector string, namespace string, urlString string, contents string) {
121+
func ExpectRemoteCurlContains(imageSelector string, namespace string, urlString string, contents string) {
122122
kubeClient, err := kube.NewKubeHelper()
123123
ExpectNoErrorWithOffset(1, err)
124124
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
125125
out, err := kubeClient.ExecByImageSelector(imageSelector, namespace, []string{"curl", urlString})
126126
if err != nil {
127127
return false, nil
128128
}
129-
return strings.TrimSpace(out) == strings.TrimSpace(contents), nil
129+
return strings.Contains(strings.TrimSpace(out), strings.TrimSpace(contents)), nil
130130
})
131131
ExpectNoErrorWithOffset(1, err)
132132
}

e2e/tests/build/build.go

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,7 @@ var _ = DevSpaceDescribe("build", func() {
246246
imageList, err := dockerClient.ImageList(ctx, image.ListOptions{})
247247
framework.ExpectNoError(err)
248248

249-
for _, image := range imageList {
250-
if len(image.RepoTags) > 0 && image.RepoTags[0] == "my-docker-username/helloworld-buildkit:latest" {
251-
err = nil
252-
break
253-
} else {
254-
err = errors.New("image not found")
255-
}
256-
}
249+
err = ensureImagePresent(imageList, "my-docker-username/helloworld-buildkit:latest")
257250
framework.ExpectNoError(err)
258251

259252
var stdout, stderr bytes.Buffer
@@ -290,14 +283,7 @@ var _ = DevSpaceDescribe("build", func() {
290283
imageList, err := dockerClient.ImageList(ctx, image.ListOptions{})
291284
framework.ExpectNoError(err)
292285

293-
for _, image := range imageList {
294-
if len(image.RepoTags) > 0 && image.RepoTags[0] == "my-docker-username/helloworld-buildkit:latest" {
295-
err = nil
296-
break
297-
} else {
298-
err = errors.New("image not found")
299-
}
300-
}
286+
err = ensureImagePresent(imageList, "my-docker-username/helloworld-buildkit:latest")
301287
framework.ExpectNoError(err)
302288
})
303289

@@ -342,14 +328,7 @@ var _ = DevSpaceDescribe("build", func() {
342328
imageList, err := dockerClient.ImageList(ctx, image.ListOptions{})
343329
framework.ExpectNoError(err)
344330

345-
for _, image := range imageList {
346-
if len(image.RepoTags) > 0 && image.RepoTags[0] == "my-docker-username/helloworld-custom-build:latest" {
347-
err = nil
348-
break
349-
} else {
350-
err = errors.New("image not found")
351-
}
352-
}
331+
err = ensureImagePresent(imageList, "my-docker-username/helloworld-custom-build:latest")
353332
framework.ExpectNoError(err)
354333
})
355334

@@ -377,14 +356,7 @@ var _ = DevSpaceDescribe("build", func() {
377356
imageList, err := dockerClient.ImageList(ctx, image.ListOptions{})
378357
framework.ExpectNoError(err)
379358
imageName := "my-docker-username/helloworld-dockerignore:latest"
380-
for _, image := range imageList {
381-
if image.RepoTags[0] == imageName {
382-
err = nil
383-
break
384-
} else {
385-
err = errors.New("image not found")
386-
}
387-
}
359+
err = ensureImagePresent(imageList, imageName)
388360
framework.ExpectNoError(err)
389361

390362
resp, err := dockerClient.ContainerCreate(ctx, &container.Config{
@@ -442,14 +414,7 @@ var _ = DevSpaceDescribe("build", func() {
442414
imageList, err := dockerClient.ImageList(ctx, image.ListOptions{})
443415
framework.ExpectNoError(err)
444416
imageName := "my-docker-username/helloworld-dockerignore-rel-path:latest"
445-
for _, image := range imageList {
446-
if image.RepoTags[0] == imageName {
447-
err = nil
448-
break
449-
} else {
450-
err = errors.New("image not found")
451-
}
452-
}
417+
err = ensureImagePresent(imageList, imageName)
453418
framework.ExpectNoError(err)
454419

455420
resp, err := dockerClient.ContainerCreate(ctx, &container.Config{
@@ -507,14 +472,7 @@ var _ = DevSpaceDescribe("build", func() {
507472
imageList, err := dockerClient.ImageList(ctx, image.ListOptions{})
508473
framework.ExpectNoError(err)
509474
imageName := "my-docker-username/helloworld-dockerignore-context:latest"
510-
for _, image := range imageList {
511-
if image.RepoTags[0] == imageName {
512-
err = nil
513-
break
514-
} else {
515-
err = errors.New("image not found")
516-
}
517-
}
475+
err = ensureImagePresent(imageList, imageName)
518476
framework.ExpectNoError(err)
519477

520478
resp, err := dockerClient.ContainerCreate(ctx, &container.Config{
@@ -556,6 +514,18 @@ func stdoutContains(stdout, content string) error {
556514
return fmt.Errorf("%s found in output", content)
557515
}
558516

517+
func ensureImagePresent(imageList []image.Summary, imageName string) error {
518+
for _, image := range imageList {
519+
for _, repoTag := range image.RepoTags {
520+
if repoTag == imageName {
521+
return nil
522+
}
523+
}
524+
}
525+
526+
return errors.New("image not found")
527+
}
528+
559529
func stderrContains(stderr, content string) error {
560530
if strings.Contains(stderr, content) {
561531
return nil

e2e/tests/command/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var _ = DevSpaceDescribe("command", func() {
7373
framework.ExpectNoError(err)
7474
defer framework.CleanupTempDir(initialDir, tempDir)
7575

76-
defer os.Unsetenv("KUBECONFIG")
76+
defer os.Unsetenv("KUBECONFIG") //nolint:errcheck
7777
err = os.Setenv("KUBECONFIG", filepath.Join(tempDir, "config"))
7878
framework.ExpectNoError(err)
7979

0 commit comments

Comments
 (0)