-
Notifications
You must be signed in to change notification settings - Fork 96
/
manager_test.go
225 lines (191 loc) · 5.1 KB
/
manager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package file_test
import (
"errors"
"os"
"path/filepath"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file/filefakes"
)
var _ = Describe("EventHandler", func() {
Describe("Replace files", Ordered, func() {
var (
mgr *file.ManagerImpl
tmpDir string
regular1, regular2, regular3, secret file.File
)
ensureFiles := func(files []file.File) {
entries, err := os.ReadDir(tmpDir)
Expect(err).ShouldNot(HaveOccurred())
Expect(entries).Should(HaveLen(len(files)))
entriesMap := make(map[string]os.DirEntry)
for _, entry := range entries {
entriesMap[entry.Name()] = entry
}
for _, f := range files {
_, ok := entriesMap[filepath.Base(f.Path)]
Expect(ok).Should(BeTrue())
info, err := os.Stat(f.Path)
Expect(err).ShouldNot(HaveOccurred())
Expect(info.IsDir()).To(BeFalse())
if f.Type == file.TypeRegular {
Expect(info.Mode()).To(Equal(os.FileMode(0o644)))
} else {
Expect(info.Mode()).To(Equal(os.FileMode(0o640)))
}
bytes, err := os.ReadFile(f.Path)
Expect(err).ShouldNot(HaveOccurred())
Expect(bytes).To(Equal(f.Content))
}
}
ensureNotExist := func(files ...file.File) {
for _, f := range files {
_, err := os.Stat(f.Path)
Expect(os.IsNotExist(err)).To(BeTrue())
}
}
BeforeAll(func() {
mgr = file.NewManagerImpl(zap.New(), file.NewStdLibOSFileManager())
tmpDir = GinkgoT().TempDir()
regular1 = file.File{
Type: file.TypeRegular,
Path: filepath.Join(tmpDir, "regular-1.conf"),
Content: []byte("regular-1"),
}
regular2 = file.File{
Type: file.TypeRegular,
Path: filepath.Join(tmpDir, "regular-2.conf"),
Content: []byte("regular-2"),
}
regular3 = file.File{
Type: file.TypeRegular,
Path: filepath.Join(tmpDir, "regular-3.conf"),
Content: []byte("regular-3"),
}
secret = file.File{
Type: file.TypeSecret,
Path: filepath.Join(tmpDir, "secret.conf"),
Content: []byte("secret"),
}
})
It("should write initial config", func() {
files := []file.File{regular1, regular2, secret}
err := mgr.ReplaceFiles(files)
Expect(err).ShouldNot(HaveOccurred())
ensureFiles(files)
})
It("should write subsequent config", func() {
files := []file.File{
regular2, // overwriting
regular3, // adding
secret, // overwriting
}
err := mgr.ReplaceFiles(files)
Expect(err).ShouldNot(HaveOccurred())
ensureFiles(files)
ensureNotExist(regular1)
})
It("should remove all files", func() {
err := mgr.ReplaceFiles(nil)
Expect(err).ShouldNot(HaveOccurred())
ensureNotExist(regular2, regular3, secret)
})
})
When("file does not exist", func() {
It("should not error", func() {
fakeOSMgr := &filefakes.FakeOSFileManager{}
mgr := file.NewManagerImpl(zap.New(), fakeOSMgr)
files := []file.File{
{
Type: file.TypeRegular,
Path: "regular-1.conf",
Content: []byte("regular-1"),
},
}
Expect(mgr.ReplaceFiles(files)).ToNot(HaveOccurred())
fakeOSMgr.RemoveReturns(os.ErrNotExist)
Expect(mgr.ReplaceFiles(files)).ToNot(HaveOccurred())
})
})
When("file type is not supported", func() {
It("should panic", func() {
mgr := file.NewManagerImpl(zap.New(), nil)
files := []file.File{
{
Type: 123,
Path: "unsupported.conf",
},
}
replace := func() {
_ = mgr.ReplaceFiles(files)
}
Expect(replace).Should(Panic())
})
})
Describe("Edge cases with IO errors", func() {
var (
files = []file.File{
{
Type: file.TypeRegular,
Path: "regular.conf",
Content: []byte("regular"),
},
{
Type: file.TypeSecret,
Path: "secret.conf",
Content: []byte("secret"),
},
}
testErr = errors.New("test error")
)
DescribeTable(
"should return error on file IO error",
func(fakeOSMgr *filefakes.FakeOSFileManager) {
mgr := file.NewManagerImpl(zap.New(), fakeOSMgr)
// special case for Remove
// to kick off removing, we need to successfully write files beforehand
if fakeOSMgr.RemoveStub != nil {
err := mgr.ReplaceFiles(files)
Expect(err).ShouldNot(HaveOccurred())
}
err := mgr.ReplaceFiles(files)
Expect(err).Should(HaveOccurred())
Expect(err).To(MatchError(testErr))
},
Entry(
"Remove",
&filefakes.FakeOSFileManager{
RemoveStub: func(s string) error {
return testErr
},
},
),
Entry(
"Create",
&filefakes.FakeOSFileManager{
CreateStub: func(s string) (*os.File, error) {
return nil, testErr
},
},
),
Entry(
"Chmod",
&filefakes.FakeOSFileManager{
ChmodStub: func(os *os.File, mode os.FileMode) error {
return testErr
},
},
),
Entry(
"Write",
&filefakes.FakeOSFileManager{
WriteStub: func(os *os.File, bytes []byte) error {
return testErr
},
},
),
)
})
})