Skip to content

Commit

Permalink
Test case for mymaster
Browse files Browse the repository at this point in the history
  • Loading branch information
samof76 committed Sep 3, 2024
1 parent 61d2ee3 commit f5dae86
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions test/integration/redisfailover/creation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,105 @@ func TestRedisFailover(t *testing.T) {
})
}

func TestRedisFailoverMyMaster(t *testing.T) {
require := require.New(t)
disableMyMaster := false

// Create signal channels.
stopC := make(chan struct{})
errC := make(chan error)

flags := &utils.CMDFlags{
KubeConfig: filepath.Join(homedir.HomeDir(), ".kube", "config"),
Development: true,
}

// Kubernetes clients.
k8sClient, customClient, aeClientset, err := utils.CreateKubernetesClients(flags)
require.NoError(err)

// Create the redis clients
redisClient := redis.New(metrics.Dummy)

clients := clients{
k8sClient: k8sClient,
rfClient: customClient,
aeClient: aeClientset,
redisClient: redisClient,
}

// Create kubernetes service.
k8sservice := k8s.New(k8sClient, customClient, aeClientset, log.Dummy, metrics.Dummy)

// Prepare namespace
prepErr := clients.prepareNS()
require.NoError(prepErr)

// Give time to the namespace to be ready
time.Sleep(15 * time.Second)

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

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

// Prepare cleanup for when the test ends
defer clients.cleanup(stopC)

// Give time to the operator to start
time.Sleep(15 * time.Second)

// Create secret
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: authSecretPath,
Namespace: namespace,
},
Data: map[string][]byte{
"password": []byte(testPass),
},
}
_, err = k8sClient.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
ok := t.Run("Check Custom Resource Creation", func(t *testing.T) {
clients.testCRCreation(t, disableMyMaster)
})
require.True(ok, "the custom resource has to be created to continue")

// Giving time to the operator to create the resources
time.Sleep(3 * time.Minute)

// Verify that auth is set and actually working
t.Run("Check that auth is set in sentinel and redis configs", clients.testAuth)

// Check custom config is set
t.Run("Check that custom config is behave expected", clients.testCustomConfig)

// Check that a Redis Statefulset is created and the size of it is the one defined by the
// Redis Failover definition created before.
t.Run("Check Redis Statefulset existing and size", clients.testRedisStatefulSet)

// Check that a Sentinel Deployment is created and the size of it is the one defined by the
// Redis Failover definition created before.
t.Run("Check Sentinel Deployment existing and size", clients.testSentinelDeployment)

// Connect to all the Redis pods and, asking to the Redis running inside them, check
// that only one of them is the master of the failover.
t.Run("Check Only One Redis Master", clients.testRedisMaster)

// Connect to all the Sentinel pods and, asking to the Sentinel running inside them,
// check that all of them are connected to the same Redis node, and also that that node
// is the master.
t.Run("Check Sentinels Checking the Redis Master", func(t *testing.T) {
clients.testSentinelMonitoring(t, disableMyMaster)
})
}

func (c *clients) testCRCreation(t *testing.T, args ...bool) {
disableMyMaster := false
if len(args) > 0 && args[0] {
Expand Down

0 comments on commit f5dae86

Please sign in to comment.