Skip to content

Commit f90fb9b

Browse files
committed
Add unit tests
1 parent 98d8e68 commit f90fb9b

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

internal/file/file_operator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,37 +174,37 @@ func (fo *FileOperator) WriteManifestFile(
174174
tempManifestFilePath := manifestPath + ".tmp"
175175
tempFile, err := os.OpenFile(tempManifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm)
176176
if err != nil {
177-
return fmt.Errorf("failed to open temporary manifest file: %w", err)
177+
return fmt.Errorf("failed to open file %s: %w", tempManifestFilePath, err)
178178
}
179179

180180
if _, err = tempFile.Write(manifestJSON); err != nil {
181181
closeFile(ctx, tempFile)
182182

183-
return fmt.Errorf("failed to write to temporary manifest file: %w", err)
183+
return fmt.Errorf("failed to write to file %s: %w", tempManifestFilePath, err)
184184
}
185185

186186
closeFile(ctx, tempFile)
187187

188188
// Verify the contents of the temporary file is JSON
189189
file, err := os.ReadFile(tempManifestFilePath)
190190
if err != nil {
191-
return fmt.Errorf("failed to read temporary manifest file: %w", err)
191+
return fmt.Errorf("failed to read file %s: %w", tempManifestFilePath, err)
192192
}
193193

194194
var manifestFiles map[string]*model.ManifestFile
195195

196196
err = json.Unmarshal(file, &manifestFiles)
197197
if err != nil {
198198
if len(file) == 0 {
199-
return fmt.Errorf("temporary manifest file is empty: %w", err)
199+
return fmt.Errorf("file %s is empty: %w", tempManifestFilePath, err)
200200
}
201201

202-
return fmt.Errorf("failed to parse temporary manifest file: %w", err)
202+
return fmt.Errorf("failed to parse file %s: %w", tempManifestFilePath, err)
203203
}
204204

205205
// Rename the temporary file to the actual manifest file path
206206
if renameError := os.Rename(tempManifestFilePath, manifestPath); renameError != nil {
207-
return fmt.Errorf("failed to rename temporary manifest file: %w", renameError)
207+
return fmt.Errorf("failed to file %s to %s: %w", tempManifestFilePath, manifestPath, renameError)
208208
}
209209

210210
return nil

internal/file/file_operator_test.go

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package file
77

88
import (
99
"context"
10+
"encoding/json"
1011
"os"
1112
"path"
1213
"path/filepath"
@@ -44,13 +45,89 @@ func TestFileOperator_Write(t *testing.T) {
4445
assert.Equal(t, fileContent, data)
4546
}
4647

48+
func TestFileOperator_WriteManifestFile(t *testing.T) {
49+
tempDir := t.TempDir()
50+
manifestPath := path.Join(tempDir, "manifest.json")
51+
52+
manifestFiles := map[string]*model.ManifestFile{
53+
"/etc/nginx/nginx.conf": {
54+
ManifestFileMeta: &model.ManifestFileMeta{
55+
Name: "/etc/nginx/nginx.conf",
56+
Size: 1024,
57+
Hash: "6d232d32d44",
58+
Referenced: true,
59+
Unmanaged: false,
60+
},
61+
},
62+
"/etc/nginx/conf.d/default.conf": {
63+
ManifestFileMeta: &model.ManifestFileMeta{
64+
Name: "/etc/nginx/conf.d/default.conf",
65+
Size: 32342,
66+
Hash: "1eh32hd3792hd329",
67+
Referenced: true,
68+
Unmanaged: false,
69+
},
70+
},
71+
}
72+
73+
fileOperator := NewFileOperator(&sync.RWMutex{})
74+
err := fileOperator.WriteManifestFile(t.Context(), manifestFiles, tempDir, manifestPath)
75+
require.NoError(t, err)
76+
77+
assert.FileExists(t, manifestPath)
78+
assert.NoFileExists(t, manifestPath+".tmp")
79+
80+
// Verify the contents can be read back
81+
data, err := os.ReadFile(manifestPath)
82+
require.NoError(t, err)
83+
84+
var readBack map[string]*model.ManifestFile
85+
err = json.Unmarshal(data, &readBack)
86+
require.NoError(t, err)
87+
assert.Equal(t, manifestFiles, readBack)
88+
}
89+
90+
func TestFileOperator_WriteManifestFile_directoryCreationError(t *testing.T) {
91+
manifestPath := "/unknown/manifest.json"
92+
manifestDir := "/unknown"
93+
94+
fileOperator := NewFileOperator(&sync.RWMutex{})
95+
err := fileOperator.WriteManifestFile(t.Context(), make(map[string]*model.ManifestFile), manifestDir, manifestPath)
96+
require.Error(t, err)
97+
assert.Contains(t, err.Error(), "unable to create directory")
98+
}
99+
100+
func TestFileOperator_WriteManifestFile_tempFileCreationError(t *testing.T) {
101+
tempDir := t.TempDir()
102+
103+
// Create a file where we want to write the manifest
104+
manifestPath := path.Join(tempDir, "manifest.json")
105+
err := os.WriteFile(manifestPath, []byte("existing"), 0o400) // readonly
106+
require.NoError(t, err)
107+
108+
// Create readonly directory to prevent temp file creation
109+
err = os.Chmod(tempDir, 0o444)
110+
require.NoError(t, err)
111+
defer func() {
112+
revertPermissionsError := os.Chmod(tempDir, 0o755)
113+
require.NoError(t, revertPermissionsError)
114+
}()
115+
116+
fileOperator := NewFileOperator(&sync.RWMutex{})
117+
err = fileOperator.WriteManifestFile(t.Context(), make(map[string]*model.ManifestFile), tempDir, manifestPath)
118+
require.Error(t, err)
119+
assert.Contains(t, err.Error(), "failed to open file")
120+
}
121+
47122
func TestFileOperator_WriteManifestFile_fileMissing(t *testing.T) {
48123
tempDir := t.TempDir()
49124
manifestPath := "/unknown/manifest.json"
50125

51126
fileOperator := NewFileOperator(&sync.RWMutex{})
52127
err := fileOperator.WriteManifestFile(t.Context(), make(map[string]*model.ManifestFile), tempDir, manifestPath)
53-
assert.Error(t, err)
128+
require.Error(t, err)
129+
130+
assert.NoFileExists(t, manifestPath+".tmp")
54131
}
55132

56133
func TestFileOperator_MoveFile_fileExists(t *testing.T) {

0 commit comments

Comments
 (0)