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

Add testing to the proxy #2007

Merged
merged 8 commits into from
Apr 13, 2022
Merged

Add testing to the proxy #2007

merged 8 commits into from
Apr 13, 2022

Conversation

facchettos
Copy link
Contributor

@facchettos facchettos commented Mar 16, 2022

Which issue this PR addresses:

This PR adds some basic testing to the proxy.

What this PR does / why we need it:

This pr refactors the http server of the proxy in order to make it more testable. Tests are in proxy_test.go

Test plan for issue:

This adds tests which were not there before.

Is there any documentation that needs to be updated for this PR?

No

@facchettos facchettos changed the title Add testing to the gateway proxy Add testing to the proxy Mar 16, 2022
Copy link
Contributor

@karanmagdani karanmagdani left a comment

Choose a reason for hiding this comment

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

Thanks for working on this @facchettos. LGTM! I just have one question for my understanding

pkg/proxy/proxy.go Outdated Show resolved Hide resolved
s-amann
s-amann previously approved these changes Mar 29, 2022
Copy link
Contributor

@s-amann s-amann left a comment

Choose a reason for hiding this comment

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

lgtm

SudoBrendan
SudoBrendan previously approved these changes Mar 29, 2022
Copy link
Collaborator

@SudoBrendan SudoBrendan left a comment

Choose a reason for hiding this comment

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

LGTM! Only one nitpick you can choose to fix or not.

Great to see so many PRs so early, great job Jeremy!

pkg/proxy/proxy.go Outdated Show resolved Hide resolved
@facchettos facchettos dismissed stale reviews from SudoBrendan and s-amann via 01e873d March 29, 2022 16:45
s-amann
s-amann previously approved these changes Mar 29, 2022
SudoBrendan
SudoBrendan previously approved these changes Apr 4, 2022
Copy link

@AldoFusterTurpin AldoFusterTurpin left a comment

Choose a reason for hiding this comment

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

Just a question about the use of []struct instead of map[string]struct in proxy_test.go.

Comment on lines +15 to +22
tests := []struct {
name string
method string
subnet string
hostname string
wantStatus int
wantErr bool
}{
Copy link

@AldoFusterTurpin AldoFusterTurpin Apr 8, 2022

Choose a reason for hiding this comment

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

Just a quick question.
Why instead of using a []struct, you use a map[string]struct where the key of the map is the test name and the value of the map is the struct you already have. This provides some benefits like:

  • ensuring unique test case names (as the keys of a map are unique)
  • Map iteration order is undefined. This means each time we run go test, our tests are going to be potentially run in a different order. This is super useful for spotting conditions where tests pass when run in statement order, but not otherwise. I don't think this is happening here but is a good practice to adopt.

More info in Dave Cheney blog, "Give your test cases names" section (or just look for "We can dry this up, even more, using a map literal syntax:").

This is just a comment, I would not reject this PR for that reason but don't feel enough confident to approve it either. 🙂

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 actually followed a recommendation from Mikalai from another PR. But this is a pattern that we have in most (if not all) other test files too.

The test cases will have a #0X appended to them if they have the same name in the struct. So it would probably be something we would lose with the map because the test would be overwritten, and we'd have to manually add the numbers to each of those. (now I actually don't know if having multiple subtests with the same name is something we want to have, so I'd wait for someone with more experience to weigh in)

Copy link
Collaborator

Choose a reason for hiding this comment

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

More info in Dave Cheney blog, "Give your test cases names" section (or just look for "We can dry this up, even more, using a map literal syntax:").

This is interesting. We shouldn't have to nest test cases as your test cases would evolve into something like the below. It shouldn't require an #0X appended as long as the names of the tests are different.

	tests := map[string]struct {
		method     string
		subnet     string
		hostname   string
		wantStatus int
		wantErr    bool
	}{
               "get https same subnet": {
			method:     http.MethodGet,
			subnet:     "127.0.0.1/24",
			hostname:   "https://127.0.0.2:123",
			wantStatus: http.StatusMethodNotAllowed,
			wantErr:    true,
		}, 
                "test-2": {
			method:     http.MethodGet,
			subnet:     "127.0.0.1/24",
			hostname:   "https://127.0.0.2:123",
			wantStatus: http.StatusMethodNotAllowed,
			wantErr:    true,
		}, 
		...
	}

That being said, I'm fine conforming to our current standard. Definitely something I would approve if it was changed going forward as well though. It is a small addition of DRY

karanmagdani
karanmagdani previously approved these changes Apr 11, 2022
Copy link
Contributor

@karanmagdani karanmagdani left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@bennerv bennerv left a comment

Choose a reason for hiding this comment

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

Great work! Couple nits, grammar, and question. otherwise LGTM

pkg/proxy/proxy.go Outdated Show resolved Hide resolved
pkg/proxy/proxy.go Outdated Show resolved Hide resolved
pkg/proxy/proxy_test.go Outdated Show resolved Hide resolved
pkg/proxy/proxy_test.go Show resolved Hide resolved
pkg/proxy/proxy_test.go Outdated Show resolved Hide resolved
@facchettos facchettos requested a review from bennerv April 11, 2022 21:00
Copy link
Collaborator

@bennerv bennerv left a comment

Choose a reason for hiding this comment

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

LGTM - can merge on green

@bennerv bennerv merged commit eab506d into Azure:master Apr 13, 2022
@facchettos facchettos deleted the proxy-refactor-and-test branch May 25, 2022 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants