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

Writing unit tests using Gomock #4110

Conversation

prietyc123
Copy link
Contributor

What type of PR is this?

/kind feature

What does does this PR do / why we need it:

This PR is for deciding which mocking framework we should use in odo. It includes mocks for interfaces, nested interfaces and example of unit tests using gomock and testify, so that team can decide their area of comfortability in writing tests. I will add the documentation once we decide the framework.

Which issue(s) this PR fixes:

Fixes #4097

PR acceptance criteria:

How to test changes / Special notes to the reviewer:
Unit tests should pass

@openshift-ci-robot openshift-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. Required by Prow. kind/feature Categorizes issue as a feature request. For PRs, that means that the PR is the implementation labels Oct 11, 2020
@@ -29,6 +31,281 @@ import (
. "github.com/openshift/odo/pkg/config"
)

// func TestGomockGetComponentFrom(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have commented out TestGomockGetComponentFrom test because currently facing some issues while retrieving the returned value in gomock framework

ComponentSettings: tt.cmpSetting,
}

cmpN := (reflect.ValueOf((mockLocalConfig.On("GetName").Return(envInfo.ComponentSettings.Name)).ReturnArguments[0])).Interface().(string)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do cleanup and modify the variable name after the final decision on the tests. This is just a kind of unit test example using testify.

@@ -0,0 +1,191 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.

package mocks
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Generated mocks for nested interface too using mockery. I found ease of generating mocks for such kind of interfaces using mockery. However I will try to add mocks for such scenario with gomock as well which is little challenging and still working on that.

How to generator mocks using mockery

$ mockery -dir=./pkg/devfile/adapters/common/ -name "ComponentAdapter|StorageAdapter" -output ./pkg/devfile/adapters/common/mocks/

// Source: ./pkg/envinfo/envinfo.go

// Package envinfo is a generated GoMock package.
package envinfo
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mocks generated for
https://github.com/openshift/odo/blob/a28e37838c5cbaa396f6675fe90a9986168165f5/pkg/envinfo/envinfo.go#L90-L97
using gomock mockgen

$ mockgen -source=./pkg/envinfo/envinfo.go -destination=./pkg/envinfo/LocalConfigProviderMocks.go -package=envinfo LocalConfigProvider

@@ -0,0 +1,99 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.

package mocks
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mocks generated for
https://github.com/openshift/odo/blob/a28e37838c5cbaa396f6675fe90a9986168165f5/pkg/envinfo/envinfo.go#L90-L97
using testify mockery

$ mockery -dir=./pkg/envinfo/ -name LocalConfigProvider -output ./pkg/envinfo/mocks/

@prietyc123 prietyc123 force-pushed the POCOnGomockAndTestifyMockingFramework branch from e03077b to 8ef2a53 Compare October 11, 2020 05:09
@codecov
Copy link

codecov bot commented Oct 11, 2020

Codecov Report

Merging #4110 into master will decrease coverage by 0.12%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4110      +/-   ##
==========================================
- Coverage   42.28%   42.15%   -0.13%     
==========================================
  Files         154      155       +1     
  Lines       13074    13114      +40     
==========================================
  Hits         5528     5528              
- Misses       6954     6994      +40     
  Partials      592      592              
Impacted Files Coverage Δ
pkg/envinfo/LocalConfigProviderMocks.go 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b33adbc...0fd0d9e. Read the comment docs.

@prietyc123
Copy link
Contributor Author

prietyc123 commented Oct 12, 2020

For gomock comparison I am going through hard time to get it, However using mockery i have completely implemented the acceptance criteria.

Issues with gomock:

@girishramnani @kadel I have fulfilled the acceptance criteria for issue #4097 using mockery and facing lots of challenges with gomock mockgen. Trying to fix the unit test with gomock and overcome its challenges with embedded interface. I will update the progress going forward.

@prietyc123 prietyc123 force-pushed the POCOnGomockAndTestifyMockingFramework branch 2 times, most recently from 4e0e2c5 to 446b5c4 Compare October 13, 2020 11:46
@prietyc123
Copy link
Contributor Author

prietyc123 commented Oct 13, 2020

Observation and challenges faced to generate mocking using gomock/testify

Pre-requisite:

  • Should have Interfaces across the code

Embedded interface mocking:

  1. Gomock
$ mockgen -destination=./pkg/devfile/adapters/common/mock/InterfaceMock.go github.com/openshift/odo/pkg/devfile/adapters/common ComponentAdapter,StorageAdapter

Generated mock - https://github.com/openshift/odo/pull/4110/files#diff-21de83616946ca64be5a4b32b6c372d1ed5d72a8d770c10a8b995b8f0baf1555

  1. Testify
