Skip to content

Commit

Permalink
fix: test (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibu1224 authored Mar 4, 2021
1 parent c3a58af commit 6b05aa1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
38 changes: 34 additions & 4 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
Expand All @@ -14,7 +15,32 @@ import (
"github.com/stretchr/testify/assert"
)

const buildUsage = `
func BuildUsage() string {

buildUsage := fmt.Sprintf(`
Usage:
build [job name] [flags]
Flags:
--artifacts-dir string Path to the host side directory which is mounted into $SD_ARTIFACTS_DIR. (default "sd-artifacts")
-e, --env stringToString Set key and value relationship which is set as environment variables of Build Container. (<key>=<value>) (default [])
--env-file string Path to config file of environment variables. '.env' format file can be used.
-h, --help help for build
-i, --interactive Attach the build container in interactive mode.
-m, --memory string Memory limit for build container, which take a positive integer, followed by a suffix of b, k, m, g.
--meta string Metadata to pass into the build environment, which is represented with JSON format
--meta-file string Path to the meta file. meta file is represented with JSON format.
--privileged Use privileged mode for container runtime.
-S, --socket string Path to the socket. It will used in build container. (default "%s")
--src-url string Specify the source url to build.
ex) git@github.com:<org>/<repo>.git[#<branch>]
https://github.com/<org>/<repo>.git[#<branch>]
--sudo Use sudo command for container runtime.
--vol strings Volumes to mount into build container.
`, launch.DefaultSocketPath())
if len(launch.DefaultSocketPath()) == 0 {
buildUsage = `
Usage:
build [job name] [flags]
Expand All @@ -36,6 +62,10 @@ Flags:
--vol strings Volumes to mount into build container.
`
}

return buildUsage
}

func TestBuildCmd(t *testing.T) {
t.Run("Success build cmd", func(t *testing.T) {
Expand Down Expand Up @@ -212,7 +242,7 @@ func TestBuildCmd(t *testing.T) {
}

err := root.Execute()
want := "Error: can't pass the both options `meta` and `meta-file`, please specify only one of them" + buildUsage
want := "Error: can't pass the both options `meta` and `meta-file`, please specify only one of them" + BuildUsage()
assert.Equal(t, want, buf.String())
assert.NotNil(t, err)
})
Expand All @@ -223,7 +253,7 @@ func TestBuildCmd(t *testing.T) {
buf := bytes.NewBuffer(nil)
root.SetOut(buf)
err := root.Execute()
want := "Error: accepts 1 arg(s), received 2" + buildUsage
want := "Error: accepts 1 arg(s), received 2" + BuildUsage()
assert.Equal(t, want, buf.String())
assert.NotNil(t, err)
})
Expand All @@ -235,7 +265,7 @@ func TestBuildCmd(t *testing.T) {
buf := bytes.NewBuffer(nil)
root.SetOut(buf)
err := root.Execute()
want := "Error: accepts 1 arg(s), received 0" + buildUsage
want := "Error: accepts 1 arg(s), received 0" + BuildUsage()
assert.Equal(t, want, buf.String())
assert.NotNil(t, err)
})
Expand Down
30 changes: 29 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"fmt"
"io"
"os"
"testing"
Expand Down Expand Up @@ -102,7 +103,33 @@ func TestRootCmd(t *testing.T) {
buf := bytes.NewBuffer(nil)
root.SetOut(buf)
err := root.Execute()
want := `Error: accepts 1 arg(s), received 0
want := `Error: accepts 1 arg(s), received 0` + fmt.Sprintf(`
Usage:
sd-local build [job name] [flags]
Flags:
--artifacts-dir string Path to the host side directory which is mounted into $SD_ARTIFACTS_DIR. (default "sd-artifacts")
-e, --env stringToString Set key and value relationship which is set as environment variables of Build Container. (<key>=<value>) (default [])
--env-file string Path to config file of environment variables. '.env' format file can be used.
-h, --help help for build
-i, --interactive Attach the build container in interactive mode.
-m, --memory string Memory limit for build container, which take a positive integer, followed by a suffix of b, k, m, g.
--meta string Metadata to pass into the build environment, which is represented with JSON format
--meta-file string Path to the meta file. meta file is represented with JSON format.
--privileged Use privileged mode for container runtime.
-S, --socket string Path to the socket. It will used in build container. (default "%s")
--src-url string Specify the source url to build.
ex) git@github.com:<org>/<repo>.git[#<branch>]
https://github.com/<org>/<repo>.git[#<branch>]
--sudo Use sudo command for container runtime.
--vol strings Volumes to mount into build container.
Global Flags:
-v, --verbose verbose output.
`, launch.DefaultSocketPath())
if len(launch.DefaultSocketPath()) == 0 {
want = `Error: accepts 1 arg(s), received 0
Usage:
sd-local build [job name] [flags]
Expand All @@ -127,6 +154,7 @@ Global Flags:
-v, --verbose verbose output.
`
}
assert.Equal(t, want, buf.String())
assert.NotNil(t, err)
})
Expand Down
3 changes: 3 additions & 0 deletions launch/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func TestRunBuild(t *testing.T) {
volume: "SD_LAUNCH_BIN",
setupImage: "launcher",
setupImageVersion: "latest",
socketPath: os.Getenv("SSH_AUTH_SOCK"),
}

testCase := []struct {
Expand Down Expand Up @@ -200,6 +201,7 @@ func TestRunBuildWithSudo(t *testing.T) {
setupImage: "launcher",
setupImageVersion: "latest",
useSudo: true,
socketPath: os.Getenv("SSH_AUTH_SOCK"),
}

testCase := []struct {
Expand Down Expand Up @@ -254,6 +256,7 @@ func TestRunBuildWithInteractiveMode(t *testing.T) {
useSudo: true,
interactiveMode: true,
interact: &mockInteract{},
socketPath: os.Getenv("SSH_AUTH_SOCK"),
}

testCase := []struct {
Expand Down

0 comments on commit 6b05aa1

Please sign in to comment.