@@ -2,28 +2,23 @@ package integration
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "math"
5
7
"regexp"
6
8
"testing"
7
9
8
10
"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"
9
14
)
10
15
11
- // From Coder organization images
12
- // const ubuntuImgID = "5f443b16-30652892427b955601330fa5"
13
-
14
16
func TestEnvsCLI (t * testing.T ) {
15
17
t .Parallel ()
16
18
17
19
run (t ,"coder-cli-env-tests" ,func (t * testing.T ,ctx context.Context ,c * tcli.ContainerRunner ) {
18
20
headlessLogin (ctx ,t ,c )
19
21
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
-
27
22
// Minimum args not received.
28
23
c .Run (ctx ,"coder envs create" ).Assert (t ,
29
24
tcli .StderrMatches (regexp .QuoteMeta ("accepts 1 arg(s), received 0" )),
@@ -49,21 +44,74 @@ func TestEnvsCLI(t *testing.T) {
49
44
tcli .Error (),
50
45
)
51
46
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
+ )
68
110
})
69
111
}
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
+ })