|
| 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