@@ -4,10 +4,8 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
- "os"
8
- "os/exec"
9
- "path/filepath"
10
- "strings"
7
+ "math/rand"
8
+ "regexp"
11
9
"testing"
12
10
"time"
13
11
@@ -17,50 +15,6 @@ import (
17
15
"cdr.dev/slog/sloggers/slogtest/assert"
18
16
)
19
17
20
- func build (path string ) error {
21
- cmd := exec .Command (
22
- "sh" , "-c" ,
23
- fmt .Sprintf ("cd ../../ && go build -o %s ./cmd/coder" , path ),
24
- )
25
- cmd .Env = append (os .Environ (), "GOOS=linux" , "CGO_ENABLED=0" )
26
-
27
- _ , err := cmd .CombinedOutput ()
28
- if err != nil {
29
- return err
30
- }
31
- return nil
32
- }
33
-
34
- var binpath string
35
-
36
- func init () {
37
- cwd , err := os .Getwd ()
38
- if err != nil {
39
- panic (err )
40
- }
41
-
42
- binpath = filepath .Join (cwd , "bin" , "coder" )
43
- err = build (binpath )
44
- if err != nil {
45
- panic (err )
46
- }
47
- }
48
-
49
- // write session tokens to the given container runner
50
- func headlessLogin (ctx context.Context , t * testing.T , runner * tcli.ContainerRunner ) {
51
- creds := login (ctx , t )
52
- cmd := exec .CommandContext (ctx , "sh" , "-c" , "mkdir -p ~/.config/coder && cat > ~/.config/coder/session" )
53
-
54
- // !IMPORTANT: be careful that this does not appear in logs
55
- cmd .Stdin = strings .NewReader (creds .token )
56
- runner .RunCmd (cmd ).Assert (t ,
57
- tcli .Success (),
58
- )
59
- runner .Run (ctx , fmt .Sprintf ("echo -ne %s > ~/.config/coder/url" , creds .url )).Assert (t ,
60
- tcli .Success (),
61
- )
62
- }
63
-
64
18
func TestCoderCLI (t * testing.T ) {
65
19
t .Parallel ()
66
20
ctx , cancel := context .WithTimeout (context .Background (), time .Minute * 5 )
@@ -116,7 +70,7 @@ func TestCoderCLI(t *testing.T) {
116
70
var user entclient.User
117
71
c .Run (ctx , `coder users ls -o json | jq -c '.[] | select( .username == "charlie")'` ).Assert (t ,
118
72
tcli .Success (),
119
- jsonUnmarshals (& user ),
73
+ stdoutUnmarshalsJSON (& user ),
120
74
)
121
75
assert .Equal (t , "user email is as expected" , "charlie@coder.com" , user .Email )
122
76
assert .Equal (t , "username is as expected" , "Charlie" , user .Name )
@@ -135,10 +89,80 @@ func TestCoderCLI(t *testing.T) {
135
89
)
136
90
}
137
91
138
- func jsonUnmarshals (target interface {}) tcli.Assertion {
92
+ func TestSecrets (t * testing.T ) {
93
+ t .Parallel ()
94
+ ctx , cancel := context .WithTimeout (context .Background (), time .Minute * 5 )
95
+ defer cancel ()
96
+
97
+ c , err := tcli .NewContainerRunner (ctx , & tcli.ContainerConfig {
98
+ Image : "codercom/enterprise-dev" ,
99
+ Name : "secrets-cli-tests" ,
100
+ BindMounts : map [string ]string {
101
+ binpath : "/bin/coder" ,
102
+ },
103
+ })
104
+ assert .Success (t , "new run container" , err )
105
+ defer c .Close ()
106
+
107
+ headlessLogin (ctx , t , c )
108
+
109
+ c .Run (ctx , "coder secrets ls" ).Assert (t ,
110
+ tcli .Success (),
111
+ )
112
+
113
+ name , value := randString (8 ), randString (8 )
114
+
115
+ c .Run (ctx , "coder secrets add" ).Assert (t ,
116
+ tcli .Error (),
117
+ tcli .StdoutEmpty (),
118
+ tcli .StderrMatches ("required flag" ),
119
+ )
120
+
121
+ c .Run (ctx , fmt .Sprintf ("coder secrets add --name %s --value %s" , name , value )).Assert (t ,
122
+ tcli .Success (),
123
+ tcli .StderrEmpty (),
124
+ )
125
+
126
+ c .Run (ctx , "coder secrets ls" ).Assert (t ,
127
+ tcli .Success (),
128
+ tcli .StderrEmpty (),
129
+ tcli .StdoutMatches ("Value" ),
130
+ tcli .StdoutMatches (regexp .QuoteMeta (name )),
131
+ )
132
+
133
+ c .Run (ctx , "coder secrets view " + name ).Assert (t ,
134
+ tcli .Success (),
135
+ tcli .StderrEmpty (),
136
+ tcli .StdoutMatches (regexp .QuoteMeta (value )),
137
+ )
138
+
139
+ c .Run (ctx , "coder secrets rm" ).Assert (t ,
140
+ tcli .Error (),
141
+ )
142
+ c .Run (ctx , "coder secrets rm " + name ).Assert (t ,
143
+ tcli .Success (),
144
+ )
145
+ c .Run (ctx , "coder secrets view " + name ).Assert (t ,
146
+ tcli .Error (),
147
+ tcli .StdoutEmpty (),
148
+ )
149
+ }
150
+
151
+ func stdoutUnmarshalsJSON (target interface {}) tcli.Assertion {
139
152
return func (t * testing.T , r * tcli.CommandResult ) {
140
153
slog .Helper ()
141
154
err := json .Unmarshal (r .Stdout , target )
142
155
assert .Success (t , "json unmarshals" , err )
143
156
}
144
157
}
158
+
159
+ var seededRand = rand .New (rand .NewSource (time .Now ().UnixNano ()))
160
+
161
+ func randString (length int ) string {
162
+ const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
163
+ b := make ([]byte , length )
164
+ for i := range b {
165
+ b [i ] = charset [seededRand .Intn (len (charset ))]
166
+ }
167
+ return string (b )
168
+ }
0 commit comments