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

Added test workflow for PR and upgraded dependencies #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

sonamtenzin6
Copy link
Contributor

@sonamtenzin6 sonamtenzin6 commented Jan 30, 2025

Description

@sonamtenzin6 sonamtenzin6 force-pushed the sonamt/test-run branch 4 times, most recently from 61bb7c6 to 0b4bdf0 Compare February 4, 2025 05:29
@sonamtenzin6 sonamtenzin6 marked this pull request as ready for review February 4, 2025 05:35
@sonamtenzin6 sonamtenzin6 requested review from a team as code owners February 4, 2025 05:35
ipaddrs.go Outdated
@@ -67,9 +67,12 @@ func (s SortIPAddrsByNetworkSize) Less(i, j int) bool {
// that have a port (e.g. a host with a /32 and port number is more
// specific and should sort first over a host with a /32 but no port
// set).
if s.IPAddrs[i].IPPort() == 0 || s.IPAddrs[j].IPPort() == 0 {
Copy link
Contributor Author

@sonamtenzin6 sonamtenzin6 Feb 4, 2025

Choose a reason for hiding this comment

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

This condition is currently incorrectly sorting for scenarios like these -

If we get an order like this, it will return false maintaining that 1>2

  1. 128.95.120.2:8600
  2. 128.95.120.2

And if we get input like this, it will still return 1>2

  1. 128.95.120.2
  2. 128.95.120.2:8600

It is essentially not sorting for these scenarios. The test TestSockAddr_IPAddrs_IPAddrsByNetworkSize was failing because of the issue

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to note here, proposal to fix this has been in #50 since two years ago. It also includes an explanation why this became an issue with Go 1.19 onwards.

Copy link
Contributor Author

@sonamtenzin6 sonamtenzin6 Feb 11, 2025

Choose a reason for hiding this comment

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

This PR will anyways need some changes to go through, I will merge #50 and take a rebase from master to incorporate the fix
cc: @schmichael

input: `{{GetDefaultInterfaces | include "type" "IPv4" | attr "name" }}`,
output: `en0`,
},
// {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Commented out these tests because they are dependent on underlying interfaces of the machine, which may change

Choose a reason for hiding this comment

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

This sounds reasonable to me, but maybe just remove the test?

@@ -16,7 +177,7 @@ func TestSockAddr_Parse(t *testing.T) {
}{
{
name: `basic include "name"`,
input: `{{GetAllInterfaces | include "name" "lo0" | printf "%v"}}`,
input: `{{. | include "name" "lo0" | printf "%v"}}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed this from GetAllInterfaces because this caused the test to depend on underlying machine interfaces. We are not getting mock input from getInputList method

@@ -265,7 +426,7 @@ func TestSockAddr_Parse(t *testing.T) {
}
t.Run(test.name, func(t *testing.T) {
t.Parallel()
out, err := socktmpl.Parse(test.input)
out, err := socktmpl.ParseIfAddrs(test.input, getInputList())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to include mocked interfaces. Parse is essentially calling ParseIfAddrs

Choose a reason for hiding this comment

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

Instead of a function that gets called in each test, can we not just create this list once at the top of this test function and then use the value in all the test? Does ParseIfAddrs mutate the input? If so, could we instead create a copy of the list in each test with copy? I think I'd prefer not to have this helper function defined at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, removed the function and replaced with a list

@sonamtenzin6 sonamtenzin6 changed the title Added build and test workflow for PRs Added test workflow for PR and fixed security vulnerabilities Feb 4, 2025
Copy link

@johanbrandhorst johanbrandhorst 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 this, the security vulnerability fix is just the mod update, and I'm guessing you decided to fix the tests because they were failing on your machine? I think that makes sense. I added some comments, let me know what you think!

- name: Setup Go
uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a
with:
go-version: '1.19'

Choose a reason for hiding this comment

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

This is an ancient release. I see that the go.mod says 1.19, but that just means it's the oldest version supported. We should probably use a matrix here, including 1.19, 1.20, 1.21, 1.22 and 1.23. See https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow#using-a-matrix-strategy if you're unsure how to create a matrix strategy.

Also CC @schmichael, what is this repositories Go compatibility policy? I'd probably suggest we update the go directive to 1.22 and use the same policy as the Go team.

Copy link
Member

Choose a reason for hiding this comment

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

SGTM. This repo doesn't need special treatment. It's just been stable/neglected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a matrix for testing on different versions

input: `{{GetDefaultInterfaces | include "type" "IPv4" | attr "name" }}`,
output: `en0`,
},
// {

Choose a reason for hiding this comment

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

This sounds reasonable to me, but maybe just remove the test?

Comment on lines 331 to 344
// {
// // NOTE(sean@): This is the HashiCorp default in 2016.
// // Indented for effect. Using "true" as the output
// // instead of printing the correct $rfc*Addrs values.
// name: "HashiCorpDefault2016",
// input: `
// {{- with $addr := . | include "type" "IP" | include "rfc" "1918|6598" | sort "address" | attr "address" -}}

{{- if ($addr | len) gt 0 -}}
{{- print "true" -}}{{/* print $addr*/ -}}
{{- end -}}
{{- end -}}`,
output: `true`,
},
// {{- if ($addr | len) gt 0 -}}
// {{- print "true" -}}{{/* print $addr*/ -}}
// {{- end -}}
// {{- end -}}`,
// output: `true`,
// },

Choose a reason for hiding this comment

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

Can this test also not work?

Copy link
Member

Choose a reason for hiding this comment

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

As above I'm +1 for removing ancient tests as opposed to commenting them out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test is currently failing, so removed it

Comment on lines 400 to 419
// {
// // Assume the private IPs available on the host are: 10.1.2.3
// // fe80::1025:f732:1001:203
// name: "GetPrivateIPs",
// input: `{{GetPrivateIPs}}`,
// output: `10.1.2.3 fe80::1025:f732:1001:203`,
// },
// {
// // Assume the public IPs available on the host are: 1.2.3.4 6.7.8.9
// name: "GetPublicIPs",
// input: `{{GetPublicIPs}}`,
// output: `1.2.3.4 6.7.8.9`,
// },
// {
// // Assume the private IPs on this host are just the IPv4 addresses:
// // 10.1.2.3 and 172.16.4.6
// name: "GetInterfaceIPs",
// input: `{{GetInterfaceIPs "en0"}}`,
// output: `10.1.2.3 and 172.16.4.6`,
// },

Choose a reason for hiding this comment

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

Remove these

@@ -265,7 +426,7 @@ func TestSockAddr_Parse(t *testing.T) {
}
t.Run(test.name, func(t *testing.T) {
t.Parallel()
out, err := socktmpl.Parse(test.input)
out, err := socktmpl.ParseIfAddrs(test.input, getInputList())

Choose a reason for hiding this comment

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

Instead of a function that gets called in each test, can we not just create this list once at the top of this test function and then use the value in all the test? Does ParseIfAddrs mutate the input? If so, could we instead create a copy of the list in each test with copy? I think I'd prefer not to have this helper function defined at all.

schmichael
schmichael previously approved these changes Feb 7, 2025
Copy link
Member

@schmichael schmichael left a comment

Choose a reason for hiding this comment

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

+1 to @johanbrandhorst's comments about bumping Go and dropping commented out tests.

Logic fix looks good.

Can we update the title and description to drop the security vulnerabilities reference or at least clarify it's a vulnerability in a dependency that is unused by go-sockaddr. go-sockaddr does not have a vulnerability, and I would hate people (or security scanners) to flag the wrong package!

Please correct me if I'm missing something.

Comment on lines 331 to 344
// {
// // NOTE(sean@): This is the HashiCorp default in 2016.
// // Indented for effect. Using "true" as the output
// // instead of printing the correct $rfc*Addrs values.
// name: "HashiCorpDefault2016",
// input: `
// {{- with $addr := . | include "type" "IP" | include "rfc" "1918|6598" | sort "address" | attr "address" -}}

{{- if ($addr | len) gt 0 -}}
{{- print "true" -}}{{/* print $addr*/ -}}
{{- end -}}
{{- end -}}`,
output: `true`,
},
// {{- if ($addr | len) gt 0 -}}
// {{- print "true" -}}{{/* print $addr*/ -}}
// {{- end -}}
// {{- end -}}`,
// output: `true`,
// },
Copy link
Member

Choose a reason for hiding this comment

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

As above I'm +1 for removing ancient tests as opposed to commenting them out.

@schmichael
Copy link
Member

schmichael commented Feb 7, 2025

This will supersede and close #50. Many apologies for missing the opportunity to merge that one!

@sonamtenzin6 sonamtenzin6 changed the title Added test workflow for PR and fixed security vulnerabilities Added test workflow for PR and upgraded dependencies Feb 13, 2025
@@ -632,7 +633,7 @@ func TestGetIfAddrs(t *testing.T) {
t.Fatalf("No loopback interfaces found, loInt nil")
}

if val := sockaddr.IfAddrAttr(*loInt, "flags"); !(val == "up|loopback|multicast" || val == "up|loopback") {
if val := sockaddr.IfAddrAttr(*loInt, "flags"); !strings.Contains(val, "up|loopback") {

Choose a reason for hiding this comment

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

Why was this changed? This isn't the same behavior anymore.

Comment on lines +15 to +19
- '1.19'
- '1.20'
- '1.21'
- '1.22'
- '1.23'

Choose a reason for hiding this comment

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

To make this a little more future proof, we can use the stable and oldstable aliases to use the current and the previous release for this.

Suggested change
- '1.19'
- '1.20'
- '1.21'
- '1.22'
- '1.23'
- 'oldstable'
- 'stable'

@@ -27,6 +27,6 @@ require (
github.com/posener/complete v1.1.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/crypto v0.32.0 // indirect

Choose a reason for hiding this comment

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

Could you also change the go stanza at the top of the file to 1.23?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants