Skip to content

Commit

Permalink
Use ginkgo's context in e2e
Browse files Browse the repository at this point in the history
Use ginkgo contexts instead and let ginkgo handle them, instead of
creating our on contexts.

Signed-off-by: Nahshon Unna-Tsameret <[email protected]>
  • Loading branch information
nunnatsa committed May 13, 2024
1 parent f67b4c9 commit e366648
Show file tree
Hide file tree
Showing 18 changed files with 301 additions and 335 deletions.
26 changes: 14 additions & 12 deletions tests/func-tests/aaq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"kubevirt.io/kubevirt/tests/flags"

hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util"

tests "github.com/kubevirt/hyperconverged-cluster-operator/tests/func-tests"
)

Expand All @@ -30,39 +31,36 @@ var _ = Describe("Test AAQ", Label("AAQ"), Serial, Ordered, func() {
tests.FlagParse()
var (
cli kubecli.KubevirtClient
ctx context.Context
)

BeforeEach(func() {
BeforeEach(func(ctx context.Context) {
var err error

cli, err = kubecli.GetKubevirtClient()
Expect(cli).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())

ctx = context.Background()

disableAAQFeatureGate(ctx, cli)
})

AfterAll(func() {
AfterAll(func(ctx context.Context) {
disableAAQFeatureGate(ctx, cli)
})

When("set the applicationAwareConfig exists", func() {
It("should create the AAQ CR and all the pods", func() {
It("should create the AAQ CR and all the pods", func(ctx context.Context) {

enableAAQFeatureGate(ctx, cli)

By("check the AAQ CR")
Eventually(func(g Gomega) bool {
Eventually(func(g Gomega, ctx context.Context) bool {
aaq := getAAQ(ctx, cli, g)
g.Expect(aaq.Status.Conditions).ToNot(BeEmpty())
return conditionsv1.IsStatusConditionTrue(aaq.Status.Conditions, conditionsv1.ConditionAvailable)
}).WithTimeout(5 * time.Minute).WithPolling(time.Second).ShouldNot(BeTrue())
}).WithTimeout(5 * time.Minute).WithPolling(time.Second).WithContext(ctx).ShouldNot(BeTrue())

By("check AAQ pods")
Eventually(func(g Gomega) {
Eventually(func(g Gomega, ctx context.Context) {
deps, err := cli.AppsV1().Deployments(flags.KubeVirtInstallNamespace).List(ctx, metav1.ListOptions{LabelSelector: "app.kubernetes.io/managed-by=aaq-operator"})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(deps.Items).To(HaveLen(2))
Expand All @@ -78,6 +76,7 @@ var _ = Describe("Test AAQ", Label("AAQ"), Serial, Ordered, func() {
g.Expect(pods.Items).To(HaveLen(int(expectedPods)))
}).WithTimeout(5 * time.Minute).
WithPolling(time.Second).
WithContext(ctx).
Should(Succeed())
})
})
Expand Down Expand Up @@ -110,21 +109,24 @@ func disableAAQFeatureGate(ctx context.Context, cli kubecli.KubevirtClient) {
setAAQFeatureGate(ctx, cli, false)

By("make sure the AAQ CR was removed")
Eventually(func() error {
Eventually(func(ctx context.Context) error {
_, err := getAAQResource(ctx, cli)
return err
}).WithTimeout(5 * time.Minute).
WithPolling(100 * time.Millisecond).
WithOffset(1).
WithContext(ctx).
Should(MatchError(errors.IsNotFound, "not found error"))
}

func setAAQFeatureGate(ctx context.Context, cli kubecli.KubevirtClient, fgState bool) {
patch := []byte(fmt.Sprintf(setAAQFGPatchTemplate, fgState))
Eventually(tests.PatchHCO).
WithArguments(ctx, cli, patch).
Eventually(func(ctx context.Context) error {
return tests.PatchHCO(ctx, cli, patch)
}).
WithTimeout(10 * time.Second).
WithPolling(100 * time.Millisecond).
WithContext(ctx).
WithOffset(2).
Should(Succeed())
}
8 changes: 4 additions & 4 deletions tests/func-tests/cli_download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ var _ = Describe("[rfe_id:5100][crit:medium][vendor:[email protected]][level:sys
flag.Parse()

var cli kubecli.KubevirtClient
BeforeEach(func() {
BeforeEach(func(ctx context.Context) {
tests.BeforeEach()
virtCli, err := kubecli.GetKubevirtClient()
Expect(err).ToNot(HaveOccurred())

cli, err = kubecli.GetKubevirtClientFromRESTConfig(virtCli.Config())
Expect(err).ToNot(HaveOccurred())
tests.FailIfNotOpenShift(cli, "ConsoleCliDownload")
tests.FailIfNotOpenShift(ctx, cli, "ConsoleCliDownload")
})

It("[test_id:6956]should create ConsoleCliDownload objects with expected spec", Label("test_id:6956"), func() {
It("[test_id:6956]should create ConsoleCliDownload objects with expected spec", Label("test_id:6956"), func(ctx context.Context) {
By("Checking existence of ConsoleCliDownload")
s := scheme.Scheme
_ = consolev1.Install(s)
Expand All @@ -44,7 +44,7 @@ var _ = Describe("[rfe_id:5100][crit:medium][vendor:[email protected]][level:sys
Name("virtctl-clidownloads-kubevirt-hyperconverged").
AbsPath("/apis", consolev1.GroupVersion.Group, consolev1.GroupVersion.Version).
Timeout(10*time.Second).
Do(context.TODO()).Into(&ccd)).To(Succeed())
Do(ctx).Into(&ccd)).To(Succeed())

ExpectWithOffset(1, ccd.Spec.Links).Should(HaveLen(6))

Expand Down
15 changes: 7 additions & 8 deletions tests/func-tests/cluster_eviction_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,33 @@ import (
var _ = Describe("Cluster level evictionStrategy default value", Serial, Ordered, func() {
tests.FlagParse()
var cli kubecli.KubevirtClient
ctx := context.TODO()

var (
initialEvictionStrategy *v1.EvictionStrategy
singleWorkerCluster bool
)

BeforeEach(func() {
BeforeEach(func(ctx context.Context) {
var err error
cli, err = kubecli.GetKubevirtClient()
Expect(cli).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())

singleWorkerCluster, err = isSingleWorkerCluster(cli)
singleWorkerCluster, err = isSingleWorkerCluster(ctx, cli)
Expect(err).ToNot(HaveOccurred())

tests.BeforeEach()
hc := tests.GetHCO(ctx, cli)
initialEvictionStrategy = hc.Spec.EvictionStrategy
})

AfterEach(func() {
AfterEach(func(ctx context.Context) {
hc := tests.GetHCO(ctx, cli)
hc.Spec.EvictionStrategy = initialEvictionStrategy
_ = tests.UpdateHCORetry(ctx, cli, hc)
})

It("Should set spec.evictionStrategy = None by default on single worker clusters", Label(tests.SingleNodeLabel), func() {
It("Should set spec.evictionStrategy = None by default on single worker clusters", Label(tests.SingleNodeLabel), func(ctx context.Context) {
Expect(singleWorkerCluster).To(BeTrue(), "this test requires single worker cluster; use the %q label to skip this test", tests.SingleNodeLabel)

hco := tests.GetHCO(ctx, cli)
Expand All @@ -54,7 +53,7 @@ var _ = Describe("Cluster level evictionStrategy default value", Serial, Ordered
Expect(hco.Spec.EvictionStrategy).To(Equal(&noneEvictionStrategy))
})

It("Should set spec.evictionStrategy = LiveMigrate by default with multiple worker node", Label(tests.HighlyAvailableClusterLabel), func() {
It("Should set spec.evictionStrategy = LiveMigrate by default with multiple worker node", Label(tests.HighlyAvailableClusterLabel), func(ctx context.Context) {
tests.FailIfSingleNode(singleWorkerCluster)
hco := tests.GetHCO(ctx, cli)
hco.Spec.EvictionStrategy = nil
Expand All @@ -66,8 +65,8 @@ var _ = Describe("Cluster level evictionStrategy default value", Serial, Ordered

})

func isSingleWorkerCluster(cli kubecli.KubevirtClient) (bool, error) {
workerNodes, err := cli.CoreV1().Nodes().List(context.TODO(), k8smetav1.ListOptions{LabelSelector: "node-role.kubernetes.io/worker"})
func isSingleWorkerCluster(ctx context.Context, cli kubecli.KubevirtClient) (bool, error) {
workerNodes, err := cli.CoreV1().Nodes().List(ctx, k8smetav1.ListOptions{LabelSelector: "node-role.kubernetes.io/worker"})
if err != nil {
return false, err
}
Expand Down
30 changes: 17 additions & 13 deletions tests/func-tests/console_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const (
var _ = Describe("kubevirt console plugin", Label(tests.OpenshiftLabel), func() {
var (
cli kubecli.KubevirtClient
ctx context.Context
expectedKubevirtConsolePluginName = "kubevirt-plugin"
consoleGVR = schema.GroupVersionResource{
Group: "console.openshift.io",
Expand All @@ -43,23 +42,22 @@ var _ = Describe("kubevirt console plugin", Label(tests.OpenshiftLabel), func()

tests.FlagParse()

BeforeEach(func() {
BeforeEach(func(ctx context.Context) {
var err error
cli, err = kubecli.GetKubevirtClient()
Expect(err).ToNot(HaveOccurred())

tests.FailIfNotOpenShift(cli, "kubevirt console plugin")
ctx = context.Background()
tests.FailIfNotOpenShift(ctx, cli, "kubevirt console plugin")

hco := tests.GetHCO(ctx, cli)
originalInfra := hco.Spec.Infra
DeferCleanup(func() {
DeferCleanup(func(ctx context.Context) {
hco.Spec.Infra = originalInfra
tests.UpdateHCORetry(ctx, cli, hco)
})
})

It("console should reach kubevirt-plugin manifests", func() {
It("console should reach kubevirt-plugin manifests", func(ctx context.Context) {
unstructured, err := cli.DynamicClient().Resource(consoleGVR).Get(ctx, expectedKubevirtConsolePluginName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -94,7 +92,7 @@ var _ = Describe("kubevirt console plugin", Label(tests.OpenshiftLabel), func()
Expect(pluginName).To(Equal(expectedKubevirtConsolePluginName))
})

It("nodePlacement should be propagated from HyperConverged CR to console-plugin and apiserver-proxy Deployments", Serial, func() {
It("nodePlacement should be propagated from HyperConverged CR to console-plugin and apiserver-proxy Deployments", Serial, func(ctx context.Context) {

expectedNodeSelector := map[string]string{
"foo": "bar",
Expand All @@ -104,52 +102,58 @@ var _ = Describe("kubevirt console plugin", Label(tests.OpenshiftLabel), func()
expectedNodeSelectorStr := string(expectedNodeSelectorBytes)
addNodeSelectorPatch := []byte(fmt.Sprintf(`[{"op": "add", "path": "/spec/infra", "value": {"nodePlacement": {"nodeSelector": %s}}}]`, expectedNodeSelectorStr))

Eventually(func() error {
Eventually(func(ctx context.Context) error {
err = tests.PatchHCO(ctx, cli, addNodeSelectorPatch)
return err
}).WithTimeout(1 * time.Minute).
WithPolling(1 * time.Millisecond).
WithContext(ctx).
Should(Succeed())

Eventually(func(g Gomega) {
Eventually(func(g Gomega, ctx context.Context) {
consoleUIDeployment, err := cli.AppsV1().Deployments(flags.KubeVirtInstallNamespace).Get(ctx, string(hcoutil.AppComponentUIPlugin), metav1.GetOptions{})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(consoleUIDeployment.Spec.Template.Spec.NodeSelector).To(Equal(expectedNodeSelector))
}).WithTimeout(1 * time.Minute).
WithPolling(100 * time.Millisecond).
WithContext(ctx).
Should(Succeed())

Eventually(func(g Gomega) {
Eventually(func(g Gomega, ctx context.Context) {
proxyUIDeployment, err := cli.AppsV1().Deployments(flags.KubeVirtInstallNamespace).Get(ctx, string(hcoutil.AppComponentUIProxy), metav1.GetOptions{})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(proxyUIDeployment.Spec.Template.Spec.NodeSelector).To(Equal(expectedNodeSelector))
}).WithTimeout(1 * time.Minute).
WithPolling(100 * time.Millisecond).
WithContext(ctx).
Should(Succeed())

// clear node placement from HyperConverged CR and verify the nodeSelector has been cleared as well from the UI Deployments
removeNodeSelectorPatch := []byte(`[{"op": "replace", "path": "/spec/infra", "value": {}}]`)
Eventually(func() error {
Eventually(func(ctx context.Context) error {
err = tests.PatchHCO(ctx, cli, removeNodeSelectorPatch)
return err
}).WithTimeout(1 * time.Minute).
WithPolling(1 * time.Millisecond).
WithContext(ctx).
Should(Succeed())

Eventually(func(g Gomega) {
Eventually(func(g Gomega, ctx context.Context) {
consoleUIDeployment, err := cli.AppsV1().Deployments(flags.KubeVirtInstallNamespace).Get(ctx, string(hcoutil.AppComponentUIPlugin), metav1.GetOptions{})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(consoleUIDeployment.Spec.Template.Spec.NodeSelector).To(BeEmpty())
}).WithTimeout(1 * time.Minute).
WithPolling(100 * time.Millisecond).
WithContext(ctx).
Should(Succeed())

Eventually(func(g Gomega) {
Eventually(func(g Gomega, ctx context.Context) {
proxyUIDeployment, err := cli.AppsV1().Deployments(flags.KubeVirtInstallNamespace).Get(ctx, string(hcoutil.AppComponentUIProxy), metav1.GetOptions{})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(proxyUIDeployment.Spec.Template.Spec.NodeSelector).To(BeEmpty())
}).WithTimeout(1 * time.Minute).
WithPolling(100 * time.Millisecond).
WithContext(ctx).
Should(Succeed())
})
})
Expand Down
8 changes: 4 additions & 4 deletions tests/func-tests/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ var _ = Describe("[rfe_id:5108][crit:medium][vendor:[email protected]][level:sys

var cli kubecli.KubevirtClient

BeforeEach(func() {
BeforeEach(func(ctx context.Context) {
tests.BeforeEach()

virtCli, err := kubecli.GetKubevirtClient()
Expect(err).ToNot(HaveOccurred())

tests.FailIfNotOpenShift(virtCli, "Dashboard configmaps")
tests.FailIfNotOpenShift(ctx, virtCli, "Dashboard configmaps")

cli, err = kubecli.GetKubevirtClientFromRESTConfig(virtCli.Config())
Expect(err).ToNot(HaveOccurred())
})

It("[test_id:5919]should create configmaps for OCP Dashboard", Label("test_id:5919"), func() {
It("[test_id:5919]should create configmaps for OCP Dashboard", Label("test_id:5919"), func(ctx context.Context) {
By("Checking expected configmaps")
s := scheme.Scheme
_ = consolev1.Install(s)
Expand All @@ -45,7 +45,7 @@ var _ = Describe("[rfe_id:5108][crit:medium][vendor:[email protected]][level:sys
}

for _, item := range items {
cm, err := cli.CoreV1().ConfigMaps(item.Namespace).Get(context.TODO(), item.Name, v1.GetOptions{})
cm, err := cli.CoreV1().ConfigMaps(item.Namespace).Get(ctx, item.Name, v1.GetOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
for _, key := range item.Keys {
_, ok := cm.Data[key]
Expand Down
Loading

0 comments on commit e366648

Please sign in to comment.