Skip to content

Commit ce338a5

Browse files
gibizerdprince
andauthored
[envtest]Stabilize version controller tests (#836)
There was a race condition between the version controller applying the finalizer to the version instance and the test case cleanup removing such finalizer. By waiting for the controller to run at least once before the test finishes is enough to avoid the race as the controller only adds the finalizer during the first reconcile run that initialize the conditions. Also cleaned up the test case itself a bit and added more precise assert. Co-authored-by: Dan Prince <[email protected]>
1 parent 8813d7e commit ce338a5

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

tests/functional/openstackversion_controller_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package functional_test
1818

1919
import (
20+
"errors"
2021
"os"
2122

2223
. "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports
@@ -27,6 +28,7 @@ import (
2728

2829
corev1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1"
2930
k8s_corev1 "k8s.io/api/core/v1"
31+
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
3032
)
3133

3234
var _ = Describe("OpenStackOperator controller", func() {
@@ -45,6 +47,17 @@ var _ = Describe("OpenStackOperator controller", func() {
4547
th.DeleteInstance,
4648
CreateOpenStackVersion(names.OpenStackVersionName, GetDefaultOpenStackVersionSpec()),
4749
)
50+
51+
// Ensure that the version instance is not marked new any more
52+
// to avoid racing between the below cleanup removing the finalizer
53+
// and the controller adding the finalizer to the new instance.
54+
th.ExpectCondition(
55+
names.OpenStackVersionName,
56+
ConditionGetterFunc(OpenStackVersionConditionGetter),
57+
corev1.OpenStackVersionInitialized,
58+
k8s_corev1.ConditionTrue,
59+
)
60+
4861
// we remove the finalizer as this is needed without the Controlplane
4962
DeferCleanup(
5063
OpenStackVersionRemoveFinalizer,
@@ -58,9 +71,16 @@ var _ = Describe("OpenStackOperator controller", func() {
5871
instance := &corev1.OpenStackVersion{}
5972
instance.ObjectMeta.Namespace = names.Namespace
6073
instance.Name = "foo"
61-
Eventually(func(g Gomega) {
62-
g.Expect(k8sClient.Create(ctx, instance)).Should(Not(Succeed()))
63-
}, timeout, interval).Should(Succeed())
74+
err := k8sClient.Create(ctx, instance)
75+
76+
Expect(err).Should(HaveOccurred())
77+
var statusError *k8s_errors.StatusError
78+
Expect(errors.As(err, &statusError)).To(BeTrue())
79+
Expect(statusError.ErrStatus.Details.Kind).To(Equal("OpenStackVersion"))
80+
Expect(statusError.ErrStatus.Message).To(
81+
ContainSubstring(
82+
"Forbidden: Only one OpenStackVersion instance is supported at this time."),
83+
)
6484

6585
})
6686

0 commit comments

Comments
 (0)