-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include purl info in the event (#5092)
- Loading branch information
Showing
7 changed files
with
378 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,11 @@ | |
package cmd | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/SAP/jenkins-library/pkg/piperutils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
@@ -157,3 +160,105 @@ func TestMavenBuild(t *testing.T) { | |
}) | ||
|
||
} | ||
|
||
func TestIsAggregatedBOM(t *testing.T) { | ||
t.Run("is aggregated BOM", func(t *testing.T) { | ||
bom := piperutils.Bom{ | ||
Metadata: piperutils.Metadata{ | ||
Properties: []piperutils.BomProperty{ | ||
{Name: "maven.goal", Value: "makeAggregateBom"}, | ||
}, | ||
}, | ||
} | ||
assert.True(t, isAggregatedBOM(bom)) | ||
}) | ||
|
||
t.Run("is not aggregated BOM", func(t *testing.T) { | ||
bom := piperutils.Bom{ | ||
Metadata: piperutils.Metadata{ | ||
Properties: []piperutils.BomProperty{ | ||
{Name: "some.property", Value: "someValue"}, | ||
}, | ||
}, | ||
} | ||
assert.False(t, isAggregatedBOM(bom)) | ||
}) | ||
} | ||
|
||
func createTempFile(t *testing.T, dir string, filename string, content string) string { | ||
filePath := filepath.Join(dir, filename) | ||
err := os.WriteFile(filePath, []byte(content), 0666) | ||
if err != nil { | ||
t.Fatalf("Failed to create temp file: %s", err) | ||
} | ||
return filePath | ||
} | ||
|
||
func TestGetPurlForThePomAndDeleteIndividualBom(t *testing.T) { | ||
t.Run("valid BOM file, non-aggregated", func(t *testing.T) { | ||
tempDir, err := piperutils.Files{}.TempDir("", "test") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp directory: %s", err) | ||
} | ||
|
||
bomContent := `<bom> | ||
<metadata> | ||
<component> | ||
<purl>pkg:maven/com.example/[email protected]</purl> | ||
</component> | ||
<properties> | ||
<property name="name1" value="value1" /> | ||
</properties> | ||
</metadata> | ||
</bom>` | ||
pomFilePath := createTempFile(t, tempDir, "pom.xml", "") | ||
bomDir := filepath.Join(tempDir, "target") | ||
if err := os.MkdirAll(bomDir, 0777); err != nil { | ||
t.Fatalf("Failed to create temp directory: %s", err) | ||
} | ||
bomFilePath := createTempFile(t, bomDir, mvnBomFilename+".xml", bomContent) | ||
defer os.Remove(bomFilePath) | ||
|
||
purl := getPurlForThePomAndDeleteIndividualBom(pomFilePath) | ||
assert.Equal(t, "pkg:maven/com.example/[email protected]", purl) | ||
_, err = os.Stat(bomFilePath) | ||
assert.True(t, os.IsNotExist(err)) | ||
}) | ||
|
||
t.Run("valid BOM file, aggregated BOM", func(t *testing.T) { | ||
tempDir, err := piperutils.Files{}.TempDir("", "test") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp directory: %s", err) | ||
} | ||
|
||
bomContent := `<bom> | ||
<metadata> | ||
<component> | ||
<purl>pkg:maven/com.example/[email protected]</purl> | ||
</component> | ||
<properties> | ||
<property name="maven.goal" value="makeAggregateBom" /> | ||
</properties> | ||
</metadata> | ||
</bom>` | ||
pomFilePath := createTempFile(t, tempDir, "pom.xml", "") | ||
bomDir := filepath.Join(tempDir, "target") | ||
if err := os.MkdirAll(bomDir, 0777); err != nil { | ||
t.Fatalf("Failed to create temp directory: %s", err) | ||
} | ||
bomFilePath := createTempFile(t, bomDir, mvnBomFilename+".xml", bomContent) | ||
|
||
purl := getPurlForThePomAndDeleteIndividualBom(pomFilePath) | ||
assert.Equal(t, "pkg:maven/com.example/[email protected]", purl) | ||
_, err = os.Stat(bomFilePath) | ||
assert.False(t, os.IsNotExist(err)) // File should not be deleted | ||
}) | ||
|
||
t.Run("BOM file does not exist", func(t *testing.T) { | ||
tempDir := t.TempDir() | ||
pomFilePath := createTempFile(t, tempDir, "pom.xml", "") // Create a temp pom file | ||
|
||
purl := getPurlForThePomAndDeleteIndividualBom(pomFilePath) | ||
assert.Equal(t, "", purl) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"github.com/SAP/jenkins-library/pkg/piperutils" | ||
"github.com/SAP/jenkins-library/pkg/versioning" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
) | ||
|
||
type npmMockUtilsBundleRelativeGlob struct { | ||
|
@@ -573,3 +574,46 @@ func TestNpmPublish(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func createTempFile(t *testing.T, dir string, filename string, content string) string { | ||
filePath := filepath.Join(dir, filename) | ||
err := os.WriteFile(filePath, []byte(content), 0666) | ||
if err != nil { | ||
t.Fatalf("Failed to create temp file: %s", err) | ||
} | ||
return filePath | ||
} | ||
|
||
func TestGetPurl(t *testing.T) { | ||
t.Run("valid BOM file", func(t *testing.T) { | ||
tempDir, err := piperutils.Files{}.TempDir("", "test") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp directory: %s", err) | ||
} | ||
|
||
bomContent := `<bom> | ||
<metadata> | ||
<component> | ||
<purl>pkg:npm/com.example/[email protected]</purl> | ||
</component> | ||
<properties> | ||
<property name="name1" value="value1" /> | ||
</properties> | ||
</metadata> | ||
</bom>` | ||
packageJsonFilePath := createTempFile(t, tempDir, "package.json", "") | ||
bomFilePath := createTempFile(t, tempDir, npmBomFilename, bomContent) | ||
defer os.Remove(bomFilePath) | ||
|
||
purl := getPurl(packageJsonFilePath) | ||
assert.Equal(t, "pkg:npm/com.example/[email protected]", purl) | ||
}) | ||
|
||
t.Run("BOM file does not exist", func(t *testing.T) { | ||
tempDir := t.TempDir() | ||
packageJsonFilePath := createTempFile(t, tempDir, "pom.xml", "") // Create a temp pom file | ||
|
||
purl := getPurl(packageJsonFilePath) | ||
assert.Equal(t, "", purl) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package piperutils | ||
|
||
import ( | ||
"encoding/xml" | ||
"github.com/SAP/jenkins-library/pkg/log" | ||
"io" | ||
"os" | ||
) | ||
|
||
// To serialize the cyclonedx BOM file | ||
type Bom struct { | ||
Metadata Metadata `xml:"metadata"` | ||
} | ||
|
||
type Metadata struct { | ||
Component BomComponent `xml:"component"` | ||
Properties []BomProperty `xml:"properties>property"` | ||
} | ||
|
||
type BomProperty struct { | ||
Name string `xml:"name,attr"` | ||
Value string `xml:"value,attr"` | ||
} | ||
|
||
type BomComponent struct { | ||
Purl string `xml:"purl"` | ||
} | ||
|
||
func GetBom(absoluteBomPath string) (Bom, error) { | ||
xmlFile, err := os.Open(absoluteBomPath) | ||
if err != nil { | ||
log.Entry().Debugf("failed to open bom file %s", absoluteBomPath) | ||
return Bom{}, err | ||
} | ||
defer xmlFile.Close() | ||
byteValue, err := io.ReadAll(xmlFile) | ||
if err != nil { | ||
log.Entry().Debugf("failed to read bom file %s", absoluteBomPath) | ||
return Bom{}, err | ||
} | ||
var bom Bom | ||
err = xml.Unmarshal(byteValue, &bom) | ||
if err != nil { | ||
log.Entry().Debugf("failed to unmarshal bom file %s", absoluteBomPath) | ||
return Bom{}, err | ||
} | ||
return bom, nil | ||
} |
Oops, something went wrong.