Skip to content

Commit

Permalink
Reinstate stdout assertions and add more tests (#25)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Tomlinson <[email protected]>
  • Loading branch information
jacobtomlinson authored Apr 13, 2022
1 parent 1f81197 commit 1f9c78f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 25 additions & 5 deletions cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,45 @@
package cmd

import (
"fmt"
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

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")
}
12 changes: 6 additions & 6 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
19 changes: 19 additions & 0 deletions internal/testdata/containers/kubeflow_broken.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion internal/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1f9c78f

Please sign in to comment.