|
| 1 | +package reconciler |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys" |
| 8 | + pacv1alpha1 "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1" |
| 9 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params" |
| 10 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients" |
| 11 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" |
| 12 | + testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients" |
| 13 | + testconcurrency "github.com/openshift-pipelines/pipelines-as-code/pkg/test/concurrency" |
| 14 | + tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" |
| 15 | + "go.uber.org/zap" |
| 16 | + zapobserver "go.uber.org/zap/zaptest/observer" |
| 17 | + "gotest.tools/v3/assert" |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + rtesting "knative.dev/pkg/reconciler/testing" |
| 20 | +) |
| 21 | + |
| 22 | +func TestQueuePipelineRun(t *testing.T) { |
| 23 | + tests := []struct { |
| 24 | + name string |
| 25 | + wantErrString string |
| 26 | + wantLog string |
| 27 | + pipelineRun *tektonv1.PipelineRun |
| 28 | + testRepo *pacv1alpha1.Repository |
| 29 | + globalRepo *pacv1alpha1.Repository |
| 30 | + runningQueue []string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "no existing order annotation", |
| 34 | + pipelineRun: &tektonv1.PipelineRun{ |
| 35 | + ObjectMeta: metav1.ObjectMeta{ |
| 36 | + Annotations: map[string]string{}, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "no repo name annotation", |
| 42 | + pipelineRun: &tektonv1.PipelineRun{ |
| 43 | + ObjectMeta: metav1.ObjectMeta{ |
| 44 | + Annotations: map[string]string{ |
| 45 | + keys.ExecutionOrder: "repo/foo1", |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + wantErrString: fmt.Sprintf("no %s annotation found", keys.Repository), |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "empty repo name annotation", |
| 53 | + pipelineRun: &tektonv1.PipelineRun{ |
| 54 | + ObjectMeta: metav1.ObjectMeta{ |
| 55 | + Annotations: map[string]string{ |
| 56 | + keys.ExecutionOrder: "repo/foo1", |
| 57 | + keys.Repository: "", |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + wantErrString: fmt.Sprintf("annotation %s is empty", keys.Repository), |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "no repo found", |
| 65 | + pipelineRun: &tektonv1.PipelineRun{ |
| 66 | + ObjectMeta: metav1.ObjectMeta{ |
| 67 | + Annotations: map[string]string{ |
| 68 | + keys.ExecutionOrder: "repo/foo1", |
| 69 | + keys.Repository: "foo", |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "merging global repository settings", |
| 76 | + globalRepo: &pacv1alpha1.Repository{ |
| 77 | + ObjectMeta: metav1.ObjectMeta{ |
| 78 | + Name: "global", |
| 79 | + Namespace: "global", |
| 80 | + }, |
| 81 | + Spec: pacv1alpha1.RepositorySpec{ |
| 82 | + Settings: &pacv1alpha1.Settings{ |
| 83 | + PipelineRunProvenance: "somewhere", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + runningQueue: []string{}, |
| 88 | + pipelineRun: &tektonv1.PipelineRun{ |
| 89 | + ObjectMeta: metav1.ObjectMeta{ |
| 90 | + Name: "test", |
| 91 | + Namespace: "test", |
| 92 | + Annotations: map[string]string{ |
| 93 | + keys.ExecutionOrder: "repo/foo1", |
| 94 | + keys.Repository: "test", |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + testRepo: &pacv1alpha1.Repository{ |
| 99 | + ObjectMeta: metav1.ObjectMeta{ |
| 100 | + Name: "test", |
| 101 | + Namespace: "test", |
| 102 | + }, |
| 103 | + Spec: pacv1alpha1.RepositorySpec{ |
| 104 | + URL: randomURL, |
| 105 | + }, |
| 106 | + }, |
| 107 | + wantLog: "Merging global repository settings with local repository settings", |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "no new PR acquired", |
| 111 | + runningQueue: []string{}, |
| 112 | + pipelineRun: &tektonv1.PipelineRun{ |
| 113 | + ObjectMeta: metav1.ObjectMeta{ |
| 114 | + Name: "test", |
| 115 | + Namespace: "test", |
| 116 | + Annotations: map[string]string{ |
| 117 | + keys.ExecutionOrder: "repo/foo1", |
| 118 | + keys.Repository: "test", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + testRepo: &pacv1alpha1.Repository{ |
| 123 | + ObjectMeta: metav1.ObjectMeta{ |
| 124 | + Name: "test", |
| 125 | + Namespace: "test", |
| 126 | + }, |
| 127 | + Spec: pacv1alpha1.RepositorySpec{ |
| 128 | + URL: randomURL, |
| 129 | + }, |
| 130 | + }, |
| 131 | + wantLog: "no new PipelineRun acquired for repo test", |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "failed to get PR from the Q after many iterations", |
| 135 | + runningQueue: []string{"test/test2"}, |
| 136 | + pipelineRun: &tektonv1.PipelineRun{ |
| 137 | + ObjectMeta: metav1.ObjectMeta{ |
| 138 | + Name: "test", |
| 139 | + Namespace: "test", |
| 140 | + Annotations: map[string]string{ |
| 141 | + keys.ExecutionOrder: "repo/foo1", |
| 142 | + keys.Repository: "test", |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + testRepo: &pacv1alpha1.Repository{ |
| 147 | + ObjectMeta: metav1.ObjectMeta{ |
| 148 | + Name: "test", |
| 149 | + Namespace: "test", |
| 150 | + }, |
| 151 | + Spec: pacv1alpha1.RepositorySpec{ |
| 152 | + URL: randomURL, |
| 153 | + }, |
| 154 | + }, |
| 155 | + wantLog: "failed to get PR", |
| 156 | + wantErrString: "max iterations reached of", |
| 157 | + }, |
| 158 | + } |
| 159 | + for _, tt := range tests { |
| 160 | + t.Run(tt.name, func(t *testing.T) { |
| 161 | + observer, logcatch := zapobserver.New(zap.InfoLevel) |
| 162 | + fakelogger := zap.New(observer).Sugar() |
| 163 | + ctx, _ := rtesting.SetupFakeContext(t) |
| 164 | + repos := []*pacv1alpha1.Repository{} |
| 165 | + if tt.testRepo != nil { |
| 166 | + repos = append(repos, tt.testRepo) |
| 167 | + } |
| 168 | + if tt.globalRepo != nil { |
| 169 | + repos = append(repos, tt.globalRepo) |
| 170 | + } |
| 171 | + testData := testclient.Data{ |
| 172 | + Repositories: repos, |
| 173 | + } |
| 174 | + stdata, informers := testclient.SeedTestData(t, ctx, testData) |
| 175 | + r := &Reconciler{ |
| 176 | + qm: testconcurrency.TestQMI{ |
| 177 | + RunningQueue: tt.runningQueue, |
| 178 | + }, |
| 179 | + repoLister: informers.Repository.Lister(), |
| 180 | + run: ¶ms.Run{ |
| 181 | + Info: info.Info{ |
| 182 | + Kube: &info.KubeOpts{ |
| 183 | + Namespace: "global", |
| 184 | + }, |
| 185 | + Controller: &info.ControllerInfo{}, |
| 186 | + }, |
| 187 | + Clients: clients.Clients{ |
| 188 | + PipelineAsCode: stdata.PipelineAsCode, |
| 189 | + Tekton: stdata.Pipeline, |
| 190 | + Kube: stdata.Kube, |
| 191 | + Log: fakelogger, |
| 192 | + }, |
| 193 | + }, |
| 194 | + } |
| 195 | + if tt.globalRepo != nil { |
| 196 | + r.run.Info.Controller.GlobalRepository = tt.globalRepo.GetName() |
| 197 | + } |
| 198 | + err := r.queuePipelineRun(ctx, fakelogger, tt.pipelineRun) |
| 199 | + if tt.wantErrString != "" { |
| 200 | + assert.ErrorContains(t, err, tt.wantErrString) |
| 201 | + return |
| 202 | + } |
| 203 | + assert.NilError(t, err) |
| 204 | + |
| 205 | + if tt.wantLog != "" { |
| 206 | + assert.Assert(t, logcatch.FilterMessage(tt.wantLog).Len() != 0, "We didn't get the expected log message", logcatch.All()) |
| 207 | + } |
| 208 | + }) |
| 209 | + } |
| 210 | +} |
0 commit comments