Skip to content

Commit

Permalink
tests: import pipeline internal builders πŸ’
Browse files Browse the repository at this point in the history
Importing pipeline internal builders in cli so that we can safely take
time to migrate out of them with less pressure (and with less
dependency problems).

Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
  • Loading branch information
vdemeester committed Sep 18, 2020
1 parent 9e59b20 commit 1d3235a
Show file tree
Hide file tree
Showing 88 changed files with 2,567 additions and 1,307 deletions.
14 changes: 14 additions & 0 deletions internal/builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Builder package for tests

This package holds `Builder` functions that can be used to create struct in
tests with less noise.

One of the most important characteristic of a unit test (and any type of test
really) is **readability**. This means it should be easy to read but most
importantly it should clearly show the intent of the test. The setup (and
cleanup) of the tests should be as small as possible to avoid the noise. Those
builders exists to help with that.

There is currently two versionned builder supported:
- [`v1alpha1`](./v1alpha1)
- [`v1beta1`](./v1beta1)
File renamed without changes.
File renamed without changes.
124 changes: 124 additions & 0 deletions internal/builder/v1alpha1/condition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builder_test

import (
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

tb "github.com/tektoncd/cli/internal/builder/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
)

func TestCondition(t *testing.T) {
condition := tb.Condition("cond-name",
tb.ConditionNamespace("foo"),
tb.ConditionLabels(
map[string]string{
"label-1": "label-value-1",
"label-2": "label-value-2",
}),
tb.ConditionAnnotations(
map[string]string{
"annotation-1": "annotation-value-1",
"annotation-2": "annotation-value-2",
}),
tb.ConditionSpec(tb.ConditionSpecCheck("", "ubuntu", tb.Command("exit 0")),
tb.ConditionDescription("Test Condition"),
tb.ConditionParamSpec("param-1", v1alpha1.ParamTypeString,
tb.ParamSpecDefault("default"),
tb.ParamSpecDescription("desc")),
tb.ConditionResource("git-resource", v1alpha1.PipelineResourceTypeGit),
tb.ConditionResource("pr", v1alpha1.PipelineResourceTypePullRequest),
),
)

expected := &v1alpha1.Condition{
ObjectMeta: metav1.ObjectMeta{
Name: "cond-name",
Namespace: "foo",
Labels: map[string]string{
"label-1": "label-value-1",
"label-2": "label-value-2",
},
Annotations: map[string]string{
"annotation-1": "annotation-value-1",
"annotation-2": "annotation-value-2",
},
},
Spec: v1alpha1.ConditionSpec{
Check: v1alpha1.Step{
Container: corev1.Container{
Image: "ubuntu",
Command: []string{"exit 0"},
},
},
Description: "Test Condition",
Params: []v1alpha1.ParamSpec{{
Name: "param-1",
Type: v1alpha1.ParamTypeString,
Description: "desc",
Default: &v1alpha1.ArrayOrString{
Type: v1alpha1.ParamTypeString,
StringVal: "default",
}}},
Resources: []v1alpha1.ResourceDeclaration{{
Name: "git-resource",
Type: "git",
}, {
Name: "pr",
Type: "pullRequest",
}},
},
}

if d := cmp.Diff(expected, condition); d != "" {
t.Fatalf("Condition diff -want, +got: %v", d)
}
}

