|
| 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 tests |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "testing" |
| 29 | + "time" |
| 30 | + |
| 31 | + "github.com/dchest/uniuri" |
| 32 | + "github.com/stretchr/testify/assert" |
| 33 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 34 | + |
| 35 | + driver "github.com/arangodb/go-driver" |
| 36 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha" |
| 37 | + "github.com/arangodb/kube-arangodb/pkg/client" |
| 38 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 39 | +) |
| 40 | + |
| 41 | +// TestChangeArgsAgents tests the creating of an active failover deployment |
| 42 | +// with default settings and once ready changes the arguments of the agents. |
| 43 | +func TestChangeArgsAgents(t *testing.T) { |
| 44 | + c := client.MustNewInCluster() |
| 45 | + kubecli := mustNewKubeClient(t) |
| 46 | + ns := getNamespace(t) |
| 47 | + |
| 48 | + // Prepare deployment config |
| 49 | + depl := newDeployment("test-chga-rs-" + uniuri.NewLen(4)) |
| 50 | + depl.Spec.Mode = api.NewMode(api.DeploymentModeActiveFailover) |
| 51 | + |
| 52 | + // Create deployment |
| 53 | + _, err := c.DatabaseV1alpha().ArangoDeployments(ns).Create(depl) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("Create deployment failed: %v", err) |
| 56 | + } |
| 57 | + // Prepare cleanup |
| 58 | + defer removeDeployment(c, depl.GetName(), ns) |
| 59 | + |
| 60 | + // Wait for deployment to be ready |
| 61 | + apiObject, err := waitUntilDeployment(c, depl.GetName(), ns, deploymentIsReady()) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("Deployment not running in time: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + // Create a database client |
| 67 | + ctx := context.Background() |
| 68 | + client := mustNewArangodDatabaseClient(ctx, kubecli, apiObject, t) |
| 69 | + |
| 70 | + // Wait for single server available |
| 71 | + if err := waitUntilVersionUp(client, nil); err != nil { |
| 72 | + t.Fatalf("ActiveFailover servers not running returning version in time: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + // Check server role |
| 76 | + assert.NoError(t, testServerRole(ctx, client, driver.ServerRoleSingleActive)) |
| 77 | + |
| 78 | + // Now change agent arguments |
| 79 | + if _, err := updateDeployment(c, depl.GetName(), ns, func(spec *api.DeploymentSpec) { |
| 80 | + spec.Agents.Args = []string{"--log.level=DEBUG"} |
| 81 | + }); err != nil { |
| 82 | + t.Fatalf("Failed to update deployment: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + // Wait until all agents have the right arguments |
| 86 | + if _, err := waitUntilDeployment(c, depl.GetName(), ns, func(d *api.ArangoDeployment) error { |
| 87 | + members := d.Status.Members |
| 88 | + if len(members.Agents) != 3 { |
| 89 | + return fmt.Errorf("Expected 3 agents, got %d", len(members.Agents)) |
| 90 | + } |
| 91 | + pods := kubecli.CoreV1().Pods(ns) |
| 92 | + for _, m := range members.Agents { |
| 93 | + pod, err := pods.Get(m.PodName, metav1.GetOptions{}) |
| 94 | + if err != nil { |
| 95 | + return maskAny(err) |
| 96 | + } |
| 97 | + found := false |
| 98 | + for _, c := range pod.Spec.Containers { |
| 99 | + if c.Name != k8sutil.ServerContainerName { |
| 100 | + continue |
| 101 | + } |
| 102 | + // Check command |
| 103 | + for _, a := range append(c.Args, c.Command...) { |
| 104 | + if a == "--log.level=DEBUG" { |
| 105 | + found = true |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + if !found { |
| 110 | + return fmt.Errorf("Did not find new argument") |
| 111 | + } |
| 112 | + } |
| 113 | + return nil |
| 114 | + }, time.Minute*10); err != nil { |
| 115 | + t.Fatalf("Deployment not updated in time: %v", err) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// TestChangeArgsDBServer tests the creating of a cluster deployment |
| 120 | +// with default settings and once ready changes the arguments of the dbservers. |
| 121 | +func TestChangeArgsDBServer(t *testing.T) { |
| 122 | + c := client.MustNewInCluster() |
| 123 | + kubecli := mustNewKubeClient(t) |
| 124 | + ns := getNamespace(t) |
| 125 | + |
| 126 | + // Prepare deployment config |
| 127 | + depl := newDeployment("test-chga-db-" + uniuri.NewLen(4)) |
| 128 | + depl.Spec.Mode = api.NewMode(api.DeploymentModeCluster) |
| 129 | + |
| 130 | + // Create deployment |
| 131 | + _, err := c.DatabaseV1alpha().ArangoDeployments(ns).Create(depl) |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("Create deployment failed: %v", err) |
| 134 | + } |
| 135 | + // Prepare cleanup |
| 136 | + defer removeDeployment(c, depl.GetName(), ns) |
| 137 | + |
| 138 | + // Wait for deployment to be ready |
| 139 | + apiObject, err := waitUntilDeployment(c, depl.GetName(), ns, deploymentIsReady()) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("Deployment not running in time: %v", err) |
| 142 | + } |
| 143 | + |
| 144 | + // Create a database client |
| 145 | + ctx := context.Background() |
| 146 | + client := mustNewArangodDatabaseClient(ctx, kubecli, apiObject, t) |
| 147 | + |
| 148 | + // Wait for cluster available |
| 149 | + if err := waitUntilVersionUp(client, nil); err != nil { |
| 150 | + t.Fatalf("Cluster servers not running returning version in time: %v", err) |
| 151 | + } |
| 152 | + |
| 153 | + // Now change agent arguments |
| 154 | + if _, err := updateDeployment(c, depl.GetName(), ns, func(spec *api.DeploymentSpec) { |
| 155 | + spec.DBServers.Args = []string{"--log.level=DEBUG"} |
| 156 | + }); err != nil { |
| 157 | + t.Fatalf("Failed to update deployment: %v", err) |
| 158 | + } |
| 159 | + |
| 160 | + // Wait until all dbservers have the right arguments |
| 161 | + if _, err := waitUntilDeployment(c, depl.GetName(), ns, func(d *api.ArangoDeployment) error { |
| 162 | + members := d.Status.Members |
| 163 | + if len(members.DBServers) != 3 { |
| 164 | + return fmt.Errorf("Expected 3 dbservers, got %d", len(members.DBServers)) |
| 165 | + } |
| 166 | + pods := kubecli.CoreV1().Pods(ns) |
| 167 | + for _, m := range members.DBServers { |
| 168 | + pod, err := pods.Get(m.PodName, metav1.GetOptions{}) |
| 169 | + if err != nil { |
| 170 | + return maskAny(err) |
| 171 | + } |
| 172 | + found := false |
| 173 | + for _, c := range pod.Spec.Containers { |
| 174 | + if c.Name != k8sutil.ServerContainerName { |
| 175 | + continue |
| 176 | + } |
| 177 | + // Check command |
| 178 | + for _, a := range append(c.Args, c.Command...) { |
| 179 | + if a == "--log.level=DEBUG" { |
| 180 | + found = true |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + if !found { |
| 185 | + return fmt.Errorf("Did not find new argument") |
| 186 | + } |
| 187 | + } |
| 188 | + return nil |
| 189 | + }, time.Minute*10); err != nil { |
| 190 | + t.Fatalf("Deployment not updated in time: %v", err) |
| 191 | + } |
| 192 | +} |
0 commit comments