-
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.
Add PipelineRun integration test and refactor unit tests
Added the beginnings of a skeleton of an integration test for PipelineRun for #61. In doing this I realized that I couldn't create PipelineParam objects with the clients b/c the plural name was wrong - it needs to be `pipelineparamses` like I'm Smeagol. I tried to work around this and stumbled on kubernetes/code-generator#53 so it doesn't seem to be possible without changing the code-generator code :( Meanwhile refactored the existing controller code to break some code out of the Reconciler, so it can be instantiated without an entire controller (instead depends only on the objects it needs). The decision about what TaskRuns to create has been separated from the logic to retrieve existing ones, and the logic to create them. (`GetTasks`, `getNextPipelineRunTaskRun`) The tests were refactored such that the success cases are in separate tests from the failure cases, so the table driven tests dont have to handle both and it's more clear what the tests are doing.
- Loading branch information
1 parent
62713cc
commit ecf71f6
Showing
14 changed files
with
631 additions
and
263 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
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,39 @@ | ||
/* | ||
Copyright 2018 The Knative 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 pipeline | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" | ||
listers "github.com/knative/build-pipeline/pkg/client/listers/pipeline/v1alpha1" | ||
) | ||
|
||
// GetTasks retrieves all Tasks instances which the pipeline p references, using | ||
// lister l. If it is unable to retrieve an instance of a references Task, it will return | ||
// an error, otherwise it returns a map from the name of the Task in the Pipeline to the | ||
// name of the Task object itself. | ||
func GetTasks(l listers.TaskLister, p *v1alpha1.Pipeline) (map[string]*v1alpha1.Task, error) { | ||
tasks := map[string]*v1alpha1.Task{} | ||
for _, pt := range p.Spec.Tasks { | ||
t, err := l.Tasks(p.Namespace).Get(pt.TaskRef.Name) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get tasks for Pipeline %q: Error getting task %q : %s", | ||
fmt.Sprintf("%s/%s", p.Namespace, p.Name), | ||
fmt.Sprintf("%s/%s", p.Namespace, pt.TaskRef.Name), err) | ||
} | ||
tasks[pt.Name] = t | ||
} | ||
return tasks, nil | ||
} |
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,79 @@ | ||
/* | ||
Copyright 2018 The Knative 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 pipeline | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" | ||
fakepipelineclientset "github.com/knative/build-pipeline/pkg/client/clientset/versioned/fake" | ||
informers "github.com/knative/build-pipeline/pkg/client/informers/externalversions" | ||
informersv1alpha1 "github.com/knative/build-pipeline/pkg/client/informers/externalversions/pipeline/v1alpha1" | ||
) | ||
|
||
func getFakeInformer() informersv1alpha1.TaskInformer { | ||
pipelineClient := fakepipelineclientset.NewSimpleClientset() | ||
sharedInfomer := informers.NewSharedInformerFactory(pipelineClient, 0) | ||
return sharedInfomer.Pipeline().V1alpha1().Tasks() | ||
} | ||
|
||
var p = &v1alpha1.Pipeline{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "namespace", | ||
Name: "pipeline", | ||
}, | ||
Spec: v1alpha1.PipelineSpec{ | ||
Tasks: []v1alpha1.PipelineTask{{ | ||
Name: "mytask1", | ||
TaskRef: v1alpha1.TaskRef{Name: "task"}, | ||
}, { | ||
Name: "mytask2", | ||
TaskRef: v1alpha1.TaskRef{Name: "task"}, | ||
}}, | ||
}, | ||
} | ||
|
||
func TestGetTasks(t *testing.T) { | ||
task := &v1alpha1.Task{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "namespace", | ||
Name: "task", | ||
}, | ||
Spec: v1alpha1.TaskSpec{}, | ||
} | ||
i := getFakeInformer() | ||
i.Informer().GetIndexer().Add(task) | ||
|
||
tasks, err := GetTasks(i.Lister(), p) | ||
if err != nil { | ||
t.Fatalf("Error getting tasks for fake pipeline %s: %s", p.ObjectMeta.Name, err) | ||
} | ||
expectedTasks := map[string]*v1alpha1.Task{ | ||
"mytask1": task, | ||
"mytask2": task, | ||
} | ||
if !reflect.DeepEqual(tasks, expectedTasks) { | ||
t.Fatalf("Expected to get map of tasks %v but got %v instead", expectedTasks, tasks) | ||
} | ||
} | ||
|
||
func TestGetTasksDoesntExist(t *testing.T) { | ||
i := getFakeInformer() | ||
_, err := GetTasks(i.Lister(), p) | ||
if err == nil { | ||
t.Fatalf("Expected error getting non-existent Tasks for Pipeline %s but got none", p.ObjectMeta.Name) | ||
} | ||
} |
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
Oops, something went wrong.