Skip to content

Commit 3aa54bc

Browse files
author
Jim Ryan
committed
add license_reporting_test.go
1 parent 9074f4a commit 3aa54bc

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

internal/configs/version1/nginx-plus.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ stream {
360360

361361
{{- if .NginxVersion.PlusGreaterThanOrEqualTo "nginx-plus-r33" }}
362362
mgmt {
363-
usage_report endpoint=product.connect.nginx.com interval=30m;
363+
usage_report endpoint=product.connect.nginx.com interval=24h;
364364
uuid_file /var/lib/nginx/nginx.id;
365365
license_token conf/license.jwt;
366366
deployment_ctx /etc/nginx/reporting/tracking.info;

internal/license_reporting/license_reporting.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func writeLicenseInfo(info *licenseInfo) {
3939
jsonData, err := json.Marshal(info)
4040
if err != nil {
4141
glog.Errorf("failed to marshal LicenseInfo to JSON: %v", err)
42+
return
4243
}
43-
4444
filePath := filepath.Join(reportingDir, reportingFile)
4545
if err := os.WriteFile(filePath, jsonData, 0o600); err != nil {
4646
glog.Errorf("failed to write license reporting info to file: %v", err)
@@ -76,17 +76,14 @@ func (lr *LicenseReporter) collectAndWrite(ctx context.Context) {
7676
if err != nil {
7777
glog.Errorf("Error collecting ClusterIDS: %v", err)
7878
}
79-
8079
nodeCount, err := clusterInfo.GetNodeCount(ctx, lr.config.K8sClientReader)
8180
if err != nil {
8281
glog.Errorf("Error collecting ClusterNodeCount: %v", err)
8382
}
84-
8583
installationID, err := clusterInfo.GetInstallationID(ctx, lr.config.K8sClientReader, lr.config.PodNSName)
8684
if err != nil {
8785
glog.Errorf("Error collecting InstallationID: %v", err)
8886
}
89-
9087
info := newLicenseInfo(clusterID, installationID, nodeCount)
9188
writeLicenseInfo(info)
9289
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package licensereporting
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
"time"
9+
10+
"k8s.io/apimachinery/pkg/types"
11+
"k8s.io/client-go/kubernetes/fake"
12+
)
13+
14+
func TestNewLicenseInfo(t *testing.T) {
15+
info := newLicenseInfo("test-cluster", "test-installation", 5)
16+
17+
if info.Integration != "nic" {
18+
t.Errorf("newLicenseInfo() Integration = %v, want %v", info.Integration, "nic")
19+
}
20+
if info.ClusterID != "test-cluster" {
21+
t.Errorf("newLicenseInfo() ClusterID = %v, want %v", info.ClusterID, "test-cluster")
22+
}
23+
if info.InstallationID != "test-installation" {
24+
t.Errorf("newLicenseInfo() InstallationID = %v, want %v", info.InstallationID, "test-installation")
25+
}
26+
if info.ClusterNodeCount != 5 {
27+
t.Errorf("newLicenseInfo() ClusterNodeCount = %v, want %v", info.ClusterNodeCount, 5)
28+
}
29+
}
30+
31+
func TestWriteLicenseInfo(t *testing.T) {
32+
tempDir := t.TempDir()
33+
oldReportingDir := reportingDir
34+
reportingDir = tempDir
35+
defer func() { reportingDir = oldReportingDir }()
36+
37+
info := newLicenseInfo("test-cluster", "test-installation", 5)
38+
writeLicenseInfo(info)
39+
40+
filePath := filepath.Join(tempDir, reportingFile)
41+
if _, err := os.Stat(filePath); os.IsNotExist(err) {
42+
t.Fatalf("Expected file %s to exist, but it doesn't", filePath)
43+
}
44+
45+
/* #nosec G304 */
46+
content, err := os.ReadFile(filePath)
47+
if err != nil {
48+
t.Fatalf("Failed to read file: %v", err)
49+
}
50+
51+
var readInfo licenseInfo
52+
err = json.Unmarshal(content, &readInfo)
53+
if err != nil {
54+
t.Fatalf("Failed to unmarshal JSON: %v", err)
55+
}
56+
57+
if readInfo != *info {
58+
t.Errorf("Written info does not match original. Got %+v, want %+v", readInfo, *info)
59+
}
60+
}
61+
62+
func TestNewLicenseReporter(t *testing.T) {
63+
cfg := LicenseReporterConfig{
64+
Period: time.Minute,
65+
K8sClientReader: fake.NewSimpleClientset(),
66+
PodNSName: types.NamespacedName{Namespace: "default", Name: "test-pod"},
67+
}
68+
69+
reporter := NewLicenseReporter(cfg)
70+
if reporter == nil {
71+
t.Fatal("NewLicenseReporter() returned nil")
72+
}
73+
if reporter.config != cfg {
74+
t.Errorf("NewLicenseReporter() config = %v, want %v", reporter.config, cfg)
75+
}
76+
}

0 commit comments

Comments
 (0)