Skip to content

Commit

Permalink
Satisfy linter
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Jan 19, 2024
1 parent f2d87c7 commit d0816cb
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 100 deletions.
15 changes: 7 additions & 8 deletions pkg/netconf/chrony_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"github.com/metal-stack/metal-hammer/pkg/api"
mn "github.com/metal-stack/metal-lib/pkg/net"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestChronyServiceEnabler_Enable(t *testing.T) {
assert := assert.New(t)

vrf := int64(104009)
external := mn.External
network := &models.V1MachineNetwork{Networktype: &external, Destinationprefixes: []string{IPv4ZeroCIDR}, Vrf: &vrf}
Expand All @@ -32,13 +31,13 @@ func TestChronyServiceEnabler_Enable(t *testing.T) {
},
}

for _, t := range tests {
e, err := newChronyServiceEnabler(t.kb)
if t.isErrorExpected {
assert.Error(err)
for _, tt := range tests {
e, err := newChronyServiceEnabler(tt.kb)
if tt.isErrorExpected {
require.Error(t, err)
} else {
assert.NoError(err)
require.NoError(t, err)
}
assert.Equal(t.vrf, e.vrf)
assert.Equal(t, tt.vrf, e.vrf)
}
}
9 changes: 5 additions & 4 deletions pkg/netconf/configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewConfigurator(t *testing.T) {
Expand All @@ -21,9 +22,9 @@ func TestNewConfigurator(t *testing.T) {
},
}

for _, test := range tests {
actual, err := NewConfigurator(test.kind, config{}, false)
assert.NoError(t, err)
assert.IsType(t, test.expected, actual)
for _, tt := range tests {
actual, err := NewConfigurator(tt.kind, config{}, false)
require.NoError(t, err)
assert.IsType(t, tt.expected, actual)
}
}
13 changes: 6 additions & 7 deletions pkg/netconf/frr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

Expand Down Expand Up @@ -72,39 +73,37 @@ func TestFrrConfigApplier(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
log := zaptest.NewLogger(t).Sugar()
kb, err := New(log, test.input)
assert.NoError(t, err)
require.NoError(t, err)
a := NewFrrConfigApplier(test.configuratorType, *kb, "")
b := bytes.Buffer{}

tpl := MustParseTpl(test.tpl)
err = a.Render(&b, *tpl)
assert.NoError(t, err)
require.NoError(t, err)

// eases adjustment of test fixtures
// just remove old test fixture after a code change
// let the new fixtures get generated
// check them manually before commit
if _, err := os.Stat(test.expectedOutput); os.IsNotExist(err) {
err = os.WriteFile(test.expectedOutput, b.Bytes(), fileModeDefault)
assert.NoError(t, err)
require.NoError(t, err)
return
}

expected, err := os.ReadFile(test.expectedOutput)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, string(expected), b.String())
})
}
}

