Skip to content

Commit

Permalink
Update examples and tests for DAG
Browse files Browse the repository at this point in the history
- Updated example PipelineRuns so that it uses `runAfter` (to make
  unit tests run first) in addition to using `from`
- Updated pipelinerun reconcile tests to also use `runAfter`
- Updated Helm end to end test to actually use the image it builds
  • Loading branch information
bobcatfish committed Feb 27, 2019
1 parent 3fe8938 commit 2f5fde4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
31 changes: 31 additions & 0 deletions examples/pipelineruns/pipelinerun.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ spec:
---
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: unit-tests
spec:
inputs:
resources:
- name: workspace
type: git
targetPath: go/src/github.com/GoogleContainerTools/skaffold
steps:
- name: run-tests
image: golang
env:
- name: GOPATH
value: /workspace/go
workingDir: /workspace/go/src/github.com/GoogleContainerTools/skaffold
command:
- make
args:
- test
---
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: build-push
spec:
Expand Down Expand Up @@ -140,7 +162,15 @@ spec:
- name: app-image
type: image
tasks:
- name: skaffold-unit-tests
taskRef:
name: unit-tests
resources:
inputs:
- name: workspace
resource: source-repo
- name: build-skaffold-web
runAfter: [skaffold-unit-tests]
taskRef:
name: build-push
params:
Expand All @@ -156,6 +186,7 @@ spec:
- name: builtImage
resource: web-image
- name: build-skaffold-app
runAfter: [skaffold-unit-tests]
taskRef:
name: build-push
params:
Expand Down
23 changes: 17 additions & 6 deletions pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,26 @@ func TestReconcile(t *testing.T) {
tb.PipelineDeclaredResource("best-image", "image"),
tb.PipelineParam("pipeline-param", tb.PipelineParamDefault("somethingdifferent")),
tb.PipelineParam("rev-param", tb.PipelineParamDefault("revision")),
// unit-test-3 uses runAfter to indicate it should run last
tb.PipelineTask("unit-test-3", "unit-test-task",
funParam, moreFunParam, templatedParam,
tb.RunAfter("unit-test-2"),
tb.PipelineTaskInputResource("workspace", "git-repo"),
tb.PipelineTaskOutputResource("image-to-use", "best-image"),
tb.PipelineTaskOutputResource("workspace", "git-repo"),
),
// unit-test-1 can run right away because it has no dependencies
tb.PipelineTask("unit-test-1", "unit-test-task",
funParam, moreFunParam, templatedParam,
tb.PipelineTaskInputResource("workspace", "git-repo"),
tb.PipelineTaskOutputResource("image-to-use", "best-image"),
tb.PipelineTaskOutputResource("workspace", "git-repo"),
),
// unit-test-2 uses `from` to indicate it should run after `unit-test-1`
tb.PipelineTask("unit-test-2", "unit-test-followup-task",
tb.PipelineTaskInputResource("workspace", "git-repo", tb.From("unit-test-1")),
),
// unit-test-cluster-task can run right away because it has no dependencies
tb.PipelineTask("unit-test-cluster-task", "unit-test-cluster-task",
tb.PipelineTaskRefKind(v1alpha1.ClusterTaskKind),
funParam, moreFunParam, templatedParam,
Expand Down Expand Up @@ -187,7 +198,7 @@ func TestReconcile(t *testing.T) {

// Check that the expected TaskRun was created
actual := clients.Pipeline.Actions()[0].(ktesting.CreateAction).GetObject()
expectedTaskRun := tb.TaskRun("test-pipeline-run-success-unit-test-1-9l9zj", "foo",
expectedTaskRun := tb.TaskRun("test-pipeline-run-success-unit-test-1-mz4c7", "foo",
tb.TaskRunOwnerReference("PipelineRun", "test-pipeline-run-success",
tb.OwnerReferenceAPIVersion("tekton.dev/v1alpha1"),
tb.Controller, tb.BlockOwnerDeletion,
Expand Down Expand Up @@ -233,13 +244,13 @@ func TestReconcile(t *testing.T) {
}

if len(reconciledRun.Status.TaskRuns) != 2 {
t.Errorf("Expected PipelineRun status to include only one TaskRun status item: %v", reconciledRun.Status.TaskRuns)
t.Errorf("Expected PipelineRun status to include both TaskRun status items that can run immediately: %v", reconciledRun.Status.TaskRuns)
}
if _, exists := reconciledRun.Status.TaskRuns["test-pipeline-run-success-unit-test-1-9l9zj"]; exists == false {
t.Error("Expected PipelineRun status to include TaskRun status")
if _, exists := reconciledRun.Status.TaskRuns["test-pipeline-run-success-unit-test-1-mz4c7"]; exists == false {
t.Errorf("Expected PipelineRun status to include TaskRun status but was %v", reconciledRun.Status.TaskRuns)
}
if _, exists := reconciledRun.Status.TaskRuns["test-pipeline-run-success-unit-test-cluster-task-mssqb"]; exists == false {
t.Error("Expected PipelineRun status to include TaskRun status")
if _, exists := reconciledRun.Status.TaskRuns["test-pipeline-run-success-unit-test-cluster-task-78c5n"]; exists == false {
t.Errorf("Expected PipelineRun status to include TaskRun status but was %v", reconciledRun.Status.TaskRuns)
}
}

Expand Down
32 changes: 23 additions & 9 deletions test/helm_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const (
)

var (
imageName string
clusterRoleBindings [3]*rbacv1.ClusterRoleBinding
tillerServiceAccount *corev1.ServiceAccount
)
Expand All @@ -66,6 +65,11 @@ func TestHelmDeployPipelineRun(t *testing.T) {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", sourceResourceName, err)
}

logger.Infof("Creating Image PipelineResource %s", sourceImageName)
if _, err := c.PipelineResourceClient.Create(getHelmImageResource(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", sourceImageName, err)
}

logger.Infof("Creating Task %s", createImageTaskName)
if _, err := c.TaskClient.Create(getCreateImageTask(namespace, t, logger)); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", createImageTaskName, err)
Expand Down Expand Up @@ -144,28 +148,34 @@ func TestHelmDeployPipelineRun(t *testing.T) {
func getGoHelloworldGitResource(namespace string) *v1alpha1.PipelineResource {
return tb.PipelineResource(sourceResourceName, namespace, tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeGit,
tb.PipelineResourceSpecParam("Url", "https://github.com/knative/build-pipeline"),
tb.PipelineResourceSpecParam("url", "https://github.com/knative/build-pipeline"),
))
}

func getCreateImageTask(namespace string, t *testing.T, logger *logging.BaseLogger) *v1alpha1.Task {
func getHelmImageResource(namespace string) *v1alpha1.PipelineResource {
// according to knative/test-infra readme (https://github.com/knative/test-infra/blob/13055d769cc5e1756e605fcb3bcc1c25376699f1/scripts/README.md)
// the KO_DOCKER_REPO will be set with according to the project where the cluster is created
// it is used here to dynamically get the docker registry to push the image to
dockerRepo := os.Getenv("KO_DOCKER_REPO")
if dockerRepo == "" {
t.Fatalf("KO_DOCKER_REPO env variable is required")
}
imageName := fmt.Sprintf("%s/%s", dockerRepo, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(sourceImageName))

imageName = fmt.Sprintf("%s/%s", dockerRepo, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(sourceImageName))
logger.Infof("Image to be pusblished: %s", imageName)
return tb.PipelineResource(sourceImageName, namespace, tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeImage,
tb.PipelineResourceSpecParam("url", imageName),
))
}

func getCreateImageTask(namespace string, t *testing.T, logger *logging.BaseLogger) *v1alpha1.Task {
return tb.Task(createImageTaskName, namespace, tb.TaskSpec(
tb.TaskInputs(tb.InputsResource("gitsource", v1alpha1.PipelineResourceTypeGit)),
tb.TaskOutputs(tb.OutputsResource("builtimage", v1alpha1.PipelineResourceTypeImage)),
tb.Step("kaniko", "gcr.io/kaniko-project/executor", tb.Args(
"--dockerfile=/workspace/gitsource/test/gohelloworld/Dockerfile",
"--context=/workspace/gitsource/",
fmt.Sprintf("--destination=%s", imageName),
"--destination=${outputs.resources.builtimage.url}",
)),
))
}
Expand All @@ -174,8 +184,9 @@ func getHelmDeployTask(namespace string) *v1alpha1.Task {
return tb.Task(helmDeployTaskName, namespace, tb.TaskSpec(
tb.TaskInputs(
tb.InputsResource("gitsource", v1alpha1.PipelineResourceTypeGit),
tb.InputsResource("image", v1alpha1.PipelineResourceTypeImage),
tb.InputsParam("pathToHelmCharts", tb.ParamDescription("Path to the helm charts")),
tb.InputsParam("image"), tb.InputsParam("chartname", tb.ParamDefault("")),
tb.InputsParam("chartname", tb.ParamDefault("")),
),
tb.Step("helm-init", "alpine/helm", tb.Args("init", "--wait")),
tb.Step("helm-deploy", "alpine/helm", tb.Args(
Expand All @@ -184,23 +195,25 @@ func getHelmDeployTask(namespace string) *v1alpha1.Task {
"--name=${inputs.params.chartname}",
"${inputs.params.pathToHelmCharts}",
"--set",
"image.repository=${inputs.params.image}",
"image.repository=${inputs.resources.image.url}",
)),
))
}

func getHelmDeployPipeline(namespace string) *v1alpha1.Pipeline {
return tb.Pipeline(helmDeployPipelineName, namespace, tb.PipelineSpec(
tb.PipelineDeclaredResource("git-repo", "git"),
tb.PipelineDeclaredResource("the-image", "image"),
tb.PipelineParam("chartname"),
tb.PipelineTask("push-image", createImageTaskName,
tb.PipelineTaskInputResource("gitsource", "git-repo"),
tb.PipelineTaskOutputResource("builtimage", "the-image"),
),
tb.PipelineTask("helm-deploy", helmDeployTaskName,
tb.PipelineTaskInputResource("gitsource", "git-repo"),
tb.PipelineTaskInputResource("image", "the-image", tb.From("push-image")),
tb.PipelineTaskParam("pathToHelmCharts", "/workspace/gitsource/test/gohelloworld/gohelloworld-chart"),
tb.PipelineTaskParam("chartname", "${params.chartname}"),
tb.PipelineTaskParam("image", imageName),
),
))
}
Expand All @@ -210,6 +223,7 @@ func getHelmDeployPipelineRun(namespace string) *v1alpha1.PipelineRun {
helmDeployPipelineName,
tb.PipelineRunParam("chartname", "gohelloworld"),
tb.PipelineRunResourceBinding("git-repo", tb.PipelineResourceBindingRef(sourceResourceName)),
tb.PipelineRunResourceBinding("the-image", tb.PipelineResourceBindingRef(sourceImageName)),
))
}

Expand Down

0 comments on commit 2f5fde4

Please sign in to comment.