Skip to content

Commit

Permalink
Use golden package for unit test for PipelineResource 🐐
Browse files Browse the repository at this point in the history
This allow to remove the potential long expected output from the test
code. See gotest.tools/v3/golden for documentation.

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester authored and tekton-robot committed Jan 19, 2020
1 parent 3e05b0b commit f326912
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 75 deletions.
37 changes: 4 additions & 33 deletions pkg/cmd/pipelineresource/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
package pipelineresource

import (
"strings"
"fmt"
"testing"

"github.com/tektoncd/cli/pkg/test"
"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"
)
Expand Down Expand Up @@ -91,21 +92,7 @@ func TestPipelineResourceDescribe_WithParams(t *testing.T) {
p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube}
pipelineresource := Command(p)
out, _ := test.ExecuteCommand(pipelineresource, "desc", "test-1", "-n", "test-ns-1")
expected := []string{
"Name: test-1",
"Namespace: test-ns-1",
"PipelineResource Type: image",
"",
"Params\n",
" NAME VALUE",
" URL quay.io/tekton/controller",
"",
"Secret Params\n",
" No secret params",
"",
}

test.AssertOutput(t, strings.Join(expected, "\n"), out)
golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name()))
}

func TestPipelineResourceDescribe_WithSecretParams(t *testing.T) {
Expand All @@ -131,21 +118,5 @@ func TestPipelineResourceDescribe_WithSecretParams(t *testing.T) {
p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube}
pipelineresource := Command(p)
out, _ := test.ExecuteCommand(pipelineresource, "desc", "test-1", "-n", "test-ns-1")
expected := []string{
"Name: test-1",
"Namespace: test-ns-1",
"PipelineResource Type: image",
"",
"Params\n",
" NAME VALUE",
" URL quay.io/tekton/controller",
" TAG latest",
"",
"Secret Params\n",
" FIELDNAME SECRETNAME",
" githubToken github-secrets",
"",
}

test.AssertOutput(t, strings.Join(expected, "\n"), out)
golden.Assert(t, out, fmt.Sprintf("%s.golden", t.Name()))
}
45 changes: 3 additions & 42 deletions pkg/cmd/pipelineresource/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package pipelineresource

import (
"fmt"
"strings"
"testing"

Expand All @@ -23,6 +24,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"
)
Expand Down Expand Up @@ -77,90 +79,49 @@ func TestPipelineResourceList(t *testing.T) {
name string
command *cobra.Command
args []string
expected []string
wantError bool
}{
{
name: "Invalid namespace",
command: command(t, pres, ns),
args: []string{"list", "-n", "invalid"},
wantError: true,
expected: []string{
"Error: namespaces \"invalid\" not found\n",
},
},
{
name: "Multiple pipeline resources",
command: command(t, pres, ns),
args: []string{"list", "-n", "test-ns-1"},
wantError: false,
expected: []string{
"NAME TYPE DETAILS",
"test-5 cloudEvent targetURI: http://sink",
"test git url: [email protected]:tektoncd/cli-new.git",
"test-2 git url: [email protected]:tektoncd/cli.git",
"test-1 image URL: quey.io/tekton/controller",
"test-3 image ---",
"",
},
},
{
name: "Single pipeline resource",
command: command(t, pres, ns),
args: []string{"list", "-n", "test-ns-2"},
wantError: false,
expected: []string{
"NAME TYPE DETAILS",
"test-4 image URL: quey.io/tekton/webhook",
"",
},
},
{
name: "Single Pipeline Resource by type",
command: command(t, pres, ns),
args: []string{"list", "-n", "test-ns-2", "-t", "image"},
wantError: false,
expected: []string{
"NAME TYPE DETAILS",
"test-4 image URL: quey.io/tekton/webhook",
"",
},
},
{
name: "Multiple Pipeline Resource by type",
command: command(t, pres, ns),
args: []string{"list", "-n", "test-ns-1", "-t", "image"},
wantError: false,
expected: []string{
"NAME TYPE DETAILS",
"test-1 image URL: quey.io/tekton/controller",
"test-3 image ---",
"",
},
},
{
name: "Empty Pipeline Resource by type",
command: command(t, pres, ns),
args: []string{"list", "-n", "test-ns-1", "-t", "storage"},
wantError: false,
expected: []string{
"No pipelineresources found.",
"",
},
},
{
name: "By template",
command: command(t, pres, ns),
args: []string{"list", "-n", "test-ns-1", "-o", "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"},
wantError: false,
expected: []string{
"test-5",
"test",
"test-2",
"test-1",
"test-3",
"",
},
},
}

Expand All @@ -171,7 +132,7 @@ func TestPipelineResourceList(t *testing.T) {
if !td.wantError && err != nil {
t.Errorf("Unexpected error: %v", err)
}
test.AssertOutput(t, strings.Join(td.expected, "\n"), out)
golden.Assert(t, out, strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-"))
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Name: test-1
Namespace: test-ns-1
PipelineResource Type: image

Params

NAME VALUE
URL quay.io/tekton/controller

Secret Params

No secret params
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Name: test-1
Namespace: test-ns-1
PipelineResource Type: image

Params

NAME VALUE
URL quay.io/tekton/controller
TAG latest

Secret Params

FIELDNAME SECRETNAME
githubToken github-secrets
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test-5
test
test-2
test-1
test-3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No pipelineresources 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,3 @@
NAME TYPE DETAILS
test-1 image URL: quey.io/tekton/controller
test-3 image ---
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NAME TYPE DETAILS
test-5 cloudEvent targetURI: http://sink
test git url: [email protected]:tektoncd/cli-new.git
test-2 git url: [email protected]:tektoncd/cli.git
test-1 image URL: quey.io/tekton/controller
test-3 image ---
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME TYPE DETAILS
test-4 image URL: quey.io/tekton/webhook
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME TYPE DETAILS
test-4 image URL: quey.io/tekton/webhook

0 comments on commit f326912

Please sign in to comment.