Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Empty file.
Empty file.
1 change: 1 addition & 0 deletions approvals/kir_test.TestCLI.Stdin.exitcode.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
7 changes: 7 additions & 0 deletions approvals/kir_test.TestCLI.Stdin.input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Pod
metadata:
name: p
spec:
containers:
- image: redis:7
Empty file.
1 change: 1 addition & 0 deletions approvals/kir_test.TestCLI.Stdin.stdout.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
redis:7
1 change: 1 addition & 0 deletions approvals/kir_test.TestCLI.Usage.exitcode.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions approvals/kir_test.TestCLI.Usage.stderr.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Usage: kir <file_path> [<file_path_2> ...] or kir -
Empty file.
1 change: 1 addition & 0 deletions approvals/kir_test.TestError.Service.exitcode.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 1 addition & 1 deletion approvals/kir_test.TestError.Service.stderr.approved.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error processing document: unsupported kind Service
error: error processing document: unsupported kind Service
1 change: 1 addition & 0 deletions approvals/kir_test.TestKind.CronJob.exitcode.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 1 addition & 1 deletion approvals/kir_test.TestKind.CronJob.stdout.approved.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
busybox:1.28
busybox:1.28
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 1 addition & 1 deletion approvals/kir_test.TestKind.DaemonSet.stdout.approved.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gcr.io/google-containers/busybox
gcr.io/google-containers/busybox
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 1 addition & 1 deletion approvals/kir_test.TestKind.Deployment.stdout.approved.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
gcr.io/google-containers/busybox
gcr.io/google-containers/sidecar
kiwigrid/k8s-sidecar
kiwigrid/k8s-sidecar
1 change: 1 addition & 0 deletions approvals/kir_test.TestKind.Job.exitcode.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 1 addition & 1 deletion approvals/kir_test.TestKind.Job.stdout.approved.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
perl
perl
1 change: 1 addition & 0 deletions approvals/kir_test.TestKind.Pod.exitcode.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 1 addition & 1 deletion approvals/kir_test.TestKind.Pod.stdout.approved.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
nginx
gcr.io/google-containers/sidecar
busybox:1.28
busybox:1.28
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 1 addition & 1 deletion approvals/kir_test.TestKind.ReplicaSet.stdout.approved.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
nginx
gcr.io/google-containers/sidecar
kiwigrid/k8s-sidecar
kiwigrid/k8s-sidecar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
registry.k8s.io/nginx-slim:0.8
gcr.io/google-containers/sidecar
kiwigrid/k8s-sidecar
kiwigrid/k8s-sidecar
1 change: 1 addition & 0 deletions approvals/kir_test.TestMultiple.exitcode.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
2 changes: 1 addition & 1 deletion approvals/kir_test.TestMultiple.stdout.approved.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
perl
nginx
gcr.io/google-containers/sidecar
gcr.io/google-containers/sidecar
62 changes: 42 additions & 20 deletions approvals/kir_test.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,71 @@
package processor

import (
"strings"
"bytes"
"io"
"os"
"strconv"
"testing"

approvals "github.com/approvals/go-approval-tests"
"github.com/mpv/kir/processor"
"github.com/mpv/kir/cmd"
)

// verify processes file and approves what the tool would print to stdout (the
// images) and to stderr (the error message, if any) as two separate golden
// files. Expected behavior lives entirely in the goldens: a change to either
// stream — an image appearing or disappearing, or an error starting or
// stopping — surfaces as a reviewable diff.
func verify(t *testing.T, file string) {
// verify runs the CLI in-process through cmd.Run — the real entry point — for
// the given args and stdin, and approves the (stdout, stderr, exit code) triple
// as three golden files. Every scenario goes through this one seam, so the
// goldens capture exactly what the tool emits. Granular, per-layer coverage
// lives in the k8s/yamlparser/processor/fileutil unit tests; this package is
// the behavioral golden layer.
func verify(t *testing.T, args []string, stdin io.Reader) {
t.Helper()

images, err := processor.ProcessFile(file)
var stdout, stderr bytes.Buffer
code := cmd.Run(args, stdin, &stdout, &stderr)

stdout := strings.Join(images, "\n")
approvals.VerifyString(t, stdout, approvals.Options().ForFile().WithAdditionalInformation("stdout"))

stderr := ""
if err != nil {
stderr = err.Error()
}
approvals.VerifyString(t, stderr, approvals.Options().ForFile().WithAdditionalInformation("stderr"))
approvals.VerifyString(t, stdout.String(), approvals.Options().ForFile().WithAdditionalInformation("stdout"))
approvals.VerifyString(t, stderr.String(), approvals.Options().ForFile().WithAdditionalInformation("stderr"))
approvals.VerifyString(t, strconv.Itoa(code), approvals.Options().ForFile().WithAdditionalInformation("exitcode"))
}

func TestKind(t *testing.T) {
kinds := []string{"Pod", "CronJob", "DaemonSet", "Deployment", "Job", "ReplicaSet", "StatefulSet"}

for _, kind := range kinds {
t.Run(kind, func(t *testing.T) {
verify(t, "kir_test.TestKind."+kind+".input.yaml")
verify(t, []string{"kir_test.TestKind." + kind + ".input.yaml"}, nil)
})
}
}

func TestError(t *testing.T) {
t.Run("Service", func(t *testing.T) {
verify(t, "kir_test.TestError.Service.input.yaml")
verify(t, []string{"kir_test.TestError.Service.input.yaml"}, nil)
})
}

func TestMultiple(t *testing.T) {
verify(t, "kir_test.TestMultiple.input.yaml")
verify(t, []string{"kir_test.TestMultiple.input.yaml"}, nil)
}

// TestCLI covers behaviour that only exists at the CLI boundary — stdin wiring,
// argument resolution, and no-args usage — which no file-argument scenario
// above reaches.
func TestCLI(t *testing.T) {
t.Run("Stdin", func(t *testing.T) {
f, err := os.Open("kir_test.TestCLI.Stdin.input.yaml")
if err != nil {
t.Fatal(err)
}
defer f.Close()
verify(t, []string{"-"}, f)
})

t.Run("MissingFile", func(t *testing.T) {
verify(t, []string{"does-not-exist.yaml"}, nil)
})

t.Run("Usage", func(t *testing.T) {
verify(t, nil, nil)
})
}
Loading