Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f521914

Browse files
committedOct 21, 2020
add tests
1 parent 991610f commit f521914

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed
 

‎ci/integration/envs_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package integration
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"regexp"
7+
"testing"
8+
9+
"cdr.dev/coder-cli/ci/tcli"
10+
)
11+
12+
// From Coder organization images
13+
const ubuntuImgID = "5f443b16-30652892427b955601330fa5"
14+
15+
func TestEnvsCLI(t *testing.T) {
16+
t.Parallel()
17+
18+
run(t, "coder-cli-env-tests", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) {
19+
headlessLogin(ctx, t, c)
20+
21+
// Ensure binary is present.
22+
c.Run(ctx, "which coder").Assert(t,
23+
tcli.Success(),
24+
tcli.StdoutMatches("/usr/sbin/coder"),
25+
tcli.StderrEmpty(),
26+
)
27+
28+
// Minimum args not received.
29+
c.Run(ctx, "coder envs create").Assert(t,
30+
tcli.StderrMatches(regexp.QuoteMeta("Error: accepts 1 arg(s), received 0")),
31+
tcli.Error(),
32+
)
33+
34+
// Successfully output help.
35+
c.Run(ctx, "coder envs create --help").Assert(t,
36+
tcli.Success(),
37+
tcli.StdoutMatches(regexp.QuoteMeta("Create a new environment under the active user.")),
38+
tcli.StderrEmpty(),
39+
)
40+
41+
// Successfully create environment.
42+
c.Run(ctx, "coder envs create --image "+ubuntuImgID+" test-ubuntu").Assert(t,
43+
tcli.Success(),
44+
// why does flog.Success write to stderr?
45+
tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"test-ubuntu\"")),
46+
)
47+
48+
// Invalid environment name should fail.
49+
c.Run(ctx, "coder envs create --image "+ubuntuImgID+" this-IS-an-invalid-EnvironmentName").Assert(t,
50+
tcli.Error(),
51+
tcli.StderrMatches(regexp.QuoteMeta("HTTP/2.0 400 Bad Request")),
52+
tcli.StderrMatches(regexp.QuoteMeta("environment name must conform to regex ^[a-z0-9]([a-z0-9-]+)?")),
53+
)
54+
55+
// Successfully provision environment with fractional resource amounts
56+
c.Run(ctx, fmt.Sprintf(`coder envs create -i %s -c 1.2 -m 1.4 non-whole-resource-amounts`, ubuntuImgID)).Assert(t,
57+
tcli.Success(),
58+
tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"non-whole-resource-amounts\"")),
59+
)
60+
61+
// Image does not exist should fail.
62+
c.Run(ctx, "coder envs create --image does-not-exist env-will-not-be-created").Assert(t,
63+
tcli.Error(),
64+
tcli.StderrMatches(regexp.QuoteMeta("does not exist")),
65+
)
66+
})
67+
}

‎internal/cmd/envs.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ coder envs create \
167167
my-env-name
168168
`,
169169
RunE: func(cmd *cobra.Command, args []string) error {
170-
// We don't need an empty-ness check on img because its value is propogated by a required flag.
171-
// cobra.ExactArgs(1) ensures our name value can't panic on an out of bounds.
170+
if img == "" {
171+
return xerrors.New("image id unset")
172+
}
173+
// ExactArgs(1) ensures our name value can't panic on an out of bounds.
172174
createReq := &coder.CreateEnvironmentRequest{
173175
Name: args[0],
174176
ImageID: img,

0 commit comments

Comments
 (0)
Failed to load comments.