Skip to content

Commit

Permalink
Change Assert* functions in syncer/test to call functions in test
Browse files Browse the repository at this point in the history
The ones in test are identical but generalized.

Also removed the import of pkg/syncer/test in pkg/fake/basic_reactors_test.go
as it caused a circular dependency.

Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
  • Loading branch information
tpantelis committed Nov 20, 2023
1 parent 6b01d8f commit 2d9a79a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
17 changes: 9 additions & 8 deletions pkg/fake/basic_reactors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/fake"
"github.com/submariner-io/admiral/pkg/resource"
"github.com/submariner-io/admiral/pkg/syncer/test"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -38,6 +37,8 @@ import (
"k8s.io/utils/ptr"
)

const testNamespace = "test-ns"

var _ = Describe("Create", func() {
t := newBasicReactorsTestDriver()

Expand Down Expand Up @@ -180,7 +181,7 @@ var _ = Describe("List", func() {

When("a field selector is specified", func() {
It("should return the correct resources", func() {
list, err := t.client.CoreV1().Pods(test.LocalNamespace).List(context.Background(), metav1.ListOptions{
list, err := t.client.CoreV1().Pods(testNamespace).List(context.Background(), metav1.ListOptions{
FieldSelector: fields.OneTermEqualSelector("metadata.name", t.pod.Name).String(),
})

Expand All @@ -199,7 +200,7 @@ var _ = Describe("Watch", func() {
JustBeforeEach(func() {
var err error

watcher, err = t.client.CoreV1().Pods(test.LocalNamespace).Watch(context.Background(), metav1.ListOptions{
watcher, err = t.client.CoreV1().Pods(testNamespace).Watch(context.Background(), metav1.ListOptions{
LabelSelector: k8slabels.SelectorFromSet(t.pod.Labels).String(),
})
Expect(err).To(Succeed())
Expand Down Expand Up @@ -257,7 +258,7 @@ var _ = Describe("DeleteCollection", func() {
},
})

err := t.client.CoreV1().Pods(test.LocalNamespace).DeleteCollection(context.Background(), metav1.DeleteOptions{},
err := t.client.CoreV1().Pods(testNamespace).DeleteCollection(context.Background(), metav1.DeleteOptions{},
metav1.ListOptions{
LabelSelector: k8slabels.SelectorFromSet(t.pod.Labels).String(),
})
Expand Down Expand Up @@ -304,7 +305,7 @@ func newBasicReactorsTestDriver() *basicReactorsTestDriver {
}

func (t *basicReactorsTestDriver) doGet(name string) (*corev1.Pod, error) {
return t.client.CoreV1().Pods(test.LocalNamespace).Get(context.Background(), name, metav1.GetOptions{})
return t.client.CoreV1().Pods(testNamespace).Get(context.Background(), name, metav1.GetOptions{})
}

func (t *basicReactorsTestDriver) assertGetSuccess(p *corev1.Pod) *corev1.Pod {
Expand All @@ -316,7 +317,7 @@ func (t *basicReactorsTestDriver) assertGetSuccess(p *corev1.Pod) *corev1.Pod {
}

func (t *basicReactorsTestDriver) doCreate(pod *corev1.Pod) (*corev1.Pod, error) {
return t.client.CoreV1().Pods(test.LocalNamespace).Create(context.Background(), pod, metav1.CreateOptions{})
return t.client.CoreV1().Pods(testNamespace).Create(context.Background(), pod, metav1.CreateOptions{})
}

func (t *basicReactorsTestDriver) assertCreateSuccess(pod *corev1.Pod) *corev1.Pod {
Expand All @@ -327,7 +328,7 @@ func (t *basicReactorsTestDriver) assertCreateSuccess(pod *corev1.Pod) *corev1.P
}

func (t *basicReactorsTestDriver) doUpdate() (*corev1.Pod, error) {
return t.client.CoreV1().Pods(test.LocalNamespace).Update(context.Background(), t.pod, metav1.UpdateOptions{})
return t.client.CoreV1().Pods(testNamespace).Update(context.Background(), t.pod, metav1.UpdateOptions{})
}

func (t *basicReactorsTestDriver) doUpdateSuccess() *corev1.Pod {
Expand All @@ -339,5 +340,5 @@ func (t *basicReactorsTestDriver) doUpdateSuccess() *corev1.Pod {

//nolint:gocritic // Ignore hugeParam
func (t *basicReactorsTestDriver) doDelete(opts metav1.DeleteOptions) error {
return t.client.CoreV1().Pods(test.LocalNamespace).Delete(context.Background(), t.pod.Name, opts)
return t.client.CoreV1().Pods(testNamespace).Delete(context.Background(), t.pod.Name, opts)
}
35 changes: 4 additions & 31 deletions pkg/syncer/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ package test

import (
"context"
"fmt"
"time"

. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/federate"
"github.com/submariner-io/admiral/pkg/resource"
"github.com/submariner-io/admiral/pkg/test"
"github.com/submariner-io/admiral/pkg/util"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metaapi "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -219,40 +217,15 @@ func SetClusterIDLabel(obj runtime.Object, clusterID string) runtime.Object {
}

func AwaitResource(client dynamic.ResourceInterface, name string) *unstructured.Unstructured {
return AwaitAndVerifyResource(client, name, nil)
return test.AwaitResource(resource.ForDynamic(client), name)
}

func AwaitAndVerifyResource(client dynamic.ResourceInterface, name string,
verify func(*unstructured.Unstructured) bool,
) *unstructured.Unstructured {
var found *unstructured.Unstructured

Eventually(func() error {
obj, err := client.Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return err
}

if verify == nil || verify(obj) {
found = obj
return nil
}

return fmt.Errorf("resource %q was found but not verified", name)
}, 5*time.Second, 50*time.Millisecond).Should(Succeed())

return found
return test.AwaitAndVerifyResource(resource.ForDynamic(client), name, verify)
}

func AwaitNoResource(client dynamic.ResourceInterface, name string) {
Eventually(func() bool {
_, err := client.Get(context.TODO(), name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return true
}

Expect(err).To(Succeed())

return false
}, 5*time.Second, 50*time.Millisecond).Should(BeTrue(), "Resource %q still exists", name)
test.AwaitNoResource(resource.ForDynamic(client), name)
}
24 changes: 18 additions & 6 deletions pkg/test/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package test

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -105,16 +106,27 @@ func AwaitStatusCondition(expCond *metav1.Condition, get func() ([]metav1.Condit
}

func AwaitResource[T runtime.Object](client resource.Interface[T], name string) T {
var obj T
return AwaitAndVerifyResource(client, name, nil)
}

func AwaitAndVerifyResource[T runtime.Object](client resource.Interface[T], name string, verify func(T) bool) T {
var found T

Eventually(func() error {
var err error
obj, err := client.Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return err
}

if verify == nil || verify(obj) {
found = obj
return nil
}

obj, err = client.Get(context.TODO(), name, metav1.GetOptions{})
return err
}, 3).Should(Succeed())
return fmt.Errorf("resource %q was found but not verified", name)
}, 3*time.Second, 50*time.Millisecond).Should(Succeed())

return obj
return found
}

func AwaitNoResource[T runtime.Object](client resource.Interface[T], name string) {
Expand Down

0 comments on commit 2d9a79a

Please sign in to comment.