-
Notifications
You must be signed in to change notification settings - Fork 73
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
Changes from 3 commits
4e609ba
de75f79
bf268cc
ec41d32
812f3ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
) | ||
|
||
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)) | ||
} |
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package k8sutil_test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this package name ok? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Package name should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a |
||
|
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. role is uppercase in reason but not in message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of In the following snipped it is used in the middle of the string in one case and in the other case it is not:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
// } |
There was a problem hiding this comment.
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(...))
)