$ mockery -dir=./pkg/devfile/adapters/common/ -name "ComponentAdapter|StorageAdapter" -output ./pkg/devfile/adapters/common/mocks/

Writing unit tests

  1. Gomock
$ mockgen -source=./pkg/envinfo/envinfo.go -destination=./pkg/envinfo/LocalConfigProviderMocks.go -package=envinfo LocalConfigProvider
  1. Testify
$ mockery -dir=./pkg/envinfo/ -name LocalConfigProvider -output ./pkg/envinfo/mocks/

Thanks @mik-dass for providing useful feedback on the unit tests part over bjn. I have updated the unit tests as per our discussion. It would be great if you review once and let me know your thoughts.

@kadel @girishramnani To the best of my knowledge I have jotted down all the pros and cons for both the mocking frameworks. Could you please let me know your thoughts on deciding factor.

@prietyc123 prietyc123 force-pushed the POCOnGomockAndTestifyMockingFramework branch from 446b5c4 to 34ec602 Compare October 13, 2020 15:44
@prietyc123
Copy link
Contributor Author

platform issue

/retest

@prietyc123 prietyc123 changed the title [WIP]POC on writing unit tests using Gomock and Testify Writing unit tests using Gomock and Testify Oct 14, 2020
@openshift-ci-robot openshift-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. Required by Prow. label Oct 14, 2020
@prietyc123
Copy link
Contributor Author

Already we have an issue for

[odo] I1014 06:25:32.544747   73002 deployments.go:146] Waiting for deployment spec update to be observed...
[odo]  ✗  Waiting for component to start [5m]
[odo]  ✗  Failed to start component with name xabhim. Error: Failed to create the component: error while waiting for deployment rollout: timeout while waiting for xabhim deployment roll out

#3256

@prietyc123
Copy link
Contributor Author

/retest

@prietyc123 prietyc123 force-pushed the POCOnGomockAndTestifyMockingFramework branch from 34ec602 to d9325b1 Compare October 19, 2020 04:40
@prietyc123
Copy link
Contributor Author

platform issue

/retest

@prietyc123
Copy link
Contributor Author

/retest

@amitkrout
Copy link
Contributor

/test unit

@prietyc123
Copy link
Contributor Author

/retest

@girishramnani
Copy link
Contributor

Final concensus is to use gomock and mockgen

@prietyc123 prietyc123 force-pushed the POCOnGomockAndTestifyMockingFramework branch from d61ba45 to 4a16805 Compare October 20, 2020 13:35
@prietyc123 prietyc123 changed the title Writing unit tests using Gomock and Testify Writing unit tests using Gomock Oct 20, 2020
@prietyc123 prietyc123 force-pushed the POCOnGomockAndTestifyMockingFramework branch from 4a16805 to a317653 Compare October 21, 2020 17:36
@prietyc123 prietyc123 force-pushed the POCOnGomockAndTestifyMockingFramework branch 2 times, most recently from 732a53c to 5335c56 Compare October 28, 2020 09:52
@prietyc123
Copy link
Contributor Author

/retest

@prietyc123 prietyc123 force-pushed the POCOnGomockAndTestifyMockingFramework branch from 5335c56 to 0fd0d9e Compare November 3, 2020 06:29
@prietyc123
Copy link
Contributor Author

/retest

Copy link
Member

@dharmit dharmit left a comment

Choose a reason for hiding this comment

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

Code /lgtm.

However, I would have preferred having comments as well.

@dharmit
Copy link
Member

dharmit commented Nov 3, 2020

I will add the documentation once we decide the framework.

I've opened #4186 so that we don't miss this aspect.

@girishramnani
Copy link
Contributor

/approve

@openshift-ci-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: girishramnani

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. label Nov 3, 2020
@girishramnani
Copy link
Contributor

the lgtm didn't kick in adding again 🙂
/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. Required by Prow. label Nov 4, 2020
@openshift-merge-robot openshift-merge-robot merged commit 55c4c5a into redhat-developer:master Nov 4, 2020
kadel pushed a commit to prietyc123/odo that referenced this pull request Nov 4, 2020
* Adding unit tests and mocks for interface and nested interface using gomock and testify

* Add testify latest verion in go mod

* Added vendor for dependencies

* Added examples with both gomock and testify for interface and embeded interface

* removed testify tests and mocks
@prietyc123 prietyc123 mentioned this pull request Feb 9, 2021
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. kind/feature Categorizes issue as a feature request. For PRs, that means that the PR is the implementation lgtm Indicates that a PR is ready to be merged. Required by Prow.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Decide a mocking framework for interface
6 participants