Skip to content

Commit

Permalink
[envtest]Stabilize version controller tests (#836)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
gibizer and dprince authored Jun 10, 2024
1 parent 8813d7e commit ce338a5
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions tests/functional/openstackversion_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package functional_test

import (
"errors"
"os"

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

corev1 "github.com/openstack-k8s-operators/openstack-operator/apis/core/v1beta1"
k8s_corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
)

var _ = Describe("OpenStackOperator controller", func() {
Expand All @@ -45,6 +47,17 @@ var _ = Describe("OpenStackOperator controller", func() {
th.DeleteInstance,
CreateOpenStackVersion(names.OpenStackVersionName, GetDefaultOpenStackVersionSpec()),
)

// Ensure that the version instance is not marked new any more
// to avoid racing between the below cleanup removing the finalizer
// and the controller adding the finalizer to the new instance.
th.ExpectCondition(
names.OpenStackVersionName,
ConditionGetterFunc(OpenStackVersionConditionGetter),
corev1.OpenStackVersionInitialized,
k8s_corev1.ConditionTrue,
)

// we remove the finalizer as this is needed without the Controlplane
DeferCleanup(
OpenStackVersionRemoveFinalizer,
Expand All @@ -58,9 +71,16 @@ var _ = Describe("OpenStackOperator controller", func() {
instance := &corev1.OpenStackVersion{}
instance.ObjectMeta.Namespace = names.Namespace
instance.Name = "foo"
Eventually(func(g Gomega) {
g.Expect(k8sClient.Create(ctx, instance)).Should(Not(Succeed()))
}, timeout, interval).Should(Succeed())
err := k8sClient.Create(ctx, instance)

Expect(err).Should(HaveOccurred())
var statusError *k8s_errors.StatusError
Expect(errors.As(err, &statusError)).To(BeTrue())
Expect(statusError.ErrStatus.Details.Kind).To(Equal("OpenStackVersion"))
Expect(statusError.ErrStatus.Message).To(
ContainSubstring(
"Forbidden: Only one OpenStackVersion instance is supported at this time."),
)

})

Expand Down

0 comments on commit ce338a5

Please sign in to comment.