1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/cnbutils/report_test.go
Pavel Busko 0de06c6207
feat(cnbBuild): write image digests to the CPE (#3602)
Co-authored-by: Johannes Dillmann <j.dillmann@sap.com>
2022-03-02 16:26:45 +01:00

49 lines
1.4 KiB
Go

package cnbutils_test
import (
"testing"
"github.com/SAP/jenkins-library/pkg/cnbutils"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
func TestDigestFromReport(t *testing.T) {
t.Run("return a digest from the report.toml", func(t *testing.T) {
mockUtils := &cnbutils.MockUtils{
FilesMock: &mock.FilesMock{},
}
mockUtils.AddFile("/layers/report.toml", []byte(`[build]
[image]
digest = "sha256:52eac630560210e5ae13eb10797c4246d6f02d425f32b9430ca00bde697c79ec"
`))
digest, err := cnbutils.DigestFromReport(mockUtils)
assert.NoError(t, err)
assert.Equal(t, "sha256:52eac630560210e5ae13eb10797c4246d6f02d425f32b9430ca00bde697c79ec", digest)
})
t.Run("fails if digest is empty", func(t *testing.T) {
mockUtils := &cnbutils.MockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
mockUtils.AddFile("/layers/report.toml", []byte(``))
digest, err := cnbutils.DigestFromReport(mockUtils)
assert.Empty(t, digest)
assert.EqualError(t, err, "image digest is empty")
})
t.Run("fails to unmarshal corrupted file", func(t *testing.T) {
mockUtils := &cnbutils.MockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
mockUtils.AddFile("/layers/report.toml", []byte(`{}`))
digest, err := cnbutils.DigestFromReport(mockUtils)
assert.Empty(t, digest)
assert.EqualError(t, err, "(1, 1): parsing error: keys cannot contain { character")
})
}