2020-03-20 13:39:32 +00:00
|
|
|
//
|
|
|
|
// DISCLAIMER
|
|
|
|
//
|
2022-01-10 11:35:49 +00:00
|
|
|
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
|
2020-03-20 13:39:32 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// Copyright holder is ArangoDB GmbH, Cologne, Germany
|
|
|
|
//
|
|
|
|
|
|
|
|
package deployment
|
|
|
|
|
|
|
|
import (
|
2021-03-23 15:47:28 +00:00
|
|
|
"context"
|
2020-03-23 22:03:16 +00:00
|
|
|
"encoding/json"
|
2020-03-20 13:39:32 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-21 07:32:02 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/deployment/features"
|
2021-08-26 07:59:16 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/deployment/resources"
|
|
|
|
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/errors"
|
2020-07-21 07:32:02 +00:00
|
|
|
|
2020-06-15 10:39:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2020-03-23 22:03:16 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
|
|
|
|
core "k8s.io/api/core/v1"
|
|
|
|
|
2020-03-20 13:39:32 +00:00
|
|
|
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func runTestCases(t *testing.T, testCases ...testCaseStruct) {
|
2020-04-27 11:54:24 +00:00
|
|
|
// This esure idempotency in generated outputs
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
t.Run(fmt.Sprintf("Iteration %d", i), func(t *testing.T) {
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
runTestCase(t, testCase)
|
|
|
|
}
|
|
|
|
})
|
2020-03-20 13:39:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTestCase(t *testing.T, testCase testCaseStruct) {
|
|
|
|
t.Run(testCase.Name, func(t *testing.T) {
|
|
|
|
// Arrange
|
2021-10-24 23:09:40 +00:00
|
|
|
if testCase.config.OperatorImage == "" {
|
|
|
|
testCase.config.OperatorImage = testImageOperator
|
|
|
|
}
|
|
|
|
|
2021-10-08 15:11:18 +00:00
|
|
|
d, eventRecorder := createTestDeployment(t, testCase.config, testCase.ArangoDeployment)
|
2020-03-20 13:39:32 +00:00
|
|
|
|
2021-10-24 23:09:40 +00:00
|
|
|
startDepl := d.status.last.DeepCopy()
|
|
|
|
|
2020-06-08 11:30:32 +00:00
|
|
|
errs := 0
|
|
|
|
for {
|
2021-10-08 15:11:18 +00:00
|
|
|
require.NoError(t, d.currentState.Refresh(context.Background()))
|
|
|
|
err := d.resources.EnsureSecrets(context.Background(), log.Logger, d.GetCachedStatus())
|
2020-06-08 11:30:32 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-06-15 10:39:05 +00:00
|
|
|
if errs > 25 {
|
2020-06-08 11:30:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
errs++
|
|
|
|
|
|
|
|
if errors.IsReconcile(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-03-20 13:39:32 +00:00
|
|
|
|
|
|
|
if testCase.Helper != nil {
|
|
|
|
testCase.Helper(t, d, &testCase)
|
|
|
|
}
|
|
|
|
|
2021-10-24 23:09:40 +00:00
|
|
|
f := startDepl.Members.AsList()
|
|
|
|
if len(f) == 0 {
|
|
|
|
f = d.status.last.Members.AsList()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add Expected pod defaults
|
|
|
|
if !testCase.DropInit {
|
|
|
|
testCase.ExpectedPod = *defaultPodAppender(t, &testCase.ExpectedPod,
|
|
|
|
addLifecycle(f[0].Member.ID,
|
|
|
|
f[0].Group == api.ServerGroupDBServers && f[0].Member.IsInitialized,
|
|
|
|
testCase.ArangoDeployment.Spec.License.GetSecretName(),
|
|
|
|
f[0].Group),
|
|
|
|
podDataSort())
|
|
|
|
}
|
|
|
|
|
2020-03-20 13:39:32 +00:00
|
|
|
// Create custom resource in the fake kubernetes API
|
2022-02-28 18:53:01 +00:00
|
|
|
_, err := d.deps.Client.Arango().DatabaseV1().ArangoDeployments(testNamespace).Create(context.Background(), d.apiObject, metav1.CreateOptions{})
|
2020-03-20 13:39:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-27 11:54:24 +00:00
|
|
|
if testCase.Resources != nil {
|
|
|
|
testCase.Resources(t, d)
|
|
|
|
}
|
2021-08-26 07:59:16 +00:00
|
|
|
|
|
|
|
// Set features
|
|
|
|
{
|
|
|
|
*features.EncryptionRotation().EnabledPointer() = testCase.Features.EncryptionRotation
|
|
|
|
require.Equal(t, testCase.Features.EncryptionRotation, *features.EncryptionRotation().EnabledPointer())
|
|
|
|
*features.JWTRotation().EnabledPointer() = testCase.Features.JWTRotation
|
|
|
|
*features.TLSSNI().EnabledPointer() = testCase.Features.TLSSNI
|
2021-10-29 07:41:41 +00:00
|
|
|
if g := testCase.Features.Graceful; g != nil {
|
|
|
|
*features.GracefulShutdown().EnabledPointer() = *g
|
|
|
|
} else {
|
|
|
|
*features.GracefulShutdown().EnabledPointer() = features.GracefulShutdown().EnabledByDefault()
|
|
|
|
}
|
2021-08-26 07:59:16 +00:00
|
|
|
*features.TLSRotation().EnabledPointer() = testCase.Features.TLSRotation
|
|
|
|
}
|
|
|
|
|
2021-08-20 13:02:36 +00:00
|
|
|
// Set Pending phase
|
|
|
|
require.NoError(t, d.status.last.Members.ForeachServerGroup(func(group api.ServerGroup, list api.MemberStatusList) error {
|
|
|
|
for _, m := range list {
|
|
|
|
if m.Phase == api.MemberPhaseNone {
|
|
|
|
m.Phase = api.MemberPhasePending
|
|
|
|
if err := d.status.last.Members.Update(m, group); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}))
|
2020-04-27 11:54:24 +00:00
|
|
|
|
2021-03-10 13:30:47 +00:00
|
|
|
// Set members
|
2021-08-26 07:59:16 +00:00
|
|
|
if err := d.status.last.Members.ForeachServerGroup(func(group api.ServerGroup, list api.MemberStatusList) error {
|
2021-03-10 13:30:47 +00:00
|
|
|
for _, m := range list {
|
2021-08-26 07:59:16 +00:00
|
|
|
|
2021-03-10 13:30:47 +00:00
|
|
|
member := api.ArangoMember{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: d.GetNamespace(),
|
|
|
|
Name: m.ArangoMemberName(d.GetName(), group),
|
|
|
|
},
|
|
|
|
Spec: api.ArangoMemberSpec{
|
|
|
|
Group: group,
|
|
|
|
ID: m.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-03-23 22:19:36 +00:00
|
|
|
c := d.WithCurrentArangoMember(m.ArangoMemberName(d.GetName(), group))
|
|
|
|
|
|
|
|
if err := c.Create(context.Background(), &member); err != nil {
|
2021-03-10 13:30:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := core.Service{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: member.GetName(),
|
|
|
|
Namespace: member.GetNamespace(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-08 15:11:18 +00:00
|
|
|
if _, err := d.ServicesModInterface().Create(context.Background(), &s, metav1.CreateOptions{}); err != nil {
|
2021-03-10 13:30:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-26 07:59:16 +00:00
|
|
|
|
2022-03-23 22:19:36 +00:00
|
|
|
require.NoError(t, d.currentState.Refresh(context.Background()))
|
2021-08-26 07:59:16 +00:00
|
|
|
|
|
|
|
groupSpec := d.apiObject.Spec.GetServerGroupSpec(group)
|
|
|
|
|
|
|
|
image, ok := d.resources.SelectImage(d.apiObject.Spec, d.status.last)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2022-03-23 22:19:36 +00:00
|
|
|
template, err := d.resources.RenderPodTemplateForMember(context.Background(), d.GetCachedStatus(), d.apiObject.Spec, d.status.last, m.ID, image)
|
2021-08-26 07:59:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
checksum, err := resources.ChecksumArangoPod(groupSpec, resources.CreatePodFromTemplate(template))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
podTemplate, err := api.GetArangoMemberPodTemplate(template, checksum)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
member.Status.Template = podTemplate
|
|
|
|
member.Spec.Template = podTemplate
|
|
|
|
|
2022-03-23 22:19:36 +00:00
|
|
|
if err := c.Update(context.Background(), func(obj *api.ArangoMember) bool {
|
|
|
|
obj.Spec.Template = podTemplate
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
2021-08-26 07:59:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-23 22:19:36 +00:00
|
|
|
if err := c.UpdateStatus(context.Background(), func(obj *api.ArangoMember, s *api.ArangoMemberStatus) bool {
|
|
|
|
s.Template = podTemplate
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
2021-08-26 07:59:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-03-10 13:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-08-26 07:59:16 +00:00
|
|
|
}); err != nil {
|
|
|
|
if testCase.ExpectedError != nil && assert.EqualError(t, err, testCase.ExpectedError.Error()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
2020-07-21 07:32:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 13:39:32 +00:00
|
|
|
// Act
|
2021-10-08 15:11:18 +00:00
|
|
|
require.NoError(t, d.currentState.Refresh(context.Background()))
|
|
|
|
err = d.resources.EnsurePods(context.Background(), d.GetCachedStatus())
|
2020-03-20 13:39:32 +00:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
if testCase.ExpectedError != nil {
|
|
|
|
|
|
|
|
if !assert.EqualError(t, err, testCase.ExpectedError.Error()) {
|
|
|
|
println(fmt.Sprintf("%+v", err))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2022-02-28 18:53:01 +00:00
|
|
|
pods, err := d.deps.Client.Kubernetes().CoreV1().Pods(testNamespace).List(context.Background(), metav1.ListOptions{})
|
2020-03-20 13:39:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, pods.Items, 1)
|
2020-03-23 22:03:16 +00:00
|
|
|
if util.BoolOrDefault(testCase.CompareChecksum, true) {
|
|
|
|
compareSpec(t, testCase.ExpectedPod.Spec, pods.Items[0].Spec)
|
|
|
|
}
|
2020-03-20 13:39:32 +00:00
|
|
|
require.Equal(t, testCase.ExpectedPod.Spec, pods.Items[0].Spec)
|
|
|
|
require.Equal(t, testCase.ExpectedPod.ObjectMeta, pods.Items[0].ObjectMeta)
|
|
|
|
|
|
|
|
if len(testCase.ExpectedEvent) > 0 {
|
|
|
|
select {
|
|
|
|
case msg := <-eventRecorder.Events:
|
|
|
|
assert.Contains(t, msg, testCase.ExpectedEvent)
|
|
|
|
default:
|
|
|
|
assert.Fail(t, "expected event", "expected event with message '%s'", testCase.ExpectedEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
status, version := d.GetStatus()
|
|
|
|
assert.Equal(t, int32(1), version)
|
|
|
|
|
|
|
|
checkEachMember := func(group api.ServerGroup, groupSpec api.ServerGroupSpec, status *api.MemberStatusList) error {
|
|
|
|
for _, m := range *status {
|
|
|
|
require.Equal(t, api.MemberPhaseCreated, m.Phase)
|
|
|
|
|
|
|
|
_, exist := m.Conditions.Get(api.ConditionTypeReady)
|
|
|
|
require.Equal(t, false, exist)
|
|
|
|
_, exist = m.Conditions.Get(api.ConditionTypeTerminated)
|
|
|
|
require.Equal(t, false, exist)
|
|
|
|
_, exist = m.Conditions.Get(api.ConditionTypeTerminating)
|
|
|
|
require.Equal(t, false, exist)
|
|
|
|
_, exist = m.Conditions.Get(api.ConditionTypeAgentRecoveryNeeded)
|
|
|
|
require.Equal(t, false, exist)
|
|
|
|
_, exist = m.Conditions.Get(api.ConditionTypeAutoUpgrade)
|
|
|
|
require.Equal(t, false, exist)
|
2020-09-17 13:05:28 +00:00
|
|
|
|
|
|
|
require.NotNil(t, m.Image)
|
|
|
|
require.True(t, m.Image.Equal(d.apiObject.Status.CurrentImage))
|
2020-03-20 13:39:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.GetServerGroupIterator().ForeachServerGroup(checkEachMember, &status)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-23 22:03:16 +00:00
|
|
|
|
|
|
|
func compareSpec(t *testing.T, a, b core.PodSpec) {
|
|
|
|
ac, err := k8sutil.GetPodSpecChecksum(a)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bc, err := k8sutil.GetPodSpecChecksum(b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
aj, err := json.Marshal(a)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bj, err := json.Marshal(b)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, string(aj), string(bj))
|
|
|
|
require.Equal(t, ac, bc)
|
|
|
|
}
|