Skip to content

Commit b248b68

Browse files
authored
ci: fix windows t.TempDir issue with concurrent delete (#4505)
1 parent 9b6a927 commit b248b68

File tree

3 files changed

+1807
-1947
lines changed

3 files changed

+1807
-1947
lines changed

internal/namespaces/instance/v1/custom_user_data_test.go

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,22 @@ func Test_UserDataList(t *testing.T) {
5252
}))
5353
}
5454

55-
func Test_UserDataFileUpload(t *testing.T) {
55+
func Test_UserDataFileUploadOn(t *testing.T) {
5656
content := "cloud-init file content"
57+
file, err := os.CreateTemp(t.TempDir(), "test")
58+
if err != nil {
59+
t.Fatalf("%s", err)
60+
}
61+
_, err = file.WriteString(content)
62+
if err != nil {
63+
t.Fatalf("%s", err)
64+
}
5765

58-
t.Run("on-cloud-init", core.Test(&core.TestConfig{
66+
t.Run("cloud-init", core.Test(&core.TestConfig{
5967
Commands: instance.GetCommands(),
6068
BeforeFunc: core.BeforeFuncCombine(
6169
core.ExecStoreBeforeCmd("Server", testServerCommand("stopped=true")),
6270
func(ctx *core.BeforeFuncCtx) error {
63-
file, _ := os.CreateTemp(t.TempDir(), "test")
64-
_, _ = file.WriteString(content)
6571
ctx.Meta["filePath"] = file.Name()
6672

6773
return nil
@@ -71,22 +77,40 @@ func Test_UserDataFileUpload(t *testing.T) {
7177
Check: core.TestCheckCombine(
7278
core.TestCheckGolden(),
7379
),
74-
AfterFunc: core.AfterFuncCombine(
75-
func(ctx *core.AfterFuncCtx) error {
76-
_ = os.RemoveAll(ctx.Meta["filePath"].(string))
80+
AfterFunc: func(_ *core.AfterFuncCtx) error {
81+
// We need to close this file explicitly because it is not closed by the os.Remove call on windows
82+
// https://github.com/golang/go/issues/50510
83+
err = file.Close()
84+
if err != nil {
85+
return err
86+
}
7787

78-
return nil
79-
},
80-
),
88+
err = os.RemoveAll(file.Name())
89+
if err != nil {
90+
return err
91+
}
92+
93+
return nil
94+
},
8195
}))
96+
}
8297

83-
t.Run("on-random-key", core.Test(&core.TestConfig{
98+
func Test_UserDataFileUploadOnRandom(t *testing.T) {
99+
content := "cloud-init file content"
100+
file, err := os.CreateTemp(t.TempDir(), "test")
101+
if err != nil {
102+
t.Fatalf("%s", err)
103+
}
104+
_, err = file.WriteString(content)
105+
if err != nil {
106+
t.Fatalf("%s", err)
107+
}
108+
109+
t.Run("key", core.Test(&core.TestConfig{
84110
Commands: instance.GetCommands(),
85111
BeforeFunc: core.BeforeFuncCombine(
86112
core.ExecStoreBeforeCmd("Server", testServerCommand("stopped=true")),
87113
func(ctx *core.BeforeFuncCtx) error {
88-
file, _ := os.CreateTemp(t.TempDir(), "test")
89-
_, _ = file.WriteString(content)
90114
ctx.Meta["filePath"] = file.Name()
91115

92116
return nil
@@ -96,12 +120,20 @@ func Test_UserDataFileUpload(t *testing.T) {
96120
Check: core.TestCheckCombine(
97121
core.TestCheckGolden(),
98122
),
99-
AfterFunc: core.AfterFuncCombine(
100-
func(ctx *core.AfterFuncCtx) error {
101-
_ = os.RemoveAll(ctx.Meta["filePath"].(string))
123+
AfterFunc: func(_ *core.AfterFuncCtx) error {
124+
// We need to close this file explicitly because it is not closed by the os.Remove call on windows
125+
// https://github.com/golang/go/issues/50510
126+
err = file.Close()
127+
if err != nil {
128+
return err
129+
}
102130

103-
return nil
104-
},
105-
),
131+
err = os.RemoveAll(file.Name())
132+
if err != nil {
133+
return err
134+
}
135+
136+
return nil
137+
},
106138
}))
107139
}

0 commit comments

Comments
 (0)