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

[e2e] Tests should check status code before attempting to unmarshal into json #6777

Closed
nrfox opened this issue Oct 25, 2023 · 13 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@nrfox
Copy link
Contributor

nrfox commented Oct 25, 2023

Many e2e tests attempt to unmarshal the kiali api response before checking the status code resulting in an unmarshalling error. This can mask the response from the server making it harder to debug the tests. The status code should first be checked before unmarshalling.

See: https://github.com/kiali/kiali/actions/runs/6413559944/job/18045431679 for an example of the error.

@nrfox nrfox added the bug Something isn't working label Oct 25, 2023
@jshaughn jshaughn added the backlog Triaged Issue added to backlog label Oct 25, 2023
@PrinsonF77
Copy link
Contributor

Hey, is this issue solved yet?

@jshaughn
Copy link
Collaborator

hi @PrinsonF77 , I'm going to say no just based on the fact that this is Open, still in the Backlog there are no PRs... Did you want to contribute here?

@PrinsonF77
Copy link
Contributor

Yes. Can you assign it to me?
Also according to what I've seen at https://github.com/kiali/kiali/blob/master/tests/integration/tests/kiali_urls_test.go ,
func TestGrafana(t *testing.T) already implements this. Something similar has to be done for the tests that do not do this check right?

@jshaughn
Copy link
Collaborator

Thanks, @PrinsonF77 ! @nrfox , please provide any guidance, thanks.

@nrfox
Copy link
Contributor Author

nrfox commented Nov 20, 2023

Sure the issue is here inside of the Grafana() call (and everywhere else this happens):

body, code, _, err := httpGETWithRetry(url, client.GetAuth(), TIMEOUT, nil, client.kialiCookies)
if err == nil {
status := new(models.GrafanaInfo)
err = json.Unmarshal(body, &status)
if err == nil {
return status, code, nil
} else {
return nil, code, err
}
} else {
return nil, code, err
}

Until we check the status code, we can't safely unmarshal into status := new(models.GrafanaInfo) since the server response for 404s for non 200 is usually just a string. When the unmarshal fails, it masks the actual error message from the server that resides in the response.Body.

What we need is to stop unmarshaling non-200 responses inside of the client calls. It looks like some tests still need to check the status code so I wouldn't return an error here for non-200 responses. It doesn't appear that anything uses the response body directly though so you can simply log the string of response.Body for non-200 responses.

@PrinsonF77
Copy link
Contributor

Thanks for the explanation. Will start working on it

@jshaughn jshaughn removed the backlog Triaged Issue added to backlog label Feb 8, 2024
@jshaughn
Copy link
Collaborator

@PrinsonF77 Will you be completing this effort?

@jshaughn jshaughn added the stale Issue has no activity label Mar 20, 2024
@nrfox
Copy link
Contributor Author

nrfox commented Mar 20, 2024

The kiali client methods can now use this method:

func getRequestAndUnmarshalInto[T any](url string, response *T) error {
body, code, _, err := httpGETWithRetry(url, client.GetAuth(), TIMEOUT, nil, client.kialiCookies)
if err != nil {
return err
}
if code != http.StatusOK {
return fmt.Errorf("non 200 response code: %d from url: %s. Body: %s", code, url, body)
}
err = json.Unmarshal(body, response)
if err != nil {
return fmt.Errorf("unable to unmarshal body into response: %T. Body: %s", response, body)
}
return nil
}
which will check the status code before unmarshaling. We just need to update the kiali client methods to use this new method.

@PrinsonF77
Copy link
Contributor

Hey @jshaughn @nrfox . Will be taking this up. Sorry for the delay.
@nrfox since getRequestandUnmarshallInto only returns the error right now, there is no way to get the status code as well, which is supposed to be returned from the kiali client methods. Will be modifying this function to also return the code. Does that sound good?

@nrfox
Copy link
Contributor Author

nrfox commented Mar 21, 2024

@PrinsonF77 I would say just update the methods that don't return a status code to use that getRequestAndUnmarshalInto function and leave the methods that already return a status code as they are. I'm guessing the status code is being returned because some tests want to check for a non-200 status. Probably there should be some lower level method like DoRequest that doesn't do any unmarshaling that those tests should use instead of one of the "client methods". If you're feeling very ambitious you can try to implement that otherwise just update the existing client methods that don't return a status to use getRequestAndUnmarshalInto.

@PrinsonF77
Copy link
Contributor

PrinsonF77 commented Mar 21, 2024

@nrfox Most of the kiali client methods return a status code. Even the Grafana method that was given as an example in #6777 (comment) .
By modifying getRequestAndUnmarshalInto to also return the code, I can rewrite the Grafana() and other methods as such

func Grafana() (*models.GrafanaInfo, int, error) {
	url := fmt.Sprintf("%s/api/grafana", client.kialiURL)
	status := new(models.GrafanaInfo)
	code, err := getRequestAndUnmarshalInto(url, status)
	if err == nil {
		return status, code, err
	} else {
		return nil, code, err
	}
}

I think this would allow for most of the kiali client methods to be rewritten in this way which reduces the redundancy and helps with returning better error messages. This won't affect any methods that return status codes.
I'm done with most of the changes and will raise a PR soon. We can discuss about this over there as well by looking at the changes. If that does not look good, I can go ahead with just rewriting the methods that don't return a status

PrinsonF77 added a commit to PrinsonF77/kiali that referenced this issue Mar 21, 2024
getRequestAndUnmarshalInto to avoid masking of errors returned by
the server (kiali#6777)
@jshaughn jshaughn removed the stale Issue has no activity label Mar 21, 2024
@nrfox
Copy link
Contributor Author

nrfox commented Mar 22, 2024

I think this would allow for most of the kiali client methods to be rewritten in this way which reduces the redundancy and helps with returning better error messages.

That sounds good.

@jshaughn
Copy link
Collaborator

jshaughn commented Apr 2, 2024

Closing on merge of #7215

@jshaughn jshaughn closed this as completed Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

3 participants