-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add integration tests for untested endpoints (#37)
* add network_test.go and token_test.go * fix assertion
- Loading branch information
1 parent
46da55d
commit c0f1931
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func isValidIPv4(ip string) bool { | ||
pattern := `^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(3[0-2]|[1-2]?[0-9])$` | ||
matched, _ := regexp.MatchString(pattern, ip) | ||
return matched | ||
} | ||
|
||
func isValidIPv6(ip string) bool { | ||
// ParseCIDR returns the IP address | ||
ipAddr, _, err := net.ParseCIDR(ip) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
// Check if the IP address is IPv6 | ||
if ipAddr.To4() == nil { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func TestGetNetwork(t *testing.T) { | ||
t.Parallel() | ||
|
||
mdNet, err := metadataClient.GetNetwork(context.Background()) | ||
assert.NoError(t, err) | ||
|
||
ipv4List := mdNet.IPv4.Public | ||
|
||
for _, ip := range ipv4List { | ||
assert.True(t, isValidIPv4(ip.String())) | ||
} | ||
|
||
ipv6Slaac := mdNet.IPv6.SLAAC | ||
ipv6LinkLocal := mdNet.IPv6.LinkLocal | ||
|
||
assert.True(t, isValidIPv6(ipv6Slaac.String())) | ||
assert.True(t, isValidIPv6(ipv6LinkLocal.String())) | ||
assert.Contains(t, ipv6LinkLocal.String(), "fe80") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerateToken(t *testing.T) { | ||
t.Parallel() | ||
|
||
result, err := metadataClient.GenerateToken(context.Background()) | ||
assert.NoError(t, err) | ||
|
||
assert.NotNil(t, result) | ||
} |