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

Migrate to cobra#86

Merged
cmoog merged 10 commits intomasterfromurfave
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletionsci/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
# ci
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Would be good to link to this from the README.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Most readers of theREADME will be customers, not developers. Doesn't really make sense to bring attention to it in my view.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Don't have to bring attention to it. Just put a link at the bottom. Customers/potential customers who care to look closely will be happy we document and care about these things.


## integration tests

### `tcli`

Package `tcli` provides a framework for writing end-to-end CLI tests.
Each test group can have its own container for executing commands in a consistent
and isolated filesystem.

### prerequisites

Assign the following environment variables to run the integration tests
against an existing Enterprise deployment instance.

```bash
export CODER_URL=...
export CODER_EMAIL=...
export CODER_PASSWORD=...
```

Then, simply run the test command from the project root

```sh
go test -v ./ci/integration
```
40 changes: 9 additions & 31 deletionsci/integration/integration_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,14 +2,11 @@ package integration

import (
"context"
"encoding/json"
"math/rand"
"testing"
"time"

"cdr.dev/coder-cli/ci/tcli"
"cdr.dev/coder-cli/internal/entclient"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest/assert"
)

Expand All@@ -34,17 +31,15 @@ func TestCoderCLI(t *testing.T) {
tcli.StderrEmpty(),
)

