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

Use golden package for unit test 🐐 #623

Merged
merged 11 commits into from
Jan 19, 2020
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
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ test-unit: ./vendor ## run unit tests
@echo "Running unit tests..."
@go test -failfast -v -cover ./...

.PHONY: test-unit-update-golden
test-unit-update-golden: ./vendor ## run unit tests (updating golden files)
@echo "Running unit tests updating golden files..."
@./hack/update-golden.sh

.PHONY: test-e2e
test-e2e: bin/tkn ## run e2e tests
@echo "Running e2e tests..."
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/google/go-cmp v0.3.1
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367 // indirect
github.com/google/licenseclassifier v0.0.0-20191208174752-5628aef0e29a // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
Expand All @@ -24,7 +23,6 @@ require (
github.com/imdario/mergo v0.3.8 // indirect
github.com/jonboulle/clockwork v0.1.1-0.20190114141812-62fb9bc030d1
github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3 // indirect
github.com/knative/test-infra v0.0.0-20191223203026-935a8f052a48 // indirect
github.com/markbates/inflect v1.0.4 // indirect
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect
github.com/mattn/go-isatty v0.0.9 // indirect
Expand All @@ -34,7 +32,6 @@ require (
github.com/onsi/gomega v1.7.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.8.1
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.4.0
Expand All @@ -47,7 +44,9 @@ require (
golang.org/x/net v0.0.0-20190923162816-aa69164e4478 // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
gotest.tools/v3 v3.0.0
k8s.io/api v0.0.0-20191004102255-dacd7df5a50b
k8s.io/apimachinery v0.0.0-20191004074956-01f8b7d1121a
Expand Down
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb
github.com/census-instrumentation/opencensus-proto v0.2.1 h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/chmouel/cobra v0.0.0-20191021105835-a78788917390 h1:r2QCtXeuDAgROGLHUyLAnC4ju/o9zGIZ+kZLaNBKgqI=
github.com/chmouel/cobra v0.0.0-20191021105835-a78788917390/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/chmouel/cobra v0.0.0-20200107083527-379e7a80af0c h1:8YsovAhAya2jnvoIn5aCqyu2E/Zl1+3KBP5tpNXL+H8=
github.com/chmouel/cobra v0.0.0-20200107083527-379e7a80af0c/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down Expand Up @@ -98,8 +96,6 @@ github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367 h1:ScAXWS+TR6MZKex+7
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
github.com/google/licenseclassifier v0.0.0-20190926221455-842c0d70d702 h1:nVgx26pAe6l/02mYomOuZssv28XkacGw/0WeiTVorqw=
github.com/google/licenseclassifier v0.0.0-20190926221455-842c0d70d702/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M=
github.com/google/licenseclassifier v0.0.0-20191208174752-5628aef0e29a h1:PCQssw0nLgVvuzH737oT/qi1j09RbvMmAQpjpNEJ0p8=
github.com/google/licenseclassifier v0.0.0-20191208174752-5628aef0e29a/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -151,8 +147,6 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:C
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/knative/test-infra v0.0.0-20191223203026-935a8f052a48 h1:kaGwu3xnMa5jZ17XxyeLTE0g9Yp59ZLy3o1tjyj4u4c=
github.com/knative/test-infra v0.0.0-20191223203026-935a8f052a48/go.mod h1:l77IWBscEV5T4sYb64/9iwRCVY4UXEIqMcAppsblHW4=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down Expand Up @@ -220,8 +214,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
Expand Down Expand Up @@ -412,13 +404,10 @@ k8s.io/api v0.0.0-20191004102255-dacd7df5a50b h1:38Nx0U83WjBqn1hUWxlgKc7mvH7WhyH
k8s.io/api v0.0.0-20191004102255-dacd7df5a50b/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA=
k8s.io/apimachinery v0.0.0-20191004074956-01f8b7d1121a h1:lDydUqHrbL/1l5ZQrqD1RIlabhmX8aiZEtxVUb+30iU=
k8s.io/apimachinery v0.0.0-20191004074956-01f8b7d1121a/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0=
k8s.io/apimachinery v0.17.0 h1:xRBnuie9rXcPxUkDizUsGvPf1cnlZCFu210op7J7LJo=
k8s.io/cli-runtime v0.0.0-20191004110054-fe9b9282443f h1:vJOrMsZe+RD884n+WQ5So2oOp7SajI0Op3oOBg64ZsY=
k8s.io/cli-runtime v0.0.0-20191004110054-fe9b9282443f/go.mod h1:qWnH3/b8sp/l7EvlDh7ulDU3UWA4P4N1NFbEEP791tM=
k8s.io/cli-runtime v0.17.0 h1:XEuStbJBHCQlEKFyTQmceDKEWOSYHZkcYWKp3SsQ9Hk=
k8s.io/client-go v0.0.0-20191004102537-eb5b9a8cfde7 h1:WyPHgjjXvF4zVVwKGZKKiJGBUW45AuN44uSOuH8euuE=
k8s.io/client-go v0.0.0-20191004102537-eb5b9a8cfde7/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
k8s.io/client-go v11.0.0+incompatible h1:LBbX2+lOwY9flffWlJM7f1Ct8V2SRNiMRDFeiwnJo9o=
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
k8s.io/kube-openapi v0.0.0-20171101183504-39a7bf85c140 h1:j1Zez+Xb4OWvCdROqeq8sP2ACi/qWV1tj/imP0/8a0k=
Expand All @@ -427,7 +416,6 @@ knative.dev/caching v0.0.0-20190719140829-2032732871ff h1:PrlDvOGvCASqW5Fs3ZGes0
knative.dev/caching v0.0.0-20190719140829-2032732871ff/go.mod h1:dHXFU6CGlLlbzaWc32g80cR92iuBSpsslDNBWI8C7eg=
knative.dev/pkg v0.0.0-20190909195211-528ad1c1dd62 h1:0neKnC9hstF5Lewj6OaAJdXqk22IZ7pnOPQ8Zl8cfq4=
knative.dev/pkg v0.0.0-20190909195211-528ad1c1dd62/go.mod h1:pgODObA1dTyhNoFxPZTTjNWfx6F0aKsKzn+vaT9XO/Q=
knative.dev/pkg v0.0.0-20200108203553-8c6241207427 h1:swUPz/GhGE0bdXX8Ys6AXhjshbCcYXgW9weH7LvK28U=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
5 changes: 5 additions & 0 deletions hack/update-golden.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# This will run `go test a/package -test.update-golden=true` on all packages that are importing `gotest.tools/v3/golden`
# This will update the golden files with the current output.
# Run this only when you are sure the output is meant to change.
go test $(go list -f '{{ .ImportPath }} {{ .TestImports }}' ./... | grep gotest.tools/v3/golden | awk '{print $1}' | tr '\n' ' ') -test.update-golden=true
18 changes: 3 additions & 15 deletions pkg/cmd/clustertask/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
package clustertask

import (
"strings"
"fmt"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/jonboulle/clockwork"
"github.com/tektoncd/cli/pkg/test"
cb "github.com/tektoncd/cli/pkg/test/builder"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
pipelinetest "github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
"gotest.tools/v3/golden"
)

func TestClusterTaskList_Empty(t *testing.T) {
Expand Down Expand Up @@ -59,17 +59,5 @@ func TestClusterTaskListOnlyClusterTasks(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

expected := []string{
"NAME AGE",
"guavas 1 minute ago",
"avocados 20 seconds ago",
"pineapple 3 weeks ago",
"",
}

text := strings.Join(expected, "\n")
if d := cmp.Diff(text, output); d != "" {
t.Errorf("Unexpected output mismatch: %s", d)
}
golden.Assert(t, output, fmt.Sprintf("%s.golden", t.Name()))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NAME AGE
guavas 1 minute ago
avocados 20 seconds ago
pineapple 3 weeks ago
50 changes: 3 additions & 47 deletions pkg/cmd/condition/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package condition

import (
"errors"
"fmt"
"strings"
"testing"
"time"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
pipelinetest "github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
"gotest.tools/v3/golden"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -75,85 +77,42 @@ func TestConditionList(t *testing.T) {
command []string
input pipelinetest.Clients
wantError bool
want []string
}{
{
name: "Invalid Namespace",
command: []string{"ls", "-n", "invalid"},
input: seeds[0],
wantError: true,
want: []string{"namespaces \"invalid\" not found"},
},
{
name: "Found no conditions",
command: []string{"ls", "-n", "empty"},
input: seeds[0],
wantError: false,
want: []string{"No conditions found", ""},
},
{
name: "Found conditions",
command: []string{"ls", "-n", "ns"},
input: seeds[0],
wantError: false,
want: []string{
"NAME AGE",
"condition1 1 minute ago",
"condition2 20 seconds ago",
"condition3 3 weeks ago",
"",
},
},
{
name: "Specify output flag",
command: []string{"ls", "-n", "ns", "--output", "yaml"},
input: seeds[0],
wantError: false,
want: []string{
"apiVersion: tekton.dev/v1alpha1",
"items:",
"- metadata:",
" creationTimestamp: \"1984-04-03T23:59:00Z\"",
" name: condition1",
" namespace: ns",
" spec:",
" check:",
" name: \"\"",
" resources: {}",
"- metadata:",
" creationTimestamp: \"1984-04-03T23:59:40Z\"",
" name: condition2",
" namespace: ns",
" spec:",
" check:",
" name: \"\"",
" resources: {}",
"- metadata:",
" creationTimestamp: \"1984-03-13T16:00:00Z\"",
" name: condition3",
" namespace: ns",
" spec:",
" check:",
" name: \"\"",
" resources: {}",
"kind: ConditionList",
"metadata: {}",
"",
},
},
{
name: "Failed to list condition resources",
command: []string{"ls", "-n", "ns"},
input: seeds[1],
wantError: true,
want: []string{"test error"},
},
{
name: "Failed to list condition resources with specify output flag",
command: []string{"ls", "-n", "ns", "--output", "yaml"},
input: seeds[1],
wantError: true,
want: []string{"test error"},
},
}

Expand All @@ -162,20 +121,17 @@ func TestConditionList(t *testing.T) {
p := &test.Params{Tekton: tp.input.Pipeline, Kube: tp.input.Kube}
pipelineResource := Command(p)

want := strings.Join(tp.want, "\n")

out, err := test.ExecuteCommand(pipelineResource, tp.command...)
if tp.wantError {
if err == nil {
t.Errorf("Error expected here")
}
test.AssertOutput(t, want, err.Error())
} else {
if err != nil {
t.Errorf("Unexpected Error")
}
test.AssertOutput(t, want, out)
}
golden.Assert(t, out, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-"))
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Failed to list conditions from ns namespace
Error: test error
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Failed to list conditions from ns namespace
Error: test error
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NAME AGE
condition1 1 minute ago
condition2 20 seconds ago
condition3 3 weeks ago
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No conditions found
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: namespaces "invalid" not found
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: tekton.dev/v1alpha1
items:
- metadata:
creationTimestamp: "1984-04-03T23:59:00Z"
name: condition1
namespace: ns
spec:
check:
name: ""
resources: {}
- metadata:
creationTimestamp: "1984-04-03T23:59:40Z"
name: condition2
namespace: ns
spec:
check:
name: ""
resources: {}
- metadata:
creationTimestamp: "1984-03-13T16:00:00Z"
name: condition3
namespace: ns
spec:
check:
name: ""
resources: {}
kind: ConditionList
metadata: {}
Loading