Skip to content

Commit

Permalink
Enhance defaultHWAddrFunc() and tests to hit 100% coverage
Browse files Browse the repository at this point in the history
This updates `defaultHWAddrFunc` to use a package-level variable for calling
`net.Interfaces()`, so that we can mock it in unit tests.

Doing so, and writing the related test, allows us to hit 100% coverage.

Signed-off-by: Tim Heckman <t@heckman.io>
  • Loading branch information
theckman committed Oct 6, 2018
1 parent 48eeef7 commit 75abe58
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
4 changes: 3 additions & 1 deletion generator.go
Expand Up @@ -284,9 +284,11 @@ func newFromHash(h hash.Hash, ns UUID, name string) UUID {
return u
}

var netInterfaces = net.Interfaces

// Returns the hardware address.
func defaultHWAddrFunc() (net.HardwareAddr, error) {
ifaces, err := net.Interfaces()
ifaces, err := netInterfaces()
if err != nil {
return []byte{}, err
}
Expand Down
99 changes: 99 additions & 0 deletions generator_test.go
Expand Up @@ -24,8 +24,10 @@ package uuid
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"net"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -376,6 +378,103 @@ func testNewV5DifferentNamespaces(t *testing.T) {
}
}

func Test_defaultHWAddrFunc(t *testing.T) {
tests := []struct {
n string
fn func() ([]net.Interface, error)
hw net.HardwareAddr
e string
}{
{
n: "error",
fn: func() ([]net.Interface, error) {
return nil, errors.New("controlled failure")
},
e: "controlled failure",
},
{
n: "no_valid_hwaddr_returned",
fn: func() ([]net.Interface, error) {
s := []net.Interface{
{
Index: 1,
MTU: 1500,
Name: "test0",
HardwareAddr: net.HardwareAddr{1, 2, 3, 4},
},
{
Index: 2,
MTU: 1500,
Name: "lo0",
HardwareAddr: net.HardwareAddr{5, 6, 7, 8},
},
}

return s, nil
},
e: "uuid: no HW address found",
},
{
n: "valid_hwaddr_returned",
fn: func() ([]net.Interface, error) {
s := []net.Interface{
{
Index: 1,
MTU: 1500,
Name: "test0",
HardwareAddr: net.HardwareAddr{1, 2, 3, 4},
},
{
Index: 2,
MTU: 1500,
Name: "lo0",
HardwareAddr: net.HardwareAddr{5, 6, 7, 8, 9, 0},
},
}

return s, nil
},
hw: net.HardwareAddr{5, 6, 7, 8, 9, 0},
},
}

for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
// set the netInterfaces variable (function) for the test
// and then set it back to default in the deferred function
netInterfaces = tt.fn
defer func() {
netInterfaces = net.Interfaces
}()

var hw net.HardwareAddr
var err error

hw, err = defaultHWAddrFunc()

if len(tt.e) > 0 {
if err == nil {
t.Fatalf("defaultHWAddrFunc() error = <nil>, should contain %q", tt.e)
}

if !strings.Contains(err.Error(), tt.e) {
t.Fatalf("defaultHWAddrFunc() error = %q, should contain %q", err.Error(), tt.e)
}

return
}

if err != nil && tt.e == "" {
t.Fatalf("defaultHWAddrFunc() error = %q, want <nil>", err.Error())
}

if !bytes.Equal(hw, tt.hw) {
t.Fatalf("hw = %#v, want %#v", hw, tt.hw)
}
})
}
}

func BenchmarkGenerator(b *testing.B) {
b.Run("NewV1", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 75abe58

Please sign in to comment.