Skip to content

Commit dd3f574

Browse files
authored
Merge pull request #32 from arangodb/test/errors
add some tests for util/k8sutil
2 parents b95a928 + 812f3ac commit dd3f574

File tree

5 files changed

+230
-1
lines changed

5 files changed

+230
-1
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ run-unit-tests: $(GOBUILDDIR) $(SOURCES)
172172
go test $(TESTVERBOSEOPTIONS) \
173173
$(REPOPATH)/pkg/apis/arangodb/v1alpha \
174174
$(REPOPATH)/pkg/deployment \
175-
$(REPOPATH)/pkg/util/k8sutil
175+
$(REPOPATH)/pkg/util/k8sutil \
176+
$(REPOPATH)/pkg/util/k8sutil/test
176177

177178
$(TESTBIN): $(GOBUILDDIR) $(SOURCES)
178179
@mkdir -p $(BINDIR)

pkg/util/k8sutil/errors_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 Jan Christoph Uhde
21+
//
22+
23+
package k8sutil
24+
25+
import (
26+
"os"
27+
"testing"
28+
29+
"github.com/stretchr/testify/assert"
30+
31+
apierrors "k8s.io/apimachinery/pkg/api/errors"
32+
"k8s.io/apimachinery/pkg/runtime/schema"
33+
"k8s.io/apimachinery/pkg/util/validation/field"
34+
)
35+
36+
var (
37+
conflictError = apierrors.NewConflict(schema.GroupResource{"groupName", "resourceName"}, "something", os.ErrInvalid)
38+
existsError = apierrors.NewAlreadyExists(schema.GroupResource{"groupName", "resourceName"}, "something")
39+
invalidError = apierrors.NewInvalid(schema.GroupKind{"groupName", "kindName"}, "something", field.ErrorList{})
40+
notFoundError = apierrors.NewNotFound(schema.GroupResource{"groupName", "resourceName"}, "something")
41+
)
42+
43+
func TestIsAlreadyExists(t *testing.T) {
44+
assert.False(t, IsAlreadyExists(conflictError))
45+
assert.False(t, IsAlreadyExists(maskAny(invalidError)))
46+
assert.True(t, IsAlreadyExists(existsError))
47+
assert.True(t, IsAlreadyExists(maskAny(existsError)))
48+
}
49+
50+
func TestIsConflict(t *testing.T) {
51+
assert.False(t, IsConflict(existsError))
52+
assert.False(t, IsConflict(maskAny(invalidError)))
53+
assert.True(t, IsConflict(conflictError))
54+
assert.True(t, IsConflict(maskAny(conflictError)))
55+
}
56+
57+
func TestIsNotFound(t *testing.T) {
58+
assert.False(t, IsNotFound(invalidError))
59+
assert.False(t, IsNotFound(maskAny(invalidError)))
60+
assert.True(t, IsNotFound(notFoundError))
61+
assert.True(t, IsNotFound(maskAny(notFoundError)))
62+
}

pkg/util/k8sutil/probes_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 Jan Christoph Uhde
21+
//
22+
23+
package k8sutil
24+
25+
import (
26+
"testing"
27+
28+
"github.com/stretchr/testify/assert"
29+
"k8s.io/api/core/v1"
30+
)
31+
32+
func TestCreate(t *testing.T) {
33+
path := "/api/version"
34+
secret := "the secret"
35+
36+
// http
37+
config := HTTPProbeConfig{path, false, secret}
38+
probe := config.Create()
39+
40+
assert.Equal(t, probe.InitialDelaySeconds, int32(30))
41+
assert.Equal(t, probe.TimeoutSeconds, int32(2))
42+
assert.Equal(t, probe.PeriodSeconds, int32(10))
43+
assert.Equal(t, probe.SuccessThreshold, int32(1))
44+
assert.Equal(t, probe.FailureThreshold, int32(3))
45+
46+
assert.Equal(t, probe.Handler.HTTPGet.Path, path)
47+
assert.Equal(t, probe.Handler.HTTPGet.HTTPHeaders[0].Name, "Authorization")
48+
assert.Equal(t, probe.Handler.HTTPGet.HTTPHeaders[0].Value, secret)
49+
assert.Equal(t, probe.Handler.HTTPGet.Port.IntValue(), 8529)
50+
assert.Equal(t, probe.Handler.HTTPGet.Scheme, v1.URISchemeHTTP)
51+
52+
// https
53+
config = HTTPProbeConfig{path, true, secret}
54+
probe = config.Create()
55+
56+
assert.Equal(t, probe.Handler.HTTPGet.Scheme, v1.URISchemeHTTPS)
57+
}

pkg/util/k8sutil/test/doc.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 Jan Christoph Uhde
21+
//
22+
23+
package test
24+
25+
// This package is used to resolve cyclic imports
26+
// between k8sutil and other packages that include
27+
// the k8sutil package. The other packages required
28+
// by the tests as they provide implementations for
29+
// interfaces defined by k8sutil that need to be tested.

pkg/util/k8sutil/test/events_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 Jan Christoph Uhde
21+
//
22+
23+
package test
24+
25+
import (
26+
"errors"
27+
"testing"
28+
29+
"github.com/stretchr/testify/assert"
30+
31+
"k8s.io/api/core/v1"
32+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
34+
api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha"
35+
"github.com/arangodb/k8s-operator/pkg/util/k8sutil"
36+
)
37+
38+
var apiObjectForTest = api.ArangoDeployment{
39+
ObjectMeta: metav1.ObjectMeta{
40+
Name: "Willy",
41+
Namespace: "Wonka",
42+
},
43+
Spec: api.DeploymentSpec{
44+
Mode: api.DeploymentModeCluster,
45+
},
46+
}
47+
48+
func TestMemberAddEvent(t *testing.T) {
49+
event := k8sutil.NewMemberAddEvent("name", "role", &apiObjectForTest)
50+
assert.Equal(t, event.Reason, "New Role Added")
51+
assert.Equal(t, event.Message, "New role name added to deployment")
52+
assert.Equal(t, event.Type, v1.EventTypeNormal)
53+
}
54+
55+
func TestMemberRemoveEvent(t *testing.T) {
56+
event := k8sutil.NewMemberRemoveEvent("name", "role", &apiObjectForTest)
57+
assert.Equal(t, event.Reason, "Role Removed")
58+
assert.Equal(t, event.Message, "Existing role name removed from the deployment")
59+
assert.Equal(t, event.Type, v1.EventTypeNormal)
60+
}
61+
62+
func TestPodGoneEvent(t *testing.T) {
63+
event := k8sutil.NewPodGoneEvent("name", "role", &apiObjectForTest)
64+
assert.Equal(t, event.Reason, "Pod Of Role Gone")
65+
assert.Equal(t, event.Message, "Pod name of member role is gone")
66+
assert.Equal(t, event.Type, v1.EventTypeNormal)
67+
}
68+
69+
func TestImmutableFieldEvent(t *testing.T) {
70+
event := k8sutil.NewImmutableFieldEvent("name", &apiObjectForTest)
71+
assert.Equal(t, event.Reason, "Immutable Field Change")
72+
assert.Equal(t, event.Message, "Changing field name is not possible. It has been reset to its original value.")
73+
assert.Equal(t, event.Type, v1.EventTypeNormal)
74+
}
75+
76+
func TestErrorEvent(t *testing.T) {
77+
event := k8sutil.NewErrorEvent("reason", errors.New("something"), &apiObjectForTest)
78+
assert.Equal(t, event.Reason, "Reason")
79+
assert.Equal(t, event.Type, v1.EventTypeWarning)
80+
}

0 commit comments

Comments
 (0)