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

created unit test for the k8 describe package #287 #289

Closed
wants to merge 9 commits into from

Conversation

Philip-21
Copy link
Member

@Philip-21 Philip-21 commented Apr 7, 2023

Signed-off-by: Philip-21 [email protected]

Description

The PR fixes for #287

I created a k8 unit test using for the describe package .

The role of mockClient is to provide a mock implementation of the meshkitkube.Client interface, which allows the Describe() to be tested in isolation from the Kubernetes API. Instead of interacting with the real Kubernetes API, the function can use the Describe() mock implementation provided by mockClient to simulate interactions with the API and verify that the function behaves correctly under different conditions.
The MockDescriber is used in the implementation of the Describe() to create a mock output that is returned when the
function is called with specific options
it takes in a Client object and a DescriberOptions object, and returns a string and an error

The approach of using a K8 api can slow down the test suite and make it more difficult to maintain.
Using mock objects and dependencies allows us to isolate the specific code we want to test and provide controlled inputs and outputs, without the need to interact with the actual Kubernetes API. This makes the tests faster, more reliable, and easier to maintain.
I added some comments where necessary to help understand the code better .

@welcome
Copy link

welcome bot commented Apr 7, 2023

Yay, your first pull request! 👍 A contributor will be by to give feedback soon. In the meantime, please review the Layer5 Community Welcome Guide and sure to join the community Slack.
Be sure to double-check that you have signed your commits. Here are instructions for making signing an implicit activity while peforming a commit.

@Philip-21
Copy link
Member Author

I am also working on the other packages, i am still going through the code base to understand it better ,so i could write the unit test.

@Philip-21 Philip-21 changed the title created unit test for the k8 describe package created unit test for the k8 describe package #287 Apr 8, 2023
@Philip-21 Philip-21 changed the title created unit test for the k8 describe package #287 created unit test for the k8 describe package Apr 8, 2023
@Philip-21 Philip-21 changed the title created unit test for the k8 describe package created unit test for the k8 describe package #287 Apr 10, 2023
@Aisuko Aisuko self-requested a review April 10, 2023 06:59
Copy link
Member

@Aisuko Aisuko left a comment

Choose a reason for hiding this comment

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

There are two suggestions please take a look. And If we can make this happen in this repo. We can add test cases to other repos to increase the coverage.

}

//run test cases
for _, tc := range testCases {
Copy link
Member

@Aisuko Aisuko Apr 10, 2023

Choose a reason for hiding this comment

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

Thanks for your contribution. We really need a unit test for all the repos. All the code is good and clean but there is consideration about using it to run all the test cases manually.
Please check the code below, we prefer to use it this way.

https://github.com/meshery/meshery-operator/blob/1ace1d307c2278325b3c7b9c741f7dc7af912529/api/v1alpha1/meshsync_types_test.go#L62-L95

Copy link
Member Author

Choose a reason for hiding this comment

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

ok this method seems new to me tho, but i would see how i can implement it . I would reach out incase i have issues or i am stuck.

)

/*
The MockDescriber is used in the implementation of the Describe function to create a mock output that is returned when the
Copy link
Member

Choose a reason for hiding this comment

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

Nice code comment and this is what we need. So, may you help add a code comment to the describe.go file. I believe if you have written a test case for it you have known what exactly it does.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, i can do this

@Aisuko
Copy link
Member

Aisuko commented Apr 10, 2023

By the way, please sign off your commit, more detail https://github.com/meshery/meshkit/pull/289/checks?check_run_id=12627332331

@Philip-21 Philip-21 force-pushed the k8-describe-package-test branch from 54b58ee to 6e9756d Compare April 10, 2023 08:22
DescribeFunc field is a function that takes in a Client
object and a DescriberOptions object, and returns a string and an error
*/
DescribeFunc: func(client *meshkitkube.Client, options DescriberOptions) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is it that we are testing over here? You can't define the function that is to be tested inside of the test. This function is to be tested

