From 6b05aa1d8e847a9f5ff4afec91a9c8747e083907 Mon Sep 17 00:00:00 2001 From: Ibuki Date: Fri, 5 Mar 2021 00:51:58 +0900 Subject: [PATCH] fix: test (#65) --- cmd/build_test.go | 38 ++++++++++++++++++++++++++++++++++---- cmd/root_test.go | 30 +++++++++++++++++++++++++++++- launch/docker_test.go | 3 +++ 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/cmd/build_test.go b/cmd/build_test.go index b52152b..5430648 100644 --- a/cmd/build_test.go +++ b/cmd/build_test.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "fmt" "io" "io/ioutil" "os" @@ -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. (=) (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:/.git[#] + https://github.com//.git[#] + --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] @@ -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) { @@ -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) }) @@ -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) }) @@ -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) }) diff --git a/cmd/root_test.go b/cmd/root_test.go index e7e0b31..8a13988 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "fmt" "io" "os" "testing" @@ -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. (=) (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:/.git[#] + https://github.com//.git[#] + --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] @@ -127,6 +154,7 @@ Global Flags: -v, --verbose verbose output. ` + } assert.Equal(t, want, buf.String()) assert.NotNil(t, err) }) diff --git a/launch/docker_test.go b/launch/docker_test.go index 4ee7b52..fa96bff 100644 --- a/launch/docker_test.go +++ b/launch/docker_test.go @@ -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 { @@ -200,6 +201,7 @@ func TestRunBuildWithSudo(t *testing.T) { setupImage: "launcher", setupImageVersion: "latest", useSudo: true, + socketPath: os.Getenv("SSH_AUTH_SOCK"), } testCase := []struct { @@ -254,6 +256,7 @@ func TestRunBuildWithInteractiveMode(t *testing.T) { useSudo: true, interactiveMode: true, interact: &mockInteract{}, + socketPath: os.Getenv("SSH_AUTH_SOCK"), } testCase := []struct {