Skip to content

Commit

Permalink
Merge pull request #1 from Themimitoof/tests/add-unit-tests-and-coverage
Browse files Browse the repository at this point in the history
Tests/add unit tests and coverage
  • Loading branch information
Themimitoof committed Aug 22, 2022
2 parents 1ff71ad + 33be9c0 commit 6f8b734
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/tests.yml
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ipg
build/
dist/
coverage.*
34 changes: 34 additions & 0 deletions src/dns_test.go
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)
}
}
69 changes: 69 additions & 0 deletions src/ip_test.go
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())
}
}
13 changes: 13 additions & 0 deletions src/math_test.go
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)
}
}

0 comments on commit 6f8b734

Please sign in to comment.