-
Notifications
You must be signed in to change notification settings - Fork 110
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
Conversation
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. |
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. |
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
By the way, please sign off your commit, more detail https://github.com/meshery/meshkit/pull/289/checks?check_run_id=12627332331 |
54b58ee
to
6e9756d
Compare
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 Describe
returns 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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest you to use https://github.com/kubernetes/client-go/blob/8005e0d28ba2db8a60bb8dbe8a5a01fed4872f67/rest/fake/fake.go#L46-L48 to mock the Kubernetes api-server response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And PrependReactor()
may useful here. https://stackoverflow.com/questions/69740891/mocking-errors-with-client-go-fake-client
There was a problem hiding this comment.
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 .
There was a problem hiding this comment.
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
057b135
to
af7fda8
Compare
Signed-off-by: Philip-21 <[email protected]>
Signed-off-by: Philip Obiora <[email protected]>
Signed-off-by: Philip-21 <[email protected]>
52dff0d
to
fd006e4
Compare
Signed-off-by: Philip Obiora <[email protected]>
Signed-off-by: Philip Obiora <[email protected]>
Signed-off-by: Philip Obiora <[email protected]>
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. |
@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. |
This Pr had DCO and commit issues so I decided to remove all files |
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. |
This issue is being automatically closed due to inactivity. However, you may choose to reopen this issue. |
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 theDescribe()
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 thefunction 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 .