Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit a4a9c35

Browse files
committed
Add env create|edit integration tests
1 parent cb3ac49 commit a4a9c35

File tree

3 files changed

+74
-27
lines changed

3 files changed

+74
-27
lines changed

ci/integration/envs_test.go

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,23 @@ package integration
22

33
import (
44
"context"
5+
"fmt"
6+
"math"
57
"regexp"
68
"testing"
79

810
"cdr.dev/coder-cli/ci/tcli"
11+
"cdr.dev/coder-cli/coder-sdk"
12+
"cdr.dev/slog/sloggers/slogtest/assert"
13+
"github.com/google/go-cmp/cmp"
914
)
1015

11-
// From Coder organization images
12-
// const ubuntuImgID = "5f443b16-30652892427b955601330fa5"
13-
1416
func TestEnvsCLI(t *testing.T) {
1517
t.Parallel()
1618

1719
run(t, "coder-cli-env-tests", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) {
1820
headlessLogin(ctx, t, c)
1921

20-
// Ensure binary is present.
21-
c.Run(ctx, "which coder").Assert(t,
22-
tcli.Success(),
23-
tcli.StdoutMatches("/usr/sbin/coder"),
24-
tcli.StderrEmpty(),
25-
)
26-
2722
// Minimum args not received.
2823
c.Run(ctx, "coder envs create").Assert(t,
2924
tcli.StderrMatches(regexp.QuoteMeta("accepts 1 arg(s), received 0")),
@@ -49,21 +44,72 @@ func TestEnvsCLI(t *testing.T) {
4944
tcli.Error(),
5045
)
5146

52-
// TODO(Faris) : uncomment this when we can safely purge the environments
53-
// the integrations tests would create in the sidecar
54-
// Successfully create environment.
55-
// c.Run(ctx, "coder envs create --image "+ubuntuImgID+" test-ubuntu").Assert(t,
56-
// tcli.Success(),
57-
// // why does flog.Success write to stderr?
58-
// tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"test-ubuntu\"")),
59-
// )
60-
61-
// TODO(Faris) : uncomment this when we can safely purge the environments
62-
// the integrations tests would create in the sidecar
63-
// Successfully provision environment with fractional resource amounts
64-
// c.Run(ctx, fmt.Sprintf(`coder envs create -i %s -c 1.2 -m 1.4 non-whole-resource-amounts`, ubuntuImgID)).Assert(t,
65-
// tcli.Success(),
66-
// tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"non-whole-resource-amounts\"")),
67-
// )
47+
name := randString(10)
48+
cpu := 2.3
49+
c.Run(ctx, fmt.Sprintf("coder envs create %s --image ubuntu --cpu %f", name, cpu)).Assert(t,
50+
tcli.Success(),
51+
)
52+
53+
t.Cleanup(func() {
54+
run(t, "coder-envs-edit-cleanup", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner){
55+
c.Run(ctx, fmt.Sprintf("coder envs rm %s --force", name)).Assert(t, tcli.Success())
56+
})
57+
})
58+
59+
c.Run(ctx, "coder envs ls").Assert(t,
60+
tcli.Success(),
61+
tcli.StdoutMatches(regexp.QuoteMeta(name)),
62+
)
63+
64+
var env coder.Environment
65+
c.Run(ctx, fmt.Sprintf(`coder envs ls -o json | jq '.[] | select(.name == "%s")'`, name)).Assert(t,
66+
tcli.Success(),
67+
tcli.StdoutJSONUnmarshal(&env),
68+
)
69+
assert.Equal(t, "environment cpu was correctly set", cpu, float64(env.CPUCores), floatComparer)
70+
71+
c.Run(ctx, fmt.Sprintf("coder envs watch-build %s", name)).Assert(t,
72+
tcli.Success(),
73+
)
74+
75+
c.Run(ctx, fmt.Sprintf("coder envs rm %s --force", name)).Assert(t,
76+
tcli.Success(),
77+
)
78+
})
79+
80+
run(t, "coder-cli-env-edit-tests", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) {
81+
headlessLogin(ctx, t, c)
82+
83+
name := randString(10)
84+
c.Run(ctx, fmt.Sprintf("coder envs create %s --image ubuntu --follow", name)).Assert(t,
85+
tcli.Success(),
86+
)
87+
t.Cleanup(func() {
88+
run(t, "coder-envs-edit-cleanup", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner){
89+
c.Run(ctx, fmt.Sprintf("coder envs rm %s --force", name)).Assert(t, tcli.Success())
90+
})
91+
})
92+
93+
cpu := 2.1
94+
c.Run(ctx, fmt.Sprintf(`coder envs edit %s --cpu %f --follow`, name, cpu)).Assert(t,
95+
tcli.Success(),
96+
)
97+
98+
var env coder.Environment
99+
c.Run(ctx, fmt.Sprintf(`coder envs ls -o json | jq '.[] | select(.name == "%s")'`, name)).Assert(t,
100+
tcli.Success(),
101+
tcli.StdoutJSONUnmarshal(&env),
102+
)
103+
assert.Equal(t, "cpu cores were updated", cpu, float64(env.CPUCores), floatComparer)
104+
105+
c.Run(ctx, fmt.Sprintf("coder envs rm %s --force", name)).Assert(t,
106+
tcli.Success(),
107+
)
68108
})
69109
}
110+
111+
var floatComparer = cmp.Comparer(func(x, y float64) bool {
112+
delta := math.Abs(x - y)
113+
mean := math.Abs(x+y) / 2.0
114+
return delta/mean < 0.001
115+
})

ci/integration/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestCoderCLI(t *testing.T) {
8787
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
8888

8989
func randString(length int) string {
90-
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
90+
const charset = "abcdefghijklmnopqrstuvwxyz"
9191
b := make([]byte, length)
9292
for i := range b {
9393
b[i] = charset[seededRand.Intn(len(charset))]

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
cdr.dev/wsep v0.0.0-20200728013649-82316a09813f
88
github.com/briandowns/spinner v1.11.1
99
github.com/fatih/color v1.9.0
10+
github.com/google/go-cmp v0.4.0
1011
github.com/gorilla/websocket v1.4.2
1112
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
1213
github.com/klauspost/compress v1.10.8 // indirect

0 commit comments

Comments
 (0)