-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Themimitoof/tests/add-unit-tests-and-coverage
Tests/add unit tests and coverage
- Loading branch information
Showing
5 changed files
with
137 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,20 @@ | ||
name: Go testing and coverage | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.18 | ||
|
||
- name: Run unit tests | ||
run: go test -v ./... --cover -coverprofile coverage.out | ||
|
||
- name: Upload coverage report to Codecov | ||
uses: codecov/codecov-action@v3 |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
ipg | ||
build/ | ||
dist/ | ||
coverage.* |
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,34 @@ | ||
package src | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestGenerateDNSRecord(t *testing.T) { | ||
r := GenerateDNSRecord("2001:db8::1", 3600, "foo") | ||
exp := "foo 3600 IN AAAA 2001:db8::1" | ||
|
||
if r != exp { | ||
t.Fatalf("%s != %s", r, exp) | ||
} | ||
} | ||
|
||
func TestGenerateDNSRecordFQDN(t *testing.T) { | ||
r := GenerateDNSRecord("2001:db8::1", 3600, "foo.bar") | ||
exp := "foo.bar. 3600 IN AAAA 2001:db8::1" | ||
|
||
if r != exp { | ||
t.Fatalf("%s != %s", r, exp) | ||
} | ||
} | ||
|
||
func TestGenerateReverseDNSRecord(t *testing.T) { | ||
ip := "b.4.7.e.0.2.4.c.a.d.f.0.c.d.4.1.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa" | ||
r := GenerateReverseDNSRecord(ip, 3600, "foo.bar") | ||
exp := fmt.Sprintf("%s. 3600 IN PTR foo.bar", ip) | ||
|
||
if r != exp { | ||
t.Fatalf("%s != %s", r, exp) | ||
} | ||
} |
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,69 @@ | ||
package src | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"testing" | ||
|
||
"github.com/c-robinson/iplib" | ||
) | ||
|
||
func TestNet6wildcard(t *testing.T) { | ||
n := iplib.Net6FromStr("2001:db8:baba:b0b0::/64") | ||
wildcard := Net6wildcard(n) | ||
exp := net.IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} | ||
|
||
if bytes.Compare(wildcard, exp) != 0 { | ||
t.Fatalf("The wildcard doesn't match with the expected one.") | ||
} | ||
} | ||
|
||
func TestGenerateRandomIP(t *testing.T) { | ||
n := iplib.Net6FromStr("2001:db8:baba:b0b0::/64") | ||
ip := GenerateRandomIP(n) | ||
exp := []byte{0x20, 0x01, 0x0d, 0xb8, 0xba, 0xba, 0xb0, 0xb0} | ||
|
||
if bytes.Compare(ip[0:8], exp) != 0 { | ||
t.Fatalf("%s is outside of %s", ip.String(), n.String()) | ||
} | ||
} | ||
|
||
func TestGenerateRandomIPKeepByteInSmallerSubnets(t *testing.T) { | ||
n := iplib.Net6FromStr("2001:db8:baba:b0b0:ee::/79") | ||
ip := GenerateRandomIP(n) | ||
|
||
if ip[9] != 0xee && ip[9] != 0xef { | ||
t.Fatalf( | ||
"Last bytes of the netpart in the generated IP has shifted. Net: %s, Generated IP: %s", | ||
n.String(), | ||
ip.String(), | ||
) | ||
} | ||
} | ||
|
||
func TestGenerateIPFromHostname(t *testing.T) { | ||
n := iplib.Net6FromStr("2001:db8:baba:b0b0::/64") | ||
ip := GenerateIPFromHostname(n, "foo.bar") | ||
|
||
if bytes.Compare(ip[13:16], []byte{0x66, 0x6f, 0x6f}) != 0 { | ||
t.Fatalf("'foo' as not been found in the generated IP address %s.", ip.String()) | ||
} | ||
} | ||
|
||
func TestGenerateIPFromHostnameRemoveVowels(t *testing.T) { | ||
n := iplib.Net6FromStr("2001:db8:baba:b0b0::/64") | ||
ip := GenerateIPFromHostname(n, "hellofolks.foo.bar") // hllflks | ||
|
||
if bytes.Compare(ip[9:16], []byte{0x68, 0x6c, 0x6c, 0x66, 0x6c, 0x6b, 0x73}) != 0 { | ||
t.Fatalf("'hllflks' as not been found in the generated IP address %s.", ip.String()) | ||
} | ||
} | ||
|
||
func TestGenerateIPFromHostnameShrink(t *testing.T) { | ||
n := iplib.Net6FromStr("2001:db8:baba:b0b0::/64") | ||
ip := GenerateIPFromHostname(n, "loadbalancer-pouet.foo.bar") // ldblncr- | ||
|
||
if bytes.Compare(ip[8:16], []byte{0x6c, 0x64, 0x62, 0x6c, 0x6e, 0x63, 0x72, 0x2d}) != 0 { | ||
t.Fatalf("'ldblncr-' as not been found in the generated IP address %s.", ip.String()) | ||
} | ||
} |
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,13 @@ | ||
package src | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestRandomNumber(t *testing.T) { | ||
n := RandomNumber(1, 10) | ||
|
||
if n < 1 && n > 10 { | ||
t.Fatalf("%d is not between 1 and 10.", n) | ||
} | ||
} |