Skip to content

Commit

Permalink
switch to go test instead of ginkgo
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt committed Mar 15, 2023
1 parent fa7e19e commit 1fd3725
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 166 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.20
require (
github.com/go-logr/logr v1.2.3
github.com/google/go-cmp v0.5.9
github.com/onsi/ginkgo/v2 v2.1.6
github.com/onsi/gomega v1.20.1
github.com/openconfig/kne v0.1.7
k8s.io/api v0.25.6
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo/v2 v2.1.6 h1:Fx2POJZfKRQcM1pH49qSZiYeu319wji004qX+GDovrU=
github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk=
github.com/onsi/gomega v1.20.1 h1:PA/3qinGoukvymdIDV8pii6tiZgC8kbmJO6Z5+b002Q=
github.com/onsi/gomega v1.20.1/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo=
github.com/openconfig/kne v0.1.7 h1:+57nqk5vZziotgFo4pyslTWbcrWYGofOn4c+OAS8ueQ=
Expand Down
191 changes: 83 additions & 108 deletions tests/integration/srlinux_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,145 +1,120 @@
/*
Copyright (c) 2021 Nokia. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package controllers
package integration_controllers_test

import (
"context"
"fmt"
"time"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
srlinuxv1 "github.com/srl-labs/srl-controller/api/v1"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("Srlinux controller", func() {
// Define utility constants for object names and testing timeouts/durations and intervals.
const (
SrlinuxName = "test-srlinux"
SrlinuxNamespace = "test"
)
const (
SrlinuxNamespace = "test"
SrlinuxName = "test-srlinux"
testImageName = "srlinux:latest"
)

Context("Srlinux controller test", func() {
ctx := context.Background()
var namespacedName = types.NamespacedName{Name: SrlinuxName, Namespace: SrlinuxNamespace}

namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: SrlinuxNamespace,
},
}
func TestSrlinuxReconciler(t *testing.T) {
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: SrlinuxNamespace,
},
}

typeNamespaceName := types.NamespacedName{Name: SrlinuxName, Namespace: SrlinuxNamespace}
setup := func(t *testing.T, g *WithT) *corev1.Namespace {
t.Helper()

BeforeEach(func() {
By("Creating the Namespace to perform the tests")
Expect(k8sClient.Create(ctx, namespace)).Should(Succeed())
})
t.Log("Creating the namespace")
g.Expect(k8sClient.Create(ctx, namespace)).Should(Succeed())

AfterEach(func() {
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)
})
return namespace
}

It("should successfully reconcile a custom resource for Srlinux created with just an image referenced", func() {
By("Checking that Srlinux resource doesn't exist in the cluster")
teardown := func(t *testing.T, _ *WithT) {
t.Helper()

srlinux := &srlinuxv1.Srlinux{}
t.Log("Deleting the namespace")
_ = k8sClient.Delete(ctx, namespace)
}

err := k8sClient.Get(ctx, typeNamespaceName, srlinux)
t.Run("Should reconcile a Srlinux custom resource", func(t *testing.T) {
g := NewWithT(t)

Expect(errors.IsNotFound(err)).To(BeTrue())
setup(t, g)
defer teardown(t, g)

By("Creating the custom resource for the Kind Srlinux")
srlinux = &srlinuxv1.Srlinux{
ObjectMeta: metav1.ObjectMeta{
Name: SrlinuxName,
Namespace: SrlinuxNamespace,
},
TypeMeta: metav1.TypeMeta{
Kind: "Srlinux",
APIVersion: "kne.srlinux.dev/v1",
},
Spec: srlinuxv1.SrlinuxSpec{
Config: &srlinuxv1.NodeConfig{
Image: "srlinux:latest",
},
t.Log("Checking that Srlinux resource doesn't exist in the cluster")
srlinux := &srlinuxv1.Srlinux{}

err := k8sClient.Get(ctx, namespacedName, srlinux)

g.Expect(errors.IsNotFound(err)).To(BeTrue())

t.Log("Creating the custom resource for the Kind Srlinux")
srlinux = &srlinuxv1.Srlinux{
ObjectMeta: metav1.ObjectMeta{
Name: SrlinuxName,
Namespace: SrlinuxNamespace,
},
TypeMeta: metav1.TypeMeta{
Kind: "Srlinux",
APIVersion: "kne.srlinux.dev/v1",
},
Spec: srlinuxv1.SrlinuxSpec{
Config: &srlinuxv1.NodeConfig{
Image: testImageName,
},
}
Expect(k8sClient.Create(ctx, srlinux)).Should(Succeed())
},
}
g.Expect(k8sClient.Create(ctx, srlinux)).Should(Succeed())

By("Checking if the custom resource was successfully created")
Eventually(func() error {
found := &srlinuxv1.Srlinux{}
t.Log("Checking if the custom resource was successfully created")
g.Eventually(func() error {
found := &srlinuxv1.Srlinux{}

return k8sClient.Get(ctx, typeNamespaceName, found)
}, 10*time.Second, time.Second).Should(Succeed())
return k8sClient.Get(ctx, namespacedName, found)
}).Should(Succeed())

// Reconcile is triggered by the creation of the custom resource
// Reconcile is triggered by the creation of the custom resource

By("Checking if Srlinux Pod was successfully created in the reconciliation")
Eventually(func() error {
found := &corev1.Pod{}
t.Log("Checking if Srlinux Pod was successfully created in the reconciliation")
g.Eventually(func() error {
found := &corev1.Pod{}

return k8sClient.Get(ctx, typeNamespaceName, found)
}, 10*time.Second, time.Second).Should(Succeed())
return k8sClient.Get(ctx, namespacedName, found)
}).Should(Succeed())

By("Ensuring the Srlinux CR Status has been updated")
Eventually(func() error {
Expect(k8sClient.Get(ctx, typeNamespaceName, srlinux)).Should(Succeed())
t.Log("Ensuring the Srlinux CR Status has been updated")
g.Eventually(func() error {
g.Expect(k8sClient.Get(ctx, namespacedName, srlinux)).Should(Succeed())

if srlinux.Status.Image != "srlinux:latest" {
return fmt.Errorf("got Srlinux.Status.Image: %s, want: %s", srlinux.Status.Image, "srlinux:latest")
}
if srlinux.Status.Image != testImageName {
return fmt.Errorf("got Srlinux.Status.Image: %s, want: %s", srlinux.Status.Image, testImageName)
}

return nil
}, 10*time.Second, time.Second).Should(Succeed())
return nil
}).Should(Succeed())

By("Deleting the custom resource for the Kind Srlinux")
Expect(k8sClient.Delete(ctx, srlinux)).Should(Succeed())
t.Log("Deleting the custom resource for the Kind Srlinux")
g.Expect(k8sClient.Delete(ctx, srlinux)).Should(Succeed())

By("Checking if the custom resource was successfully deleted")
Eventually(func() error {
found := &srlinuxv1.Srlinux{}
t.Log("Checking if the custom resource was successfully deleted")
g.Eventually(func() error {
found := &srlinuxv1.Srlinux{}

return k8sClient.Get(ctx, typeNamespaceName, found)
}, 10*time.Second, time.Second).ShouldNot(Succeed())
return k8sClient.Get(ctx, namespacedName, found)
}).ShouldNot(Succeed())

// because there are no controllers monitoring built-in resources in the envtest cluster,
// objects do not get deleted, even if an OwnerReference is set up
// because there are no controllers monitoring built-in resources in the envtest cluster,
// objects do not get deleted, even if an OwnerReference is set up

// Reconcile is triggered by the deletion of the custom resource
})
// Reconcile is triggered by the deletion of the custom resource
})
})
}
Loading

0 comments on commit 1fd3725

Please sign in to comment.