@@ -2,27 +2,46 @@ package integration
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "math"
7
+ "net/url"
5
8
"regexp"
6
9
"testing"
10
+ "time"
7
11
8
12
"cdr.dev/coder-cli/ci/tcli"
13
+ "cdr.dev/coder-cli/coder-sdk"
14
+ "cdr.dev/slog"
15
+ "cdr.dev/slog/sloggers/slogtest"
16
+ "cdr.dev/slog/sloggers/slogtest/assert"
17
+ "github.com/google/go-cmp/cmp"
9
18
)
10
19
11
- // From Coder organization images
12
- // const ubuntuImgID = "5f443b16-30652892427b955601330fa5"
20
+ func cleanupClient (t * testing.T , ctx context.Context ) * coder.Client {
21
+ creds := login (ctx , t )
22
+
23
+ u , err := url .Parse (creds .url )
24
+ assert .Success (t , "parse base url" , err )
25
+
26
+ return & coder.Client {BaseURL : u , Token : creds .token }
27
+ }
28
+
29
+ func cleanupEnv (t * testing.T , client * coder.Client , envID string ) func () {
30
+ return func () {
31
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
32
+ defer cancel ()
33
+
34
+ slogtest .Info (t , "cleanuping up environment" , slog .F ("env_id" , envID ))
35
+ _ = client .DeleteEnvironment (ctx , envID )
36
+ }
37
+ }
13
38
14
39
func TestEnvsCLI (t * testing.T ) {
15
40
t .Parallel ()
16
41
17
42
run (t , "coder-cli-env-tests" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
18
43
headlessLogin (ctx , t , c )
19
-
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
- )
44
+ client := cleanupClient (t , ctx )
26
45
27
46
// Minimum args not received.
28
47
c .Run (ctx , "coder envs create" ).Assert (t ,
@@ -49,21 +68,74 @@ func TestEnvsCLI(t *testing.T) {
49
68
tcli .Error (),
50
69
)
51
70
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
- // )
71
+ name := randString (10 )
72
+ cpu := 2.3
73
+ c .Run (ctx , fmt .Sprintf ("coder envs create %s --image ubuntu --cpu %f" , name , cpu )).Assert (t ,
74
+ tcli .Success (),
75
+ )
76
+
77
+ c .Run (ctx , "coder envs ls" ).Assert (t ,
78
+ tcli .Success (),
79
+ tcli .StdoutMatches (regexp .QuoteMeta (name )),
80
+ )
81
+
82
+ var env coder.Environment
83
+ c .Run (ctx , fmt .Sprintf (`coder envs ls -o json | jq '.[] | select(.name == "%s")'` , name )).Assert (t ,
84
+ tcli .Success (),
85
+ tcli .StdoutJSONUnmarshal (& env ),
86
+ )
87
+
88
+ // attempt to cleanup the environment even if tests fail
89
+ t .Cleanup (cleanupEnv (t , client , env .ID ))
90
+
91
+ assert .Equal (t , "environment cpu was correctly set" , cpu , float64 (env .CPUCores ), floatComparer )
92
+
93
+ c .Run (ctx , fmt .Sprintf ("coder envs watch-build %s" , name )).Assert (t ,
94
+ tcli .Success (),
95
+ )
96
+
97
+ c .Run (ctx , fmt .Sprintf ("coder envs rm %s --force" , name )).Assert (t ,
98
+ tcli .Success (),
99
+ )
100
+ })
101
+
102
+ run (t , "coder-cli-env-edit-tests" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
103
+ headlessLogin (ctx , t , c )
104
+ client := cleanupClient (t , ctx )
105
+
106
+ name := randString (10 )
107
+ c .Run (ctx , fmt .Sprintf ("coder envs create %s --image ubuntu --follow" , name )).Assert (t ,
108
+ tcli .Success (),
109
+ )
110
+
111
+ var env coder.Environment
112
+ c .Run (ctx , fmt .Sprintf (`coder envs ls -o json | jq '.[] | select(.name == "%s")'` , name )).Assert (t ,
113
+ tcli .Success (),
114
+ tcli .StdoutJSONUnmarshal (& env ),
115
+ )
116
+
117
+ // attempt to cleanup the environment even if tests fail
118
+ t .Cleanup (cleanupEnv (t , client , env .ID ))
119
+
120
+ cpu := 2.1
121
+ c .Run (ctx , fmt .Sprintf (`coder envs edit %s --cpu %f --follow` , name , cpu )).Assert (t ,
122
+ tcli .Success (),
123
+ )
124
+
125
+ c .Run (ctx , fmt .Sprintf (`coder envs ls -o json | jq '.[] | select(.name == "%s")'` , name )).Assert (t ,
126
+ tcli .Success (),
127
+ tcli .StdoutJSONUnmarshal (& env ),
128
+ )
129
+ assert .Equal (t , "cpu cores were updated" , cpu , float64 (env .CPUCores ), floatComparer )
130
+
131
+ c .Run (ctx , fmt .Sprintf ("coder envs rm %s --force" , name )).Assert (t ,
132
+ tcli .Success (),
133
+ )
68
134
})
69
135
}
136
+
137
+ var floatComparer = cmp .Comparer (func (x , y float64 ) bool {
138
+ delta := math .Abs (x - y )
139
+ mean := math .Abs (x + y ) / 2.0
140
+ return delta / mean < 0.001
141
+ })
0 commit comments