diff --git a/Makefile b/Makefile index 6c62b6c..da79136 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,7 @@ test: ## Run tests testprep: ## Run test prerequisite tasks docker build -t container-canary/kubeflow:shouldpass -f internal/testdata/containers/kubeflow.Dockerfile . + docker build -t container-canary/kubeflow:shouldfail -f internal/testdata/containers/kubeflow_broken.Dockerfile . version: @echo version: $(VERSION) diff --git a/cmd/validate_test.go b/cmd/validate_test.go index c842493..f0bd72c 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -18,7 +18,7 @@ package cmd import ( - "fmt" + "bytes" "testing" "github.com/stretchr/testify/assert" @@ -26,17 +26,37 @@ import ( func TestValidate(t *testing.T) { assert := assert.New(t) + b := new(bytes.Buffer) + rootCmd.SetOut(b) + rootCmd.SetErr(b) rootCmd.SetArgs([]string{"validate", "--file", "../examples/kubeflow.yaml", "container-canary/kubeflow:shouldpass"}) err := rootCmd.Execute() + assert.Nil(err, "should not error") - if err != nil { - fmt.Printf("%+v", err) - } + assert.Contains(b.String(), "Validating container-canary/kubeflow:shouldpass against kubeflow", "did not validate") + assert.Contains(b.String(), "validation passed", "did not pass") +} + +func TestValidateFails(t *testing.T) { + assert := assert.New(t) + b := new(bytes.Buffer) + rootCmd.SetOut(b) + rootCmd.SetErr(b) + rootCmd.SetArgs([]string{"validate", "--file", "../examples/kubeflow.yaml", "container-canary/kubeflow:shouldfail"}) + err := rootCmd.Execute() + + assert.NotNil(err, "should fail") + assert.Contains(b.String(), "validation failed", "did not fail") } func TestFileDoesNotExist(t *testing.T) { assert := assert.New(t) - rootCmd.SetArgs([]string{"validate", "--file", "foo.yaml", "container-canary/kubeflow:shouldpass"}) + b := new(bytes.Buffer) + rootCmd.SetOut(b) + rootCmd.SetErr(b) + rootCmd.SetArgs([]string{"validate", "--file", "foo.yaml", "container-canary/kubeflow:doesnotexist"}) err := rootCmd.Execute() + assert.NotNil(err, "did not error") + assert.Contains(b.String(), "Cannot find container-canary/kubeflow:doesnotexist", "did not fail") } diff --git a/examples/README.md b/examples/README.md index c85651f..1e0dd2a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -7,12 +7,12 @@ For example the Kubeflow example from the README can be used directly like this. ```console $ canary validate --file https://raw.githubusercontent.com/NVIDIA/container-canary/main/examples/kubeflow.yaml public.ecr.aws/j1r0q0g6/notebooks/notebook-servers/jupyter-scipy:v1.5.0-rc.1 Validating public.ecr.aws/j1r0q0g6/notebooks/notebook-servers/jupyter-scipy:v1.5.0-rc.1 against kubeflow - 👩 User is jovyan [true] - 🆔 User ID is 1000 [true] - 🏠 Home directory is /home/jovyan [true] - 🌏 Exposes an HTTP interface on port 8888 [true] - 🧭 Correctly routes the NB_PREFIX [true] - 🔓 Sets 'Access-Control-Allow-Origin: *' header [true] + 🏠 Home directory is /home/jovyan [passed] + 👩 User is jovyan [passed] + 🆔 User ID is 1000 [passed] + 🌏 Exposes an HTTP interface on port 8888 [passed] + 🔓 Sets 'Access-Control-Allow-Origin: *' header [passed] + 🧭 Correctly routes the NB_PREFIX [passed] validation passed ``` diff --git a/internal/testdata/containers/kubeflow_broken.Dockerfile b/internal/testdata/containers/kubeflow_broken.Dockerfile new file mode 100644 index 0000000..23b90c5 --- /dev/null +++ b/internal/testdata/containers/kubeflow_broken.Dockerfile @@ -0,0 +1,19 @@ +FROM python + +ARG UNAME=bob +ARG UID=1000 +ARG GID=1000 +RUN groupadd -g $GID -o $UNAME +RUN useradd -m -u $UID -g $GID -o -s /bin/bash $UNAME + +# Oh no this should be jovyan! +USER bob + +# This should be /home/jovyan +WORKDIR /home/bob + +ENV PATH=/home/bob/.local/bin:$PATH + +RUN pip install jupyterlab + +CMD jupyter lab --ip=0.0.0.0 diff --git a/internal/validator/validator.go b/internal/validator/validator.go index 1c1b8c4..707dc88 100644 --- a/internal/validator/validator.go +++ b/internal/validator/validator.go @@ -225,7 +225,7 @@ func Validate(image string, configPath string, cmd *cobra.Command, debug bool) ( if err != nil { tty = bufio.NewReader(os.Stdin) } - p := tea.NewProgram(m, tea.WithInput(tty)) + p := tea.NewProgram(m, tea.WithInput(tty), tea.WithOutput(cmd.OutOrStderr())) out, err := p.StartReturningModel() if err != nil { return false, err