[fix] fix nginx temp configs cleanup (#11569)

Signed-off-by: Stepan Paksashvili <stepan.paksashvili@flant.com>
This commit is contained in:
Stepan Paksashvili 2024-10-22 18:38:53 +03:00 committed by GitHub
parent c9d33b75d5
commit dc3acbd786
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 11 deletions

View file

@ -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
}
@ -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
}
@ -1135,7 +1134,7 @@ func cleanTempNginxCfg() error {
}
for _, file := range files {
err := os.Remove(file)
err = os.Remove(file)
if err != nil {
return err
}

View file

@ -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")
@ -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()
@ -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
}
@ -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)
}
}