mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
0de06c6207
Co-authored-by: Johannes Dillmann <j.dillmann@sap.com>
49 lines
1.4 KiB
Go
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")
|
|
})
|
|
}
|