func TestFRRValidator_Validate(t *testing.T) {
assert := assert.New(t)
log := zaptest.NewLogger(t).Sugar()

validator := frrValidator{
log: log,
}
actual := validator.Validate()
assert.NotNil(actual)
assert.NotNil(actual.Error())
require.Error(t, actual)
}
10 changes: 5 additions & 5 deletions pkg/netconf/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func TestNameHostname(t *testing.T) {
assert := assert.New(t)
expected, err := os.ReadFile("testdata/hostname")
assert.NoError(err)
require.NoError(t, err)

log := zaptest.NewLogger(t).Sugar()
kb, err := New(log, "testdata/firewall.yaml")
assert.NoError(err)
require.NoError(t, err)

a := newHostnameApplier(*kb, "")
b := bytes.Buffer{}

tpl := MustParseTpl(tplHostname)
err = a.Render(&b, *tpl)
assert.NoError(err)
assert.Equal(string(expected), b.String())
require.NoError(t, err)
assert.Equal(t, string(expected), b.String())
}
11 changes: 5 additions & 6 deletions pkg/netconf/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func TestNewHostsApplier(t *testing.T) {
assert := assert.New(t)

expected, err := os.ReadFile("testdata/hosts")
assert.NoError(err)
require.NoError(t, err)

log := zaptest.NewLogger(t).Sugar()
kb, err := New(log, "testdata/firewall.yaml")
assert.NoError(err)
require.NoError(t, err)
a := newHostsApplier(*kb, "")
b := bytes.Buffer{}

tpl := MustParseTpl(tplHosts)
err = a.Render(&b, *tpl)
assert.NoError(err)
assert.Equal(string(expected), b.String())
require.NoError(t, err)
assert.Equal(t, string(expected), b.String())
}
4 changes: 2 additions & 2 deletions pkg/netconf/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func TestIfacesApplier(t *testing.T) {
systemdNetworkPath = old
}()
kb, err := New(log, tc.input)
assert.NoError(t, err)
require.NoError(t, err)
a := newIfacesApplier(tc.configuratorType, *kb)
a.Apply()
if equal, s := equalDirs(systemdNetworkPath, tc.expectedOutput); !equal {
Expand Down
102 changes: 51 additions & 51 deletions pkg/netconf/knowledgebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,86 +8,86 @@ import (
"github.com/metal-stack/metal-hammer/pkg/api"
mn "github.com/metal-stack/metal-lib/pkg/net"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func mustNewKnowledgeBase(t *testing.T) config {
log := zaptest.NewLogger(t).Sugar()

d, err := New(log, "testdata/firewall.yaml")
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, d)

return *d
}

func TestNewKnowledgeBase(t *testing.T) {
assert := assert.New(t)

d := mustNewKnowledgeBase(t)

assert.Equal("firewall", d.Hostname)
assert.NotEmpty(d.Networks)
assert.Equal(5, len(d.Networks))
assert.Equal(t, "firewall", d.Hostname)
assert.NotEmpty(t, d.Networks)
assert.Len(t, d.Networks, 5)

// private network
n := d.Networks[0]
assert.Equal(1, len(n.Ips))
assert.Equal("10.0.16.2", n.Ips[0])
assert.Equal(1, len(n.Prefixes))
assert.Equal("10.0.16.0/22", n.Prefixes[0])
assert.True(*n.Private)
assert.Equal(mn.PrivatePrimaryUnshared, *n.Networktype)
assert.Equal(int64(3981), *n.Vrf)
assert.Len(t, n.Ips, 1)
assert.Equal(t, "10.0.16.2", n.Ips[0])
assert.Len(t, n.Prefixes, 1)
assert.Equal(t, "10.0.16.0/22", n.Prefixes[0])
assert.True(t, *n.Private)
assert.Equal(t, mn.PrivatePrimaryUnshared, *n.Networktype)
assert.Equal(t, int64(3981), *n.Vrf)

// private shared network
n = d.Networks[1]
assert.Equal(1, len(n.Ips))
assert.Equal("10.0.18.2", n.Ips[0])
assert.Equal(1, len(n.Prefixes))
assert.Equal("10.0.18.0/22", n.Prefixes[0])
assert.True(*n.Private)
assert.Equal(mn.PrivateSecondaryShared, *n.Networktype)
assert.Equal(int64(3982), *n.Vrf)
assert.Len(t, n.Ips, 1)
assert.Equal(t, "10.0.18.2", n.Ips[0])
assert.Len(t, n.Prefixes, 1)
assert.Equal(t, "10.0.18.0/22", n.Prefixes[0])
assert.True(t, *n.Private)
assert.Equal(t, mn.PrivateSecondaryShared, *n.Networktype)
assert.Equal(t, int64(3982), *n.Vrf)

// public networks
n = d.Networks[2]
assert.Equal(1, len(n.Destinationprefixes))
assert.Equal(IPv4ZeroCIDR, n.Destinationprefixes[0])
assert.Equal(1, len(n.Ips))
assert.Equal("185.1.2.3", n.Ips[0])
assert.Equal(2, len(n.Prefixes))
assert.Equal("185.1.2.0/24", n.Prefixes[0])
assert.Equal("185.27.0.0/22", n.Prefixes[1])
assert.False(*n.Underlay)
assert.False(*n.Private)
assert.True(*n.Nat)
assert.Equal(mn.External, *n.Networktype)
assert.Equal(int64(104009), *n.Vrf)
assert.Len(t, n.Destinationprefixes, 1)
assert.Equal(t, IPv4ZeroCIDR, n.Destinationprefixes[0])
assert.Len(t, n.Ips, 1)
assert.Equal(t, "185.1.2.3", n.Ips[0])
assert.Len(t, n.Prefixes, 2)
assert.Equal(t, "185.1.2.0/24", n.Prefixes[0])
assert.Equal(t, "185.27.0.0/22", n.Prefixes[1])
assert.False(t, *n.Underlay)
assert.False(t, *n.Private)
assert.True(t, *n.Nat)
assert.Equal(t, mn.External, *n.Networktype)
assert.Equal(t, int64(104009), *n.Vrf)

// underlay network
n = d.Networks[3]
assert.Equal(int64(4200003073), *n.Asn)
assert.Equal(1, len(n.Ips))
assert.Equal("10.1.0.1", n.Ips[0])
assert.Equal(1, len(n.Prefixes))
assert.Equal("10.0.12.0/22", n.Prefixes[0])
assert.True(*n.Underlay)
assert.Equal(mn.Underlay, *n.Networktype)
assert.Equal(t, int64(4200003073), *n.Asn)
assert.Len(t, n.Ips, 1)
assert.Equal(t, "10.1.0.1", n.Ips[0])
assert.Len(t, n.Prefixes, 1)
assert.Equal(t, "10.0.12.0/22", n.Prefixes[0])
assert.True(t, *n.Underlay)
assert.Equal(t, mn.Underlay, *n.Networktype)

// public network mpls
n = d.Networks[4]
assert.Equal(1, len(n.Destinationprefixes))
assert.Equal("100.127.1.0/24", n.Destinationprefixes[0])
assert.Equal(1, len(n.Ips))
assert.Equal("100.127.129.1", n.Ips[0])
assert.Equal(1, len(n.Prefixes))
assert.Equal("100.127.129.0/24", n.Prefixes[0])
assert.False(*n.Underlay)
assert.False(*n.Private)
assert.True(*n.Nat)
assert.Equal(mn.External, *n.Networktype)
assert.Equal(int64(104010), *n.Vrf)
assert.Len(t, n.Destinationprefixes, 1)
assert.Equal(t, "100.127.1.0/24", n.Destinationprefixes[0])
assert.Len(t, n.Ips, 1)
assert.Equal(t, "100.127.129.1", n.Ips[0])
assert.Len(t, n.Prefixes, 1)
assert.Equal(t, "100.127.129.0/24", n.Prefixes[0])
assert.False(t, *n.Underlay)
assert.False(t, *n.Private)
assert.True(t, *n.Nat)
assert.Equal(t, mn.External, *n.Networktype)
assert.Equal(t, int64(104010), *n.Vrf)
}

var (
Expand Down Expand Up @@ -184,10 +184,10 @@ func TestKnowledgeBase_Validate(t *testing.T) {
t.Run(fmt.Sprintf("testcase %d - kind %v", i, kind), func(t *testing.T) {
actualErr := test.kb.Validate(kind)
if test.expectedErrMsg == "" {
assert.NoError(t, actualErr)
require.NoError(t, actualErr)
return
}
assert.EqualError(t, actualErr, test.expectedErrMsg, "expected error: %s", test.expectedErrMsg)
require.EqualError(t, actualErr, test.expectedErrMsg, "expected error: %s", test.expectedErrMsg)
})
}
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/netconf/nftables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

func TestCompileNftRules(t *testing.T) {
assert := assert.New(t)

tests := []struct {
input string
Expand Down Expand Up @@ -54,18 +54,18 @@ func TestCompileNftRules(t *testing.T) {
tt := tt
t.Run(tt.input, func(t *testing.T) {
expected, err := os.ReadFile(tt.expected)
assert.NoError(err)
require.NoError(t, err)

kb, err := New(log, tt.input)
assert.NoError(err)
require.NoError(t, err)

a := newNftablesConfigApplier(*kb, nil, tt.enableDNSProxy)
b := bytes.Buffer{}

tpl := MustParseTpl(TplNftables)
err = a.Render(&b, *tpl)
assert.NoError(err)
assert.Equal(string(expected), b.String())
require.NoError(t, err)
assert.Equal(t, string(expected), b.String())
})
}
}
4 changes: 2 additions & 2 deletions pkg/netconf/routemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

Expand Down Expand Up @@ -259,7 +259,7 @@ func Test_importRulesForNetwork(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
kb, err := New(log, tt.input)
assert.NoError(t, err)
require.NoError(t, err)
err = kb.Validate(Firewall)
if err != nil {
t.Errorf("%s is not valid: %v", tt.input, err)
Expand Down
Loading

0 comments on commit d0816cb

Please sign in to comment.