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

Unit-tests for UCNF Close and Request functions and CircleCI go test job #38

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

shanchunyang0919
Copy link

@shanchunyang0919 shanchunyang0919 commented Nov 18, 2020

PR for NSMNSE-35
link

Added mock functions unit tests for pkg/universal-cnf/config/composite.go::Request(), Close():

  1. created composite_test.go
  2. used mock functions for the unit tests

In CircleCI config:

  1. created an env setup defaults-go-tester for go test using cimg/go:1.15 image
  2. created a job go-test-integration which runs all the go tests (*_test.go) under cisco-app-networking/nsm-nse directory
  3. added job go-test-integration into both original workflow and tagged workflow

CircleCI image for golang: reference

----Updates----

  • refactored composite_test.go by adding table-driven tests, rewriting mock functions, and modifying method names
  • in .circleci/config.yml, changed from export GOPATH to setting default GOPATH in defaults-go-tester

----Updates----(12/11/2020)

  • modify .circleci/config.yml in order to test all go tests recursively (excluding specific directories)

Copy link
Contributor

@tiswanso tiswanso left a comment

Choose a reason for hiding this comment

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

Generally looks like a good start for framing up the UT for this part. I know there's some follow-on subtasks being worked by others so will try to get a bit more details from their work before approving.

CircleCI additional job looks good other than the nit comment I added. Good job finding and using the circleci standard go container.

For the test stuff, the mocks look basically good but as mentioned above I'd like to get a little more details on additional tests being worked.

working_directory: ~/go/src/github.com/cisco-app-networking/nsm-nse/
# runs all the go test recursively
command: |
export GOPATH=~/go
Copy link
Contributor

Choose a reason for hiding this comment

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

you wouldn't have to do this if you just change the default GOPATH var setting in defaults-go-tester section

Copy link
Author

Choose a reason for hiding this comment

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

Hello, Tim. Thank you for leaving this comment. I will make modifications.

_, err = ucnfe.Close(ctx, conn)
g.Expect(err).Should(HaveOccurred(), fmt.Sprintf("ERROR : %v", err))

}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: need a newline for compatibility with some tools (e.g. diff/patch)

}

// mock implementor
type MockUcnfBackendReturnNil struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

question: is the "ReturnNil" name suffix a golang mock pattern you've seen or just a name you chose?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for asking, this was the name that I chose.

return nil
}

func createUcnfEndpoint() (e *nseconfig.Endpoint) {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, this function should be named "createNseConfigEndpoint"

return
}

func createConnection() (conn *connection.Connection) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe it's a good idea to start a pattern of naming functions that are primarily to operate on NSM objects as "Nsm" format, e.g. this would be "createNsmConnection"

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the advices. I will start working on the these.

return nil
}

func (m *MockUcnfBackendReturnNil) ProcessDPConfig(dpconfig interface{}, update bool) error {
Copy link
Member

@ondrej-fabry ondrej-fabry Nov 20, 2020

Choose a reason for hiding this comment

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

It would be much simpler and also more flexible to inject custom behavior (implementation) of these methods by having single mock backed with fields defined as funcs:

type MockUcnfBackendProcessDPConfig struct {
    //...
    processDPConfig func(dpconfig interface{}, update bool) error
}
//...
func (m *MockUcnfBackendProcessDPConfig) ProcessDPConfig(dpconfig interface{}, update bool) error {
    if m.processDPConfig == nil {
        return nil
    }
    return m.processDPConfig(dpconfig, update)
}

This way you will get the "..ReturnNil" behavior by default when creating MockUcnfBackendProcessDPConfig and also make it possible to override the implementation by simply setting the field value:

mocked := &MockUcnfBackendProcessDPConfig{
    processDPConfig: func(dpconfig interface{}, update bool) error {
        return fmt.Errorf("failed to run ProcessDPConfig() method")
    },
}

This will make it possible to have a single mocked backed struct and much less redundant code.

Copy link
Author

@shanchunyang0919 shanchunyang0919 Nov 20, 2020

Choose a reason for hiding this comment

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

Hello Ondrej, thank you very much for the suggestions. I will make modifications.

@tiswanso
Copy link
Contributor

Please create this PR against the wwwin-github.cisco.com repo and we can get folks to give their final approval and merge it.

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.

4 participants