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

Add test YAML helpers #3905

Merged
merged 1 commit into from
Apr 30, 2021
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
64 changes: 30 additions & 34 deletions test/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"context"
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativetest "knative.dev/pkg/test"
)
Expand All @@ -42,25 +40,24 @@ func TestTaskRunPipelineRunStatus(t *testing.T) {
defer tearDown(ctx, t, c, namespace)

t.Logf("Creating Task and TaskRun in namespace %s", namespace)
task := &v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{Name: "banana", Namespace: namespace},
Spec: v1beta1.TaskSpec{
Steps: []v1beta1.Step{{Container: corev1.Container{
Image: "busybox",
Command: []string{"ls", "-la"},
}}},
},
}
task := mustParseTask(t, `
metadata:
name: banana
spec:
steps:
- name: foo
image: busybox
command: ['ls', '-la']`)
if _, err := c.TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task: %s", err)
}
taskRun := &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{Name: "apple", Namespace: namespace},
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{Name: "banana"},
ServiceAccountName: "inexistent",
},
}
taskRun := mustParseTaskRun(t, `
metadata:
name: apple
spec:
taskRef:
name: banana
serviceAccountName: inexistent`)
if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create TaskRun: %s", err)
}
Expand All @@ -70,25 +67,24 @@ func TestTaskRunPipelineRunStatus(t *testing.T) {
t.Errorf("Error waiting for TaskRun to finish: %s", err)
}

pipeline := &v1beta1.Pipeline{
ObjectMeta: metav1.ObjectMeta{Name: "tomatoes", Namespace: namespace},
Spec: v1beta1.PipelineSpec{
Tasks: []v1beta1.PipelineTask{{
Name: "foo",
TaskRef: &v1beta1.TaskRef{Name: "banana"},
}},
},
}
pipelineRun := &v1beta1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{Name: "pear", Namespace: namespace},
Spec: v1beta1.PipelineRunSpec{
PipelineRef: &v1beta1.PipelineRef{Name: "tomatoes"},
ServiceAccountName: "inexistent",
},
}
pipeline := mustParsePipeline(t, `
metadata:
name: tomatoes
spec:
tasks:
- name: foo
taskRef:
name: banana`)
if _, err := c.PipelineClient.Create(ctx, pipeline, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", "tomatoes", err)
}
pipelineRun := mustParsePipelineRun(t, `
metadata:
name: pear
spec:
pipelineRef:
name: tomatoes
serviceAccountName: inexistent`)
if _, err := c.PipelineRunClient.Create(ctx, pipelineRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create PipelineRun `%s`: %s", "pear", err)
}
Expand Down
67 changes: 67 additions & 0 deletions test/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2021 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 test

import (
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/client/clientset/versioned/scheme"
"k8s.io/apimachinery/pkg/runtime"
)

func mustParseYAML(t *testing.T, yaml string, i runtime.Object) {
if _, _, err := scheme.Codecs.UniversalDeserializer().Decode([]byte(yaml), nil, i); err != nil {
t.Fatalf("mustParseYAML (%s): %v", yaml, err)
}
}

func mustParseTaskRun(t *testing.T, yaml string) *v1beta1.TaskRun {
var tr v1beta1.TaskRun
yaml = `apiVersion: tekton.dev/v1beta1
kind: TaskRun
` + yaml
mustParseYAML(t, yaml, &tr)
return &tr
}

func mustParseTask(t *testing.T, yaml string) *v1beta1.Task {
var task v1beta1.Task
yaml = `apiVersion: tekton.dev/v1beta1
kind: Task
` + yaml
mustParseYAML(t, yaml, &task)
return &task
}

func mustParsePipelineRun(t *testing.T, yaml string) *v1beta1.PipelineRun {
var pr v1beta1.PipelineRun
yaml = `apiVersion: tekton.dev/v1beta1
kind: PipelineRun
` + yaml
mustParseYAML(t, yaml, &pr)
return &pr
}

func mustParsePipeline(t *testing.T, yaml string) *v1beta1.Pipeline {
var pipeline v1beta1.Pipeline
yaml = `apiVersion: tekton.dev/v1beta1
kind: Pipeline
` + yaml
mustParseYAML(t, yaml, &pipeline)
return &pipeline
}