Skip to content

Commit

Permalink
Fix test failure due to TempDir not removing files
Browse files Browse the repository at this point in the history
This only happens on our runners, but it is quite consistently crashing
tests.
  • Loading branch information
EduardGomezEscandell committed Feb 9, 2024
1 parent f731532 commit 565d35f
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions windows-agent/internal/cloudinit/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ import (
"github.com/stretchr/testify/require"
)

//nolint:revive // This is a test helper
func newForTesting(t *testing.T, ctx context.Context, conf cloudinit.Config, publicDir string) (cloudinit.CloudInit, error) {
t.Helper()

t.Cleanup(func() {
// Clean up the data directory, otherwise TempDir will fail
_ = os.RemoveAll(filepath.Join(publicDir, ".cloud-init"))
})

return cloudinit.New(ctx, conf, publicDir)
}

func TestNew(t *testing.T) {
t.Parallel()

Expand All @@ -38,7 +50,8 @@ func TestNew(t *testing.T) {
subcriptionErr: tc.breakWriteAgentData,
}

_, err := cloudinit.New(ctx, conf, publicDir)
_, err := newForTesting(t, ctx, conf, publicDir)

if tc.wantErr {
require.Error(t, err, "Cloud-init creation should have returned an error")
return
Expand Down Expand Up @@ -132,7 +145,7 @@ url = www.example.com/new/rickroll
}

// Test a clean filesystem (New calls WriteAgentData internally)
ci, err := cloudinit.New(ctx, conf, publicDir)
ci, err := newForTesting(t, ctx, conf, publicDir)
require.NoError(t, err, "cloudinit.New should return no error")
require.FileExists(t, path, "New() should have created an agent cloud-init file")

Expand Down Expand Up @@ -270,7 +283,7 @@ data:
conf := &mockConfig{}

// Test a clean filesystem (New calls WriteAgentData internally)
ci, err := cloudinit.New(ctx, conf, publicDir)
ci, err := newForTesting(t, ctx, conf, publicDir)
require.NoError(t, err, "Setup: cloud-init New should return no errors")

if !tc.noOldData {
Expand Down Expand Up @@ -347,7 +360,7 @@ func TestRemoveDistroData(t *testing.T) {
dir := filepath.Join(publicDir, ".cloud-init")
path := filepath.Join(dir, distroName+".user-data")

ci, err := cloudinit.New(ctx, &mockConfig{}, publicDir)
ci, err := newForTesting(t, ctx, &mockConfig{}, publicDir)
require.NoError(t, err, "Setup: cloud-init New should return no errors")

if !tc.dirDoesNotExist {
Expand Down

0 comments on commit 565d35f

Please sign in to comment.