Skip to content

Commit

Permalink
Add integration tests for redis standalone
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Cesbron <[email protected]>
  • Loading branch information
MathieuCesbron committed Jan 14, 2024
1 parent 7665d4a commit 79c4fb7
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 66 deletions.
39 changes: 20 additions & 19 deletions .github/workflows/operator-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ env:
DockerImagName: docker.io/opstree/redis-operator
BuildDocs: true
AppVersion: "v0.15.2"
DOCKERFILE_PATH: '**/Dockerfile'
DOCKERFILE_PATH: "**/Dockerfile"

jobs:
gofmt:
Expand Down Expand Up @@ -54,6 +54,8 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: ${{ env.GOLANG_VERSION }}
- name: Install integration test dependencies
run: make integration-test-setup
- name: Run Go Tests with coverage
run: go test ./... -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage to Codecov
Expand All @@ -62,7 +64,7 @@ jobs:
file: ./coverage.txt
fail_ci_if_error: false
verbose: true

code_quality_golang_ci_lint:
needs: [gofmt, govet]
runs-on: ubuntu-latest
Expand All @@ -72,16 +74,16 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: ${{ env.GOLANG_VERSION }}

- name: Download Go modules
run: go mod download

- name: Check disk space
run: df -h

- name: List Go module cache
run: ls -la $(go env GOPATH)/pkg/mod

- name: Run GolangCI-Lint
uses: golangci/golangci-lint-action@v3
with:
Expand All @@ -105,7 +107,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
arch: ['amd64', 'arm64']
arch: ["amd64", "arm64"]
steps:
- name: Checkout Code
uses: actions/checkout@v2
Expand All @@ -125,19 +127,19 @@ jobs:
needs: [container_quality_dockerfile_lint]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build multi-arch image
uses: docker/build-push-action@v2
with:
context: .
platforms: linux/arm64,linux/amd64
push: false
tags: ${{ env.DockerImagName }}:latest
- name: Build multi-arch image
uses: docker/build-push-action@v2
with:
context: .
platforms: linux/arm64,linux/amd64
push: false
tags: ${{ env.DockerImagName }}:latest

gosec_scan:
needs: [build_go_binary]
Expand All @@ -152,4 +154,3 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

GOSEC_OUTPUT: "junit-xml:/github/workspace/gosec-results.xml"

6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,8 @@ e2e-kind-setup:

.PHONY: e2e-test
e2e-test: e2e-kind-setup install-kuttl
$(shell pwd)/bin/kuttl test --config tests/_config/kuttl-test.yaml
$(shell pwd)/bin/kuttl test --config tests/_config/kuttl-test.yaml

.PHONY: integration-test-setup
integration-test-setup:
./hack/integrationSetup.sh
59 changes: 59 additions & 0 deletions controllers/redis_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package controllers

import (
"context"
"time"

redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

const (
ns = "default"
redisStandaloneName = "redis-standalone"

timeout = time.Second * 5
interval = time.Millisecond * 250
)

var _ = Describe("Redis standalone test", func() {
Context("When creating a redis standalone CR", func() {
It("should create a statefulset", func() {
ctx := context.Background()

var runAsUser int64 = 1000
var fsGroup int64 = 1000
redisStandaloneCR := redisv1beta2.Redis{
TypeMeta: metav1.TypeMeta{
APIVersion: "redisv1beta2/apiVersion",
Kind: "Redis",
},
ObjectMeta: metav1.ObjectMeta{
Name: redisStandaloneName,
Namespace: ns,
},
Spec: redisv1beta2.RedisSpec{
PodSecurityContext: &corev1.PodSecurityContext{
RunAsUser: &runAsUser,
FSGroup: &fsGroup,
},
// KubernetesConfig: redisv1beta2.KubernetesConfig{},
},
}
Expect(k8sClient.Create(ctx, &redisStandaloneCR)).Should(Succeed())

Eventually(func() error {
service := &corev1.Service{}
err := k8sClient.Get(ctx, types.NamespacedName{
Name: redisStandaloneName,
Namespace: ns,
}, service)
return err
}, timeout, interval).Should(BeNil())
})
})
})
76 changes: 50 additions & 26 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,63 +18,87 @@ package controllers

import (
"path/filepath"
"testing"
"time"

// redisv1beta1 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta1"
redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2"
. "github.com/onsi/ginkgo"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"

// "k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

// var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment

// func TestAPIs(t *testing.T) {
// RegisterFailHandler(Fail)
func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

// RunSpecsWithDefaultAndCustomReporters(t,
// "Controller Suite",
// []Reporter{printer.NewlineReporter{}})
// }
RunSpecs(t, "Controller suite")
}

var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
CRDInstallOptions: envtest.CRDInstallOptions{
MaxTime: 60 * time.Second,
},
}

cfg, err := testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())

err = redisv1beta2.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
// err = redisv1beta1.AddToScheme(scheme.Scheme)
// Expect(err).ToNot(HaveOccurred())

err = redisv1beta2.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
Expect(err).ToNot(HaveOccurred())

// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

}, 60)
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient).ToNot(BeNil())

k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
})
Expect(err).ToNot(HaveOccurred())

k8sClient, err := kubernetes.NewForConfig(cfg)
Expect(err).ToNot(HaveOccurred())

err = (&RedisReconciler{
Client: k8sManager.GetClient(),
K8sClient: k8sClient,
Scheme: k8sManager.GetScheme(),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

go func() {
defer GinkgoRecover()
err = k8sManager.Start(ctrl.SetupSignalHandler())
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
gexec.KillAndWait(4 * time.Second)

// Teardown the test environment once controller is fnished.
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
}()

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/banzaicloud/k8s-objectmatcher v1.7.0
github.com/go-logr/logr v1.2.4
github.com/onsi/ginkgo v1.16.5
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
github.com/pkg/errors v0.9.1
github.com/redis/go-redis/v9 v9.2.1
Expand All @@ -31,12 +31,14 @@ require (
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand All @@ -47,7 +49,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
Expand All @@ -63,11 +64,11 @@ require (
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.3 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.28.0 // indirect
Expand Down
Loading

0 comments on commit 79c4fb7

Please sign in to comment.