func TestConditionWithScript(t *testing.T) {
condition := tb.Condition("cond-name",
tb.ConditionNamespace("foo"),
tb.ConditionSpec(tb.ConditionSpecCheck("", "ubuntu"),
tb.ConditionSpecCheckScript("ls /tmp"),
),
)

expected := &v1alpha1.Condition{
ObjectMeta: metav1.ObjectMeta{
Name: "cond-name",
Namespace: "foo",
},
Spec: v1alpha1.ConditionSpec{
Check: v1alpha1.Step{
Container: corev1.Container{
Image: "ubuntu",
},
Script: "ls /tmp",
},
},
}

if d := cmp.Diff(expected, condition); d != "" {
t.Fatalf("Condition diff -want, +got: %v", d)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package builder

import (
v1beta1 "github.com/tektoncd/pipeline/internal/builder/v1beta1"
v1beta1 "github.com/tektoncd/cli/internal/builder/v1beta1"
)

// ContainerOp is an operation which modifies a Container struct.
Expand Down
File renamed without changes.
154 changes: 154 additions & 0 deletions internal/builder/v1alpha1/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builder_test

import (
"testing"

"github.com/google/go-cmp/cmp"
tb "github.com/tektoncd/cli/internal/builder/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
)

// This is a "hack" to make the example "look" like tests
var t *testing.T

func ExampleTask() {
// You can declare re-usable modifiers
myStep := tb.Step("myimage")
// … and use them in a Task definition
myTask := tb.Task("my-task", tb.TaskSpec(
tb.Step("myotherimage", tb.StepCommand("/mycmd")),
myStep,
))
// … and another one.
myOtherTask := tb.Task("my-other-task",
tb.TaskSpec(myStep,
tb.TaskInputs(tb.InputsResource("workspace", v1alpha1.PipelineResourceTypeGit)),
),
)
expectedTask := &v1alpha1.Task{
// […]
}
expectedOtherTask := &v1alpha1.Task{
// […]
}
// […]
if d := cmp.Diff(expectedTask, myTask); d != "" {
t.Fatalf("Task diff -want, +got: %v", d)
}
if d := cmp.Diff(expectedOtherTask, myOtherTask); d != "" {
t.Fatalf("Task diff -want, +got: %v", d)
}
}

func ExampleClusterTask() {
myClusterTask := tb.ClusterTask("my-task", tb.ClusterTaskSpec(
tb.Step("myotherimage", tb.StepCommand("/mycmd")),
))
expectedClusterTask := &v1alpha1.Task{
// […]
}
// […]
if d := cmp.Diff(expectedClusterTask, myClusterTask); d != "" {
t.Fatalf("ClusterTask diff -want, +got: %v", d)
}
}

func ExampleTaskRun() {
// A simple definition, with a Task reference
myTaskRun := tb.TaskRun("my-taskrun", tb.TaskRunSpec(
tb.TaskRunTaskRef("my-task"),
))
// … or a more complex one with inline TaskSpec
myTaskRunWithSpec := tb.TaskRun("my-taskrun-with-spec", tb.TaskRunSpec(
tb.TaskRunInputs(
tb.TaskRunInputsParam("myarg", "foo"),
tb.TaskRunInputsResource("workspace", tb.TaskResourceBindingRef("git-resource")),
),
tb.TaskRunTaskSpec(
tb.TaskInputs(
tb.InputsResource("workspace", v1alpha1.PipelineResourceTypeGit),
tb.InputsParamSpec("myarg", v1alpha1.ParamTypeString, tb.ParamSpecDefault("mydefault")),
),
tb.Step("myimage", tb.StepCommand("/mycmd"),
tb.StepArgs("--my-arg=$(inputs.params.myarg)"),
),
),
))
expectedTaskRun := &v1alpha1.TaskRun{
// […]
}
expectedTaskRunWithSpec := &v1alpha1.TaskRun{
// […]
}
// […]
if d := cmp.Diff(expectedTaskRun, myTaskRun); d != "" {
t.Fatalf("Task diff -want, +got: %v", d)
}
if d := cmp.Diff(expectedTaskRunWithSpec, myTaskRunWithSpec); d != "" {
t.Fatalf("Task diff -want, +got: %v", d)
}
}

func ExamplePipeline() {
pipeline := tb.Pipeline("tomatoes",
tb.PipelineSpec(tb.PipelineTask("foo", "banana")),
)
expectedPipeline := &v1alpha1.Pipeline{
// […]
}
// […]
if d := cmp.Diff(expectedPipeline, pipeline); d != "" {
t.Fatalf("Task diff -want, +got: %v", d)
}
}

func ExamplePipelineRun() {
pipelineRun := tb.PipelineRun("pear",
tb.PipelineRunSpec("tomatoes", tb.PipelineRunServiceAccountName("inexistent")),
)
expectedPipelineRun := &v1alpha1.PipelineRun{
// […]
}
// […]
if d := cmp.Diff(expectedPipelineRun, pipelineRun); d != "" {
t.Fatalf("Task diff -want, +got: %v", d)
}
}

func ExamplePipelineResource() {
gitResource := tb.PipelineResource("git-resource", tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeGit, tb.PipelineResourceSpecParam("URL", "https://foo.git"),
))
imageResource := tb.PipelineResource("image-resource", tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeImage, tb.PipelineResourceSpecParam("URL", "gcr.io/kristoff/sven"),
))
expectedGitResource := v1alpha1.PipelineResource{
// […]
}
expectedImageResource := v1alpha1.PipelineResource{
// […]
}
// […]
if d := cmp.Diff(expectedGitResource, gitResource); d != "" {
t.Fatalf("Task diff -want, +got: %v", d)
}
if d := cmp.Diff(expectedImageResource, imageResource); d != "" {
t.Fatalf("Task diff -want, +got: %v", d)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package builder

import (
v1beta1 "github.com/tektoncd/pipeline/internal/builder/v1beta1"
v1beta1 "github.com/tektoncd/cli/internal/builder/v1beta1"
)

// OwnerReferenceOp is an operation which modifies an OwnerReference struct.
Expand Down
File renamed without changes.
44 changes: 44 additions & 0 deletions internal/builder/v1alpha1/param_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builder_test

import (
"testing"

"github.com/google/go-cmp/cmp"
tb "github.com/tektoncd/cli/internal/builder/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
)

func TestGenerateString(t *testing.T) {
value := tb.ArrayOrString("somestring")
expectedValue := &v1alpha1.ArrayOrString{
Type: v1alpha1.ParamTypeString,
StringVal: "somestring",
}
if d := cmp.Diff(expectedValue, value); d != "" {
t.Fatalf("ArrayOrString diff -want, +got: %v", d)
}
}

func TestGenerateArray(t *testing.T) {
value := tb.ArrayOrString("some", "array", "elements")
expectedValue := &v1alpha1.ArrayOrString{
Type: v1alpha1.ParamTypeArray,
ArrayVal: []string{"some", "array", "elements"},
}
if d := cmp.Diff(expectedValue, value); d != "" {
t.Fatalf("ArrayOrString diff -want, +got: %v", d)
}
}
File renamed without changes.
Loading

0 comments on commit 1d3235a

Please sign in to comment.