|
4 | 4 | package controller |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
| 8 | + "errors" |
7 | 9 | "fmt" |
| 10 | + "testing" |
8 | 11 | "time" |
9 | 12 |
|
| 13 | + "github.com/go-logr/logr" |
10 | 14 | tfc "github.com/hashicorp/go-tfe" |
| 15 | + "github.com/hashicorp/go-tfe/mocks" |
11 | 16 | appv1alpha2 "github.com/hashicorp/hcp-terraform-operator/api/v1alpha2" |
12 | 17 | "github.com/hashicorp/hcp-terraform-operator/internal/pointer" |
13 | 18 | . "github.com/onsi/ginkgo/v2" |
14 | 19 | . "github.com/onsi/gomega" |
| 20 | + gomock "go.uber.org/mock/gomock" |
15 | 21 | corev1 "k8s.io/api/core/v1" |
16 | | - "k8s.io/apimachinery/pkg/api/errors" |
| 22 | + k8sapierrors "k8s.io/apimachinery/pkg/api/errors" |
17 | 23 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
18 | 26 | ) |
19 | 27 |
|
20 | 28 | var _ = Describe("Agent Pool controller", Ordered, func() { |
@@ -77,7 +85,7 @@ var _ = Describe("Agent Pool controller", Ordered, func() { |
77 | 85 | Expect(k8sClient.Delete(ctx, instance)).To(Succeed()) |
78 | 86 | Eventually(func() bool { |
79 | 87 | err := k8sClient.Get(ctx, namespacedName, instance) |
80 | | - return errors.IsNotFound(err) |
| 88 | + return k8sapierrors.IsNotFound(err) |
81 | 89 | }).Should(BeTrue()) |
82 | 90 | }) |
83 | 91 |
|
@@ -194,3 +202,146 @@ var _ = Describe("Agent Pool controller", Ordered, func() { |
194 | 202 | }) |
195 | 203 | }) |
196 | 204 | }) |
| 205 | + |
| 206 | +func TestPendingWorkspaceRuns(t *testing.T) { |
| 207 | + tests := []struct { |
| 208 | + name string |
| 209 | + mockRuns []*tfc.Run |
| 210 | + mockErr error |
| 211 | + expectedCount int32 |
| 212 | + expectError bool |
| 213 | + }{ |
| 214 | + { |
| 215 | + name: "returns error from client", |
| 216 | + mockErr: errors.New("api error"), |
| 217 | + expectedCount: 0, |
| 218 | + expectError: true, |
| 219 | + }, |
| 220 | + { |
| 221 | + name: "counts plan-only runs", |
| 222 | + mockRuns: []*tfc.Run{ |
| 223 | + {ID: "run1", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 224 | + {ID: "run2", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws2"}}, |
| 225 | + }, |
| 226 | + expectedCount: 2, |
| 227 | + expectError: false, |
| 228 | + }, |
| 229 | + { |
| 230 | + name: "skips user interaction runs", |
| 231 | + mockRuns: []*tfc.Run{ |
| 232 | + {ID: "run1", PlanOnly: false, Status: tfc.RunPlanned, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 233 | + {ID: "run2", PlanOnly: false, Status: tfc.RunPolicyOverride, Workspace: &tfc.Workspace{ID: "ws2"}}, |
| 234 | + }, |
| 235 | + expectedCount: 0, |
| 236 | + expectError: false, |
| 237 | + }, |
| 238 | + { |
| 239 | + name: "counts normal pending runs", |
| 240 | + mockRuns: []*tfc.Run{ |
| 241 | + {ID: "run1", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 242 | + {ID: "run2", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws2"}}, |
| 243 | + }, |
| 244 | + expectedCount: 2, |
| 245 | + expectError: false, |
| 246 | + }, |
| 247 | + { |
| 248 | + name: "mix of plan-only and normal runs", |
| 249 | + mockRuns: []*tfc.Run{ |
| 250 | + {ID: "run1", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 251 | + {ID: "run2", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws2"}}, |
| 252 | + }, |
| 253 | + expectedCount: 2, |
| 254 | + expectError: false, |
| 255 | + }, |
| 256 | + { |
| 257 | + name: "plan-only runs for single workspace", |
| 258 | + mockRuns: []*tfc.Run{ |
| 259 | + {ID: "run1", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 260 | + {ID: "run2", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 261 | + {ID: "run3", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 262 | + {ID: "run4", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 263 | + }, |
| 264 | + expectedCount: 4, |
| 265 | + expectError: false, |
| 266 | + }, |
| 267 | + { |
| 268 | + name: "single apply and multiple plan-only runs for single workspace", |
| 269 | + mockRuns: []*tfc.Run{ |
| 270 | + {ID: "run1", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 271 | + {ID: "run2", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 272 | + {ID: "run3", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 273 | + {ID: "run4", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 274 | + {ID: "run5", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 275 | + }, |
| 276 | + expectedCount: 5, |
| 277 | + expectError: false, |
| 278 | + }, |
| 279 | + { |
| 280 | + name: "mix of plan-only and apply runs for single workspace", |
| 281 | + mockRuns: []*tfc.Run{ |
| 282 | + {ID: "run1", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 283 | + {ID: "run2", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 284 | + {ID: "run3", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 285 | + {ID: "run4", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 286 | + {ID: "run5", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 287 | + }, |
| 288 | + expectedCount: 4, |
| 289 | + expectError: false, |
| 290 | + }, |
| 291 | + { |
| 292 | + name: "mix of plan-only and apply runs for multiple workspaces", |
| 293 | + mockRuns: []*tfc.Run{ |
| 294 | + {ID: "run1", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 295 | + {ID: "run2", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws2"}}, |
| 296 | + {ID: "run3", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws3"}}, |
| 297 | + {ID: "run4", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 298 | + {ID: "run5", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 299 | + }, |
| 300 | + expectedCount: 5, |
| 301 | + expectError: false, |
| 302 | + }, |
| 303 | + { |
| 304 | + name: "mix of plan-only and apply runs for two workspaces", |
| 305 | + mockRuns: []*tfc.Run{ |
| 306 | + {ID: "run1", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws1"}}, |
| 307 | + {ID: "run2", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws2"}}, |
| 308 | + {ID: "run3", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws2"}}, |
| 309 | + {ID: "run4", PlanOnly: true, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws2"}}, |
| 310 | + {ID: "run5", PlanOnly: false, Status: tfc.RunPlanning, Workspace: &tfc.Workspace{ID: "ws2"}}, |
| 311 | + }, |
| 312 | + expectedCount: 4, |
| 313 | + expectError: false, |
| 314 | + }, |
| 315 | + } |
| 316 | + |
| 317 | + for _, tt := range tests { |
| 318 | + t.Run(tt.name, func(t *testing.T) { |
| 319 | + ctrl := gomock.NewController(t) |
| 320 | + defer ctrl.Finish() |
| 321 | + |
| 322 | + mockRuns := mocks.NewMockRuns(ctrl) |
| 323 | + mockRuns.EXPECT(). |
| 324 | + ListForOrganization(gomock.Any(), "test-org", gomock.Any()). |
| 325 | + Return(&tfc.OrganizationRunList{Items: tt.mockRuns, PaginationNextPrev: &tfc.PaginationNextPrev{NextPage: 0}}, tt.mockErr) |
| 326 | + |
| 327 | + ap := &agentPoolInstance{ |
| 328 | + tfClient: HCPTerraformClient{Client: &tfc.Client{Runs: mockRuns}}, |
| 329 | + instance: appv1alpha2.AgentPool{ |
| 330 | + Spec: appv1alpha2.AgentPoolSpec{ |
| 331 | + Name: "test-pool", |
| 332 | + Organization: "test-org", |
| 333 | + }, |
| 334 | + }, |
| 335 | + log: logr.Logger{}, |
| 336 | + } |
| 337 | + |
| 338 | + count, err := pendingWorkspaceRuns(context.Background(), ap) |
| 339 | + if tt.expectError { |
| 340 | + assert.Error(t, err) |
| 341 | + } else { |
| 342 | + assert.NoError(t, err) |
| 343 | + assert.Equal(t, tt.expectedCount, count) |
| 344 | + } |
| 345 | + }) |
| 346 | + } |
| 347 | +} |
0 commit comments