Skip to content

Commit

Permalink
test: Add integration tests for untested endpoints (#37)
Browse files Browse the repository at this point in the history
* add network_test.go and token_test.go

* fix assertion
  • Loading branch information
ykim-akamai authored Mar 4, 2024
1 parent 46da55d commit c0f1931
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/integration/network_test.go
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")
}
17 changes: 17 additions & 0 deletions test/integration/token_test.go
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)
}

0 comments on commit c0f1931

Please sign in to comment.