-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathinitialize_test.go
209 lines (181 loc) · 5.13 KB
/
initialize_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
package main
import (
"context"
"errors"
"io"
"os"
"path/filepath"
"testing"
"github.com/go-logr/logr"
. "github.com/onsi/gomega"
"github.com/nginx/nginx-gateway-fabric/internal/framework/helpers"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/licensing/licensingfakes"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/nginx/config/configfakes"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/nginx/file"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/nginx/file/filefakes"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/state/dataplane"
)
func TestInitialize_OSS(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
fakeFileMgr := &filefakes.FakeOSFileManager{}
ic := initializeConfig{
fileManager: fakeFileMgr,
logger: logr.Discard(),
copy: copyFiles{
destDirName: "destDir",
srcFileNames: []string{"src1", "src2"},
},
plus: false,
}
err := initialize(ic)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(fakeFileMgr.CreateCallCount()).To(Equal(2))
g.Expect(fakeFileMgr.OpenCallCount()).To(Equal(2))
g.Expect(fakeFileMgr.CopyCallCount()).To(Equal(2))
}
func TestInitialize_OSS_Error(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
openErr := errors.New("open error")
fakeFileMgr := &filefakes.FakeOSFileManager{
OpenStub: func(_ string) (*os.File, error) {
return nil, openErr
},
}
ic := initializeConfig{
fileManager: fakeFileMgr,
logger: logr.Discard(),
copy: copyFiles{
destDirName: "destDir",
srcFileNames: []string{"src1", "src2"},
},
plus: false,
}
err := initialize(ic)
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError(openErr))
}
func TestInitialize_Plus(t *testing.T) {
t.Parallel()
tests := []struct {
name string
collectErr error
depCtx dataplane.DeploymentContext
}{
{
name: "normal",
collectErr: nil,
depCtx: dataplane.DeploymentContext{
Integration: "ngf",
ClusterID: helpers.GetPointer("cluster-id"),
InstallationID: helpers.GetPointer("install-id"),
ClusterNodeCount: helpers.GetPointer(2),
},
},
{
name: "collecting deployment context errors",
collectErr: errors.New("collect error"),
depCtx: dataplane.DeploymentContext{
Integration: "ngf",
InstallationID: helpers.GetPointer("install-id"),
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
fakeFileMgr := &filefakes.FakeOSFileManager{}
fakeCollector := &licensingfakes.FakeCollector{
CollectStub: func(_ context.Context) (dataplane.DeploymentContext, error) {
return test.depCtx, test.collectErr
},
}
fakeGenerator := &configfakes.FakeGenerator{}
ic := initializeConfig{
fileManager: fakeFileMgr,
logger: logr.Discard(),
collector: fakeCollector,
fileGenerator: fakeGenerator,
copy: copyFiles{
destDirName: "destDir",
srcFileNames: []string{"src1", "src2"},
},
plus: true,
}
g.Expect(initialize(ic)).To(Succeed())
// copies
g.Expect(fakeFileMgr.OpenCallCount()).To(Equal(2))
g.Expect(fakeFileMgr.CopyCallCount()).To(Equal(2))
// 2 copies, 1 write deploy ctx
g.Expect(fakeFileMgr.CreateCallCount()).To(Equal(3))
// write deploy ctx
g.Expect(fakeGenerator.GenerateDeploymentContextCallCount()).To(Equal(1))
g.Expect(fakeGenerator.GenerateDeploymentContextArgsForCall(0)).To(Equal(test.depCtx))
g.Expect(fakeCollector.CollectCallCount()).To(Equal(1))
g.Expect(fakeFileMgr.WriteCallCount()).To(Equal(1))
g.Expect(fakeFileMgr.ChmodCallCount()).To(Equal(1))
})
}
}
func TestCopyFile(t *testing.T) {
t.Parallel()
g := NewWithT(t)
src, err := os.CreateTemp(os.TempDir(), "testfile")
g.Expect(err).ToNot(HaveOccurred())
defer os.Remove(src.Name())
dest, err := os.MkdirTemp(os.TempDir(), "testdir")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dest)
g.Expect(copyFile(file.NewStdLibOSFileManager(), src.Name(), dest)).To(Succeed())
_, err = os.Stat(filepath.Join(dest, filepath.Base(src.Name())))
g.Expect(err).ToNot(HaveOccurred())
}
func TestCopyFileErrors(t *testing.T) {
t.Parallel()
openErr := errors.New("open error")
createErr := errors.New("create error")
copyErr := errors.New("copy error")
tests := []struct {
fileMgr *filefakes.FakeOSFileManager
expErr error
name string
}{
{
name: "can't open src file",
fileMgr: &filefakes.FakeOSFileManager{
OpenStub: func(_ string) (*os.File, error) {
return nil, openErr
},
},
expErr: openErr,
},
{
name: "can't create dest file",
fileMgr: &filefakes.FakeOSFileManager{
CreateStub: func(_ string) (*os.File, error) {
return nil, createErr
},
},
expErr: createErr,
},
{
name: "can't copy contents",
fileMgr: &filefakes.FakeOSFileManager{
CopyStub: func(_ io.Writer, _ io.Reader) error {
return copyErr
},
},
expErr: copyErr,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
err := copyFile(test.fileMgr, "source", "destDir")
g.Expect(err).To(MatchError(test.expErr))
})
}
}