diff --git a/Makefile b/Makefile index a4b32ce3f..2f57c880d 100644 --- a/Makefile +++ b/Makefile @@ -167,6 +167,7 @@ run-unit-tests: $(GOBUILDDIR) $(SOURCES) golang:$(GOVERSION) \ go test -v \ $(REPOPATH)/pkg/apis/arangodb/v1alpha \ + $(REPOPATH)/pkg/deployment \ $(REPOPATH)/pkg/util/k8sutil $(TESTBIN): $(GOBUILDDIR) $(SOURCES) diff --git a/pkg/deployment/pod_creator.go b/pkg/deployment/pod_creator.go index 5a66e60ca..71420c33a 100644 --- a/pkg/deployment/pod_creator.go +++ b/pkg/deployment/pod_creator.go @@ -26,13 +26,16 @@ import ( "fmt" "net" "path/filepath" + "sort" "strconv" + "strings" api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha" "github.com/arangodb/k8s-operator/pkg/util/arangod" "github.com/arangodb/k8s-operator/pkg/util/constants" "github.com/arangodb/k8s-operator/pkg/util/k8sutil" "github.com/pkg/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type optionPair struct { @@ -40,8 +43,19 @@ type optionPair struct { Value string } +// CompareTo returns -1 if o < other, 0 if o == other, 1 otherwise +func (o optionPair) CompareTo(other optionPair) int { + rc := strings.Compare(o.Key, other.Key) + if rc < 0 { + return -1 + } else if rc > 0 { + return 1 + } + return strings.Compare(o.Value, other.Value) +} + // createArangodArgs creates command line arguments for an arangod server in the given group. -func (d *Deployment) createArangodArgs(apiObject *api.ArangoDeployment, group api.ServerGroup, spec api.ServerGroupSpec, agents api.MemberStatusList, id string) []string { +func createArangodArgs(apiObject metav1.Object, deplSpec api.DeploymentSpec, group api.ServerGroup, svrSpec api.ServerGroupSpec, agents api.MemberStatusList, id string) []string { options := make([]optionPair, 0, 64) // Endpoint @@ -56,7 +70,7 @@ func (d *Deployment) createArangodArgs(apiObject *api.ArangoDeployment, group ap ) // Authentication - if apiObject.Spec.IsAuthenticated() { + if deplSpec.IsAuthenticated() { // With authentication options = append(options, optionPair{"--server.authentication", "true"}, @@ -71,7 +85,7 @@ func (d *Deployment) createArangodArgs(apiObject *api.ArangoDeployment, group ap // Storage engine options = append(options, - optionPair{"--server.storage-engine", string(apiObject.Spec.StorageEngine)}, + optionPair{"--server.storage-engine", string(deplSpec.StorageEngine)}, ) // Logging @@ -94,7 +108,7 @@ func (d *Deployment) createArangodArgs(apiObject *api.ArangoDeployment, group ap }*/ // RocksDB - if apiObject.Spec.RocksDB.IsEncrypted() { + if deplSpec.RocksDB.IsEncrypted() { keyPath := filepath.Join(k8sutil.RocksDBEncryptionVolumeMountDir, "key") options = append(options, optionPair{"--rocksdb.encryption-keyfile", keyPath}, @@ -121,7 +135,7 @@ func (d *Deployment) createArangodArgs(apiObject *api.ArangoDeployment, group ap optionPair{"--cluster.my-id", id}, optionPair{"--agency.activate", "true"}, optionPair{"--agency.my-address", myTCPURL}, - optionPair{"--agency.size", strconv.Itoa(apiObject.Spec.Agents.Count)}, + optionPair{"--agency.size", strconv.Itoa(deplSpec.Agents.Count)}, optionPair{"--agency.supervision", "true"}, optionPair{"--foxx.queues", "false"}, optionPair{"--server.statistics", "false"}, @@ -162,7 +176,7 @@ func (d *Deployment) createArangodArgs(apiObject *api.ArangoDeployment, group ap optionPair{"--foxx.queues", "true"}, optionPair{"--server.statistics", "true"}, ) - if apiObject.Spec.Mode == api.DeploymentModeResilientSingle { + if deplSpec.Mode == api.DeploymentModeResilientSingle { addAgentEndpoints = true options = append(options, optionPair{"--replication.automatic-failover", "true"}, @@ -182,17 +196,20 @@ func (d *Deployment) createArangodArgs(apiObject *api.ArangoDeployment, group ap } } - args := make([]string, 0, len(options)+len(spec.Args)) + args := make([]string, 0, len(options)+len(svrSpec.Args)) + sort.Slice(options, func(i, j int) bool { + return options[i].CompareTo(options[j]) < 0 + }) for _, o := range options { args = append(args, o.Key+"="+o.Value) } - args = append(args, spec.Args...) + args = append(args, svrSpec.Args...) return args } // createArangoSyncArgs creates command line arguments for an arangosync server in the given group. -func (d *Deployment) createArangoSyncArgs(apiObject *api.ArangoDeployment, group api.ServerGroup, spec api.ServerGroupSpec, agents api.MemberStatusList, id string) []string { +func createArangoSyncArgs(apiObject *api.ArangoDeployment, group api.ServerGroup, spec api.ServerGroupSpec, agents api.MemberStatusList, id string) []string { // TODO return nil } @@ -291,7 +308,7 @@ func (d *Deployment) ensurePods(apiObject *api.ArangoDeployment) error { // Create pod role := group.AsRole() if group.IsArangod() { - args := d.createArangodArgs(apiObject, group, spec, d.status.Members.Agents, m.ID) + args := createArangodArgs(apiObject, apiObject.Spec, group, spec, d.status.Members.Agents, m.ID) env := make(map[string]k8sutil.EnvValue) livenessProbe, err := d.createLivenessProbe(apiObject, group) if err != nil { @@ -318,7 +335,7 @@ func (d *Deployment) ensurePods(apiObject *api.ArangoDeployment) error { return maskAny(err) } } else if group.IsArangosync() { - args := d.createArangoSyncArgs(apiObject, group, spec, d.status.Members.Agents, m.ID) + args := createArangoSyncArgs(apiObject, group, spec, d.status.Members.Agents, m.ID) env := make(map[string]k8sutil.EnvValue) livenessProbe, err := d.createLivenessProbe(apiObject, group) if err != nil { diff --git a/pkg/deployment/pod_creator_agent_args_test.go b/pkg/deployment/pod_creator_agent_args_test.go new file mode 100644 index 000000000..dace0e19d --- /dev/null +++ b/pkg/deployment/pod_creator_agent_args_test.go @@ -0,0 +1,162 @@ +// +// DISCLAIMER +// +// Copyright 2018 ArangoDB GmbH, Cologne, Germany +// +// 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 +// +// Author Ewout Prangsma +// + +package deployment + +import ( + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha" +) + +// TestCreateArangodArgsAgent tests createArangodArgs for agent. +func TestCreateArangodArgsAgent(t *testing.T) { + // Default deployment + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, + } + apiObject.Spec.SetDefaults("test") + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupAgents, apiObject.Spec.Agents, agents, "a1") + assert.Equal(t, + []string{ + "--agency.activate=true", + "--agency.endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--agency.endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--agency.my-address=tcp://name-agent-a1.name-int.ns.svc:8529", + "--agency.size=3", + "--agency.supervision=true", + "--cluster.my-id=a1", + "--database.directory=/data", + "--foxx.queues=false", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=false", + "--server.storage-engine=rocksdb", + }, + cmdline, + ) + } + + // No authentication, mmfiles + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, + } + apiObject.Spec.SetDefaults("test") + apiObject.Spec.Authentication.JWTSecretName = "None" + apiObject.Spec.StorageEngine = api.StorageEngineMMFiles + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupAgents, apiObject.Spec.Agents, agents, "a1") + assert.Equal(t, + []string{ + "--agency.activate=true", + "--agency.endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--agency.endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--agency.my-address=tcp://name-agent-a1.name-int.ns.svc:8529", + "--agency.size=3", + "--agency.supervision=true", + "--cluster.my-id=a1", + "--database.directory=/data", + "--foxx.queues=false", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=false", + "--server.endpoint=tcp://[::]:8529", + "--server.statistics=false", + "--server.storage-engine=mmfiles", + }, + cmdline, + ) + } + + // Custom args + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, + } + apiObject.Spec.SetDefaults("test") + apiObject.Spec.Agents.Args = []string{"--foo1", "--foo2"} + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupAgents, apiObject.Spec.Agents, agents, "a1") + assert.Equal(t, + []string{ + "--agency.activate=true", + "--agency.endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--agency.endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--agency.my-address=tcp://name-agent-a1.name-int.ns.svc:8529", + "--agency.size=3", + "--agency.supervision=true", + "--cluster.my-id=a1", + "--database.directory=/data", + "--foxx.queues=false", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=false", + "--server.storage-engine=rocksdb", + "--foo1", + "--foo2", + }, + cmdline, + ) + } +} diff --git a/pkg/deployment/pod_creator_coordinator_args_test.go b/pkg/deployment/pod_creator_coordinator_args_test.go new file mode 100644 index 000000000..d9cd5406e --- /dev/null +++ b/pkg/deployment/pod_creator_coordinator_args_test.go @@ -0,0 +1,159 @@ +// +// DISCLAIMER +// +// Copyright 2018 ArangoDB GmbH, Cologne, Germany +// +// 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 +// +// Author Ewout Prangsma +// + +package deployment + +import ( + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha" +) + +// TestCreateArangodArgsCoordinator tests createArangodArgs for coordinator. +func TestCreateArangodArgsCoordinator(t *testing.T) { + // Default deployment + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, + } + apiObject.Spec.SetDefaults("test") + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupCoordinators, apiObject.Spec.Coordinators, agents, "id1") + assert.Equal(t, + []string{ + "--cluster.agency-endpoint=tcp://name-agent-a1.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--cluster.my-address=tcp://name-coordinator-id1.name-int.ns.svc:8529", + "--cluster.my-id=id1", + "--cluster.my-role=COORDINATOR", + "--database.directory=/data", + "--foxx.queues=true", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=true", + "--server.storage-engine=rocksdb", + }, + cmdline, + ) + } + + // No authentication + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, + } + apiObject.Spec.SetDefaults("test") + apiObject.Spec.Authentication.JWTSecretName = "None" + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupCoordinators, apiObject.Spec.Coordinators, agents, "id1") + assert.Equal(t, + []string{ + "--cluster.agency-endpoint=tcp://name-agent-a1.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--cluster.my-address=tcp://name-coordinator-id1.name-int.ns.svc:8529", + "--cluster.my-id=id1", + "--cluster.my-role=COORDINATOR", + "--database.directory=/data", + "--foxx.queues=true", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=false", + "--server.endpoint=tcp://[::]:8529", + "--server.statistics=true", + "--server.storage-engine=rocksdb", + }, + cmdline, + ) + } + + // Custom args, RocksDB + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, + } + apiObject.Spec.SetDefaults("test") + apiObject.Spec.Coordinators.Args = []string{"--foo1", "--foo2"} + apiObject.Spec.StorageEngine = api.StorageEngineMMFiles + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupCoordinators, apiObject.Spec.Coordinators, agents, "id1") + assert.Equal(t, + []string{ + "--cluster.agency-endpoint=tcp://name-agent-a1.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--cluster.my-address=tcp://name-coordinator-id1.name-int.ns.svc:8529", + "--cluster.my-id=id1", + "--cluster.my-role=COORDINATOR", + "--database.directory=/data", + "--foxx.queues=true", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=true", + "--server.storage-engine=mmfiles", + "--foo1", + "--foo2", + }, + cmdline, + ) + } +} diff --git a/pkg/deployment/pod_creator_dbserver_args_test.go b/pkg/deployment/pod_creator_dbserver_args_test.go new file mode 100644 index 000000000..e8ca64f84 --- /dev/null +++ b/pkg/deployment/pod_creator_dbserver_args_test.go @@ -0,0 +1,159 @@ +// +// DISCLAIMER +// +// Copyright 2018 ArangoDB GmbH, Cologne, Germany +// +// 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 +// +// Author Ewout Prangsma +// + +package deployment + +import ( + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha" +) + +// TestCreateArangodArgsDBServer tests createArangodArgs for dbserver. +func TestCreateArangodArgsDBServer(t *testing.T) { + // Default deployment + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, + } + apiObject.Spec.SetDefaults("test") + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupDBServers, apiObject.Spec.DBServers, agents, "id1") + assert.Equal(t, + []string{ + "--cluster.agency-endpoint=tcp://name-agent-a1.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--cluster.my-address=tcp://name-dbserver-id1.name-int.ns.svc:8529", + "--cluster.my-id=id1", + "--cluster.my-role=PRIMARY", + "--database.directory=/data", + "--foxx.queues=false", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=true", + "--server.storage-engine=rocksdb", + }, + cmdline, + ) + } + + // No authentication + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, + } + apiObject.Spec.SetDefaults("test") + apiObject.Spec.Authentication.JWTSecretName = "None" + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupDBServers, apiObject.Spec.DBServers, agents, "id1") + assert.Equal(t, + []string{ + "--cluster.agency-endpoint=tcp://name-agent-a1.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--cluster.my-address=tcp://name-dbserver-id1.name-int.ns.svc:8529", + "--cluster.my-id=id1", + "--cluster.my-role=PRIMARY", + "--database.directory=/data", + "--foxx.queues=false", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=false", + "--server.endpoint=tcp://[::]:8529", + "--server.statistics=true", + "--server.storage-engine=rocksdb", + }, + cmdline, + ) + } + + // Custom args, MMFiles + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, + } + apiObject.Spec.SetDefaults("test") + apiObject.Spec.StorageEngine = api.StorageEngineMMFiles + apiObject.Spec.DBServers.Args = []string{"--foo1", "--foo2"} + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupDBServers, apiObject.Spec.DBServers, agents, "id1") + assert.Equal(t, + []string{ + "--cluster.agency-endpoint=tcp://name-agent-a1.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--cluster.my-address=tcp://name-dbserver-id1.name-int.ns.svc:8529", + "--cluster.my-id=id1", + "--cluster.my-role=PRIMARY", + "--database.directory=/data", + "--foxx.queues=false", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=true", + "--server.storage-engine=mmfiles", + "--foo1", + "--foo2", + }, + cmdline, + ) + } +} diff --git a/pkg/deployment/pod_creator_single_args_test.go b/pkg/deployment/pod_creator_single_args_test.go new file mode 100644 index 000000000..a02b04ee0 --- /dev/null +++ b/pkg/deployment/pod_creator_single_args_test.go @@ -0,0 +1,180 @@ +// +// DISCLAIMER +// +// Copyright 2018 ArangoDB GmbH, Cologne, Germany +// +// 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 +// +// Author Ewout Prangsma +// + +package deployment + +import ( + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha" + "github.com/stretchr/testify/assert" +) + +// TestCreateArangodArgsSingle tests createArangodArgs for single server. +func TestCreateArangodArgsSingle(t *testing.T) { + // Default deployment + { + apiObject := &api.ArangoDeployment{ + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeSingle, + }, + } + apiObject.Spec.SetDefaults("test") + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupSingle, apiObject.Spec.Single, nil, "id1") + assert.Equal(t, + []string{ + "--database.directory=/data", + "--foxx.queues=true", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=true", + "--server.storage-engine=rocksdb", + }, + cmdline, + ) + } + + // Default deployment with mmfiles + { + apiObject := &api.ArangoDeployment{ + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeSingle, + StorageEngine: api.StorageEngineMMFiles, + }, + } + apiObject.Spec.SetDefaults("test") + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupSingle, apiObject.Spec.Single, nil, "id1") + assert.Equal(t, + []string{ + "--database.directory=/data", + "--foxx.queues=true", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=true", + "--server.storage-engine=mmfiles", + }, + cmdline, + ) + } + + // No authentication + { + apiObject := &api.ArangoDeployment{ + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeSingle, + }, + } + apiObject.Spec.Authentication.JWTSecretName = "None" + apiObject.Spec.SetDefaults("test") + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupSingle, apiObject.Spec.Single, nil, "id1") + assert.Equal(t, + []string{ + "--database.directory=/data", + "--foxx.queues=true", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=false", + "--server.endpoint=tcp://[::]:8529", + "--server.statistics=true", + "--server.storage-engine=rocksdb", + }, + cmdline, + ) + } + + // Custom args + { + apiObject := &api.ArangoDeployment{ + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeSingle, + }, + } + apiObject.Spec.Single.Args = []string{"--foo1", "--foo2"} + apiObject.Spec.SetDefaults("test") + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupSingle, apiObject.Spec.Single, nil, "id1") + assert.Equal(t, + []string{ + "--database.directory=/data", + "--foxx.queues=true", + "--log.level=INFO", + "--log.output=+", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=true", + "--server.storage-engine=rocksdb", + "--foo1", + "--foo2", + }, + cmdline, + ) + } + + // Resilient single + { + apiObject := &api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeResilientSingle, + }, + } + apiObject.Spec.SetDefaults("test") + agents := api.MemberStatusList{ + api.MemberStatus{ID: "a1"}, + api.MemberStatus{ID: "a2"}, + api.MemberStatus{ID: "a3"}, + } + cmdline := createArangodArgs(apiObject, apiObject.Spec, api.ServerGroupSingle, apiObject.Spec.Single, agents, "id1") + assert.Equal(t, + []string{ + "--cluster.agency-endpoint=tcp://name-agent-a1.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a2.name-int.ns.svc:8529", + "--cluster.agency-endpoint=tcp://name-agent-a3.name-int.ns.svc:8529", + "--cluster.my-address=tcp://name-single-id1.name-int.ns.svc:8529", + "--cluster.my-id=id1", + "--cluster.my-role=SINGLE", + "--database.directory=/data", + "--foxx.queues=true", + "--log.level=INFO", + "--log.output=+", + "--replication.automatic-failover=true", + "--server.authentication=true", + "--server.endpoint=tcp://[::]:8529", + "--server.jwt-secret=$(ARANGOD_JWT_SECRET)", + "--server.statistics=true", + "--server.storage-engine=rocksdb", + }, + cmdline, + ) + } +}