Skip to content

Commit

Permalink
tests for reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyom committed Jul 1, 2019
1 parent c0168f1 commit 41472de
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
76 changes: 74 additions & 2 deletions pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,78 @@ func TestReconcilePropagateAnnotations(t *testing.T) {
}
}

func TestReconcileWithConditionChecks(t *testing.T) {}
func TestReconcileWithConditionChecks(t *testing.T) {
names.TestingSeed()
conditions := []*v1alpha1.Condition{{
ObjectMeta: metav1.ObjectMeta{
Name: "always-true",
Namespace: "foo",
},
Spec: v1alpha1.ConditionSpec{
Check: corev1.Container{
Image: "foo",
Args: []string{"bar"},
},
},
}}
ps := []*v1alpha1.Pipeline{tb.Pipeline("test-pipeline", "foo", tb.PipelineSpec(
tb.PipelineTask("hello-world-1", "hello-world", tb.PipelineTaskCondition("always-true") ),
))}
prs := []*v1alpha1.PipelineRun{tb.PipelineRun("test-pipeline-run-with-conditions", "foo",
tb.PipelineRunAnnotation("PipelineRunAnnotation", "PipelineRunValue"),
tb.PipelineRunSpec("test-pipeline",
tb.PipelineRunServiceAccount("test-sa"),
),
)}
ts := []*v1alpha1.Task{tb.Task("hello-world", "foo")}

d := test.Data{
PipelineRuns: prs,
Pipelines: ps,
Tasks: ts,
Conditions: conditions,
}

// create fake recorder for testing
fr := record.NewFakeRecorder(2)

testAssets := getPipelineRunController(t, d, fr)
c := testAssets.Controller
clients := testAssets.Clients

err := c.Reconciler.Reconcile(context.Background(), "foo/test-pipeline-run-with-conditions")
if err != nil {
t.Errorf("Did not expect to see error when reconciling completed PipelineRun but saw %s", err)
}

// Check that the PipelineRun was reconciled correctly
_, err = clients.Pipeline.Tekton().PipelineRuns("foo").Get("test-pipeline-run-with-conditions", metav1.GetOptions{})
if err != nil {
t.Fatalf("Somehow had error getting completed reconciled run out of fake client: %s", err)
}

// Check that the expected TaskRun was created
actual := clients.Pipeline.Actions()[0].(ktesting.CreateAction).GetObject().(*v1alpha1.TaskRun)
if actual == nil {
t.Errorf("Expected a TaskRun to be created, but it wasn't.")
}
expectedTaskRun := tb.TaskRun("test-pipeline-run-with-conditions-hello-world-1-9l9zj-alw-mz4c7", "foo",
tb.TaskRunOwnerReference("PipelineRun", "test-pipeline-run-with-conditions",
tb.OwnerReferenceAPIVersion("tekton.dev/v1alpha1"),
tb.Controller, tb.BlockOwnerDeletion,
),
tb.TaskRunLabel("tekton.dev/pipeline", "test-pipeline"),
tb.TaskRunLabel(pipeline.GroupName+pipeline.PipelineTaskLabelKey, "hello-world-1"),
tb.TaskRunLabel("tekton.dev/pipelineRun", "test-pipeline-run-with-conditions"),
tb.TaskRunLabel("tekton.dev/pipelineConditionCheck", "test-pipeline-run-with-conditions-hello-world-1-9l9zj-alw-mz4c7"),
tb.TaskRunAnnotation("PipelineRunAnnotation", "PipelineRunValue"),
tb.TaskRunSpec(
tb.TaskRunTaskSpec(tb.Step("", "foo", tb.Args("bar"))),
tb.TaskRunServiceAccount("test-sa"),
),
)

func TestReconcileWithFailedConditionChecks(t *testing.T) {}
if d := cmp.Diff(actual, expectedTaskRun); d != "" {
t.Errorf("expected to see TaskRun %v created. Diff %s", expectedTaskRun, d)
}
}
11 changes: 11 additions & 0 deletions test/builder/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ func PipelineTaskOutputResource(name, resource string) PipelineTaskOp {
}
}

// PipelineTaskCondition adds a condition to the PipelineTask with the
// specified conditionRef
func PipelineTaskCondition(conditionRef string) PipelineTaskOp {
return func(pt *v1alpha1.PipelineTask) {
c := v1alpha1.TaskCondition{
ConditionRef: conditionRef,
}
pt.Conditions = append(pt.Conditions, c)
}
}

// PipelineRun creates a PipelineRun with default values.
// Any number of PipelineRun modifier can be passed to transform it.
func PipelineRun(name, namespace string, ops ...PipelineRunOp) *v1alpha1.PipelineRun {
Expand Down

0 comments on commit 41472de

Please sign in to comment.