Skip to content

Commit

Permalink
Merge pull request spotahome#344 from spotahome/integration-test
Browse files Browse the repository at this point in the history
Add Integration test to run with github-actions
  • Loading branch information
ese authored Jan 12, 2022
2 parents db1e316 + c0d41eb commit bf700a2
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 33 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,21 @@ jobs:
with:
go-version: 1.17
- run: make ci-unit-test

integration-test:
name: Integration test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Install conntrack
run: sudo apt-get install -y conntrack
- uses: medyagh/[email protected]
with:
kubernetes-version: 1.22.2
driver: none
- name: Add redisfailover CRD
run: kubectl apply -f manifests/databases.spotahome.com_redisfailovers.yaml
- run: make ci-integration-test
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ PORT := 9710
# CMDs
UNIT_TEST_CMD := go test `go list ./... | grep -v /vendor/` -v
GO_GENERATE_CMD := go generate `go list ./... | grep -v /vendor/`
GO_INTEGRATION_TEST_CMD := go test `go list ./... | grep test/integration` -v -tags='integration'
GET_DEPS_CMD := dep ensure
UPDATE_DEPS_CMD := dep ensure
MOCKS_CMD := go generate ./mocks
Expand Down Expand Up @@ -112,6 +113,10 @@ unit-test: docker-build
ci-unit-test:
$(UNIT_TEST_CMD)

.PHONY: ci-integration-test
ci-integration-test:
$(GO_INTEGRATION_TEST_CMD)

.PHONY: integration-test
integration-test:
./scripts/integration-tests.sh
Expand All @@ -122,7 +127,7 @@ helm-test:

# Run all tests
.PHONY: test
test: ci-unit-test integration-test helm-test
test: ci-unit-test ci-integration-test helm-test

