Skip to content

Commit

Permalink
Convert test/cluster_resource_test.go's Tekton structs to YAML
Browse files Browse the repository at this point in the history
Part of tektoncd#4276

Signed-off-by: Andrew Bayer <[email protected]>
  • Loading branch information
abayer committed Oct 6, 2021
1 parent 0603d01 commit 7663f76
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 106 deletions.
184 changes: 78 additions & 106 deletions test/cluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ package test

import (
"context"
"fmt"
"testing"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
resourcev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
resources "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativetest "knative.dev/pkg/test"
Expand Down Expand Up @@ -58,17 +59,17 @@ func TestClusterResource(t *testing.T) {
}

t.Logf("Creating cluster PipelineResource %s", resourceName)
if _, err := c.PipelineResourceClient.Create(ctx, getClusterResource(resourceName, secretName), metav1.CreateOptions{}); err != nil {
if _, err := c.PipelineResourceClient.Create(ctx, getClusterResource(t, resourceName, secretName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create cluster Pipeline Resource `%s`: %s", resourceName, err)
}

t.Logf("Creating Task %s", taskName)
if _, err := c.TaskClient.Create(ctx, getClusterResourceTask(namespace, taskName, configName), metav1.CreateOptions{}); err != nil {
if _, err := c.TaskClient.Create(ctx, getClusterResourceTask(t, namespace, taskName, configName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", taskName, err)
}

t.Logf("Creating TaskRun %s", taskRunName)
if _, err := c.TaskRunClient.Create(ctx, getClusterResourceTaskRun(namespace, taskRunName, taskName, resourceName), metav1.CreateOptions{}); err != nil {
if _, err := c.TaskRunClient.Create(ctx, getClusterResourceTaskRun(t, namespace, taskRunName, taskName, resourceName), metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create Taskrun `%s`: %s", taskRunName, err)
}

Expand All @@ -78,45 +79,30 @@ func TestClusterResource(t *testing.T) {
}
}

func getClusterResource(name, sname string) *v1alpha1.PipelineResource {
return &v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.PipelineResourceSpec{
Type: v1alpha1.PipelineResourceTypeCluster,
Params: []v1alpha1.ResourceParam{
{
Name: "Name",
Value: "helloworld-cluster",
},
{
Name: "Url",
Value: "https://1.1.1.1",
},
{
Name: "username",
Value: "test-user",
},
{
Name: "password",
Value: "test-password",
},
},
SecretParams: []v1alpha1.SecretParam{
{
FieldName: "cadata",
SecretKey: "cadatakey",
SecretName: sname,
},
{
FieldName: "token",
SecretKey: "tokenkey",
SecretName: sname,
},
},
},
}
func getClusterResource(t *testing.T, name, sname string) *resourcev1alpha1.PipelineResource {
return mustParsePipelineResource(t, fmt.Sprintf(`
metadata:
name: %s
spec:
type: cluster
params:
- name: Name
value: helloworld-cluster
- name: Url
value: https://1.1.1.1
- name: username
value: test-user
- name: password
value: test-password
secrets:
- fieldName: cadata
secretKey: cadatakey
secretName: %s
- fieldName: token
secretKey: tokenkey
secretName: %s
`, name, sname, sname))

}

func getClusterResourceTaskSecret(namespace, name string) *corev1.Secret {
Expand All @@ -132,70 +118,56 @@ func getClusterResourceTaskSecret(namespace, name string) *corev1.Secret {
}
}

func getClusterResourceTask(namespace, name, configName string) *v1beta1.Task {
return &v1beta1.Task{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
Spec: v1beta1.TaskSpec{
Resources: &v1beta1.TaskResources{
Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{
Name: "target-cluster",
Type: resources.PipelineResourceTypeCluster,
}}},
},
Volumes: []corev1.Volume{{
Name: "config-vol",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: configName,
},
},
},
}},
Steps: []v1beta1.Step{{Container: corev1.Container{
Name: "check-file-existence",
Image: "ubuntu",
Command: []string{"cat"},
Args: []string{"$(resources.inputs.target-cluster.path)/kubeconfig"},
}}, {Container: corev1.Container{
Name: "check-config-data",
Image: "ubuntu",
Command: []string{"cat"},
Args: []string{"/config/test.data"},
VolumeMounts: []corev1.VolumeMount{{
Name: "config-vol",
MountPath: "/config",
}},
}}, {Container: corev1.Container{
Name: "check-contents",
Image: "ubuntu",
Command: []string{"bash"},
Args: []string{"-c", "cmp -b $(resources.inputs.target-cluster.path)/kubeconfig /config/test.data"},
VolumeMounts: []corev1.VolumeMount{{
Name: "config-vol",
MountPath: "/config",
}},
}},
},
},
}
func getClusterResourceTask(t *testing.T, namespace, name, configName string) *v1beta1.Task {
return mustParseTask(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
resources:
inputs:
- name: target-cluster
type: cluster
volumes:
- name: config-vol
configMap:
name: %s
steps:
- name: check-file-existence
image: ubuntu
command: ['cat']
args: ['$(resources.inputs.target-cluster.path)/kubeconfig']
- name: check-config-data
image: ubuntu
command: ['cat']
args: ['/config/test.data']
volumeMounts:
- name: config-vol
mountPath: /config
- name: check-contents
image: ubuntu
command: ['bash']
args: ['-c', 'cmp -b $(resources.inputs.target-cluster.path)/kubeconfig /config/test.data']
volumeMounts:
- name: config-vol
mountPath: /config
`, name, namespace, configName))
}

func getClusterResourceTaskRun(namespace, name, taskName, resName string) *v1beta1.TaskRun {
return &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
Spec: v1beta1.TaskRunSpec{
TaskRef: &v1beta1.TaskRef{Name: taskName},
Resources: &v1beta1.TaskRunResources{
Inputs: []v1beta1.TaskResourceBinding{{
PipelineResourceBinding: v1beta1.PipelineResourceBinding{
Name: "target-cluster",
ResourceRef: &v1beta1.PipelineResourceRef{Name: resName},
},
}},
},
},
}
func getClusterResourceTaskRun(t *testing.T, namespace, name, taskName, resName string) *v1beta1.TaskRun {
return mustParseTaskRun(t, fmt.Sprintf(`
metadata:
name: %s
namespace: %s
spec:
taskRef:
name: %s
resources:
inputs:
- name: target-cluster
resourceRef:
name: %s
`, name, namespace, taskName, resName))
}

func getClusterConfigMap(namespace, name string) *corev1.ConfigMap {
Expand Down
12 changes: 12 additions & 0 deletions test/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package test
import (
"testing"

resourcev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/client/clientset/versioned/scheme"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -65,3 +68,12 @@ kind: Pipeline
mustParseYAML(t, yaml, &pipeline)
return &pipeline
}

func mustParsePipelineResource(t *testing.T, yaml string) *resourcev1alpha1.PipelineResource {
var resource v1alpha1.PipelineResource
yaml = `apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
` + yaml
mustParseYAML(t, yaml, &resource)
return &resource
}

0 comments on commit 7663f76

Please sign in to comment.