Skip to content

Commit

Permalink
[fix] fix nginx temp configs cleanup (#11569)
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Paksashvili <[email protected]>
  • Loading branch information
ipaqsa authored Oct 22, 2024
1 parent c9d33b7 commit dc3acbd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
9 changes: 4 additions & 5 deletions internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,7 @@ func (n *NGINXController) testTemplate(cfg []byte) error {
if len(cfg) == 0 {
return fmt.Errorf("invalid NGINX configuration (empty)")
}
tmpDir := os.TempDir() + "/nginx"
tmpfile, err := os.CreateTemp(tmpDir, tempNginxPattern)
tmpfile, err := os.CreateTemp(filepath.Join(os.TempDir(), "nginx"), tempNginxPattern)
if err != nil {
return err
}
Expand Down Expand Up @@ -1112,11 +1111,11 @@ func (n *NGINXController) createLuaConfig(cfg *ngx_config.Configuration) error {
func cleanTempNginxCfg() error {
var files []string

err := filepath.Walk(os.TempDir(), func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(filepath.Join(os.TempDir(), "nginx"), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && os.TempDir() != path {
if info.IsDir() && path != filepath.Join(os.TempDir(), "nginx") {
return filepath.SkipDir
}

Expand All @@ -1135,7 +1134,7 @@ func cleanTempNginxCfg() error {
}

for _, file := range files {
err := os.Remove(file)
err = os.Remove(file)
if err != nil {
return err
}
Expand Down
24 changes: 18 additions & 6 deletions internal/ingress/controller/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,11 @@ func TestCleanTempNginxCfg(t *testing.T) {
t.Fatal(err)
}

tmpfile, err := os.CreateTemp("", tempNginxPattern)
tmpfile, err := os.CreateTemp(filepath.Join(os.TempDir(), "nginx"), tempNginxPattern)
if err != nil {
t.Fatal(err)
}
expectedDeletedFile := tmpfile.Name()
defer tmpfile.Close()

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

tmpfile, err = os.CreateTemp("", tempNginxPattern)
tmpfile, err = os.CreateTemp(filepath.Join(os.TempDir(), "nginx"), tempNginxPattern)
if err != nil {
t.Fatal(err)
}
expectedFile := tmpfile.Name()
defer tmpfile.Close()

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

var files []string

err = filepath.Walk(os.TempDir(), func(path string, info os.FileInfo, _ error) error {
if info.IsDir() && os.TempDir() != path {
err = filepath.Walk(filepath.Join(os.TempDir(), "nginx"), func(path string, info os.FileInfo, _ error) error {
if info.IsDir() && filepath.Join(os.TempDir(), "nginx") != path {
return filepath.SkipDir
}

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

if len(files) != 1 {
t.Errorf("expected one file but %d were found", len(files))
// some other files can be created by other tests
var found bool
for _, file := range files {
if file == expectedDeletedFile {
t.Errorf("file %s should be deleted", file)
}
if file == expectedFile {
found = true
}
}
if !found {
t.Errorf("file %s should not be deleted", expectedFile)
}
}

Expand Down

0 comments on commit dc3acbd

Please sign in to comment.