Skip to content

Commit dc3acbd

Browse files
authored
[fix] fix nginx temp configs cleanup (#11569)
Signed-off-by: Stepan Paksashvili <stepan.paksashvili@flant.com>
1 parent c9d33b7 commit dc3acbd

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

internal/ingress/controller/nginx.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,7 @@ func (n *NGINXController) testTemplate(cfg []byte) error {
643643
if len(cfg) == 0 {
644644
return fmt.Errorf("invalid NGINX configuration (empty)")
645645
}
646-
tmpDir := os.TempDir() + "/nginx"
647-
tmpfile, err := os.CreateTemp(tmpDir, tempNginxPattern)
646+
tmpfile, err := os.CreateTemp(filepath.Join(os.TempDir(), "nginx"), tempNginxPattern)
648647
if err != nil {
649648
return err
650649
}
@@ -1112,11 +1111,11 @@ func (n *NGINXController) createLuaConfig(cfg *ngx_config.Configuration) error {
11121111
func cleanTempNginxCfg() error {
11131112
var files []string
11141113

1115-
err := filepath.Walk(os.TempDir(), func(path string, info os.FileInfo, err error) error {
1114+
err := filepath.Walk(filepath.Join(os.TempDir(), "nginx"), func(path string, info os.FileInfo, err error) error {
11161115
if err != nil {
11171116
return err
11181117
}
1119-
if info.IsDir() && os.TempDir() != path {
1118+
if info.IsDir() && path != filepath.Join(os.TempDir(), "nginx") {
11201119
return filepath.SkipDir
11211120
}
11221121

@@ -1135,7 +1134,7 @@ func cleanTempNginxCfg() error {
11351134
}
11361135

11371136
for _, file := range files {
1138-
err := os.Remove(file)
1137+
err = os.Remove(file)
11391138
if err != nil {
11401139
return err
11411140
}

internal/ingress/controller/nginx_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,11 @@ func TestCleanTempNginxCfg(t *testing.T) {
361361
t.Fatal(err)
362362
}
363363

364-
tmpfile, err := os.CreateTemp("", tempNginxPattern)
364+
tmpfile, err := os.CreateTemp(filepath.Join(os.TempDir(), "nginx"), tempNginxPattern)
365365
if err != nil {
366366
t.Fatal(err)
367367
}
368+
expectedDeletedFile := tmpfile.Name()
368369
defer tmpfile.Close()
369370

370371
dur, err := time.ParseDuration("-10m")
@@ -378,10 +379,11 @@ func TestCleanTempNginxCfg(t *testing.T) {
378379
t.Fatal(err)
379380
}
380381

381-
tmpfile, err = os.CreateTemp("", tempNginxPattern)
382+
tmpfile, err = os.CreateTemp(filepath.Join(os.TempDir(), "nginx"), tempNginxPattern)
382383
if err != nil {
383384
t.Fatal(err)
384385
}
386+
expectedFile := tmpfile.Name()
385387
defer tmpfile.Close()
386388

387389
err = cleanTempNginxCfg()
@@ -391,8 +393,8 @@ func TestCleanTempNginxCfg(t *testing.T) {
391393

392394
var files []string
393395

394-
err = filepath.Walk(os.TempDir(), func(path string, info os.FileInfo, _ error) error {
395-
if info.IsDir() && os.TempDir() != path {
396+
err = filepath.Walk(filepath.Join(os.TempDir(), "nginx"), func(path string, info os.FileInfo, _ error) error {
397+
if info.IsDir() && filepath.Join(os.TempDir(), "nginx") != path {
396398
return filepath.SkipDir
397399
}
398400

@@ -405,8 +407,18 @@ func TestCleanTempNginxCfg(t *testing.T) {
405407
t.Fatal(err)
406408
}
407409

408-
if len(files) != 1 {
409-
t.Errorf("expected one file but %d were found", len(files))
410+
// some other files can be created by other tests
411+
var found bool
412+
for _, file := range files {
413+
if file == expectedDeletedFile {
414+
t.Errorf("file %s should be deleted", file)
415+
}
416+
if file == expectedFile {
417+
found = true
418+
}
419+
}
420+
if !found {
421+
t.Errorf("file %s should not be deleted", expectedFile)
410422
}
411423
}
412424

0 commit comments

Comments
 (0)