Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding command line tests for arangod commandlines. #23

Merged
merged 5 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 28 additions & 11 deletions pkg/deployment/pod_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,36 @@ 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 {
Key string
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
Expand All @@ -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"},
Expand All @@ -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
Expand All @@ -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},
Expand All @@ -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"},
Expand Down Expand Up @@ -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"},
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
162 changes: 162 additions & 0 deletions pkg/deployment/pod_creator_agent_args_test.go
Original file line number Diff line number Diff line change
@@ -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,
)
}
}
Loading