Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commitcbdd4b6

Browse files
committed
Add integration tests for Secrets commands
1 parent01e9b77 commitcbdd4b6

File tree

18 files changed

+269
-155
lines changed

18 files changed

+269
-155
lines changed

‎ci/integration/integration_test.go

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"os"
8-
"os/exec"
9-
"path/filepath"
10-
"strings"
7+
"math/rand"
8+
"regexp"
119
"testing"
1210
"time"
1311

@@ -17,50 +15,6 @@ import (
1715
"cdr.dev/slog/sloggers/slogtest/assert"
1816
)
1917

20-
funcbuild(pathstring)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-
iferr!=nil {
29-
returnerr
30-
}
31-
returnnil
32-
}
33-
34-
varbinpathstring
35-
36-
funcinit() {
37-
cwd,err:=os.Getwd()
38-
iferr!=nil {
39-
panic(err)
40-
}
41-
42-
binpath=filepath.Join(cwd,"bin","coder")
43-
err=build(binpath)
44-
iferr!=nil {
45-
panic(err)
46-
}
47-
}
48-
49-
// write session tokens to the given container runner
50-
funcheadlessLogin(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-
6418
funcTestCoderCLI(t*testing.T) {
6519
t.Parallel()
6620
ctx,cancel:=context.WithTimeout(context.Background(),time.Minute*5)
@@ -116,7 +70,7 @@ func TestCoderCLI(t *testing.T) {
11670
varuser entclient.User
11771
c.Run(ctx,`coder users ls -o json | jq -c '.[] | select( .username == "charlie")'`).Assert(t,
11872
tcli.Success(),
119-
jsonUnmarshals(&user),
73+
stdoutUnmarshalsJSON(&user),
12074
)
12175
assert.Equal(t,"user email is as expected","charlie@coder.com",user.Email)
12276
assert.Equal(t,"username is as expected","Charlie",user.Name)
@@ -135,10 +89,80 @@ func TestCoderCLI(t *testing.T) {
13589
)
13690
}
13791

138-
funcjsonUnmarshals(targetinterface{}) tcli.Assertion {
92+
funcTestSecrets(t*testing.T) {
93+
t.Parallel()
94+
ctx,cancel:=context.WithTimeout(context.Background(),time.Minute*5)
95+
defercancel()
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+
deferc.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 create").Assert(t,
116+
tcli.Error(),
117+
tcli.StdoutEmpty(),
118+
tcli.StderrMatches("required flag"),
119+
)
120+
121+
c.Run(ctx,fmt.Sprintf("coder secrets create --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+
funcstdoutUnmarshalsJSON(targetinterface{}) tcli.Assertion {
139152
returnfunc(t*testing.T,r*tcli.CommandResult) {
140153
slog.Helper()
141154
err:=json.Unmarshal(r.Stdout,target)
142155
assert.Success(t,"json unmarshals",err)
143156
}
144157
}
158+
159+
varseededRand=rand.New(rand.NewSource(time.Now().UnixNano()))
160+
161+
funcrandString(lengthint)string {
162+
constcharset="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
163+
b:=make([]byte,length)
164+
fori:=rangeb {
165+
b[i]=charset[seededRand.Intn(len(charset))]
166+
}
167+
returnstring(b)
168+
}

‎ci/integration/setup_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package integration
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"strings"
10+
"testing"
11+
12+
"cdr.dev/coder-cli/ci/tcli"
13+
"golang.org/x/xerrors"
14+
)
15+
16+
varbinpathstring
17+
18+
// initialize integration tests by building the coder-cli binary
19+
funcinit() {
20+
cwd,err:=os.Getwd()
21+
iferr!=nil {
22+
panic(err)
23+
}
24+
25+
binpath=filepath.Join(cwd,"bin","coder")
26+
err=build(binpath)
27+
iferr!=nil {
28+
panic(err)
29+
}
30+
}
31+
32+
// build the coder-cli binary and move to the integration testing bin directory
33+
funcbuild(pathstring)error {
34+
cmd:=exec.Command(
35+
"sh","-c",
36+
fmt.Sprintf("cd ../../ && go build -o %s ./cmd/coder",path),
37+
)
38+
cmd.Env=append(os.Environ(),"GOOS=linux","CGO_ENABLED=0")
39+
40+
out,err:=cmd.CombinedOutput()
41+
iferr!=nil {
42+
returnxerrors.Errorf("failed to build coder-cli (%v): %w",string(out),err)
43+
}
44+
returnnil
45+
}
46+
47+
// write session tokens to the given container runner
48+
funcheadlessLogin(ctx context.Context,t*testing.T,runner*tcli.ContainerRunner) {
49+
creds:=login(ctx,t)
50+
cmd:=exec.CommandContext(ctx,"sh","-c","mkdir -p ~/.config/coder && cat > ~/.config/coder/session")
51+
52+
// !IMPORTANT: be careful that this does not appear in logs
53+
cmd.Stdin=strings.NewReader(creds.token)
54+
runner.RunCmd(cmd).Assert(t,
55+
tcli.Success(),
56+
)
57+
runner.Run(ctx,fmt.Sprintf("echo -ne %s > ~/.config/coder/url",creds.url)).Assert(t,
58+
tcli.Success(),
59+
)
60+
}

‎ci/tcli/tcli.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,16 @@ type Assertable struct {
163163
}
164164

165165
// Assert runs the Assertable and
166-
func (aAssertable)Assert(t*testing.T,option...Assertion) {
166+
func (a*Assertable)Assert(t*testing.T,option...Assertion) {
167167
slog.Helper()
168168
var (
169169
stdout bytes.Buffer
170170
stderr bytes.Buffer
171171
resultCommandResult
172172
)
173+
ifa.cmd==nil {
174+
slogtest.Fatal(t,"test failed to initialize: no command specified")
175+
}
173176

174177
a.cmd.Stdout=&stdout
175178
a.cmd.Stderr=&stderr

‎cmd/coder/auth.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ package main
33
import (
44
"net/url"
55

6-
"cdr.dev/coder-cli/internal/xcli"
7-
86
"cdr.dev/coder-cli/internal/config"
97
"cdr.dev/coder-cli/internal/entclient"
108
)
119

1210
funcrequireAuth()*entclient.Client {
1311
sessionToken,err:=config.Session.Read()
14-
xcli.RequireSuccess(err,"read session: %v (did you run coder login?)",err)
12+
requireSuccess(err,"read session: %v (did you run coder login?)",err)
1513

1614
rawURL,err:=config.URL.Read()
17-
xcli.RequireSuccess(err,"read url: %v (did you run coder login?)",err)
15+
requireSuccess(err,"read url: %v (did you run coder login?)",err)
1816

1917
u,err:=url.Parse(rawURL)
20-
xcli.RequireSuccess(err,"url misformatted: %v (try runing coder login)",err)
18+
requireSuccess(err,"url misformatted: %v (try runing coder login)",err)
2119

2220
return&entclient.Client{
2321
BaseURL:u,

‎cmd/coder/ceapi.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"cdr.dev/coder-cli/internal/xcli"
5-
64
"go.coder.com/flog"
75

86
"cdr.dev/coder-cli/internal/entclient"
@@ -29,18 +27,18 @@ outer:
2927
// getEnvs returns all environments for the user.
3028
funcgetEnvs(client*entclient.Client) []entclient.Environment {
3129
me,err:=client.Me()
32-
xcli.RequireSuccess(err,"get self: %+v",err)
30+
requireSuccess(err,"get self: %+v",err)
3331

3432
orgs,err:=client.Orgs()
35-
xcli.RequireSuccess(err,"get orgs: %+v",err)
33+
requireSuccess(err,"get orgs: %+v",err)
3634

3735
orgs=userOrgs(me,orgs)
3836

3937
varallEnvs []entclient.Environment
4038

4139
for_,org:=rangeorgs {
4240
envs,err:=client.Envs(me,org)
43-
xcli.RequireSuccess(err,"get envs for %v: %+v",org.Name,err)
41+
requireSuccess(err,"get envs for %v: %+v",org.Name,err)
4442

4543
for_,env:=rangeenvs {
4644
allEnvs=append(allEnvs,env)

‎cmd/coder/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
_"net/http/pprof"
77
"os"
88

9-
"cdr.dev/coder-cli/internal/xterminal"
9+
"cdr.dev/coder-cli/internal/x/xterminal"
1010
"github.com/spf13/pflag"
1111

1212
"go.coder.com/flog"
@@ -62,3 +62,10 @@ func main() {
6262

6363
cli.RunRoot(&rootCmd{})
6464
}
65+
66+
// requireSuccess prints the given message and format args as a fatal error if err != nil
67+
funcrequireSuccess(errerror,msgstring,args...interface{}) {
68+
iferr!=nil {
69+
flog.Fatal(msg,args...)
70+
}
71+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp