Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstate stdout assertions and add more tests #25

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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