c.Run(ctx, "coder version").Assert(t,
c.Run(ctx, "coder--version").Assert(t,
tcli.StderrEmpty(),
tcli.Success(),
tcli.StdoutMatches("linux"),
)

c.Run(ctx, "coder help").Assert(t,
c.Run(ctx, "coder--help").Assert(t,
tcli.Success(),
tcli.StderrMatches("Commands:"),
tcli.StderrMatches("Usage: coder"),
tcli.StdoutEmpty(),
tcli.StdoutMatches("Available Commands"),
)

headlessLogin(ctx, t, c)
Expand All@@ -53,8 +48,12 @@ func TestCoderCLI(t *testing.T) {
tcli.Success(),
)

c.Run(ctx, "coder envs ls").Assert(t,
tcli.Success(),
)

c.Run(ctx, "coder urls").Assert(t,
tcli.Error(),
tcli.Success(),
)

c.Run(ctx, "coder sync").Assert(t,
Expand All@@ -65,36 +64,15 @@ func TestCoderCLI(t *testing.T) {
tcli.Error(),
)

var user entclient.User
c.Run(ctx, `coder users ls -o json | jq -c '.[] | select( .username == "charlie")'`).Assert(t,
tcli.Success(),
stdoutUnmarshalsJSON(&user),
)
assert.Equal(t, "user email is as expected", "charlie@coder.com", user.Email)
assert.Equal(t, "username is as expected", "Charlie", user.Name)

c.Run(ctx, "coder users ls -o human | grep charlie").Assert(t,
tcli.Success(),
tcli.StdoutMatches("charlie"),
)

c.Run(ctx, "coder logout").Assert(t,
tcli.Success(),
)

c.Run(ctx, "coder envs").Assert(t,
c.Run(ctx, "coder envs ls").Assert(t,
tcli.Error(),
)
}

func stdoutUnmarshalsJSON(target interface{}) tcli.Assertion {
return func(t *testing.T, r *tcli.CommandResult) {
slog.Helper()
err := json.Unmarshal(r.Stdout, target)
assert.Success(t, "json unmarshals", err)
}
}

var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))

func randString(length int) string {
Expand Down
4 changes: 0 additions & 4 deletionsci/integration/secrets_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,7 +36,6 @@ func TestSecrets(t *testing.T) {

c.Run(ctx, "coder secrets create").Assert(t,
tcli.Error(),
tcli.StdoutEmpty(),
)

// this tests the "Value:" prompt fallback
Expand DownExpand Up@@ -85,9 +84,6 @@ func TestSecrets(t *testing.T) {
c.Run(ctx, fmt.Sprintf("echo %s > ~/secret.json", value)).Assert(t,
tcli.Success(),
)
c.Run(ctx, fmt.Sprintf("coder secrets create %s --from-literal %s --from-file ~/secret.json", name, value)).Assert(t,
tcli.Error(),
)
c.Run(ctx, fmt.Sprintf("coder secrets create %s --from-file ~/secret.json", name)).Assert(t,
tcli.Success(),
)
Expand Down
3 changes: 2 additions & 1 deletionci/integration/setup_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@ import (
"golang.org/x/xerrors"
)

// binpath is populated during package initialization with a path to the coder binary
var binpath string

// initialize integration tests by building the coder-cli binary
Expand All@@ -39,7 +40,7 @@ func build(path string) error {

out, err := cmd.CombinedOutput()
if err != nil {
return xerrors.Errorf("failed tobuild coder-cli (%v): %w", string(out), err)
return xerrors.Errorf("build coder-cli (%v): %w", string(out), err)
}
return nil
}
Expand Down
56 changes: 56 additions & 0 deletionsci/integration/users_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
package integration

import (
"context"
"testing"
"time"

"cdr.dev/coder-cli/ci/tcli"
"cdr.dev/coder-cli/internal/entclient"
"cdr.dev/slog/sloggers/slogtest/assert"
)

func TestUsers(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

c, err := tcli.NewContainerRunner(ctx, &tcli.ContainerConfig{
Image: "codercom/enterprise-dev",
Name: "users-cli-tests",
BindMounts: map[string]string{
binpath: "/bin/coder",
},
})
assert.Success(t, "new run container", err)
defer c.Close()

c.Run(ctx, "which coder").Assert(t,
tcli.Success(),
tcli.StdoutMatches("/usr/sbin/coder"),
tcli.StderrEmpty(),
)

headlessLogin(ctx, t, c)

var user entclient.User
c.Run(ctx, `coder users ls --output json | jq -c '.[] | select( .username == "charlie")'`).Assert(t,
tcli.Success(),
tcli.StdoutJSONUnmarshal(&user),
)
assert.Equal(t, "user email is as expected", "charlie@coder.com", user.Email)
assert.Equal(t, "username is as expected", "Charlie", user.Name)

c.Run(ctx, "coder users ls --output human | grep charlie").Assert(t,
tcli.Success(),
tcli.StdoutMatches("charlie"),
)

c.Run(ctx, "coder logout").Assert(t,
tcli.Success(),
)

c.Run(ctx, "coder users ls").Assert(t,
tcli.Error(),
)
}
25 changes: 22 additions & 3 deletionsci/tcli/tcli.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@ package tcli
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os/exec"
Expand DownExpand Up@@ -76,7 +77,7 @@ func NewContainerRunner(ctx context.Context, config *ContainerConfig) (*Containe
out, err := cmd.CombinedOutput()
if err != nil {
return nil, xerrors.Errorf(
"failed tostart testing container %q, (%s): %w",
"start testing container %q, (%s): %w",
config.Name, string(out), err)
}

Expand All@@ -97,7 +98,7 @@ func (r *ContainerRunner) Close() error {
out, err := cmd.CombinedOutput()
if err != nil {
return xerrors.Errorf(
"failed tostop testing container %q, (%s): %w",
"stop testing container %q, (%s): %w",
r.name, string(out), err)
}
return nil
Expand DownExpand Up@@ -290,7 +291,7 @@ func matches(t *testing.T, name, pattern string, target []byte) {

ok, err := regexp.Match(pattern, target)
if err != nil {
slogtest.Fatal(t, "failed toattempt regexp match", append(fields, slog.Error(err))...)
slogtest.Fatal(t, "attempt regexp match", append(fields, slog.Error(err))...)
}
if !ok {
slogtest.Fatal(t, "expected to find pattern, no match found", fields...)
Expand DownExpand Up@@ -329,3 +330,21 @@ func DurationGreaterThan(dur time.Duration) Assertion {
}
}
}

// StdoutJSONUnmarshal attempts to unmarshal stdout into the given target
func StdoutJSONUnmarshal(target interface{}) Assertion {
return func(t *testing.T, r *CommandResult) {
slog.Helper()
err := json.Unmarshal(r.Stdout, target)
assert.Success(t, "stdout json unmarshals", err)
}
}

// StderrJSONUnmarshal attempts to unmarshal stderr into the given target
func StderrJSONUnmarshal(target interface{}) Assertion {
return func(t *testing.T, r *CommandResult) {
slog.Helper()
err := json.Unmarshal(r.Stdout, target)
assert.Success(t, "stderr json unmarshals", err)
}
}
26 changes: 22 additions & 4 deletionscmd/coder/auth.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,20 +5,38 @@ import (

"cdr.dev/coder-cli/internal/config"
"cdr.dev/coder-cli/internal/entclient"
"golang.org/x/xerrors"

"go.coder.com/flog"
)

// requireAuth exits the process with a nonzero exit code if the user is not authenticated to make requests
func requireAuth() *entclient.Client {
client, err := newClient()
if err != nil {
flog.Fatal("%v", err)
}
return client
}

func newClient() (*entclient.Client, error) {
sessionToken, err := config.Session.Read()
requireSuccess(err, "read session: %v (did you run coder login?)", err)
if err != nil {
return nil, xerrors.Errorf("read session: %v (did you run coder login?)", err)
}

rawURL, err := config.URL.Read()
requireSuccess(err, "read url: %v (did you run coder login?)", err)
if err != nil {
return nil, xerrors.Errorf("read url: %v (did you run coder login?)", err)
}

u, err := url.Parse(rawURL)
requireSuccess(err, "url misformatted: %v (try runing coder login)", err)
if err != nil {
return nil, xerrors.Errorf("url misformatted: %v (try runing coder login)", err)
}

return &entclient.Client{
BaseURL: u,
Token: sessionToken,
}
}, nil
}
35 changes: 22 additions & 13 deletionscmd/coder/ceapi.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
package main

import (
"golang.org/x/xerrors"

"go.coder.com/flog"

"cdr.dev/coder-cli/internal/entclient"
Expand All@@ -25,43 +27,50 @@ outer:
}

// getEnvs returns all environments for the user.
func getEnvs(client *entclient.Client) []entclient.Environment {
func getEnvs(client *entclient.Client)([]entclient.Environment, error) {
me, err := client.Me()
requireSuccess(err, "get self: %+v", err)
if err != nil {
return nil, xerrors.Errorf("get self: %+v", err)
}

orgs, err := client.Orgs()
requireSuccess(err, "get orgs: %+v", err)
if err != nil {
return nil, xerrors.Errorf("get orgs: %+v", err)
}

orgs = userOrgs(me, orgs)

var allEnvs []entclient.Environment

for _, org := range orgs {
envs, err := client.Envs(me, org)
requireSuccess(err, "get envs for %v: %+v", org.Name, err)
if err != nil {
return nil, xerrors.Errorf("get envs for %v: %+v", org.Name, err)
}

for _, env := range envs {
allEnvs = append(allEnvs, env)
}
}

return allEnvs
return allEnvs, nil
}

// findEnv returns a single environment by name (if it exists.)
func findEnv(client *entclient.Client, name string) entclient.Environment {
envs := getEnvs(client)
func findEnv(client *entclient.Client, name string) (*entclient.Environment, error) {
envs, err := getEnvs(client)
if err != nil {
return nil, xerrors.Errorf("get environments: %w", err)
}

var found []string

for _, env := range envs {
found = append(found, env.Name)
if env.Name == name {
return env
return&env, nil
}
}

flog.Info("found %q", found)
flog.Fatal("environment %q not found", name)
panic("unreachable")
flog.Error("found %q", found)
flog.Error("%q not found", name)
return nil, xerrors.New("environment not found")
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp