-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Versions for builders and internal package 📦
This adds versions for the builder: v1alpha1 and v1beta1. This also migrate the builder to `internal/builder` while keeping an alias on `test/builder` for user of this package (marking it as deprecated). Signed-off-by: Vincent Demeester <[email protected]>
- Loading branch information
1 parent
f96cf2a
commit 2201e1b
Showing
116 changed files
with
8,273 additions
and
2,386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# 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 two types of functions defined in that package : | ||
|
||
* *Builders*: create and return a struct | ||
* *Modifiers*: return a function | ||
that will operate on a given struct. They can be applied to other | ||
Modifiers or Builders. | ||
|
||
Most of the Builder (and Modifier) that accepts Modifiers defines a type | ||
(`TypeOp`) that can be satisfied by existing function in this package, from | ||
other package _or_ inline. An example would be the following. | ||
|
||
```go | ||
// Definition | ||
type TaskRunOp func(*v1alpha1.TaskRun) | ||
func TaskRun(name, namespace string, ops ...TaskRunOp) *v1alpha1.TaskRun { | ||
// […] | ||
} | ||
func TaskRunOwnerReference(kind, name string) TaskRunOp { | ||
return func(t *v1alpha1.TaskRun) { | ||
// […] | ||
} | ||
} | ||
// Usage | ||
t := TaskRun("foo", "bar", func(t *v1alpha1.TaskRun){ | ||
// Do something with the Task struct | ||
// […] | ||
}) | ||
``` | ||
|
||
The main reason to define the `Op` type, and using it in the methods signatures | ||
is to group Modifier function together. It makes it easier to see what is a | ||
Modifier (or Builder) and on what it operates. | ||
|
||
By convention, this package is import with the "tb" as alias. The examples make | ||
that assumption. | ||
|
||
## Example | ||
|
||
Let's look at a non-exhaustive example. | ||
|
||
```go | ||
package builder_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
tb "github.com/tektoncd/pipeline/internal/builder/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func MyTest(t *testing.T) { | ||
// You can declare re-usable modifiers | ||
myStep := tb.Step("my-step", "myimage") | ||
// … and use them in a Task definition | ||
myTask := tb.Task("my-task", "namespace", tb.TaskSpec( | ||
tb.Step("simple-step", "myotherimage", tb.Command("/mycmd")), | ||
myStep, | ||
)) | ||
// … and another one. | ||
myOtherTask := tb.Task("my-other-task", "namespace", | ||
tb.TaskSpec(myStep, | ||
tb.TaskInputs(tb.InputsResource("workspace", v1alpha1.PipelineResourceTypeGit)), | ||
), | ||
) | ||
myClusterTask := tb.ClusterTask("my-task", tb.ClusterTaskSpec( | ||
tb.Step("simple-step", "myotherimage", tb.Command("/mycmd")), | ||
)) | ||
// A simple definition, with a Task reference | ||
myTaskRun := tb.TaskRun("my-taskrun", "namespace", tb.TaskRunSpec( | ||
tb.TaskRunTaskRef("my-task"), | ||
)) | ||
// … or a more complex one with inline TaskSpec | ||
myTaskRunWithSpec := tb.TaskRun("my-taskrun-with-spec", "namespace", 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.InputsParam("myarg", tb.ParamDefault("mydefault")), | ||
), | ||
tb.Step("mycontainer", "myimage", tb.Command("/mycmd"), | ||
tb.Args("--my-arg=$(inputs.params.myarg)"), | ||
), | ||
), | ||
)) | ||
// Pipeline | ||
pipeline := tb.Pipeline("tomatoes", "namespace", | ||
tb.PipelineSpec(tb.PipelineTask("foo", "banana")), | ||
) | ||
// … and PipelineRun | ||
pipelineRun := tb.PipelineRun("pear", "namespace", | ||
tb.PipelineRunSpec("tomatoes", tb.PipelineRunServiceAccount("inexistent")), | ||
) | ||
// And do something with them | ||
// […] | ||
if _, err := c.PipelineClient.Create(pipeline); err != nil { | ||
t.Fatalf("Failed to create Pipeline `%s`: %s", "tomatoes", err) | ||
} | ||
if _, err := c.PipelineRunClient.Create(pipelineRun); err != nil { | ||
t.Fatalf("Failed to create PipelineRun `%s`: %s", "pear", err) | ||
} | ||
// […] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
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 | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
) | ||
|
||
// ConditionOp is an operation which modifies a Condition struct. | ||
type ConditionOp func(*v1alpha1.Condition) | ||
|
||
// ConditionSpecOp is an operation which modifies a ConditionSpec struct. | ||
type ConditionSpecOp func(spec *v1alpha1.ConditionSpec) | ||
|
||
// Condition creates a Condition with default values. | ||
// Any number of Condition modifiers can be passed to transform it. | ||
func Condition(name string, ops ...ConditionOp) *v1alpha1.Condition { | ||
condition := &v1alpha1.Condition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
}, | ||
} | ||
for _, op := range ops { | ||
op(condition) | ||
} | ||
return condition | ||
} | ||
|
||
// ConditionNamespace sets the namespace on the condition | ||
func ConditionNamespace(namespace string) ConditionOp { | ||
return func(t *v1alpha1.Condition) { | ||
t.ObjectMeta.Namespace = namespace | ||
} | ||
} | ||
|
||
// ConditionLabels sets the labels on the condition. | ||
func ConditionLabels(labels map[string]string) ConditionOp { | ||
return func(Condition *v1alpha1.Condition) { | ||
if Condition.ObjectMeta.Labels == nil { | ||
Condition.ObjectMeta.Labels = map[string]string{} | ||
} | ||
for key, value := range labels { | ||
Condition.ObjectMeta.Labels[key] = value | ||
} | ||
} | ||
} | ||
|
||
// ConditionSpec creates a ConditionSpec with default values. | ||
// Any number of ConditionSpec modifiers can be passed to transform it. | ||
func ConditionSpec(ops ...ConditionSpecOp) ConditionOp { | ||
return func(Condition *v1alpha1.Condition) { | ||
ConditionSpec := &Condition.Spec | ||
for _, op := range ops { | ||
op(ConditionSpec) | ||
} | ||
Condition.Spec = *ConditionSpec | ||
} | ||
} | ||
|
||
// ConditionSpecCheck adds a Container, with the specified name and image, to the Condition Spec Check. | ||
// Any number of Container modifiers can be passed to transform it. | ||
func ConditionSpecCheck(name, image string, ops ...ContainerOp) ConditionSpecOp { | ||
return func(spec *v1alpha1.ConditionSpec) { | ||
c := &corev1.Container{ | ||
Name: name, | ||
Image: image, | ||
} | ||
for _, op := range ops { | ||
op(c) | ||
} | ||
spec.Check.Container = *c | ||
} | ||
} | ||
|
||
// ConditionDescription sets the description of the condition | ||
func ConditionDescription(desc string) ConditionSpecOp { | ||
return func(spec *v1alpha1.ConditionSpec) { | ||
spec.Description = desc | ||
} | ||
} | ||
|
||
// ConditionSpecCheckScript adds a script to the Spec. | ||
func ConditionSpecCheckScript(script string) ConditionSpecOp { | ||
return func(spec *v1alpha1.ConditionSpec) { | ||
spec.Check.Script = script | ||
} | ||
} | ||
|
||
// ConditionParamSpec adds a param, with specified name, to the Spec. | ||
// Any number of ParamSpec modifiers can be passed to transform it. | ||
func ConditionParamSpec(name string, pt v1alpha1.ParamType, ops ...ParamSpecOp) ConditionSpecOp { | ||
return func(ps *v1alpha1.ConditionSpec) { | ||
pp := &v1alpha1.ParamSpec{Name: name, Type: pt} | ||
for _, op := range ops { | ||
op(pp) | ||
} | ||
ps.Params = append(ps.Params, *pp) | ||
} | ||
} | ||
|
||
// ConditionResource adds a resource with specified name, and type to the ConditionSpec. | ||
func ConditionResource(name string, resourceType v1alpha1.PipelineResourceType) ConditionSpecOp { | ||
return func(spec *v1alpha1.ConditionSpec) { | ||
r := v1alpha1.ResourceDeclaration{ | ||
Name: name, | ||
Type: resourceType, | ||
} | ||
spec.Resources = append(spec.Resources, r) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
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/pipeline/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.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", | ||
}, | ||
}, | ||
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) | ||
} | ||
|
||
} |
Oops, something went wrong.