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

feat(evpn unit test): adding evpn mock unit test #276

Merged
merged 2 commits into from
Aug 29, 2023

Conversation

atulpatel261194
Copy link
Contributor

image

EvpnClient.go Outdated Show resolved Hide resolved
@glimchb
Copy link
Member

glimchb commented Aug 28, 2023

  • please add line to

    godpu/Makefile

    Line 29 in c02fd8d

    mockery --config=mocks/.mockery.yaml --name=InvClient --dir=inventory
    to generate new evpn mockery
  • and network folder to

    godpu/Makefile

    Line 23 in c02fd8d

    ginkgo grpc inventory
    to run tests

@codecov
Copy link

codecov bot commented Aug 29, 2023

Codecov Report

Merging #276 (06847d3) into main (c02fd8d) will not change coverage.
The diff coverage is n/a.

@@          Coverage Diff          @@
##            main    #276   +/-   ##
=====================================
  Coverage   8.07%   8.07%           
=====================================
  Files         13      13           
  Lines       2006    2006           
=====================================
  Hits         162     162           
  Misses      1826    1826           
  Partials      18      18           

@atulpatel261194 atulpatel261194 marked this pull request as ready for review August 29, 2023 11:36
@atulpatel261194 atulpatel261194 requested a review from a team as a code owner August 29, 2023 11:36
Copy link
Member

@glimchb glimchb left a comment

Choose a reason for hiding this comment

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

Lgtm

@glimchb glimchb merged commit 32aa38d into opiproject:main Aug 29, 2023
19 checks passed
mockEvpnClient.On("DeleteBridgePort", mock.Anything, mock.Anything, mock.Anything).
Return(nil, expectedError)

resultResponse, err := mockEvpnClient.DeleteBridgePort(context.TODO(), "bp1", true)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@atulpatel261194 A bit late, but why do you test the generated mock here in these tests? Testing of mocks adds little value to our test suite.
I think we should test the implementation(evpnClientImpl) calls a corresponding method in pb.BridgePortServiceClient instead...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@artek-koltun testing the generated mocks protects the changes in .proto files.
i believe we can't test the real api's which create, delete and update different port, bridge, svi and vrf. if there is any way i can test these api's directly Please let me know.

Copy link
Collaborator

@artek-koltun artek-koltun Nov 24, 2023

Choose a reason for hiding this comment

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

There is actually a way. You can use dependency injection to replace the real connection components. The test would look like

func TestCreateBridgePortSuccess(t *testing.T) {
	wantRequest := &_go.CreateBridgePortRequest{
		BridgePortId: "bp1",
		BridgePort: &pb.BridgePort{
			Spec: &pb.BridgePortSpec{
				MacAddress:     []byte("00:11:22:aa:bb:cc"), // Is it valid approach? Should we use net.ParseMac()?
				Ptype:          _go.BridgePortType_ACCESS,
				LogicalBridges: []string{"lb1", "lb2"},
			},
		},
	}
	wantResponse := wantRequest.BridgePort

	mockClient := mocks.NewBridgePortServiceClient(t)
	mockClient.EXPECT().CreateBridgePort(mock.Anything, wantRequest).
		Return(proto.Clone(wantResponse).(*pb.BridgePort), nil)

	connClosed := false
	mockConn := mocks.NewConnector(t)
	mockConn.EXPECT().NewConn().Return(
		&grpc.ClientConn{},
		func() { connClosed = true },
		nil,
	)

	c, _ := network.NewBridgePortWithArgs(
		mockConn,
		func(grpc.ClientConnInterface) pb.BridgePortServiceClient {
			return mockClient
		},
	)
	
	response, err := c.CreateBridgePort(
		context.Background(),
		"bp1", "00:11:22:aa:bb:cc", "access", []string{"lb1", "lb2"},
	)

	assert.NoError(t, err)
	assert.True(t, proto.Equal(response, wantResponse))
	assert.Equal(t, true, connClosed)
}

Here you can see how I tested nvme subsystem with the table approach

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@artek-koltun Sure, i will update the test cases. thank you for the inputs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants