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

add some tests for util/k8sutil/erros.go #32

Merged
merged 5 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions pkg/util/k8sutil/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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"
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests for masked errors. (assert.X(t, maskAny(...)))

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.True(t, IsAlreadyExists(existsError))
}

func TestIsConflict(t *testing.T) {
assert.False(t, IsConflict(existsError))
assert.True(t, IsConflict(conflictError))
}

func TestIsNotFound(t *testing.T) {
assert.False(t, IsNotFound(invalidError))
assert.True(t, IsNotFound(notFoundError))
}
35 changes: 35 additions & 0 deletions pkg/util/k8sutil/probes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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)
}
64 changes: 64 additions & 0 deletions pkg/util/k8sutil/test/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package k8sutil_test
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this package name ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where should I state the reason (cyclic import) that led to package creation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Package name should be test since the folder is named that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a doc.go with only a header, package test followed by a comment describing about the cycle import.


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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

role is uppercase in reason but not in message

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of strings.Title seems to be a bit inconsistent.

In the following snipped it is used in the middle of the string in one case and in the other case it is not:

 62 // NewPodGoneEvent creates an event indicating that a pod is missing
 63 func NewPodGoneEvent(podName, role string, apiObject APIObject) *v1.Event {
 64     event := newDeploymentEvent(apiObject)
 65     event.Type = v1.EventTypeNormal
 66     event.Reason = fmt.Sprintf("Pod Of %s Gone", strings.Title(role))
 67     event.Message = fmt.Sprintf("Pod %s of member %s is gone", podName, role)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm following the etcd-operator on this

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)
}

// // not accessible outside the package
// func TestDeploymentEvent(t *testing.T) {
// event := k8sutil.New("member name", "role", &apiObjectForTest)
// assert.Equal(t, event.Type, v1.EventTypeNormal)
// }