Golang assertion issues

122 views
Skip to first unread message

nishant gupta

unread,
Jan 29, 2021, 12:53:01 PM1/29/21
to golang-nuts
Hi Gophers,

Being new to programming and to Golang i am running into issues while asserting my test case. Looking for help here from the pros -

Here is my function i want to Unit test -
func validateK8ResourcesLength(resource prm.Resource) error {
name := resource.GetName()
namespace := resource.GetNamespace()
k8sResourceName := fmt.Sprintf("%s-%s", namespace, name)
if len(k8sResourceName) > common.K8sNameLength {
return fmt.Errorf("Namespace (%s) and Name (%s) length combined must not be greater than 48 chars", namespace, name)
}
return nil
}


Here is test case written -
func TestK8testing(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockContext := prmmock.NewMockStepContext(ctrl)
mockResource := prmmock.NewMockResource(ctrl)
mockClient := prmmock.NewMockClient(ctrl)
// testClient := fake.NewFakeClientWithScheme(runtime.NewScheme())
lables := make(map[string]string)
mockClient.EXPECT().GetResource(gomock.Any(), gomock.Any()).Return(mockResource, nil).AnyTimes()
mockContext.EXPECT().Resource().Return(mockResource).AnyTimes()
mockResource.EXPECT().GetKind().Return("data-movement-v1-nonexist").AnyTimes()
mockResource.EXPECT().GetName().Return("test-dm-more-then-forty-eight-charactors-this-should-fail").AnyTimes()
mockResource.EXPECT().GetNamespace().Return("ns-more-then-forty-eight-charactors-this-should-fail").AnyTimes()
mockResource.EXPECT().GetLabels().Return(lables).AnyTimes()
mockResource.EXPECT().GetID().Return(prm.NewResourceID("hcc-dm", "data-movement-v1", "test-dm")).AnyTimes()

err := validateK8ResourcesLength(mockResource)
assert.Error(t, err)
if assert.Error(t, err) {
//assert.Contains(t, err, "length combined must not be greater")
assert.Equal(t, "Namespace (ns-more-then-forty-eight-charactors-this-should-fail) and Name (test-dm-more-then-forty-eight-charactors-this-should-fail) length combined must not be greater than 48 chars", err)
}
}


Somehow i am getting error while i try to use contain or equal.
--- FAIL: TestK8testing (0.00s)
/Users/ngupta59/hcc-dm/pkg/dm-agent/cmd/action_validate_dmjob_test.go:296:
Error Trace: action_validate_dmjob_test.go:296
Error: Not equal:
expected: string("Namespace (ns-more-then-forty-eight-charactors-this-should-fail) and Name (test-dm-more-then-forty-eight-charactors-this-should-fail) length combined must not be greater than 48 chars")
actual : *errors.errorString(&errors.errorString{s:"Namespace (ns-more-then-forty-eight-charactors-this-should-fail) and Name (test-dm-more-then-forty-eight-charactors-this-should-fail) length combined must not be greater than 48 chars"})
Test: TestK8testing
FAIL
FAIL




Any help is appreciated in this regards. Thanks in advance. 
 


Anderson Queiroz

unread,
Feb 1, 2021, 5:39:11 AM2/1/21
to golang-nuts
Hi,

Well, you are using a 3th party lib for assertion, and you haven't told which one, so it's hard to reason about it. one idea is to check the library's docs to ensure it's ding the comparison as you expect.
2 things would help to get some insight on your problem are:
 - format the code, it's hard to read it. If possible try to reproduce on https://play.golang.org/ and post the link here.
 - show which libs you're using. Which is the library used for assertion? The `errors` package is from the standard library or a 3td party one?

best,

Jan Mercl

unread,
Feb 1, 2021, 5:48:54 AM2/1/21
to nishant gupta, golang-nuts
On Fri, Jan 29, 2021 at 6:52 PM nishant gupta <nishant...@gmail.com> wrote:

> Being new to programming and to Golang i am running into issues while asserting my test case. Looking for help here from the pros -

> Any help is appreciated in this regards. Thanks in advance.

Possibly not a popular advice: When possible, avoid using mocking and
asserting packages for your work. More so, if you're, as stated,
starting with programming and Go. They might be useful in some
projects, for some people, not me.

Please note the name of the programming language, it is Go.

peterGo

unread,
Feb 1, 2021, 9:46:23 AM2/1/21
to golang-nuts
nishant gupta,

You have a bug in your validateK8ResourcesLength function.

A fix:


Peter
Reply all
Reply to author
Forward
0 new messages