.PHONY: go-generate
go-generate: docker-build
Expand Down
1 change: 0 additions & 1 deletion cmd/redisoperator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (m *Main) Run() error {

// Create the metrics client.
metricsRecorder := metrics.NewRecorder(metricsNamespace, prometheus.DefaultRegisterer)
//kooperMetricsServer := kmetrics.NewPrometheus(registry)

// Serve metrics.
go func() {
Expand Down
8 changes: 6 additions & 2 deletions metrics/dummy.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package metrics

import koopercontroller "github.com/spotahome/kooper/v2/controller"
import (
koopercontroller "github.com/spotahome/kooper/v2/controller"
)

// Dummy is a handy instnce of a dummy instrumenter, most of the times it will be used on tests.
var Dummy = &dummy{}
var Dummy = &dummy{
MetricsRecorder: koopercontroller.DummyMetricsRecorder,
}

// dummy is a dummy implementation of Instrumenter.
type dummy struct {
Expand Down
2 changes: 1 addition & 1 deletion scripts/integration-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function cleanup {
trap cleanup EXIT

echo "=> Preparing minikube for running integration tests"
$SUDO minikube start --vm-driver=none --kubernetes-version=v1.15.6
$SUDO minikube start --vm-driver=none --kubernetes-version=v1.22.3

echo "=> Waiting for minikube to start"
sleep 30
Expand Down
15 changes: 2 additions & 13 deletions service/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,10 @@ func (c *client) MonitorRedisWithPort(ip, monitor, port, quorum, password string
rClient := rediscli.NewClient(options)
defer rClient.Close()
cmd := rediscli.NewBoolCmd("SENTINEL", "REMOVE", masterName)
err := rClient.Process(cmd)
if err != nil {
return err
}
err = rClient.Process(cmd)
if err != nil {
return err
}
_ = rClient.Process(cmd)
// We'll continue even if it fails, the priority is to have the redises monitored
cmd = rediscli.NewBoolCmd("SENTINEL", "MONITOR", masterName, monitor, port, quorum)
err = rClient.Process(cmd)
if err != nil {
return err
}
err = rClient.Process(cmd)
err := rClient.Process(cmd)
if err != nil {
return err
}
Expand Down
36 changes: 21 additions & 15 deletions test/integration/redisfailover/creation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package redisfailover_test

import (
"context"
"fmt"
"path/filepath"
"testing"
Expand All @@ -17,6 +18,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"

_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/client-go/util/homedir"

Expand Down Expand Up @@ -52,12 +54,12 @@ func (c *clients) prepareNS() error {
Name: namespace,
},
}
_, err := c.k8sClient.CoreV1().Namespaces().Create(ns)
_, err := c.k8sClient.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
return err
}

func (c *clients) cleanup(stopC chan struct{}) {
c.k8sClient.CoreV1().Namespaces().Delete(namespace, &metav1.DeleteOptions{})
c.k8sClient.CoreV1().Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{})
close(stopC)
}

Expand Down Expand Up @@ -98,9 +100,11 @@ func TestRedisFailover(t *testing.T) {
time.Sleep(15 * time.Second)

// Create operator and run.
redisfailoverOperator := redisfailover.New(redisfailover.Config{}, k8sservice, redisClient, metrics.Dummy, kmetrics.Dummy, log.Dummy)
redisfailoverOperator, err := redisfailover.New(redisfailover.Config{}, k8sservice, redisClient, metrics.Dummy, log.Dummy)
require.NoError(err)

go func() {
errC <- redisfailoverOperator.Run(stopC)
errC <- redisfailoverOperator.Run(context.Background())
}()

// Prepare cleanup for when the test ends
Expand All @@ -119,7 +123,7 @@ func TestRedisFailover(t *testing.T) {
"password": []byte(testPass),
},
}
_, err = stdclient.CoreV1().Secrets(namespace).Create(secret)
_, err = stdclient.CoreV1().Secrets(namespace).Create(context.Background(), secret, metav1.CreateOptions{})
require.NoError(err)

// Check that if we create a RedisFailover, it is certainly created and we can get it
Expand Down Expand Up @@ -174,23 +178,23 @@ func (c *clients) testCRCreation(t *testing.T) {
},
}

c.rfClient.DatabasesV1().RedisFailovers(namespace).Create(toCreate)
gotRF, err := c.rfClient.DatabasesV1().RedisFailovers(namespace).Get(name, metav1.GetOptions{})
c.rfClient.DatabasesV1().RedisFailovers(namespace).Create(context.Background(), toCreate, metav1.CreateOptions{})
gotRF, err := c.rfClient.DatabasesV1().RedisFailovers(namespace).Get(context.Background(), name, metav1.GetOptions{})

assert.NoError(err)
assert.Equal(toCreate.Spec, gotRF.Spec)
}

func (c *clients) testRedisStatefulSet(t *testing.T) {
assert := assert.New(t)
redisSS, err := c.k8sClient.AppsV1().StatefulSets(namespace).Get(fmt.Sprintf("rfr-%s", name), metav1.GetOptions{})
redisSS, err := c.k8sClient.AppsV1().StatefulSets(namespace).Get(context.Background(), fmt.Sprintf("rfr-%s", name), metav1.GetOptions{})
assert.NoError(err)
assert.Equal(redisSize, int32(redisSS.Status.Replicas))
}

func (c *clients) testSentinelDeployment(t *testing.T) {
assert := assert.New(t)
sentinelD, err := c.k8sClient.AppsV1().Deployments(namespace).Get(fmt.Sprintf("rfs-%s", name), metav1.GetOptions{})
sentinelD, err := c.k8sClient.AppsV1().Deployments(namespace).Get(context.Background(), fmt.Sprintf("rfs-%s", name), metav1.GetOptions{})
assert.NoError(err)
assert.Equal(3, int(sentinelD.Status.Replicas))
}
Expand All @@ -199,13 +203,15 @@ func (c *clients) testRedisMaster(t *testing.T) {
assert := assert.New(t)
masters := []string{}

redisSS, err := c.k8sClient.AppsV1().StatefulSets(namespace).Get(fmt.Sprintf("rfr-%s", name), metav1.GetOptions{})
redisSS, err := c.k8sClient.AppsV1().StatefulSets(namespace).Get(context.Background(), fmt.Sprintf("rfr-%s", name), metav1.GetOptions{})
assert.NoError(err)

listOptions := metav1.ListOptions{
LabelSelector: labels.FormatLabels(redisSS.Spec.Selector.MatchLabels),
}
redisPodList, err := c.k8sClient.CoreV1().Pods(namespace).List(listOptions)

redisPodList, err := c.k8sClient.CoreV1().Pods(namespace).List(context.Background(), listOptions)

assert.NoError(err)

for _, pod := range redisPodList.Items {
Expand All @@ -222,13 +228,13 @@ func (c *clients) testSentinelMonitoring(t *testing.T) {
assert := assert.New(t)
masters := []string{}

sentinelD, err := c.k8sClient.AppsV1().Deployments(namespace).Get(fmt.Sprintf("rfs-%s", name), metav1.GetOptions{})
sentinelD, err := c.k8sClient.AppsV1().Deployments(namespace).Get(context.Background(), fmt.Sprintf("rfs-%s", name), metav1.GetOptions{})
assert.NoError(err)

listOptions := metav1.ListOptions{
LabelSelector: labels.FormatLabels(sentinelD.Spec.Selector.MatchLabels),
}
sentinelPodList, err := c.k8sClient.CoreV1().Pods(namespace).List(listOptions)
sentinelPodList, err := c.k8sClient.CoreV1().Pods(namespace).List(context.Background(), listOptions)
assert.NoError(err)

for _, pod := range sentinelPodList.Items {
Expand All @@ -250,12 +256,12 @@ func (c *clients) testAuth(t *testing.T) {

assert := assert.New(t)

redisCfg, err := c.k8sClient.CoreV1().ConfigMaps(namespace).Get(fmt.Sprintf("rfr-%s", name), metav1.GetOptions{})
redisCfg, err := c.k8sClient.CoreV1().ConfigMaps(namespace).Get(context.Background(), fmt.Sprintf("rfr-%s", name), metav1.GetOptions{})
assert.NoError(err)
assert.Contains(redisCfg.Data["redis.conf"], "requirepass "+testPass)
assert.Contains(redisCfg.Data["redis.conf"], "masterauth "+testPass)

redisSS, err := c.k8sClient.AppsV1().StatefulSets(namespace).Get(fmt.Sprintf("rfr-%s", name), metav1.GetOptions{})
redisSS, err := c.k8sClient.AppsV1().StatefulSets(namespace).Get(context.Background(), fmt.Sprintf("rfr-%s", name), metav1.GetOptions{})
assert.NoError(err)

assert.Len(redisSS.Spec.Template.Spec.Containers, 2)
Expand Down

0 comments on commit bf700a2

Please sign in to comment.