@@ -6,13 +6,12 @@ package cri_test
66
77import (
88 "encoding/json"
9- "errors"
109 "os"
1110 "testing"
1211 "time"
1312
1413 "github.com/cosi-project/runtime/pkg/resource"
15- "github.com/siderolabs/go-retry/retry "
14+ "github.com/stretchr/testify/assert "
1615 "github.com/stretchr/testify/suite"
1716
1817 "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/cri"
@@ -24,7 +23,7 @@ import (
2423func (suite * CRISeccompProfileFileSuite ) TestReconcileSeccompProfileFile () {
2524 // need to mock mountStatus so that the controller moves ahead with the actual code
2625 mountStatus := runtimeres .NewMountStatus (runtimeres .NamespaceName , "EPHEMERAL" )
27- suite .Require (). NoError ( suite . State (). Create (suite . Ctx (), mountStatus ) )
26+ suite .Create (mountStatus )
2827
2928 for _ , tt := range []struct {
3029 seccompProfileName string
@@ -46,67 +45,55 @@ func (suite *CRISeccompProfileFileSuite) TestReconcileSeccompProfileFile() {
4645 seccompProfiles := criseccompresource .NewSeccompProfile (tt .seccompProfileName )
4746 seccompProfiles .TypedSpec ().Name = tt .seccompProfileName
4847 seccompProfiles .TypedSpec ().Value = tt .seccompProfileValue
49- suite .Require (). NoError ( suite . State (). Create (suite . Ctx (), seccompProfiles ) )
48+ suite .Create (seccompProfiles )
5049
51- suite .AssertWithin (1 * time .Second , 100 * time .Millisecond , func () error {
52- if _ , err := os .Stat (suite .seccompProfilesDirectory + "/" + tt .seccompProfileName ); err != nil {
53- if errors .Is (err , os .ErrNotExist ) {
54- return retry .ExpectedError (err )
55- }
50+ suite .EventuallyWithT (func (collect * assert.CollectT ) {
51+ asrt := assert .New (collect )
5652
57- return err
53+ if ! asrt .FileExists (suite .seccompProfilesDirectory + "/" + tt .seccompProfileName ) {
54+ return
5855 }
5956
6057 seccompProfileContent , err := os .ReadFile (suite .seccompProfilesDirectory + "/" + tt .seccompProfileName )
61- suite . Assert () .NoError (err )
58+ asrt .NoError (err )
6259
6360 expectedSeccompProfileContent , err := json .Marshal (tt .seccompProfileValue )
64- suite . Assert () .NoError (err )
61+ asrt .NoError (err )
6562
66- suite .Assert ().Equal (seccompProfileContent , expectedSeccompProfileContent )
67-
68- return nil
69- })
63+ asrt .Equal (seccompProfileContent , expectedSeccompProfileContent )
64+ }, time .Second , 100 * time .Millisecond )
7065 }
7166
7267 // create a directory and file manually in the seccomp profile directory
7368 // ensure that the controller deletes the manually created directory/file
7469 // also ensure that an update doesn't update existing files timestamp
75- suite .Assert ().NoError (os .Mkdir (suite .seccompProfilesDirectory + "/test" , 0o755 ))
76- suite .Assert ().NoError (os .WriteFile (suite .seccompProfilesDirectory + "/test.json" , []byte ("{}" ), 0o644 ))
70+ suite .Require ().NoError (os .Mkdir (suite .seccompProfilesDirectory + "/test" , 0o755 ))
71+ suite .Require ().NoError (os .WriteFile (suite .seccompProfilesDirectory + "/test.json" , []byte ("{}" ), 0o644 ))
7772
7873 auditJSONSeccompProfile , err := os .Stat (suite .seccompProfilesDirectory + "/audit.json" )
79- suite .Assert ().NoError (err )
74+ suite .Require ().NoError (err )
8075
8176 // delete deny.json resource
82- suite .Assert ().NoError (suite .State ().Destroy (suite .Ctx (), resource .NewMetadata (criseccompresource .NamespaceName , criseccompresource .SeccompProfileType , "deny.json" , resource .VersionUndefined )))
77+ suite .Require ().NoError (suite .State ().Destroy (suite .Ctx (), resource .NewMetadata (criseccompresource .NamespaceName , criseccompresource .SeccompProfileType , "deny.json" , resource .VersionUndefined )))
8378
84- suite .AssertWithin (1 * time .Second , 100 * time .Millisecond , func () error {
85- auditJSONSeccompProfileAfterUpdate , err := os .Stat (suite .seccompProfilesDirectory + "/audit.json" )
86- if err != nil {
87- if errors .Is (err , os .ErrNotExist ) {
88- return retry .ExpectedError (err )
89- }
79+ suite .EventuallyWithT (func (collect * assert.CollectT ) {
80+ asrt := assert .New (collect )
9081
91- return err
82+ auditJSONSeccompProfileAfterUpdate , err := os .Stat (suite .seccompProfilesDirectory + "/audit.json" )
83+ if ! asrt .NoError (err ) {
84+ return
9285 }
9386
94- suite .Eventually (func () bool {
95- return suite .NoFileExists (suite .seccompProfilesDirectory + "/deny.json" )
96- }, 1 * time .Second , 100 * time .Millisecond )
87+ asrt .Equal (auditJSONSeccompProfile .ModTime (), auditJSONSeccompProfileAfterUpdate .ModTime ())
88+ }, 1 * time .Second , 100 * time .Millisecond )
9789
98- suite .Eventually (func () bool {
99- return suite .NoFileExists (suite .seccompProfilesDirectory + "/test.json" )
100- }, 1 * time .Second , 100 * time .Millisecond )
90+ suite .EventuallyWithT (func (collect * assert.CollectT ) {
91+ asrt := assert .New (collect )
10192
102- suite .Eventually (func () bool {
103- return suite .NoDirExists (suite .seccompProfilesDirectory + "/test" )
104- }, 1 * time .Second , 100 * time .Millisecond )
105-
106- suite .Assert ().Equal (auditJSONSeccompProfile .ModTime (), auditJSONSeccompProfileAfterUpdate .ModTime ())
107-
108- return nil
109- })
93+ asrt .NoFileExists (suite .seccompProfilesDirectory + "/deny.json" )
94+ asrt .NoFileExists (suite .seccompProfilesDirectory + "/test.json" )
95+ asrt .NoDirExists (suite .seccompProfilesDirectory + "/test" )
96+ }, 1 * time .Second , 100 * time .Millisecond )
11097}
11198
11299func TestSeccompProfileFileSuite (t * testing.T ) {
0 commit comments