Skip to content

Commit 1f9c78f

Browse files
Reinstate stdout assertions and add more tests (#25)
Signed-off-by: Jacob Tomlinson <[email protected]>
1 parent 1f81197 commit 1f9c78f

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test: ## Run tests
3131

3232
testprep: ## Run test prerequisite tasks
3333
docker build -t container-canary/kubeflow:shouldpass -f internal/testdata/containers/kubeflow.Dockerfile .
34+
docker build -t container-canary/kubeflow:shouldfail -f internal/testdata/containers/kubeflow_broken.Dockerfile .
3435

3536
version:
3637
@echo version: $(VERSION)

cmd/validate_test.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,45 @@
1818
package cmd
1919

2020
import (
21-
"fmt"
21+
"bytes"
2222
"testing"
2323

2424
"github.com/stretchr/testify/assert"
2525
)
2626

2727
func TestValidate(t *testing.T) {
2828
assert := assert.New(t)
29+
b := new(bytes.Buffer)
30+
rootCmd.SetOut(b)
31+
rootCmd.SetErr(b)
2932
rootCmd.SetArgs([]string{"validate", "--file", "../examples/kubeflow.yaml", "container-canary/kubeflow:shouldpass"})
3033
err := rootCmd.Execute()
34+
3135
assert.Nil(err, "should not error")
32-
if err != nil {
33-
fmt.Printf("%+v", err)
34-
}
36+
assert.Contains(b.String(), "Validating container-canary/kubeflow:shouldpass against kubeflow", "did not validate")
37+
assert.Contains(b.String(), "validation passed", "did not pass")
38+
}
39+
40+
func TestValidateFails(t *testing.T) {
41+
assert := assert.New(t)
42+
b := new(bytes.Buffer)
43+
rootCmd.SetOut(b)
44+
rootCmd.SetErr(b)
45+
rootCmd.SetArgs([]string{"validate", "--file", "../examples/kubeflow.yaml", "container-canary/kubeflow:shouldfail"})
46+
err := rootCmd.Execute()
47+
48+
assert.NotNil(err, "should fail")
49+
assert.Contains(b.String(), "validation failed", "did not fail")
3550
}
3651

3752
func TestFileDoesNotExist(t *testing.T) {
3853
assert := assert.New(t)
39-
rootCmd.SetArgs([]string{"validate", "--file", "foo.yaml", "container-canary/kubeflow:shouldpass"})
54+
b := new(bytes.Buffer)
55+
rootCmd.SetOut(b)
56+
rootCmd.SetErr(b)
57+
rootCmd.SetArgs([]string{"validate", "--file", "foo.yaml", "container-canary/kubeflow:doesnotexist"})
4058
err := rootCmd.Execute()
59+
4160
assert.NotNil(err, "did not error")
61+
assert.Contains(b.String(), "Cannot find container-canary/kubeflow:doesnotexist", "did not fail")
4262
}

examples/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ For example the Kubeflow example from the README can be used directly like this.
77
```console
88
$ 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
99
Validating public.ecr.aws/j1r0q0g6/notebooks/notebook-servers/jupyter-scipy:v1.5.0-rc.1 against kubeflow
10-
👩 User is jovyan [true]
11-
🆔 User ID is 1000 [true]
12-
🏠 Home directory is /home/jovyan [true]
13-
🌏 Exposes an HTTP interface on port 8888 [true]
14-
🧭 Correctly routes the NB_PREFIX [true]
15-
🔓 Sets 'Access-Control-Allow-Origin: *' header [true]
10+
🏠 Home directory is /home/jovyan [passed]
11+
👩 User is jovyan [passed]
12+
🆔 User ID is 1000 [passed]
13+
🌏 Exposes an HTTP interface on port 8888 [passed]
14+
🔓 Sets 'Access-Control-Allow-Origin: *' header [passed]
15+
🧭 Correctly routes the NB_PREFIX [passed]
1616
validation passed
1717
```
1818

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM python
2+
3+
ARG UNAME=bob
4+
ARG UID=1000
5+
ARG GID=1000
6+
RUN groupadd -g $GID -o $UNAME
7+
RUN useradd -m -u $UID -g $GID -o -s /bin/bash $UNAME
8+
9+
# Oh no this should be jovyan!
10+
USER bob
11+
12+
# This should be /home/jovyan
13+
WORKDIR /home/bob
14+
15+
ENV PATH=/home/bob/.local/bin:$PATH
16+
17+
RUN pip install jupyterlab
18+
19+
CMD jupyter lab --ip=0.0.0.0

internal/validator/validator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func Validate(image string, configPath string, cmd *cobra.Command, debug bool) (
225225
if err != nil {
226226
tty = bufio.NewReader(os.Stdin)
227227
}
228-
p := tea.NewProgram(m, tea.WithInput(tty))
228+
p := tea.NewProgram(m, tea.WithInput(tty), tea.WithOutput(cmd.OutOrStderr()))
229229
out, err := p.StartReturningModel()
230230
if err != nil {
231231
return false, err

0 commit comments

Comments
 (0)