|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package deployment |
| 24 | + |
| 25 | +import ( |
| 26 | + "testing" |
| 27 | + |
| 28 | + "github.com/rs/zerolog" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | + |
| 32 | + api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha" |
| 33 | +) |
| 34 | + |
| 35 | +// TestCreatePlanSingleScale creates a `single` deployment to test the creating of scaling plan. |
| 36 | +func TestCreatePlanSingleScale(t *testing.T) { |
| 37 | + log := zerolog.Nop() |
| 38 | + spec := api.DeploymentSpec{ |
| 39 | + Mode: api.DeploymentModeSingle, |
| 40 | + } |
| 41 | + spec.SetDefaults("test") |
| 42 | + |
| 43 | + // Test with empty status |
| 44 | + var status api.DeploymentStatus |
| 45 | + newPlan, changed := createPlan(log, nil, spec, status) |
| 46 | + assert.True(t, changed) |
| 47 | + assert.Len(t, newPlan, 0) // Single mode does not scale |
| 48 | + |
| 49 | + // Test with 1 single member |
| 50 | + status.Members.Single = api.MemberStatusList{ |
| 51 | + api.MemberStatus{ |
| 52 | + ID: "id", |
| 53 | + PodName: "something", |
| 54 | + }, |
| 55 | + } |
| 56 | + newPlan, changed = createPlan(log, nil, spec, status) |
| 57 | + assert.True(t, changed) |
| 58 | + assert.Len(t, newPlan, 0) // Single mode does not scale |
| 59 | + |
| 60 | + // Test with 2 single members (which should not happen) and try to scale down |
| 61 | + status.Members.Single = api.MemberStatusList{ |
| 62 | + api.MemberStatus{ |
| 63 | + ID: "id1", |
| 64 | + PodName: "something1", |
| 65 | + }, |
| 66 | + api.MemberStatus{ |
| 67 | + ID: "id1", |
| 68 | + PodName: "something1", |
| 69 | + }, |
| 70 | + } |
| 71 | + newPlan, changed = createPlan(log, nil, spec, status) |
| 72 | + assert.True(t, changed) |
| 73 | + assert.Len(t, newPlan, 0) // Single mode does not scale |
| 74 | +} |
| 75 | + |
| 76 | +// TestCreatePlanResilientSingleScale creates a `resilientsingle` deployment to test the creating of scaling plan. |
| 77 | +func TestCreatePlanResilientSingleScale(t *testing.T) { |
| 78 | + log := zerolog.Nop() |
| 79 | + spec := api.DeploymentSpec{ |
| 80 | + Mode: api.DeploymentModeResilientSingle, |
| 81 | + } |
| 82 | + spec.SetDefaults("test") |
| 83 | + spec.Single.Count = 2 |
| 84 | + |
| 85 | + // Test with empty status |
| 86 | + var status api.DeploymentStatus |
| 87 | + newPlan, changed := createPlan(log, nil, spec, status) |
| 88 | + assert.True(t, changed) |
| 89 | + require.Len(t, newPlan, 2) |
| 90 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[0].Type) |
| 91 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[1].Type) |
| 92 | + |
| 93 | + // Test with 1 single member |
| 94 | + status.Members.Single = api.MemberStatusList{ |
| 95 | + api.MemberStatus{ |
| 96 | + ID: "id", |
| 97 | + PodName: "something", |
| 98 | + }, |
| 99 | + } |
| 100 | + newPlan, changed = createPlan(log, nil, spec, status) |
| 101 | + assert.True(t, changed) |
| 102 | + require.Len(t, newPlan, 1) |
| 103 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[0].Type) |
| 104 | + assert.Equal(t, api.ServerGroupSingle, newPlan[0].Group) |
| 105 | + |
| 106 | + // Test scaling down from 4 members to 2 |
| 107 | + status.Members.Single = api.MemberStatusList{ |
| 108 | + api.MemberStatus{ |
| 109 | + ID: "id1", |
| 110 | + PodName: "something1", |
| 111 | + }, |
| 112 | + api.MemberStatus{ |
| 113 | + ID: "id2", |
| 114 | + PodName: "something2", |
| 115 | + }, |
| 116 | + api.MemberStatus{ |
| 117 | + ID: "id3", |
| 118 | + PodName: "something3", |
| 119 | + }, |
| 120 | + api.MemberStatus{ |
| 121 | + ID: "id4", |
| 122 | + PodName: "something4", |
| 123 | + }, |
| 124 | + } |
| 125 | + newPlan, changed = createPlan(log, nil, spec, status) |
| 126 | + assert.True(t, changed) |
| 127 | + require.Len(t, newPlan, 2) // Note: Downscaling is only down 1 at a time |
| 128 | + assert.Equal(t, api.ActionTypeShutdownMember, newPlan[0].Type) |
| 129 | + assert.Equal(t, api.ActionTypeRemoveMember, newPlan[1].Type) |
| 130 | + assert.Equal(t, api.ServerGroupSingle, newPlan[0].Group) |
| 131 | + assert.Equal(t, api.ServerGroupSingle, newPlan[1].Group) |
| 132 | +} |
| 133 | + |
| 134 | +// TestCreatePlanClusterScale creates a `cluster` deployment to test the creating of scaling plan. |
| 135 | +func TestCreatePlanClusterScale(t *testing.T) { |
| 136 | + log := zerolog.Nop() |
| 137 | + spec := api.DeploymentSpec{ |
| 138 | + Mode: api.DeploymentModeCluster, |
| 139 | + } |
| 140 | + spec.SetDefaults("test") |
| 141 | + |
| 142 | + // Test with empty status |
| 143 | + var status api.DeploymentStatus |
| 144 | + newPlan, changed := createPlan(log, nil, spec, status) |
| 145 | + assert.True(t, changed) |
| 146 | + require.Len(t, newPlan, 6) // Adding 3 dbservers & 3 coordinators (note: agents do not scale now) |
| 147 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[0].Type) |
| 148 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[1].Type) |
| 149 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[2].Type) |
| 150 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[3].Type) |
| 151 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[4].Type) |
| 152 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[5].Type) |
| 153 | + assert.Equal(t, api.ServerGroupDBServers, newPlan[0].Group) |
| 154 | + assert.Equal(t, api.ServerGroupDBServers, newPlan[1].Group) |
| 155 | + assert.Equal(t, api.ServerGroupDBServers, newPlan[2].Group) |
| 156 | + assert.Equal(t, api.ServerGroupCoordinators, newPlan[3].Group) |
| 157 | + assert.Equal(t, api.ServerGroupCoordinators, newPlan[4].Group) |
| 158 | + assert.Equal(t, api.ServerGroupCoordinators, newPlan[5].Group) |
| 159 | + |
| 160 | + // Test with 2 dbservers & 1 coordinator |
| 161 | + status.Members.DBServers = api.MemberStatusList{ |
| 162 | + api.MemberStatus{ |
| 163 | + ID: "db1", |
| 164 | + PodName: "something1", |
| 165 | + }, |
| 166 | + api.MemberStatus{ |
| 167 | + ID: "db2", |
| 168 | + PodName: "something2", |
| 169 | + }, |
| 170 | + } |
| 171 | + status.Members.Coordinators = api.MemberStatusList{ |
| 172 | + api.MemberStatus{ |
| 173 | + ID: "cr1", |
| 174 | + PodName: "coordinator1", |
| 175 | + }, |
| 176 | + } |
| 177 | + newPlan, changed = createPlan(log, nil, spec, status) |
| 178 | + assert.True(t, changed) |
| 179 | + require.Len(t, newPlan, 3) |
| 180 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[0].Type) |
| 181 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[1].Type) |
| 182 | + assert.Equal(t, api.ActionTypeAddMember, newPlan[2].Type) |
| 183 | + assert.Equal(t, api.ServerGroupDBServers, newPlan[0].Group) |
| 184 | + assert.Equal(t, api.ServerGroupCoordinators, newPlan[1].Group) |
| 185 | + assert.Equal(t, api.ServerGroupCoordinators, newPlan[2].Group) |
| 186 | + |
| 187 | + // Now scale down |
| 188 | + status.Members.DBServers = api.MemberStatusList{ |
| 189 | + api.MemberStatus{ |
| 190 | + ID: "db1", |
| 191 | + PodName: "something1", |
| 192 | + }, |
| 193 | + api.MemberStatus{ |
| 194 | + ID: "db2", |
| 195 | + PodName: "something2", |
| 196 | + }, |
| 197 | + api.MemberStatus{ |
| 198 | + ID: "db3", |
| 199 | + PodName: "something3", |
| 200 | + }, |
| 201 | + } |
| 202 | + status.Members.Coordinators = api.MemberStatusList{ |
| 203 | + api.MemberStatus{ |
| 204 | + ID: "cr1", |
| 205 | + PodName: "coordinator1", |
| 206 | + }, |
| 207 | + api.MemberStatus{ |
| 208 | + ID: "cr2", |
| 209 | + PodName: "coordinator2", |
| 210 | + }, |
| 211 | + } |
| 212 | + spec.DBServers.Count = 1 |
| 213 | + spec.Coordinators.Count = 1 |
| 214 | + newPlan, changed = createPlan(log, nil, spec, status) |
| 215 | + assert.True(t, changed) |
| 216 | + require.Len(t, newPlan, 5) // Note: Downscaling is done 1 at a time |
| 217 | + assert.Equal(t, api.ActionTypeCleanOutMember, newPlan[0].Type) |
| 218 | + assert.Equal(t, api.ActionTypeShutdownMember, newPlan[1].Type) |
| 219 | + assert.Equal(t, api.ActionTypeRemoveMember, newPlan[2].Type) |
| 220 | + assert.Equal(t, api.ActionTypeShutdownMember, newPlan[3].Type) |
| 221 | + assert.Equal(t, api.ActionTypeRemoveMember, newPlan[4].Type) |
| 222 | + assert.Equal(t, api.ServerGroupDBServers, newPlan[0].Group) |
| 223 | + assert.Equal(t, api.ServerGroupDBServers, newPlan[1].Group) |
| 224 | + assert.Equal(t, api.ServerGroupDBServers, newPlan[2].Group) |
| 225 | + assert.Equal(t, api.ServerGroupCoordinators, newPlan[3].Group) |
| 226 | + assert.Equal(t, api.ServerGroupCoordinators, newPlan[4].Group) |
| 227 | +} |
0 commit comments