|
| 1 | +package pipelineascode |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1" |
| 9 | + pacerrors "github.com/openshift-pipelines/pipelines-as-code/pkg/errors" |
| 10 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/events" |
| 11 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" |
| 12 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/provider" |
| 13 | + testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients" |
| 14 | + testprovider "github.com/openshift-pipelines/pipelines-as-code/pkg/test/provider" |
| 15 | + "go.uber.org/zap" |
| 16 | + zapobserver "go.uber.org/zap/zaptest/observer" |
| 17 | + "gotest.tools/v3/assert" |
| 18 | + rtesting "knative.dev/pkg/reconciler/testing" |
| 19 | +) |
| 20 | + |
| 21 | +func TestCheckAccessOrErrror(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + allowIt bool |
| 25 | + sender string |
| 26 | + accountID string |
| 27 | + createStatusError bool |
| 28 | + expectedErr bool |
| 29 | + expectedAllowed bool |
| 30 | + expectedErrMsg string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "user is allowed", |
| 34 | + allowIt: true, |
| 35 | + expectedAllowed: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "user is not allowed - no account ID", |
| 39 | + allowIt: false, |
| 40 | + sender: "johndoe", |
| 41 | + expectedAllowed: false, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "user is not allowed - with account ID", |
| 45 | + allowIt: false, |
| 46 | + sender: "johndoe", |
| 47 | + accountID: "user123", |
| 48 | + expectedAllowed: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "create status error", |
| 52 | + allowIt: false, |
| 53 | + sender: "johndoe", |
| 54 | + createStatusError: true, |
| 55 | + expectedErr: true, |
| 56 | + expectedErrMsg: "failed to run create status", |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + // Setup observer to capture logs |
| 63 | + observerCore, _ := zapobserver.New(zap.InfoLevel) |
| 64 | + logger := zap.New(observerCore).Sugar() |
| 65 | + |
| 66 | + // Create test event |
| 67 | + testEvent := &info.Event{ |
| 68 | + Sender: tt.sender, |
| 69 | + AccountID: tt.accountID, |
| 70 | + } |
| 71 | + |
| 72 | + // Create mock provider |
| 73 | + prov := &testprovider.TestProviderImp{ |
| 74 | + AllowIT: tt.allowIt, |
| 75 | + } |
| 76 | + |
| 77 | + // Set createStatus error if needed |
| 78 | + if tt.createStatusError { |
| 79 | + prov.CreateStatusErorring = true |
| 80 | + } |
| 81 | + |
| 82 | + ctx, _ := rtesting.SetupFakeContext(t) |
| 83 | + stdata, _ := testclient.SeedTestData(t, ctx, testclient.Data{}) |
| 84 | + // Create mock event emitter |
| 85 | + eventEmitter := events.NewEventEmitter(stdata.Kube, logger) |
| 86 | + |
| 87 | + // Create PacRun |
| 88 | + p := &PacRun{ |
| 89 | + event: testEvent, |
| 90 | + vcx: prov, |
| 91 | + logger: logger, |
| 92 | + eventEmitter: eventEmitter, |
| 93 | + } |
| 94 | + |
| 95 | + // Call the function |
| 96 | + repo := &v1alpha1.Repository{} |
| 97 | + status := provider.StatusOpts{} |
| 98 | + allowed, err := p.checkAccessOrErrror(context.Background(), repo, status, "via test") |
| 99 | + |
| 100 | + // Verify results |
| 101 | + if tt.expectedErr { |
| 102 | + assert.Assert(t, err != nil, "Expected error but got nil") |
| 103 | + if tt.expectedErrMsg != "" { |
| 104 | + assert.Assert(t, err.Error() != "", "Expected error message but got empty string") |
| 105 | + assert.ErrorContains(t, err, tt.expectedErrMsg) |
| 106 | + } |
| 107 | + } else { |
| 108 | + assert.NilError(t, err) |
| 109 | + } |
| 110 | + |
| 111 | + assert.Equal(t, tt.expectedAllowed, allowed) |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestReportValidationErrors(t *testing.T) { |
| 117 | + tests := []struct { |
| 118 | + name string |
| 119 | + validationErrors []*pacerrors.PacYamlValidations |
| 120 | + expectCommentCreation bool |
| 121 | + }{ |
| 122 | + { |
| 123 | + name: "no validation errors", |
| 124 | + validationErrors: []*pacerrors.PacYamlValidations{}, |
| 125 | + expectCommentCreation: false, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "tekton validation errors", |
| 129 | + validationErrors: []*pacerrors.PacYamlValidations{ |
| 130 | + { |
| 131 | + Name: "test-pipeline-1", |
| 132 | + Err: errors.New("invalid pipeline spec"), |
| 133 | + Schema: "tekton.dev", |
| 134 | + }, |
| 135 | + }, |
| 136 | + expectCommentCreation: true, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "non-tekton schema errors", |
| 140 | + validationErrors: []*pacerrors.PacYamlValidations{ |
| 141 | + { |
| 142 | + Name: "test-other", |
| 143 | + Err: errors.New("some error"), |
| 144 | + Schema: "other.schema", |
| 145 | + }, |
| 146 | + }, |
| 147 | + expectCommentCreation: false, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "ignored errors by regex", |
| 151 | + validationErrors: []*pacerrors.PacYamlValidations{ |
| 152 | + { |
| 153 | + Name: "test-ignored", |
| 154 | + Err: errors.New("no kind test is registered for version v1 in scheme"), |
| 155 | + Schema: "tekton.dev", |
| 156 | + }, |
| 157 | + }, |
| 158 | + expectCommentCreation: false, |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "create comment error", |
| 162 | + validationErrors: []*pacerrors.PacYamlValidations{ |
| 163 | + { |
| 164 | + Name: "test-pipeline", |
| 165 | + Err: errors.New("validation error"), |
| 166 | + Schema: "tekton.dev", |
| 167 | + }, |
| 168 | + }, |
| 169 | + expectCommentCreation: true, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + for _, tt := range tests { |
| 174 | + t.Run(tt.name, func(t *testing.T) { |
| 175 | + // Setup observer to capture logs |
| 176 | + observerCore, logs := zapobserver.New(zap.InfoLevel) |
| 177 | + logger := zap.New(observerCore).Sugar() |
| 178 | + ctx, _ := rtesting.SetupFakeContext(t) |
| 179 | + |
| 180 | + // Create test event |
| 181 | + testEvent := &info.Event{} |
| 182 | + |
| 183 | + // Create mock provider |
| 184 | + prov := &testprovider.TestProviderImp{} |
| 185 | + |
| 186 | + stdata, _ := testclient.SeedTestData(t, ctx, testclient.Data{}) |
| 187 | + |
| 188 | + // Create mock event emitter |
| 189 | + eventEmitter := events.NewEventEmitter(stdata.Kube, logger) |
| 190 | + // Create PacRun |
| 191 | + p := &PacRun{ |
| 192 | + event: testEvent, |
| 193 | + vcx: prov, |
| 194 | + logger: logger, |
| 195 | + eventEmitter: eventEmitter, |
| 196 | + } |
| 197 | + |
| 198 | + // Call the function |
| 199 | + repo := &v1alpha1.Repository{} |
| 200 | + p.reportValidationErrors(context.Background(), repo, tt.validationErrors) |
| 201 | + |
| 202 | + // Verify results |
| 203 | + // Verify log messages for validation errors |
| 204 | + logEntries := logs.All() |
| 205 | + errorLogCount := 0 |
| 206 | + for _, entry := range logEntries { |
| 207 | + if entry.Level == zap.ErrorLevel { |
| 208 | + errorLogCount++ |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + // We should have at least one error log per validation error |
| 213 | + assert.Assert(t, errorLogCount >= len(tt.validationErrors), |
| 214 | + "Expected at least %d error logs, got %d", len(tt.validationErrors), errorLogCount) |
| 215 | + }) |
| 216 | + } |
| 217 | +} |
0 commit comments