@@ -11,21 +11,16 @@ import (
1111 "net/url"
1212 "os"
1313 "path/filepath"
14- "sync"
1514 "testing"
1615 "time"
1716
18- "github.com/cosi-project/runtime/pkg/controller/runtime"
1917 "github.com/cosi-project/runtime/pkg/resource"
20- "github.com/cosi-project/runtime/pkg/state"
21- "github.com/cosi-project/runtime/pkg/state/impl/inmem"
22- "github.com/cosi-project/runtime/pkg/state/impl/namespaced"
2318 "github.com/siderolabs/go-pointer"
2419 "github.com/siderolabs/go-retry/retry"
2520 "github.com/stretchr/testify/assert"
2621 "github.com/stretchr/testify/suite"
27- "go.uber.org/zap/zaptest"
2822
23+ "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/ctest"
2924 netctrl "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/network"
3025 v1alpha1runtime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
3126 "github.com/siderolabs/talos/internal/pkg/mount/v3"
@@ -39,15 +34,7 @@ import (
3934)
4035
4136type EtcFileConfigSuite struct {
42- suite.Suite
43-
44- state state.State
45-
46- runtime * runtime.Runtime
47- wg sync.WaitGroup
48-
49- ctx context.Context //nolint:containedctx
50- ctxCancel context.CancelFunc
37+ ctest.DefaultSuite
5138
5239 cfg * config.MachineConfig
5340 defaultAddress * network.NodeAddress
@@ -60,18 +47,7 @@ type EtcFileConfigSuite struct {
6047 etcRoot xfs.Root
6148}
6249
63- func (suite * EtcFileConfigSuite ) SetupTest () {
64- suite .ctx , suite .ctxCancel = context .WithTimeout (context .Background (), 3 * time .Minute )
65-
66- suite .state = state .WrapCore (namespaced .NewState (inmem .Build ))
67-
68- var err error
69-
70- suite .runtime , err = runtime .NewRuntime (suite .state , zaptest .NewLogger (suite .T ()))
71- suite .Require ().NoError (err )
72-
73- suite .startRuntime ()
74-
50+ func (suite * EtcFileConfigSuite ) ExtraSetup () {
7551 ok , err := v1alpha1runtime .KernelCapabilities ().OpentreeOnAnonymousFS ()
7652 suite .Require ().NoError (err )
7753
@@ -87,7 +63,7 @@ func (suite *EtcFileConfigSuite) SetupTest() {
8763 suite .Require ().NoError (suite .etcRoot .OpenFS ())
8864 suite .Assert ().NoFileExists (suite .podResolvConfPath )
8965
90- suite .Require ().NoError (suite .runtime .RegisterController (& netctrl.EtcFileController {
66+ suite .Require ().NoError (suite .Runtime () .RegisterController (& netctrl.EtcFileController {
9167 V1Alpha1Mode : v1alpha1runtime .ModeMetal ,
9268 EtcRoot : suite .etcRoot ,
9369 BindMountTarget : suite .bindMountTarget ,
@@ -149,12 +125,6 @@ func (suite *EtcFileConfigSuite) SetupTest() {
149125 suite .hostDNSConfig .TypedSpec ().ServiceHostDNSAddress = netip .MustParseAddr ("169.254.116.108" )
150126}
151127
152- func (suite * EtcFileConfigSuite ) startRuntime () {
153- suite .wg .Go (func () {
154- suite .Assert ().NoError (suite .runtime .Run (suite .ctx ))
155- })
156- }
157-
158128type etcFileContents struct {
159129 hosts string
160130 resolvConf string
@@ -164,7 +134,7 @@ type etcFileContents struct {
164134//nolint:gocyclo
165135func (suite * EtcFileConfigSuite ) testFiles (resources []resource.Resource , contents etcFileContents ) {
166136 for _ , r := range resources {
167- suite .Require (). NoError ( suite . state . Create (suite . ctx , r ) )
137+ suite .Create (r )
168138 }
169139
170140 var (
@@ -184,10 +154,8 @@ func (suite *EtcFileConfigSuite) testFiles(resources []resource.Resource, conten
184154 unexpectedIDs = append (unexpectedIDs , "hosts" )
185155 }
186156
187- assertResources (
188- suite .ctx ,
189- suite .T (),
190- suite .state ,
157+ ctest .AssertResources (
158+ suite ,
191159 expectedIDs ,
192160 func (r * files.EtcFileSpec , asrt * assert.Assertions ) {
193161 switch r .Metadata ().ID () {
@@ -198,6 +166,7 @@ func (suite *EtcFileConfigSuite) testFiles(resources []resource.Resource, conten
198166 }
199167 },
200168 )
169+
201170 suite .Assert ().NoError (
202171 retry .Constant (10 * time .Second , retry .WithUnits (100 * time .Millisecond )).Retry (func () error {
203172 if contents .resolvGlobalConf == "" {
@@ -231,7 +200,7 @@ func (suite *EtcFileConfigSuite) testFiles(resources []resource.Resource, conten
231200 )
232201
233202 for _ , id := range unexpectedIDs {
234- assertNoResource [* files.EtcFileSpec ](suite . ctx , suite . T (), suite . state , id )
203+ ctest . AssertNoResource [* files.EtcFileSpec ](suite , id )
235204 }
236205}
237206
@@ -248,6 +217,19 @@ func (suite *EtcFileConfigSuite) TestComplete() {
248217 )
249218}
250219
220+ func (suite * EtcFileConfigSuite ) TestExtraHostsNoHostname () {
221+ suite .resolverStatus .TypedSpec ().SearchDomains = []string {"foo.example.com" }
222+
223+ suite .testFiles (
224+ []resource.Resource {suite .cfg , suite .resolverStatus , suite .hostDNSConfig },
225+ etcFileContents {
226+ hosts : "127.0.0.1 localhost\n ::1 localhost ip6-localhost ip6-loopback\n ff02::1 ip6-allnodes\n ff02::2 ip6-allrouters\n 10.0.0.1 a b\n 10.0.0.2 c d\n " ,
227+ resolvConf : "nameserver 127.0.0.53\n \n search foo.example.com\n " ,
228+ resolvGlobalConf : "nameserver 169.254.116.108\n \n search foo.example.com\n " ,
229+ },
230+ )
231+ }
232+
251233func (suite * EtcFileConfigSuite ) TestNoExtraHosts () {
252234 suite .resolverStatus .TypedSpec ().SearchDomains = []string {"foo.example.com" }
253235
@@ -301,7 +283,7 @@ func (suite *EtcFileConfigSuite) TestOnlyResolvers() {
301283 suite .testFiles (
302284 []resource.Resource {suite .resolverStatus , suite .hostDNSConfig },
303285 etcFileContents {
304- hosts : "" ,
286+ hosts : "127.0.0.1 localhost \n ::1 localhost ip6-localhost ip6-loopback \n ff02::1 ip6-allnodes \n ff02::2 ip6-allrouters \n " ,
305287 resolvConf : "nameserver 127.0.0.53\n " ,
306288 resolvGlobalConf : "nameserver 169.254.116.108\n " ,
307289 },
@@ -319,11 +301,7 @@ func (suite *EtcFileConfigSuite) TestOnlyHostname() {
319301 )
320302}
321303
322- func (suite * EtcFileConfigSuite ) TearDownTest () {
323- suite .T ().Log ("tear down" )
324-
325- suite .ctxCancel ()
326-
304+ func (suite * EtcFileConfigSuite ) ExtraTearDown () {
327305 if _ , err := os .Lstat (suite .podResolvConfPath ); err == nil {
328306 if suite .etcRoot .FSType () == "os" {
329307 suite .Require ().NoError (os .Remove (suite .podResolvConfPath ))
@@ -332,8 +310,6 @@ func (suite *EtcFileConfigSuite) TearDownTest() {
332310 }
333311 }
334312
335- suite .wg .Wait ()
336-
337313 if suite .etcRoot != nil {
338314 suite .Require ().NoError (os .RemoveAll (suite .bindMountTarget ))
339315
@@ -342,9 +318,25 @@ func (suite *EtcFileConfigSuite) TearDownTest() {
342318}
343319
344320func TestEtcFileConfigSuite (t * testing.T ) {
321+ t .Parallel ()
322+
345323 if os .Geteuid () != 0 {
346324 t .Skip ("skipping test that requires root privileges" )
347325 }
348326
349- suite .Run (t , new (EtcFileConfigSuite ))
327+ s := & EtcFileConfigSuite {
328+ DefaultSuite : ctest.DefaultSuite {
329+ Timeout : 10 * time .Second ,
330+ },
331+ }
332+
333+ s .AfterSetup = func (* ctest.DefaultSuite ) {
334+ s .ExtraSetup ()
335+ }
336+
337+ s .AfterTearDown = func (* ctest.DefaultSuite ) {
338+ s .ExtraTearDown ()
339+ }
340+
341+ suite .Run (t , s )
350342}
0 commit comments