func Describe(client *meshkitkube.Client, options DescriberOptions) (string, error) {

Ideally, you need to create a mock http server using httpmock and configure it to mock kube api server's response.

Copy link
Member Author

@Philip-21 Philip-21 Apr 10, 2023

Choose a reason for hiding this comment

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

The DescribeFunc wasnt used for testing it was the MockClient and tc.Options were used as a parameter for testing. Instead DescribeFunc is a testcase field that creates a MockDescriber for testing.
func (m *MockDescriber) Describe(client *meshkitkube.Client, options DescriberOptions) (string, error) {
return m.DescribeFunc(client, options)
}

which takes in parameters mockClient and tc.Options
simply returns the result of the DescribeFunc instance
The DescribeFunc creates a mock output that is returned when the
function (Describe()) is called with specific options.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok thanks for the suggestion, i would create a mock http server response for the k8 api.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with you here but I believe these codes are still useful. If we assume that MockDescriber has a function named Describe mock the real Describereturns it will be useful. Maybe he does not want to mock any of the API server's responses and only wants to test the function itself. Although, it looks like the code does not test the target function features. We can change a little bit of this code to test the edge situation for the real Describe function.

Is that sound good guys?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can discuss an outline for the mock test. It determines what's your target. After we define the scope, it would have a clear roadmap for mocking the test suite.

Copy link
Member Author

Choose a reason for hiding this comment

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

The approach of using a K8 api to return any response can slow down the test suite and make it more difficult to maintain.
like @Revolyssup had said i should create a mock response for the api , which is what i will implement .

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok thanks I will look into it .

Copy link
Member

@Aisuko Aisuko Apr 13, 2023

Choose a reason for hiding this comment

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

You are welcome. It is useful for all our repos. If you need any help, please let me know. https://github.com/kubernetes/client-go/blob/8005e0d28ba2db8a60bb8dbe8a5a01fed4872f67/testing/fake.go#L103

@Philip-21 Philip-21 force-pushed the k8-describe-package-test branch from 057b135 to af7fda8 Compare April 19, 2023 02:05
@Philip-21 Philip-21 force-pushed the k8-describe-package-test branch from 52dff0d to fd006e4 Compare April 19, 2023 09:06
Signed-off-by: Philip Obiora <[email protected]>
Signed-off-by: Philip Obiora <[email protected]>
@stale
Copy link

stale bot commented Jun 8, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the issue/stale Issue has not had any activity for an extended period of time label Jun 8, 2023
@leecalcote
Copy link
Member

@Aisuko thanks for helping getting this PR into shape and ready for merge. Do you mind giving this another review?

@Philip-21 is there any outstanding feedback to address?

// @nebula-aac

@stale stale bot removed the issue/stale Issue has not had any activity for an extended period of time label Jun 8, 2023
@Aisuko
Copy link
Member

Aisuko commented Jun 9, 2023

@Aisuko thanks for helping getting this PR into shape and ready for merge. Do you mind giving this another review?

@Philip-21 is there any outstanding feedback to address?

// @nebula-aac

You are welcome @leecalcote. I saw that @Philip-21 remove all the files.

@Philip-21
Copy link
Member Author

@Aisuko thanks for helping getting this PR into shape and ready for merge. Do you mind giving this another review?

@Philip-21 is there any outstanding feedback to address?

// @nebula-aac

This Pr had DCO and commit issues so I decided to remove all files
The issue is continued on #319
I'm Still communicating with Ashish on the next step .

@stale
Copy link

stale bot commented Jul 26, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the issue/stale Issue has not had any activity for an extended period of time label Jul 26, 2023
@stale
Copy link

stale bot commented Aug 6, 2023

This issue is being automatically closed due to inactivity. However, you may choose to reopen this issue.

@stale stale bot closed this Aug 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue/stale Issue has not had any activity for an extended period of time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants