Skip to content

Commit 02a0768

Browse files
author
Kate Osborn
committed
more tests
1 parent c814140 commit 02a0768

File tree

6 files changed

+291
-24
lines changed

6 files changed

+291
-24
lines changed

cmd/gateway/commands.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ import (
1414
"k8s.io/apimachinery/pkg/types"
1515
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1616
"k8s.io/klog/v2"
17+
ctlr "sigs.k8s.io/controller-runtime"
18+
"sigs.k8s.io/controller-runtime/pkg/client"
1719
"sigs.k8s.io/controller-runtime/pkg/log"
1820
ctlrZap "sigs.k8s.io/controller-runtime/pkg/log/zap"
1921

2022
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/provisioner"
2123
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static"
2224
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
25+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/licensing"
2326
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file"
2427
)
2528

@@ -541,6 +544,17 @@ func createInitializeCommand() *cobra.Command {
541544
return fmt.Errorf("could not get pod nsname: %w", err)
542545
}
543546

547+
clusterCfg := ctlr.GetConfigOrDie()
548+
k8sReader, err := client.New(clusterCfg, client.Options{})
549+
if err != nil {
550+
return fmt.Errorf("unable to initialize k8s client: %w", err)
551+
}
552+
553+
dcc := licensing.NewDeploymentContextCollector(licensing.DeploymentContextCollectorConfig{
554+
K8sClientReader: k8sReader,
555+
PodNSName: podNsName,
556+
})
557+
544558
logger := ctlrZap.New()
545559
klog.SetLogger(logger)
546560
logger.Info(
@@ -553,10 +567,10 @@ func createInitializeCommand() *cobra.Command {
553567
log.SetLogger(logger)
554568

555569
return initialize(initializeConfig{
556-
controllerPodNSName: podNsName,
557-
fileManager: file.NewStdLibOSFileManager(),
558-
logger: logger,
559-
plus: plus,
570+
fileManager: file.NewStdLibOSFileManager(),
571+
logger: logger,
572+
plus: plus,
573+
collector: dcc,
560574
copy: copyFiles{
561575
srcFileNames: srcFiles,
562576
destDirName: dest,

cmd/gateway/commands_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"io"
5+
"os"
56
"testing"
67

78
. "github.com/onsi/gomega"
@@ -658,3 +659,25 @@ func TestGetBuildInfo(t *testing.T) {
658659
g.Expect(commitTime).To(Not(Equal("unknown")))
659660
g.Expect(dirtyBuild).To(Not(Equal("unknown")))
660661
}
662+
663+
func TestGetPodNsName(t *testing.T) {
664+
t.Parallel()
665+
g := NewWithT(t)
666+
667+
g.Expect(os.Setenv("POD_NAMESPACE", "default")).To(Succeed())
668+
g.Expect(os.Setenv("POD_NAME", "my-pod")).To(Succeed())
669+
670+
nsname, err := getPodNsName()
671+
g.Expect(err).To(Not(HaveOccurred()))
672+
g.Expect(nsname).To(Equal(types.NamespacedName{Name: "my-pod", Namespace: "default"}))
673+
674+
g.Expect(os.Unsetenv("POD_NAMESPACE")).To(Succeed())
675+
nsname, err = getPodNsName()
676+
g.Expect(err).To(HaveOccurred())
677+
g.Expect(nsname).To(Equal(types.NamespacedName{}))
678+
679+
g.Expect(os.Unsetenv("POD_NAME")).To(Succeed())
680+
nsname, err = getPodNsName()
681+
g.Expect(err).To(HaveOccurred())
682+
g.Expect(nsname).To(Equal(types.NamespacedName{}))
683+
}

cmd/gateway/initialize.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
"time"
88

99
"github.com/go-logr/logr"
10-
"k8s.io/apimachinery/pkg/types"
11-
ctlr "sigs.k8s.io/controller-runtime"
12-
"sigs.k8s.io/controller-runtime/pkg/client"
1310

1411
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/licensing"
1512
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config"
@@ -23,11 +20,11 @@ type copyFiles struct {
2320
}
2421

2522
type initializeConfig struct {
26-
controllerPodNSName types.NamespacedName
27-
fileManager file.OSFileManager
28-
logger logr.Logger
29-
copy copyFiles
30-
plus bool
23+
collector licensing.Collector
24+
fileManager file.OSFileManager
25+
logger logr.Logger
26+
copy copyFiles
27+
plus bool
3128
}
3229

3330
func initialize(cfg initializeConfig) error {
@@ -42,21 +39,10 @@ func initialize(cfg initializeConfig) error {
4239
return nil
4340
}
4441

45-
clusterCfg := ctlr.GetConfigOrDie()
46-
k8sReader, err := client.New(clusterCfg, client.Options{})
47-
if err != nil {
48-
return fmt.Errorf("unable to initialize k8s client: %w", err)
49-
}
50-
51-
dcc := licensing.NewDeploymentContextCollector(licensing.DeploymentContextCollectorConfig{
52-
K8sClientReader: k8sReader,
53-
PodNSName: cfg.controllerPodNSName,
54-
})
55-
5642
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
5743
defer cancel()
5844

59-
depCtx, err := dcc.Collect(ctx, cfg.logger.WithName("deployCtxCollector"))
45+
depCtx, err := cfg.collector.Collect(ctx, cfg.logger.WithName("deployCtxCollector"))
6046
if err != nil {
6147
return fmt.Errorf("failed to collect deployment context: %w", err)
6248
}

cmd/gateway/initialize_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,132 @@
11
package main
22

33
import (
4+
"context"
45
"errors"
56
"io"
67
"os"
78
"path/filepath"
89
"testing"
910

11+
"github.com/go-logr/logr"
1012
. "github.com/onsi/gomega"
13+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1114

15+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/licensing/licensingfakes"
1216
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file"
1317
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file/filefakes"
18+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/dataplane"
1419
)
1520

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+
16130
func TestCopyFile(t *testing.T) {
17131
t.Parallel()
18132
g := NewWithT(t)

internal/mode/static/licensing/collector.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import (
1212
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/telemetry"
1313
)
1414

15+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
16+
17+
//counterfeiter:generate . Collector
18+
19+
// Collector collects licensing information for N+.
20+
type Collector interface {
21+
Collect(ctx context.Context, log logr.Logger) (dataplane.DeploymentContext, error)
22+
}
23+
1524
const integrationID = "ngf"
1625

1726
// DeploymentContextCollectorConfig contains the configuration for the DeploymentContextCollector.

0 commit comments

Comments
 (0)