@@ -4,20 +4,44 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"math"
7
+ "net/url"
7
8
"regexp"
8
9
"testing"
10
+ "time"
9
11
10
12
"cdr.dev/coder-cli/ci/tcli"
11
13
"cdr.dev/coder-cli/coder-sdk"
14
+ "cdr.dev/slog"
15
+ "cdr.dev/slog/sloggers/slogtest"
12
16
"cdr.dev/slog/sloggers/slogtest/assert"
13
17
"github.com/google/go-cmp/cmp"
14
18
)
15
19
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
+ }
38
+
16
39
func TestEnvsCLI (t * testing.T ) {
17
40
t .Parallel ()
18
41
19
42
run (t , "coder-cli-env-tests" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
20
43
headlessLogin (ctx , t , c )
44
+ client := cleanupClient (t , ctx )
21
45
22
46
// Minimum args not received.
23
47
c .Run (ctx , "coder envs create" ).Assert (t ,
@@ -50,13 +74,6 @@ func TestEnvsCLI(t *testing.T) {
50
74
tcli .Success (),
51
75
)
52
76
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
77
c .Run (ctx , "coder envs ls" ).Assert (t ,
61
78
tcli .Success (),
62
79
tcli .StdoutMatches (regexp .QuoteMeta (name )),
@@ -67,6 +84,10 @@ func TestEnvsCLI(t *testing.T) {
67
84
tcli .Success (),
68
85
tcli .StdoutJSONUnmarshal (& env ),
69
86
)
87
+
88
+ // attempt to cleanup the environment even if tests fail
89
+ t .Cleanup (cleanupEnv (t , client , env .ID ))
90
+
70
91
assert .Equal (t , "environment cpu was correctly set" , cpu , float64 (env .CPUCores ), floatComparer )
71
92
72
93
c .Run (ctx , fmt .Sprintf ("coder envs watch-build %s" , name )).Assert (t ,
@@ -80,24 +101,27 @@ func TestEnvsCLI(t *testing.T) {
80
101
81
102
run (t , "coder-cli-env-edit-tests" , func (t * testing.T , ctx context.Context , c * tcli.ContainerRunner ) {
82
103
headlessLogin (ctx , t , c )
104
+ client := cleanupClient (t , ctx )
83
105
84
106
name := randString (10 )
85
107
c .Run (ctx , fmt .Sprintf ("coder envs create %s --image ubuntu --follow" , name )).Assert (t ,
86
108
tcli .Success (),
87
109
)
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
- })
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 ))
94
119
95
120
cpu := 2.1
96
121
c .Run (ctx , fmt .Sprintf (`coder envs edit %s --cpu %f --follow` , name , cpu )).Assert (t ,
97
122
tcli .Success (),
98
123
)
99
124
100
- var env coder.Environment
101
125
c .Run (ctx , fmt .Sprintf (`coder envs ls -o json | jq '.[] | select(.name == "%s")'` , name )).Assert (t ,
102
126
tcli .Success (),
103
127
tcli .StdoutJSONUnmarshal (& env ),
0 commit comments