Skip to content

Commit

Permalink
fix: redo assertHostnames in HostnameMergeSuite.TestMerge
Browse files Browse the repository at this point in the history
Use `rtestutils.AssertResources` for hostnames test.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Mar 6, 2023
1 parent 8ea4bfa commit e71cc66
Show file tree
Hide file tree
Showing 19 changed files with 659 additions and 1,556 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ require (
github.com/containernetworking/plugins v1.2.0
github.com/coreos/go-iptables v0.6.0
github.com/coreos/go-semver v0.3.1
github.com/cosi-project/runtime v0.3.0-alpha.7
github.com/cosi-project/runtime v0.3.0-alpha.8
github.com/docker/distribution v2.8.1+incompatible
github.com/docker/docker v23.0.1+incompatible
github.com/docker/go-connections v0.4.0
Expand Down Expand Up @@ -197,7 +197,7 @@ require (
github.com/gorilla/mux v1.8.0 // indirect
github.com/gosuri/uilive v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-memdb v1.3.4 // indirect
Expand Down
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cosi-project/runtime v0.3.0-alpha.7 h1:Zzvv9Y8lfjRKQz5rvn4Jq98Sb+d45ymPMisqORwrau0=
github.com/cosi-project/runtime v0.3.0-alpha.7/go.mod h1:pKny4SfaG5BBJ1x4aIWc5OyN90oXMhcbrNdC3wgdoYg=
github.com/cosi-project/runtime v0.3.0-alpha.8 h1:/3SEV/8BdQd3NMSounHLKM216JKePl1TQBdvJ6HduwY=
github.com/cosi-project/runtime v0.3.0-alpha.8/go.mod h1:PjzGCJydvt9PMJJ5sno/Ru6EIbIODgfuxSBEPDl15fg=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
Expand Down Expand Up @@ -762,8 +762,8 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 h1:1JYBfzqrWPcCclBwxFCPAou9n+q86mfnu7NAeHfte7A=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0/go.mod h1:YDZoGHuwE+ov0c8smSH49WLF3F2LaWnYYuDVd+EWrc0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 h1:gDLXvp5S9izjldquuoAhDzccbskOL6tDC5jMSyx3zxE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2/go.mod h1:7pdNwVWBBHGiCxa9lAszqCJMbfTISJ7oMftp8+UGV08=
github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
Expand Down Expand Up @@ -1369,7 +1369,7 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
Expand Down
143 changes: 40 additions & 103 deletions internal/app/machined/pkg/controllers/network/address_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
"time"

"github.com/cosi-project/runtime/pkg/controller/runtime"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/resource/rtestutils"
"github.com/cosi-project/runtime/pkg/state"
"github.com/cosi-project/runtime/pkg/state/impl/inmem"
"github.com/cosi-project/runtime/pkg/state/impl/namespaced"
"github.com/siderolabs/go-pointer"
"github.com/siderolabs/go-procfs/procfs"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

netctrl "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/network"
Expand Down Expand Up @@ -69,61 +69,22 @@ func (suite *AddressConfigSuite) startRuntime() {
}()
}

func (suite *AddressConfigSuite) assertAddresses(requiredIDs []string, check func(*network.AddressSpec) error) error {
missingIDs := make(map[string]struct{}, len(requiredIDs))

for _, id := range requiredIDs {
missingIDs[id] = struct{}{}
}

resources, err := suite.state.List(
suite.ctx,
resource.NewMetadata(network.ConfigNamespaceName, network.AddressSpecType, "", resource.VersionUndefined),
)
if err != nil {
return err
}

for _, res := range resources.Items {
_, required := missingIDs[res.Metadata().ID()]
if !required {
continue
}

delete(missingIDs, res.Metadata().ID())

if err = check(res.(*network.AddressSpec)); err != nil {
return retry.ExpectedError(err)
}
}

if len(missingIDs) > 0 {
return retry.ExpectedError(fmt.Errorf("some resources are missing: %q", missingIDs))
}

return nil
func (suite *AddressConfigSuite) assertAddresses(requiredIDs []string, check func(*network.AddressSpec, *assert.Assertions)) {
assertResources(suite.ctx, suite.T(), suite.state, requiredIDs, check, rtestutils.WithNamespace(network.ConfigNamespaceName))
}

func (suite *AddressConfigSuite) TestLoopback() {
suite.Require().NoError(suite.runtime.RegisterController(&netctrl.AddressConfigController{}))

suite.startRuntime()

suite.Assert().NoError(
retry.Constant(3*time.Second, retry.WithUnits(100*time.Millisecond)).Retry(
func() error {
return suite.assertAddresses(
[]string{
"default/lo/127.0.0.1/8",
}, func(r *network.AddressSpec) error {
suite.Assert().Equal("lo", r.TypedSpec().LinkName)
suite.Assert().Equal(nethelpers.ScopeHost, r.TypedSpec().Scope)

return nil
},
)
},
),
suite.assertAddresses(
[]string{
"default/lo/127.0.0.1/8",
}, func(r *network.AddressSpec, asrt *assert.Assertions) {
asrt.Equal("lo", r.TypedSpec().LinkName)
asrt.Equal(nethelpers.ScopeHost, r.TypedSpec().Scope)
},
)
}

Expand All @@ -138,26 +99,18 @@ func (suite *AddressConfigSuite) TestCmdline() {

suite.startRuntime()

suite.Assert().NoError(
retry.Constant(3*time.Second, retry.WithUnits(100*time.Millisecond)).Retry(
func() error {
return suite.assertAddresses(
[]string{
"cmdline/eth1/172.20.0.2/24",
"cmdline/eth4/10.3.5.7/24",
}, func(r *network.AddressSpec) error {
switch r.Metadata().ID() {
case "cmdline/eth1/172.20.0.2/24":
suite.Assert().Equal("eth1", r.TypedSpec().LinkName)
case "cmdline/eth4/10.3.5.7/24":
suite.Assert().Equal("eth4", r.TypedSpec().LinkName)
}

return nil
},
)
},
),
suite.assertAddresses(
[]string{
"cmdline/eth1/172.20.0.2/24",
"cmdline/eth4/10.3.5.7/24",
}, func(r *network.AddressSpec, asrt *assert.Assertions) {
switch r.Metadata().ID() {
case "cmdline/eth1/172.20.0.2/24":
asrt.Equal("eth1", r.TypedSpec().LinkName)
case "cmdline/eth4/10.3.5.7/24":
asrt.Equal("eth4", r.TypedSpec().LinkName)
}
},
)
}

Expand Down Expand Up @@ -190,21 +143,13 @@ func (suite *AddressConfigSuite) TestCmdlineNoNetmask() {

suite.Assert().NotEmpty(ifaceName)

suite.Assert().NoError(
retry.Constant(3*time.Second, retry.WithUnits(100*time.Millisecond)).Retry(
func() error {
return suite.assertAddresses(
[]string{
fmt.Sprintf("cmdline/%s/172.20.0.2/32", ifaceName),
}, func(r *network.AddressSpec) error {
suite.Assert().Equal(ifaceName, r.TypedSpec().LinkName)
suite.Assert().Equal(network.ConfigCmdline, r.TypedSpec().ConfigLayer)

return nil
},
)
},
),
suite.assertAddresses(
[]string{
fmt.Sprintf("cmdline/%s/172.20.0.2/32", ifaceName),
}, func(r *network.AddressSpec, asrt *assert.Assertions) {
asrt.Equal(ifaceName, r.TypedSpec().LinkName)
asrt.Equal(network.ConfigCmdline, r.TypedSpec().ConfigLayer)
},
)
}

Expand Down Expand Up @@ -276,24 +221,16 @@ func (suite *AddressConfigSuite) TestMachineConfiguration() {

suite.Require().NoError(suite.state.Create(suite.ctx, cfg))

suite.Assert().NoError(
retry.Constant(3*time.Second, retry.WithUnits(100*time.Millisecond)).Retry(
func() error {
return suite.assertAddresses(
[]string{
"configuration/eth2/2001:470:6d:30e:8ed2:b60c:9d2f:803a/64",
"configuration/eth3/192.168.0.24/28",
"configuration/eth5/10.5.0.7/32",
"configuration/eth6/10.5.0.8/32",
"configuration/eth6/2001:470:6d:30e:8ed2:b60c:9d2f:803b/64",
"configuration/eth0.24/10.0.0.1/8",
"configuration/eth0.25/11.0.0.1/8",
}, func(r *network.AddressSpec) error {
return nil
},
)
},
),
suite.assertAddresses(
[]string{
"configuration/eth2/2001:470:6d:30e:8ed2:b60c:9d2f:803a/64",
"configuration/eth3/192.168.0.24/28",
"configuration/eth5/10.5.0.7/32",
"configuration/eth6/10.5.0.8/32",
"configuration/eth6/2001:470:6d:30e:8ed2:b60c:9d2f:803b/64",
"configuration/eth0.24/10.0.0.1/8",
"configuration/eth0.25/11.0.0.1/8",
}, func(r *network.AddressSpec, asrt *assert.Assertions) {},
)
}

Expand Down

0 comments on commit e71cc66

Please sign in to comment.