Skip to content

Commit

Permalink
add more tests to netconf type
Browse files Browse the repository at this point in the history
  • Loading branch information
tinyzimmer committed Oct 3, 2023
1 parent 01a5876 commit 820a546
Showing 1 changed file with 167 additions and 21 deletions.
188 changes: 167 additions & 21 deletions internal/types/netconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package types

import (
"encoding/json"
"io"
"log/slog"
"os"
"testing"

Expand All @@ -29,6 +31,25 @@ import (
func TestNetConf(t *testing.T) {
t.Parallel()

testConf := &NetConf{
Kubernetes: Kubernetes{
Kubeconfig: "foo",
NodeName: "bar",
K8sAPIRoot: "http://localhost:8080",
Namespace: "baz",
},
Interface: Interface{
MTU: 1234,
DisableIPv4: true,
DisableIPv6: true,
},
LogLevel: "info",
}
testData, err := json.Marshal(testConf)
if err != nil {
t.Fatal("marshal test data", err)
}

t.Run("Defaults", func(t *testing.T) {
t.Parallel()
// Make sure an empty configuration produces the correct defaults.
Expand Down Expand Up @@ -68,25 +89,6 @@ func TestNetConf(t *testing.T) {
t.Run("Decoders", func(t *testing.T) {
t.Parallel()

testConf := &NetConf{
Kubernetes: Kubernetes{
Kubeconfig: "foo",
NodeName: "bar",
K8sAPIRoot: "http://localhost:8080",
Namespace: "baz",
},
Interface: Interface{
MTU: 1234,
DisableIPv4: true,
DisableIPv6: true,
},
LogLevel: "info",
}
testData, err := json.Marshal(testConf)
if err != nil {
t.Fatal("marshal test data", err)
}

t.Run("FromFile", func(t *testing.T) {
t.Parallel()
f, err := os.CreateTemp("", "")
Expand All @@ -109,6 +111,13 @@ func TestNetConf(t *testing.T) {
if !testConf.DeepEqual(conf) {
t.Errorf("expected config to be equal to test config, got %v", conf)
}
t.Run("NonExist", func(t *testing.T) {
t.Parallel()
_, err := LoadNetConfFromFile("nonexist")
if err == nil {
t.Error("expected error, got nil")
}
})
})

t.Run("FromArgs", func(t *testing.T) {
Expand All @@ -123,22 +132,159 @@ func TestNetConf(t *testing.T) {
t.Errorf("expected config to be equal to test config, got %v", conf)
}
})

t.Run("InvalidData", func(t *testing.T) {
_, err := DecodeNetConf([]byte("invalid"))
if err == nil {
t.Error("expected error, got nil")
}
})
})

t.Run("Logging", func(t *testing.T) {
t.Parallel()

t.Run("NewLogger", func(t *testing.T) {
t.Parallel()
// NewLogger should never return nil.
log := testConf.NewLogger(&skel.CmdArgs{})
if log == nil {
t.Error("expected logger to not be nil")
}
})

t.Run("LogLevel", func(t *testing.T) {
t.Run("LogWriter", func(t *testing.T) {
t.Parallel()
conf := &NetConf{}
tc := []struct {
name string
level string
expected io.Writer
}{
{
name: "Default",
expected: os.Stderr,
},
{
name: "Debug",
level: "debug",
expected: os.Stderr,
},
{
name: "Info",
level: "info",
expected: os.Stderr,
},
{
name: "Warn",
level: "warn",
expected: os.Stderr,
},
{
name: "Error",
level: "error",
expected: os.Stderr,
},
{
name: "Silent",
level: "silent",
expected: io.Discard,
},
{
name: "Off",
level: "off",
expected: io.Discard,
},
}
for _, c := range tc {
conf.LogLevel = c.level
if conf.LogWriter() != c.expected {
t.Errorf("expected log writer to be %v, got %v", c.expected, conf.LogWriter())
}
}
})

t.Run("LogWriter", func(t *testing.T) {
t.Run("LogLevels", func(t *testing.T) {
t.Parallel()
conf := &NetConf{}
tc := []struct {
name string
level string
expected slog.Level
}{
{
name: "Default",
expected: slog.LevelInfo,
},
{
name: "Debug",
level: "debug",
expected: slog.LevelDebug,
},
{
name: "DebugAllCaps",
level: "DEBUG",
expected: slog.LevelDebug,
},
{
name: "DebugMixedCase",
level: "DeBuG",
expected: slog.LevelDebug,
},
{
name: "Info",
level: "info",
expected: slog.LevelInfo,
},
{
name: "InfoAllCaps",
level: "INFO",
expected: slog.LevelInfo,
},
{
name: "InfoMixedCase",
level: "InFo",
expected: slog.LevelInfo,
},
{
name: "Warn",
level: "warn",
expected: slog.LevelWarn,
},
{
name: "WarnAllCaps",
level: "WARN",
expected: slog.LevelWarn,
},
{
name: "WarnMixedCase",
level: "WaRn",
expected: slog.LevelWarn,
},
{
name: "Error",
level: "error",
expected: slog.LevelError,
},
{
name: "ErrorAllCaps",
level: "ERROR",
expected: slog.LevelError,
},
{
name: "ErrorMixedCase",
level: "ErRoR",
expected: slog.LevelError,
},
}
for _, c := range tc {
conf.LogLevel = c.level
if conf.SlogLevel() != c.expected {
t.Errorf("expected slog level to be %v, got %v", c.expected, conf.SlogLevel())
}
}
})

})

t.Run("PeerContainers", func(t *testing.T) {
Expand Down

0 comments on commit 820a546

Please sign in to comment.