|
| 1 | +/* |
| 2 | +Copyright 2022 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | + |
| 26 | + "github.com/fluxcd/pkg/apis/meta" |
| 27 | + "github.com/fluxcd/pkg/runtime/conditions" |
| 28 | + "github.com/fluxcd/pkg/runtime/patch" |
| 29 | + |
| 30 | + "github.com/fluxcd/source-controller/internal/object" |
| 31 | +) |
| 32 | + |
| 33 | +// waitForSourceDeletion is a generic test helper to wait for object deletion of |
| 34 | +// any source kind. |
| 35 | +func waitForSourceDeletion(ctx context.Context, g *WithT, obj conditions.Setter) { |
| 36 | + g.THelper() |
| 37 | + |
| 38 | + key := client.ObjectKeyFromObject(obj) |
| 39 | + g.Eventually(func() bool { |
| 40 | + if err := testEnv.Get(ctx, key, obj); err != nil { |
| 41 | + return apierrors.IsNotFound(err) |
| 42 | + } |
| 43 | + return false |
| 44 | + }, timeout).Should(BeTrue()) |
| 45 | +} |
| 46 | + |
| 47 | +// waitForSuspended is a generic test helper to wait for object to be suspended |
| 48 | +// of any source kind. |
| 49 | +func waitForSuspended(ctx context.Context, g *WithT, obj conditions.Setter) { |
| 50 | + g.THelper() |
| 51 | + |
| 52 | + key := client.ObjectKeyFromObject(obj) |
| 53 | + g.Eventually(func() bool { |
| 54 | + if err := testEnv.Get(ctx, key, obj); err != nil { |
| 55 | + return false |
| 56 | + } |
| 57 | + suspended, err := object.GetSuspend(obj) |
| 58 | + if err != nil { |
| 59 | + return false |
| 60 | + } |
| 61 | + return suspended == true |
| 62 | + }, timeout).Should(BeTrue()) |
| 63 | +} |
| 64 | + |
| 65 | +// waitForSourceReadyWithArtifact is a generic test helper to wait for an object |
| 66 | +// to be ready of any source kind that have artifact in status when ready. |
| 67 | +func waitForSourceReadyWithArtifact(ctx context.Context, g *WithT, obj conditions.Setter) { |
| 68 | + g.THelper() |
| 69 | + waitForSourceReady(ctx, g, obj, true) |
| 70 | +} |
| 71 | + |
| 72 | +// waitForSourceReadyWithoutArtifact is a generic test helper to wait for an object |
| 73 | +// to be ready of any source kind that don't have artifact in status when ready. |
| 74 | +func waitForSourceReadyWithoutArtifact(ctx context.Context, g *WithT, obj conditions.Setter) { |
| 75 | + g.THelper() |
| 76 | + waitForSourceReady(ctx, g, obj, false) |
| 77 | +} |
| 78 | + |
| 79 | +// waitForSourceReady is a generic test helper to wait for an object to be |
| 80 | +// ready of any source kind. |
| 81 | +func waitForSourceReady(ctx context.Context, g *WithT, obj conditions.Setter, withArtifact bool) { |
| 82 | + g.THelper() |
| 83 | + |
| 84 | + key := client.ObjectKeyFromObject(obj) |
| 85 | + g.Eventually(func() bool { |
| 86 | + if err := testEnv.Get(ctx, key, obj); err != nil { |
| 87 | + return false |
| 88 | + } |
| 89 | + if withArtifact { |
| 90 | + artifact, err := object.GetArtifact(obj) |
| 91 | + if err != nil { |
| 92 | + return false |
| 93 | + } |
| 94 | + if artifact == nil { |
| 95 | + return false |
| 96 | + } |
| 97 | + } |
| 98 | + if !conditions.IsReady(obj) { |
| 99 | + return false |
| 100 | + } |
| 101 | + readyCondition := conditions.Get(obj, meta.ReadyCondition) |
| 102 | + statusObservedGen, err := object.GetStatusObservedGeneration(obj) |
| 103 | + if err != nil { |
| 104 | + return false |
| 105 | + } |
| 106 | + return obj.GetGeneration() == readyCondition.ObservedGeneration && |
| 107 | + obj.GetGeneration() == statusObservedGen |
| 108 | + }, timeout).Should(BeTrue()) |
| 109 | +} |
| 110 | + |
| 111 | +// testSuspendedObjectDeleteWithArtifact is a generic test helper to test if a |
| 112 | +// suspended object can be deleted for objects that have artifact in status when |
| 113 | +// ready. |
| 114 | +func testSuspendedObjectDeleteWithArtifact(ctx context.Context, g *WithT, obj conditions.Setter) { |
| 115 | + g.THelper() |
| 116 | + testSuspendedObjectDelete(ctx, g, obj, true) |
| 117 | +} |
| 118 | + |
| 119 | +// testSuspendedObjectDeleteWithoutArtifact is a generic test helper to test if |
| 120 | +// a suspended object can be deleted for objects that don't have artifact in |
| 121 | +// status when ready. |
| 122 | +func testSuspendedObjectDeleteWithoutArtifact(ctx context.Context, g *WithT, obj conditions.Setter) { |
| 123 | + g.THelper() |
| 124 | + testSuspendedObjectDelete(ctx, g, obj, false) |
| 125 | +} |
| 126 | + |
| 127 | +// testSuspendedObjectDelete is a generic test helper to test if a suspended |
| 128 | +// object can be deleted. |
| 129 | +func testSuspendedObjectDelete(ctx context.Context, g *WithT, obj conditions.Setter, withArtifact bool) { |
| 130 | + g.THelper() |
| 131 | + |
| 132 | + // Create the object and wait for it to be ready. |
| 133 | + g.Expect(testEnv.Create(ctx, obj)).To(Succeed()) |
| 134 | + waitForSourceReady(ctx, g, obj, withArtifact) |
| 135 | + |
| 136 | + // Suspend the object. |
| 137 | + patchHelper, err := patch.NewHelper(obj, testEnv.Client) |
| 138 | + g.Expect(err).ToNot(HaveOccurred()) |
| 139 | + g.Expect(object.SetSuspend(obj, true)).ToNot(HaveOccurred()) |
| 140 | + g.Expect(patchHelper.Patch(ctx, obj)).ToNot(HaveOccurred()) |
| 141 | + waitForSuspended(ctx, g, obj) |
| 142 | + |
| 143 | + // Delete the object. |
| 144 | + g.Expect(testEnv.Delete(ctx, obj)).To(Succeed()) |
| 145 | + waitForSourceDeletion(ctx, g, obj) |
| 146 | +} |
0 commit comments