|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "errors" |
5 | 6 | "io" |
6 | 7 | "os" |
7 | 8 | "path/filepath" |
8 | 9 | "testing" |
9 | 10 |
|
| 11 | + "github.com/go-logr/logr" |
10 | 12 | . "github.com/onsi/gomega" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
11 | 14 |
|
| 15 | + "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/licensing/licensingfakes" |
12 | 16 | "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file" |
13 | 17 | "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file/filefakes" |
| 18 | + "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/dataplane" |
14 | 19 | ) |
15 | 20 |
|
| 21 | +func TestInitialize_OSS(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + g := NewGomegaWithT(t) |
| 24 | + |
| 25 | + fakeFileMgr := &filefakes.FakeOSFileManager{} |
| 26 | + |
| 27 | + ic := initializeConfig{ |
| 28 | + fileManager: fakeFileMgr, |
| 29 | + logger: zap.New(), |
| 30 | + copy: copyFiles{ |
| 31 | + destDirName: "destDir", |
| 32 | + srcFileNames: []string{"src1", "src2"}, |
| 33 | + }, |
| 34 | + plus: false, |
| 35 | + } |
| 36 | + |
| 37 | + err := initialize(ic) |
| 38 | + g.Expect(err).To(BeNil()) |
| 39 | + g.Expect(fakeFileMgr.CreateCallCount()).To(Equal(2)) |
| 40 | + g.Expect(fakeFileMgr.OpenCallCount()).To(Equal(2)) |
| 41 | + g.Expect(fakeFileMgr.CopyCallCount()).To(Equal(2)) |
| 42 | +} |
| 43 | + |
| 44 | +func TestInitialize_OSS_Error(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + g := NewGomegaWithT(t) |
| 47 | + |
| 48 | + openErr := errors.New("open error") |
| 49 | + fakeFileMgr := &filefakes.FakeOSFileManager{ |
| 50 | + OpenStub: func(_ string) (*os.File, error) { |
| 51 | + return nil, openErr |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + ic := initializeConfig{ |
| 56 | + fileManager: fakeFileMgr, |
| 57 | + logger: zap.New(), |
| 58 | + copy: copyFiles{ |
| 59 | + destDirName: "destDir", |
| 60 | + srcFileNames: []string{"src1", "src2"}, |
| 61 | + }, |
| 62 | + plus: false, |
| 63 | + } |
| 64 | + |
| 65 | + err := initialize(ic) |
| 66 | + g.Expect(err).To(HaveOccurred()) |
| 67 | + g.Expect(err).To(MatchError(openErr)) |
| 68 | +} |
| 69 | + |
| 70 | +func TestInitialize_Plus(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + g := NewGomegaWithT(t) |
| 73 | + |
| 74 | + fakeFileMgr := &filefakes.FakeOSFileManager{} |
| 75 | + fakeCollector := &licensingfakes.FakeCollector{} |
| 76 | + |
| 77 | + ic := initializeConfig{ |
| 78 | + fileManager: fakeFileMgr, |
| 79 | + logger: zap.New(), |
| 80 | + collector: fakeCollector, |
| 81 | + copy: copyFiles{ |
| 82 | + destDirName: "destDir", |
| 83 | + srcFileNames: []string{"src1", "src2"}, |
| 84 | + }, |
| 85 | + plus: true, |
| 86 | + } |
| 87 | + |
| 88 | + err := initialize(ic) |
| 89 | + g.Expect(err).ToNot(HaveOccurred()) |
| 90 | + // copies |
| 91 | + g.Expect(fakeFileMgr.OpenCallCount()).To(Equal(2)) |
| 92 | + g.Expect(fakeFileMgr.CopyCallCount()).To(Equal(2)) |
| 93 | + |
| 94 | + // 2 copies, 1 write deploy ctx |
| 95 | + g.Expect(fakeFileMgr.CreateCallCount()).To(Equal(3)) |
| 96 | + // write deploy ctx |
| 97 | + g.Expect(fakeCollector.CollectCallCount()).To(Equal(1)) |
| 98 | + g.Expect(fakeFileMgr.WriteCallCount()).To(Equal(1)) |
| 99 | + g.Expect(fakeFileMgr.ChmodCallCount()).To(Equal(1)) |
| 100 | +} |
| 101 | + |
| 102 | +func TestInitialize_Plus_Error(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + g := NewGomegaWithT(t) |
| 105 | + |
| 106 | + collectErr := errors.New("collect error") |
| 107 | + fakeFileMgr := &filefakes.FakeOSFileManager{} |
| 108 | + fakeCollector := &licensingfakes.FakeCollector{ |
| 109 | + CollectStub: func(_ context.Context, _ logr.Logger) (dataplane.DeploymentContext, error) { |
| 110 | + return dataplane.DeploymentContext{}, collectErr |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + ic := initializeConfig{ |
| 115 | + fileManager: fakeFileMgr, |
| 116 | + logger: zap.New(), |
| 117 | + collector: fakeCollector, |
| 118 | + copy: copyFiles{ |
| 119 | + destDirName: "destDir", |
| 120 | + srcFileNames: []string{"src1", "src2"}, |
| 121 | + }, |
| 122 | + plus: true, |
| 123 | + } |
| 124 | + |
| 125 | + err := initialize(ic) |
| 126 | + g.Expect(err).To(HaveOccurred()) |
| 127 | + g.Expect(err).To(MatchError(collectErr)) |
| 128 | +} |
| 129 | + |
16 | 130 | func TestCopyFile(t *testing.T) { |
17 | 131 | t.Parallel() |
18 | 132 | g := NewWithT(t) |
|
0 commit comments