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

Commitf50cfc0

Browse files
author
Russtopia
committed
Merge branch 'master' into 4411-coder-cli-named-devurls
2 parentscc55a4a +3a54328 commitf50cfc0

File tree

17 files changed

+577
-103
lines changed

17 files changed

+577
-103
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func NewContainerRunner(ctx context.Context, config *ContainerConfig) (*Containe
6565
args:= []string{
6666
"run",
6767
"--name",config.Name,
68+
"--network","host",
6869
"-it","-d",
6970
}
7071
args=append(args,mountArgs(config.BindMounts)...)
@@ -163,13 +164,16 @@ type Assertable struct {
163164
}
164165

165166
// Assert runs the Assertable and
166-
func (aAssertable)Assert(t*testing.T,option...Assertion) {
167+
func (a*Assertable)Assert(t*testing.T,option...Assertion) {
167168
slog.Helper()
168169
var (
169170
stdout bytes.Buffer
170171
stderr bytes.Buffer
171172
resultCommandResult
172173
)
174+
ifa.cmd==nil {
175+
slogtest.Fatal(t,"test failed to initialize: no command specified")
176+
}
173177

174178
a.cmd.Stdout=&stdout
175179
a.cmd.Stderr=&stderr

‎cmd/coder/auth.go

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

6-
"go.coder.com/flog"
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-
iferr!=nil {
15-
flog.Fatal("read session: %v (did you run coder login?)",err)
16-
}
12+
requireSuccess(err,"read session: %v (did you run coder login?)",err)
1713

1814
rawURL,err:=config.URL.Read()
19-
iferr!=nil {
20-
flog.Fatal("read url: %v (did you run coder login?)",err)
21-
}
15+
requireSuccess(err,"read url: %v (did you run coder login?)",err)
2216

2317
u,err:=url.Parse(rawURL)
24-
iferr!=nil {
25-
flog.Fatal("url misformatted: %v (try runing coder login)",err)
26-
}
18+
requireSuccess(err,"url misformatted: %v (try runing coder login)",err)
2719

2820
return&entclient.Client{
2921
BaseURL:u,

‎cmd/coder/ceapi.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,19 @@ outer:
2727
// getEnvs returns all environments for the user.
2828
funcgetEnvs(client*entclient.Client) []entclient.Environment {
2929
me,err:=client.Me()
30-
iferr!=nil {
31-
flog.Fatal("get self: %+v",err)
32-
}
30+
requireSuccess(err,"get self: %+v",err)
3331

3432
orgs,err:=client.Orgs()
35-
iferr!=nil {
36-
flog.Fatal("get orgs: %+v",err)
37-
}
33+
requireSuccess(err,"get orgs: %+v",err)
3834

3935
orgs=userOrgs(me,orgs)
4036

4137
varallEnvs []entclient.Environment
4238

4339
for_,org:=rangeorgs {
4440
envs,err:=client.Envs(me,org)
45-
iferr!=nil {
46-
flog.Fatal("get envs for %v: %+v",org.Name,err)
47-
}
41+
requireSuccess(err,"get envs for %v: %+v",org.Name,err)
42+
4843
for_,env:=rangeenvs {
4944
allEnvs=append(allEnvs,env)
5045
}

‎cmd/coder/main.go

Lines changed: 9 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"
@@ -43,6 +43,7 @@ func (r *rootCmd) Subcommands() []cli.Command {
4343
&versionCmd{},
4444
&configSSHCmd{},
4545
&usersCmd{},
46+
&secretsCmd{},
4647
}
4748
}
4849

@@ -61,3 +62,10 @@ func main() {
6162

6263
cli.RunRoot(&rootCmd{})
6364
}
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