diff --git a/Makefile b/Makefile index f3efa95e0..54cf1c82e 100644 --- a/Makefile +++ b/Makefile @@ -171,7 +171,8 @@ run-unit-tests: $(GOBUILDDIR) $(SOURCES) go test $(TESTVERBOSEOPTIONS) \ $(REPOPATH)/pkg/apis/arangodb/v1alpha \ $(REPOPATH)/pkg/deployment \ - $(REPOPATH)/pkg/util/k8sutil + $(REPOPATH)/pkg/util/k8sutil \ + $(REPOPATH)/pkg/util/k8sutil/test $(TESTBIN): $(GOBUILDDIR) $(SOURCES) @mkdir -p $(BINDIR) diff --git a/pkg/util/k8sutil/errors_test.go b/pkg/util/k8sutil/errors_test.go new file mode 100644 index 000000000..54c3e474f --- /dev/null +++ b/pkg/util/k8sutil/errors_test.go @@ -0,0 +1,62 @@ +// +// 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 Jan Christoph Uhde +// + +package k8sutil + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/util/validation/field" +) + +var ( + conflictError = apierrors.NewConflict(schema.GroupResource{"groupName", "resourceName"}, "something", os.ErrInvalid) + existsError = apierrors.NewAlreadyExists(schema.GroupResource{"groupName", "resourceName"}, "something") + invalidError = apierrors.NewInvalid(schema.GroupKind{"groupName", "kindName"}, "something", field.ErrorList{}) + notFoundError = apierrors.NewNotFound(schema.GroupResource{"groupName", "resourceName"}, "something") +) + +func TestIsAlreadyExists(t *testing.T) { + assert.False(t, IsAlreadyExists(conflictError)) + assert.False(t, IsAlreadyExists(maskAny(invalidError))) + assert.True(t, IsAlreadyExists(existsError)) + assert.True(t, IsAlreadyExists(maskAny(existsError))) +} + +func TestIsConflict(t *testing.T) { + assert.False(t, IsConflict(existsError)) + assert.False(t, IsConflict(maskAny(invalidError))) + assert.True(t, IsConflict(conflictError)) + assert.True(t, IsConflict(maskAny(conflictError))) +} + +func TestIsNotFound(t *testing.T) { + assert.False(t, IsNotFound(invalidError)) + assert.False(t, IsNotFound(maskAny(invalidError))) + assert.True(t, IsNotFound(notFoundError)) + assert.True(t, IsNotFound(maskAny(notFoundError))) +} diff --git a/pkg/util/k8sutil/probes_test.go b/pkg/util/k8sutil/probes_test.go new file mode 100644 index 000000000..d3302509d --- /dev/null +++ b/pkg/util/k8sutil/probes_test.go @@ -0,0 +1,57 @@ +// +// 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 Jan Christoph Uhde +// + +package k8sutil + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "k8s.io/api/core/v1" +) + +func TestCreate(t *testing.T) { + path := "/api/version" + secret := "the secret" + + // http + config := HTTPProbeConfig{path, false, secret} + probe := config.Create() + + assert.Equal(t, probe.InitialDelaySeconds, int32(30)) + assert.Equal(t, probe.TimeoutSeconds, int32(2)) + assert.Equal(t, probe.PeriodSeconds, int32(10)) + assert.Equal(t, probe.SuccessThreshold, int32(1)) + assert.Equal(t, probe.FailureThreshold, int32(3)) + + assert.Equal(t, probe.Handler.HTTPGet.Path, path) + assert.Equal(t, probe.Handler.HTTPGet.HTTPHeaders[0].Name, "Authorization") + assert.Equal(t, probe.Handler.HTTPGet.HTTPHeaders[0].Value, secret) + assert.Equal(t, probe.Handler.HTTPGet.Port.IntValue(), 8529) + assert.Equal(t, probe.Handler.HTTPGet.Scheme, v1.URISchemeHTTP) + + // https + config = HTTPProbeConfig{path, true, secret} + probe = config.Create() + + assert.Equal(t, probe.Handler.HTTPGet.Scheme, v1.URISchemeHTTPS) +} diff --git a/pkg/util/k8sutil/test/doc.go b/pkg/util/k8sutil/test/doc.go new file mode 100644 index 000000000..e5c774f9b --- /dev/null +++ b/pkg/util/k8sutil/test/doc.go @@ -0,0 +1,29 @@ +// +// 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 Jan Christoph Uhde +// + +package test + +// This package is used to resolve cyclic imports +// between k8sutil and other packages that include +// the k8sutil package. The other packages required +// by the tests as they provide implementations for +// interfaces defined by k8sutil that need to be tested. diff --git a/pkg/util/k8sutil/test/events_test.go b/pkg/util/k8sutil/test/events_test.go new file mode 100644 index 000000000..8aba4097d --- /dev/null +++ b/pkg/util/k8sutil/test/events_test.go @@ -0,0 +1,80 @@ +// +// 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 Jan Christoph Uhde +// + +package test + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" + + "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha" + "github.com/arangodb/k8s-operator/pkg/util/k8sutil" +) + +var apiObjectForTest = api.ArangoDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "Willy", + Namespace: "Wonka", + }, + Spec: api.DeploymentSpec{ + Mode: api.DeploymentModeCluster, + }, +} + +func TestMemberAddEvent(t *testing.T) { + event := k8sutil.NewMemberAddEvent("name", "role", &apiObjectForTest) + assert.Equal(t, event.Reason, "New Role Added") + assert.Equal(t, event.Message, "New role name added to deployment") + assert.Equal(t, event.Type, v1.EventTypeNormal) +} + +func TestMemberRemoveEvent(t *testing.T) { + event := k8sutil.NewMemberRemoveEvent("name", "role", &apiObjectForTest) + assert.Equal(t, event.Reason, "Role Removed") + assert.Equal(t, event.Message, "Existing role name removed from the deployment") + assert.Equal(t, event.Type, v1.EventTypeNormal) +} + +func TestPodGoneEvent(t *testing.T) { + event := k8sutil.NewPodGoneEvent("name", "role", &apiObjectForTest) + assert.Equal(t, event.Reason, "Pod Of Role Gone") + assert.Equal(t, event.Message, "Pod name of member role is gone") + assert.Equal(t, event.Type, v1.EventTypeNormal) +} + +func TestImmutableFieldEvent(t *testing.T) { + event := k8sutil.NewImmutableFieldEvent("name", &apiObjectForTest) + assert.Equal(t, event.Reason, "Immutable Field Change") + assert.Equal(t, event.Message, "Changing field name is not possible. It has been reset to its original value.") + assert.Equal(t, event.Type, v1.EventTypeNormal) +} + +func TestErrorEvent(t *testing.T) { + event := k8sutil.NewErrorEvent("reason", errors.New("something"), &apiObjectForTest) + assert.Equal(t, event.Reason, "Reason") + assert.Equal(t, event.Type, v1.EventTypeWarning) +}