|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/anchore/syft/syft/pkg" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/bottlerocket-os/bottlerocket-sdk/sbomtool/go/internal/commands/merge" |
| 14 | +) |
| 15 | + |
| 16 | +func TestMergeIntegration(t *testing.T) { |
| 17 | + // GIVEN: Two SPDX SBOM files with overlapping packages |
| 18 | + // WHEN: Merge command is executed |
| 19 | + // THEN: Merged SBOM should be created with proper deduplication |
| 20 | + |
| 21 | + tempDir := t.TempDir() |
| 22 | + |
| 23 | + // Create test SPDX SBOMs |
| 24 | + sbom1 := createTestSPDXSBOM("app1", []TestPackage{ |
| 25 | + {Name: "pkg1", Version: "1.0.0"}, |
| 26 | + {Name: "pkg2", Version: "2.0.0"}, |
| 27 | + }) |
| 28 | + sbom2 := createTestSPDXSBOM("app2", []TestPackage{ |
| 29 | + {Name: "pkg1", Version: "1.0.0"}, // Duplicate |
| 30 | + {Name: "pkg3", Version: "3.0.0"}, |
| 31 | + }) |
| 32 | + |
| 33 | + // Write test files |
| 34 | + file1 := filepath.Join(tempDir, "sbom1.json") |
| 35 | + file2 := filepath.Join(tempDir, "sbom2.json") |
| 36 | + outputFile := filepath.Join(tempDir, "merged.json") |
| 37 | + |
| 38 | + require.NoError(t, writeJSONFile(file1, sbom1)) |
| 39 | + require.NoError(t, writeJSONFile(file2, sbom2)) |
| 40 | + |
| 41 | + // Execute merge command |
| 42 | + rootCmd := createRootCommand() |
| 43 | + rootCmd.SetArgs([]string{ |
| 44 | + "merge", |
| 45 | + "--output", outputFile, |
| 46 | + file1, file2, |
| 47 | + }) |
| 48 | + |
| 49 | + err := rootCmd.Execute() |
| 50 | + require.NoError(t, err, "Merge command should execute successfully") |
| 51 | + |
| 52 | + // Verify output file exists |
| 53 | + assert.FileExists(t, outputFile, "Merged SBOM file should be created") |
| 54 | + |
| 55 | + // Test the in-memory SBOM directly (like deduplication tests) |
| 56 | + // Re-run the merge logic to get the in-memory result |
| 57 | + config := merge.MergeConfig{ |
| 58 | + OutputPath: outputFile, |
| 59 | + Level: 0, |
| 60 | + } |
| 61 | + result, err := merge.Merge(config, []string{file1, file2}) |
| 62 | + require.NoError(t, err, "Direct merge should work") |
| 63 | + |
| 64 | + // Test the in-memory SBOM packages |
| 65 | + packages := make([]pkg.Package, 0) |
| 66 | + for p := range result.MergedSBOM.Artifacts.Packages.Enumerate() { |
| 67 | + packages = append(packages, p) |
| 68 | + } |
| 69 | + |
| 70 | + // Basic structure validation on in-memory SBOM |
| 71 | + assert.Greater(t, len(packages), 0, "Should have packages after merge") |
| 72 | + assert.Equal(t, 3, len(packages), "Should have exactly 3 packages after deduplication (pkg1, pkg2, pkg3)") |
| 73 | + |
| 74 | + // Verify specific packages exist |
| 75 | + packageNames := make([]string, len(packages)) |
| 76 | + for i, p := range packages { |
| 77 | + packageNames[i] = p.Name |
| 78 | + } |
| 79 | + assert.Contains(t, packageNames, "pkg1") |
| 80 | + assert.Contains(t, packageNames, "pkg2") |
| 81 | + assert.Contains(t, packageNames, "pkg3") |
| 82 | +} |
| 83 | + |
| 84 | +func TestMergeIntegrationCycloneDX(t *testing.T) { |
| 85 | + // GIVEN: Two CycloneDX SBOM files with overlapping packages |
| 86 | + // WHEN: Merge command is executed |
| 87 | + // THEN: Merged SBOM should be created with proper deduplication |
| 88 | + |
| 89 | + tempDir := t.TempDir() |
| 90 | + |
| 91 | + // Create test CycloneDX SBOMs |
| 92 | + sbom1 := createTestCycloneDXSBOM("app1", []TestPackage{ |
| 93 | + {Name: "pkg1", Version: "1.0.0"}, |
| 94 | + {Name: "pkg2", Version: "2.0.0"}, |
| 95 | + }) |
| 96 | + sbom2 := createTestCycloneDXSBOM("app2", []TestPackage{ |
| 97 | + {Name: "pkg1", Version: "1.0.0"}, // Duplicate |
| 98 | + {Name: "pkg3", Version: "3.0.0"}, |
| 99 | + }) |
| 100 | + |
| 101 | + // Write test files |
| 102 | + file1 := filepath.Join(tempDir, "sbom1.json") |
| 103 | + file2 := filepath.Join(tempDir, "sbom2.json") |
| 104 | + outputFile := filepath.Join(tempDir, "merged.json") |
| 105 | + |
| 106 | + require.NoError(t, writeJSONFile(file1, sbom1)) |
| 107 | + require.NoError(t, writeJSONFile(file2, sbom2)) |
| 108 | + |
| 109 | + // Execute merge command |
| 110 | + rootCmd := createRootCommand() |
| 111 | + rootCmd.SetArgs([]string{ |
| 112 | + "merge", |
| 113 | + "--output", outputFile, |
| 114 | + file1, file2, |
| 115 | + }) |
| 116 | + |
| 117 | + err := rootCmd.Execute() |
| 118 | + require.NoError(t, err, "Merge command should execute successfully") |
| 119 | + |
| 120 | + // Verify output file exists |
| 121 | + assert.FileExists(t, outputFile, "Merged SBOM file should be created") |
| 122 | + |
| 123 | + // Test the in-memory SBOM directly (like deduplication tests) |
| 124 | + // Re-run the merge logic to get the in-memory result |
| 125 | + config := merge.MergeConfig{ |
| 126 | + OutputPath: outputFile, |
| 127 | + Level: 0, |
| 128 | + } |
| 129 | + result, err := merge.Merge(config, []string{file1, file2}) |
| 130 | + require.NoError(t, err, "Direct merge should work") |
| 131 | + |
| 132 | + // Test the in-memory SBOM packages |
| 133 | + packages := make([]pkg.Package, 0) |
| 134 | + for p := range result.MergedSBOM.Artifacts.Packages.Enumerate() { |
| 135 | + packages = append(packages, p) |
| 136 | + } |
| 137 | + |
| 138 | + // Basic structure validation on in-memory SBOM |
| 139 | + assert.Greater(t, len(packages), 0, "Should have packages after merge") |
| 140 | + // CycloneDX includes metadata components (app1, app2) plus deduplicated libraries (pkg1, pkg2, pkg3) |
| 141 | + assert.Equal(t, 5, len(packages), "Should have 5 packages: 2 app components + 3 deduplicated libraries") |
| 142 | + |
| 143 | + // Verify specific library packages exist (the actual dependencies) |
| 144 | + packageNames := make([]string, len(packages)) |
| 145 | + for i, p := range packages { |
| 146 | + packageNames[i] = p.Name |
| 147 | + } |
| 148 | + assert.Contains(t, packageNames, "pkg1") |
| 149 | + assert.Contains(t, packageNames, "pkg2") |
| 150 | + assert.Contains(t, packageNames, "pkg3") |
| 151 | + assert.Contains(t, packageNames, "app1") // Metadata component |
| 152 | + assert.Contains(t, packageNames, "app2") // Metadata component |
| 153 | +} |
| 154 | + |
| 155 | +// TestPackage represents a simple package for testing |
| 156 | +type TestPackage struct { |
| 157 | + Name string |
| 158 | + Version string |
| 159 | +} |
| 160 | + |
| 161 | +// createTestSPDXSBOM creates a minimal SPDX SBOM for testing |
| 162 | +func createTestSPDXSBOM(name string, packages []TestPackage) map[string]interface{} { |
| 163 | + spdxPackages := make([]map[string]interface{}, len(packages)) |
| 164 | + for i, pkg := range packages { |
| 165 | + spdxPackages[i] = map[string]interface{}{ |
| 166 | + "SPDXID": "SPDXRef-Package-" + pkg.Name, |
| 167 | + "name": pkg.Name, |
| 168 | + "versionInfo": pkg.Version, |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return map[string]interface{}{ |
| 173 | + "spdxVersion": "SPDX-2.3", |
| 174 | + "dataLicense": "CC0-1.0", |
| 175 | + "SPDXID": "SPDXRef-DOCUMENT", |
| 176 | + "name": name, |
| 177 | + "documentNamespace": "https://example.com/" + name, |
| 178 | + "packages": spdxPackages, |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +// createTestCycloneDXSBOM creates a minimal CycloneDX SBOM for testing |
| 183 | +func createTestCycloneDXSBOM(name string, packages []TestPackage) map[string]interface{} { |
| 184 | + components := make([]map[string]interface{}, len(packages)) |
| 185 | + for i, pkg := range packages { |
| 186 | + components[i] = map[string]interface{}{ |
| 187 | + "type": "library", |
| 188 | + "name": pkg.Name, |
| 189 | + "version": pkg.Version, |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return map[string]interface{}{ |
| 194 | + "bomFormat": "CycloneDX", |
| 195 | + "specVersion": "1.6", |
| 196 | + "version": 1, |
| 197 | + "metadata": map[string]interface{}{ |
| 198 | + "component": map[string]interface{}{ |
| 199 | + "type": "application", |
| 200 | + "name": name, |
| 201 | + }, |
| 202 | + }, |
| 203 | + "components": components, |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +// writeJSONFile writes data as JSON to a file |
| 208 | +func writeJSONFile(path string, data interface{}) error { |
| 209 | + jsonData, err := json.MarshalIndent(data, "", " ") |
| 210 | + if err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + return os.WriteFile(path, jsonData, 0644) |
| 214 | +} |
0 commit comments