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

Commit 2494de0

Browse files
committed
Add env create|edit integration tests
1 parent a171882 commit 2494de0

File tree

3 files changed

+76
-27
lines changed

3 files changed

+76
-27
lines changed

ci/integration/envs_test.go

Lines changed: 74 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,74 @@ 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+
headlessLogin(ctx, t, c)
56+
c.Run(ctx, fmt.Sprintf("coder envs rm %s --force", name)).Assert(t)
57+
})
58+
})
59+
60+
c.Run(ctx, "coder envs ls").Assert(t,
61+
tcli.Success(),
62+
tcli.StdoutMatches(regexp.QuoteMeta(name)),
63+
)
64+
65+
var env coder.Environment
66+
c.Run(ctx, fmt.Sprintf(`coder envs ls -o json | jq '.[] | select(.name == "%s")'`, name)).Assert(t,
67+
tcli.Success(),
68+
tcli.StdoutJSONUnmarshal(&env),
69+
)
70+
assert.Equal(t, "environment cpu was correctly set", cpu, float64(env.CPUCores), floatComparer)
71+
72+
c.Run(ctx, fmt.Sprintf("coder envs watch-build %s", name)).Assert(t,
73+
tcli.Success(),
74+
)
75+
76+
c.Run(ctx, fmt.Sprintf("coder envs rm %s --force", name)).Assert(t,
77+
tcli.Success(),
78+
)
79+
})
80+
81+
run(t, "coder-cli-env-edit-tests", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) {
82+
headlessLogin(ctx, t, c)
83+
84+
name := randString(10)
85+
c.Run(ctx, fmt.Sprintf("coder envs create %s --image ubuntu --follow", name)).Assert(t,
86+
tcli.Success(),
87+
)
88+
t.Cleanup(func() {
89+
run(t, "coder-envs-edit-cleanup", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) {
90+
headlessLogin(ctx, t, c)
91+
c.Run(ctx, fmt.Sprintf("coder envs rm %s --force", name)).Assert(t)
92+
})
93+
})
94+
95+
cpu := 2.1
96+
c.Run(ctx, fmt.Sprintf(`coder envs edit %s --cpu %f --follow`, name, cpu)).Assert(t,
97+
tcli.Success(),
98+
)
99+
100+
var env coder.Environment
101+
c.Run(ctx, fmt.Sprintf(`coder envs ls -o json | jq '.[] | select(.name == "%s")'`, name)).Assert(t,
102+
tcli.Success(),
103+
tcli.StdoutJSONUnmarshal(&env),
104+
)
105+
assert.Equal(t, "cpu cores were updated", cpu, float64(env.CPUCores), floatComparer)
106+
107+
c.Run(ctx, fmt.Sprintf("coder envs rm %s --force", name)).Assert(t,
108+
tcli.Success(),
109+
)
68110
})
69111
}
112+
113+
var floatComparer = cmp.Comparer(func(x, y float64) bool {
114+
delta := math.Abs(x - y)
115+
mean := math.Abs(x+y) / 2.0
116+
return delta/mean < 0.001
117+
})

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy