1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/cnbBuild_test.go
Haegi 3f4b32f7ba
cnbBuild step MVP (#3021)
* Implement cnbBuild step

Co-authored-by: Benjamin Haegenlaeuer <benjamin.haegenlaeuer@sap.com>

* Add cnbBuild groovy test

Co-authored-by: Benjamin Haegenlaeuer <benjamin.haegenlaeuer@sap.com>

* Add basic documentation template

Co-authored-by: Philipp Stehle <philipp.stehle@sap.com>

* Support specifiying name, tag and registry

Co-authored-by: Pavel Busko <pbusko@users.noreply.github.com>

Co-authored-by: Johannes Dillmann <j.dillmann@sap.com>
Co-authored-by: Philipp Stehle <philipp.stehle@sap.com>
Co-authored-by: Pavel Busko <pbusko@users.noreply.github.com>
2021-08-18 12:10:55 +02:00

80 lines
2.6 KiB
Go

package cmd
import (
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/stretchr/testify/assert"
)
type cnbBuildMockUtils struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func newCnbBuildTestsUtils() cnbBuildMockUtils {
utils := cnbBuildMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return utils
}
func TestRunCnbBuild(t *testing.T) {
t.Parallel()
commonPipelineEnvironment := cnbBuildCommonPipelineEnvironment{}
t.Run("success case", func(t *testing.T) {
t.Parallel()
registy := "some-registry"
config := cnbBuildOptions{
ContainerImageName: "my-image",
ContainerImageTag: "0.0.1",
ContainerRegistryURL: fmt.Sprintf("https://%s", registy),
DockerConfigJSON: "/path/to/config.json",
}
utils := newCnbBuildTestsUtils()
utils.FilesMock.AddFile(config.DockerConfigJSON, []byte(`{"auths":{"my-registry":{"auth":"dXNlcjpwYXNz"}}}`))
err := runCnbBuild(&config, &telemetry.CustomData{}, utils, &commonPipelineEnvironment)
assert.NoError(t, err)
runner := utils.ExecMockRunner
assert.Contains(t, runner.Env, "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}")
assert.Equal(t, "/cnb/lifecycle/detector", runner.Calls[0].Exec)
assert.Equal(t, "/cnb/lifecycle/builder", runner.Calls[1].Exec)
assert.Equal(t, "/cnb/lifecycle/exporter", runner.Calls[2].Exec)
assert.Equal(t, []string{fmt.Sprintf("%s/%s:%s", registy, config.ContainerImageName, config.ContainerImageTag), fmt.Sprintf("%s/%s:latest", registy, config.ContainerImageName)}, runner.Calls[2].Params)
})
t.Run("error case: Invalid DockerConfigJSON file", func(t *testing.T) {
t.Parallel()
config := cnbBuildOptions{
ContainerImage: "my-image",
DockerConfigJSON: "/path/to/config.json",
}
utils := newCnbBuildTestsUtils()
utils.FilesMock.AddFile(config.DockerConfigJSON, []byte(`{"auths":{"my-registry":"dXNlcjpwYXNz"}}`))
err := runCnbBuild(&config, nil, utils, &commonPipelineEnvironment)
assert.EqualError(t, err, "failed to parse DockerConfigJSON file '/path/to/config.json': json: cannot unmarshal string into Go struct field ConfigFile.auths of type types.AuthConfig")
})
t.Run("error case: DockerConfigJSON file not there", func(t *testing.T) {
t.Parallel()
config := cnbBuildOptions{
ContainerImage: "my-image",
DockerConfigJSON: "not-there",
}
utils := newCnbBuildTestsUtils()
err := runCnbBuild(&config, nil, utils, &commonPipelineEnvironment)
assert.EqualError(t, err, "failed to read DockerConfigJSON file 'not-there': could not read 'not-there'")
})
}