2023-05-03 18:02:11 +02:00
//go:build unit
// +build unit
2021-08-18 12:10:55 +02:00
package cmd
import (
2022-01-31 13:27:29 +02:00
"encoding/json"
2021-08-18 12:10:55 +02:00
"fmt"
2021-10-01 13:48:24 +02:00
"net/http"
2022-10-25 14:35:24 +02:00
"path/filepath"
2021-08-18 12:10:55 +02:00
"testing"
2023-08-04 13:31:33 +02:00
"github.com/SAP/jenkins-library/pkg/buildpacks"
2021-09-14 16:14:50 +02:00
"github.com/SAP/jenkins-library/pkg/cnbutils"
2022-02-16 14:28:51 +02:00
piperconf "github.com/SAP/jenkins-library/pkg/config"
2021-10-01 13:48:24 +02:00
piperhttp "github.com/SAP/jenkins-library/pkg/http"
2021-08-18 12:10:55 +02:00
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/telemetry"
2022-08-18 10:03:24 +02:00
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/fake"
2021-10-11 11:33:51 +02:00
"github.com/jarcoal/httpmock"
2021-08-18 12:10:55 +02:00
"github.com/stretchr/testify/assert"
2022-02-01 09:23:53 +02:00
"github.com/stretchr/testify/require"
2021-08-18 12:10:55 +02:00
)
2022-01-14 17:49:45 +02:00
const imageRegistry = "some-registry"
2021-09-14 16:14:50 +02:00
func newCnbBuildTestsUtils ( ) cnbutils . MockUtils {
2023-07-06 11:34:05 +02:00
imageStub := func ( imageRef , target string ) ( v1 . Image , error ) {
fakeImage := & fake . FakeImage { }
var imageConfig v1 . Config
switch imageRef {
case "pre-test" :
imageConfig = v1 . Config {
Labels : map [ string ] string {
"io.buildpacks.buildpackage.metadata" : "{\"id\": \"pre-testbuildpack\", \"version\": \"0.0.1\"}" ,
} ,
}
case "post-test" :
imageConfig = v1 . Config {
Labels : map [ string ] string {
"io.buildpacks.buildpackage.metadata" : "{\"id\": \"post-testbuildpack\", \"version\": \"0.0.1\"}" ,
} ,
}
default :
imageConfig = v1 . Config {
Labels : map [ string ] string {
"io.buildpacks.buildpackage.metadata" : "{\"id\": \"testbuildpack\", \"version\": \"0.0.1\"}" ,
} ,
}
}
fakeImage . ConfigFileReturns ( & v1 . ConfigFile {
Config : imageConfig ,
} , nil )
return fakeImage , nil
}
2021-09-14 16:14:50 +02:00
utils := cnbutils . MockUtils {
2021-08-18 12:10:55 +02:00
ExecMockRunner : & mock . ExecMockRunner { } ,
FilesMock : & mock . FilesMock { } ,
2023-07-06 11:34:05 +02:00
DownloadMock : & mock . DownloadMock {
ImageContentStub : imageStub ,
ImageInfoStub : func ( imageRef string ) ( v1 . Image , error ) {
return imageStub ( imageRef , "" )
2022-08-18 10:03:24 +02:00
} ,
} ,
2023-07-06 11:34:05 +02:00
}
2022-08-18 10:03:24 +02:00
2023-07-06 11:34:05 +02:00
utils . AddFile ( "/cnb/order.toml" , [ ] byte ( ` [ [ order ] ]
[ [ order . group ] ]
id = "buildpacks/java"
version = "1.8.0"
[ [ order ] ]
[ [ order . group ] ]
id = "buildpacks/nodejs"
version = "1.6.0" ` ) )
2022-03-02 17:26:45 +02:00
utils . AddFile ( "/layers/report.toml" , [ ] byte ( ` [ build ]
[ image ]
tags = [ "localhost:5000/not-found:0.0.1" ]
digest = "sha256:52eac630560210e5ae13eb10797c4246d6f02d425f32b9430ca00bde697c79ec"
manifest - size = 2388 ` ) )
2021-08-18 12:10:55 +02:00
return utils
}
2021-09-14 16:14:50 +02:00
func addBuilderFiles ( utils * cnbutils . MockUtils ) {
2021-12-07 15:06:35 +02:00
utils . FilesMock . AddFile ( creatorPath , [ ] byte ( ` xyz ` ) )
}
2022-02-15 15:39:14 +02:00
func assertLifecycleCalls ( t * testing . T , runner * mock . ExecMockRunner , callNo int ) {
require . GreaterOrEqual ( t , len ( runner . Calls ) , callNo )
assert . Equal ( t , creatorPath , runner . Calls [ callNo - 1 ] . Exec )
2021-12-07 15:06:35 +02:00
for _ , arg := range [ ] string { "-no-color" , "-buildpacks" , "/cnb/buildpacks" , "-order" , "/cnb/order.toml" , "-platform" , "/tmp/platform" } {
2022-02-15 15:39:14 +02:00
assert . Contains ( t , runner . Calls [ callNo - 1 ] . Params , arg )
2021-09-14 15:38:58 +02:00
}
}
2022-10-25 14:35:24 +02:00
func assetBuildEnv ( t * testing . T , utils cnbutils . MockUtils , key , value string ) bool {
env , err := utils . FilesMock . ReadFile ( filepath . Join ( "/tmp/platform/env/" , key ) )
if ! assert . NoError ( t , err ) {
return false
}
return assert . Equal ( t , value , string ( env ) )
}
2021-08-18 12:10:55 +02:00
func TestRunCnbBuild ( t * testing . T ) {
2023-10-04 12:44:48 +02:00
configOptions . OpenFile = piperconf . OpenPiperFile
2021-08-18 12:10:55 +02:00
2022-02-01 09:23:53 +02:00
t . Run ( "prefers direct configuration" , func ( t * testing . T ) {
2022-01-14 17:49:45 +02:00
t . Parallel ( )
commonPipelineEnvironment := cnbBuildCommonPipelineEnvironment { }
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "0.0.1" ,
ContainerRegistryURL : fmt . Sprintf ( "https://%s" , imageRegistry ) ,
DockerConfigJSON : "/path/to/config.json" ,
2022-10-27 15:18:32 +02:00
RunImage : "my-run-image" ,
2022-12-12 10:33:54 +02:00
DefaultProcess : "my-process" ,
2022-01-14 17:49:45 +02:00
}
projectToml := ` [ project ]
id = "io.buildpacks.my-app"
`
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
utils . FilesMock . AddFile ( "project.toml" , [ ] byte ( projectToml ) )
addBuilderFiles ( & utils )
2022-02-16 14:28:51 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & commonPipelineEnvironment , & piperhttp . Client { } )
2022-01-14 17:49:45 +02:00
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2022-01-14 17:49:45 +02:00
runner := utils . ExecMockRunner
assert . Contains ( t , runner . Env , "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}" )
2023-08-09 09:18:48 +02:00
assertLifecycleCalls ( t , runner , 2 )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , imageRegistry , config . ContainerImageName , config . ContainerImageTag ) )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "-run-image" )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "my-run-image" )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "-process-type" )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "my-process" )
2022-01-14 17:49:45 +02:00
assert . Equal ( t , config . ContainerRegistryURL , commonPipelineEnvironment . container . registryURL )
assert . Equal ( t , "my-image:0.0.1" , commonPipelineEnvironment . container . imageNameTag )
2022-06-14 15:14:44 +02:00
assert . Equal ( t , ` { "cnbBuild":[ { "dockerImage":"paketobuildpacks/builder:base"}]} ` , commonPipelineEnvironment . custom . buildSettingsInfo )
2022-01-14 17:49:45 +02:00
} )
2022-02-01 09:23:53 +02:00
t . Run ( "prefers project descriptor" , func ( t * testing . T ) {
2022-01-14 17:49:45 +02:00
t . Parallel ( )
commonPipelineEnvironment := cnbBuildCommonPipelineEnvironment { }
config := cnbBuildOptions {
ContainerImageTag : "0.0.1" ,
ContainerRegistryURL : fmt . Sprintf ( "https://%s" , imageRegistry ) ,
DockerConfigJSON : "/path/to/config.json" ,
ProjectDescriptor : "project.toml" ,
}
projectToml := ` [ project ]
id = "io.buildpacks.my-app"
`
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
utils . FilesMock . AddFile ( "project.toml" , [ ] byte ( projectToml ) )
addBuilderFiles ( & utils )
2023-08-04 13:31:33 +02:00
telemetryData := & telemetry . CustomData { }
err := callCnbBuild ( & config , telemetryData , & utils , & commonPipelineEnvironment , & piperhttp . Client { } )
2022-01-14 17:49:45 +02:00
2022-02-01 09:23:53 +02:00
require . NoError ( t , err )
2022-01-14 17:49:45 +02:00
runner := utils . ExecMockRunner
assert . Contains ( t , runner . Env , "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}" )
2023-08-09 09:18:48 +02:00
assertLifecycleCalls ( t , runner , 2 )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , imageRegistry , "io-buildpacks-my-app" , config . ContainerImageTag ) )
2022-01-14 17:49:45 +02:00
assert . Equal ( t , config . ContainerRegistryURL , commonPipelineEnvironment . container . registryURL )
assert . Equal ( t , "io-buildpacks-my-app:0.0.1" , commonPipelineEnvironment . container . imageNameTag )
2022-02-01 09:23:53 +02:00
2022-03-02 17:26:45 +02:00
assert . Equal ( t , "sha256:52eac630560210e5ae13eb10797c4246d6f02d425f32b9430ca00bde697c79ec" , commonPipelineEnvironment . container . imageDigest )
assert . Contains ( t , commonPipelineEnvironment . container . imageDigests , "sha256:52eac630560210e5ae13eb10797c4246d6f02d425f32b9430ca00bde697c79ec" )
2022-02-01 09:23:53 +02:00
customDataAsString := telemetryData . Custom1
2023-08-04 13:31:33 +02:00
customData := & buildpacks . BuildpacksTelemetry { }
err = json . Unmarshal ( [ ] byte ( customDataAsString ) , customData )
2022-02-01 09:23:53 +02:00
require . NoError ( t , err )
2022-02-15 15:39:14 +02:00
assert . Equal ( t , 1 , len ( customData . Data ) )
assert . Equal ( t , "root" , string ( customData . Data [ 0 ] . Path ) )
2022-01-14 17:49:45 +02:00
} )
2021-08-26 14:26:54 +02:00
t . Run ( "success case (registry with https)" , func ( t * testing . T ) {
2021-08-18 12:10:55 +02:00
t . Parallel ( )
2021-12-07 15:06:35 +02:00
commonPipelineEnvironment := cnbBuildCommonPipelineEnvironment { }
2021-08-18 12:10:55 +02:00
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "0.0.1" ,
2022-01-14 17:49:45 +02:00
ContainerRegistryURL : fmt . Sprintf ( "https://%s" , imageRegistry ) ,
2021-08-18 12:10:55 +02:00
DockerConfigJSON : "/path/to/config.json" ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
2021-09-14 15:38:58 +02:00
addBuilderFiles ( & utils )
2021-08-18 12:10:55 +02:00
2022-02-16 14:28:51 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & commonPipelineEnvironment , & piperhttp . Client { } )
2021-08-18 12:10:55 +02:00
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2021-08-18 12:10:55 +02:00
runner := utils . ExecMockRunner
assert . Contains ( t , runner . Env , "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}" )
2023-08-09 09:18:48 +02:00
assertLifecycleCalls ( t , runner , 2 )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , imageRegistry , config . ContainerImageName , config . ContainerImageTag ) )
2022-01-14 17:49:45 +02:00
assert . Equal ( t , config . ContainerRegistryURL , commonPipelineEnvironment . container . registryURL )
2021-12-07 15:06:35 +02:00
assert . Equal ( t , "my-image:0.0.1" , commonPipelineEnvironment . container . imageNameTag )
2021-08-26 14:26:54 +02:00
} )
t . Run ( "success case (registry without https)" , func ( t * testing . T ) {
t . Parallel ( )
2021-12-07 15:06:35 +02:00
commonPipelineEnvironment := cnbBuildCommonPipelineEnvironment { }
2021-08-26 14:26:54 +02:00
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "0.0.1" ,
2022-01-14 17:49:45 +02:00
ContainerRegistryURL : imageRegistry ,
2021-08-26 14:26:54 +02:00
DockerConfigJSON : "/path/to/config.json" ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
2021-09-14 15:38:58 +02:00
addBuilderFiles ( & utils )
2021-08-26 14:26:54 +02:00
2022-02-16 14:28:51 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & commonPipelineEnvironment , & piperhttp . Client { } )
2021-09-14 16:14:50 +02:00
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2021-09-14 16:14:50 +02:00
runner := utils . ExecMockRunner
assert . Contains ( t , runner . Env , "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}" )
2023-08-09 09:18:48 +02:00
assertLifecycleCalls ( t , runner , 2 )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , config . ContainerRegistryURL , config . ContainerImageName , config . ContainerImageTag ) )
2022-01-14 17:49:45 +02:00
assert . Equal ( t , fmt . Sprintf ( "https://%s" , config . ContainerRegistryURL ) , commonPipelineEnvironment . container . registryURL )
2021-12-07 15:06:35 +02:00
assert . Equal ( t , "my-image:0.0.1" , commonPipelineEnvironment . container . imageNameTag )
2021-09-14 16:14:50 +02:00
} )
2021-10-08 11:20:05 +02:00
t . Run ( "success case (custom buildpacks and custom env variables, renaming docker conf file, additional tag)" , func ( t * testing . T ) {
2021-09-14 16:14:50 +02:00
t . Parallel ( )
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "0.0.1" ,
2022-01-14 17:49:45 +02:00
ContainerRegistryURL : imageRegistry ,
2021-10-01 10:05:15 +02:00
DockerConfigJSON : "/path/to/test.json" ,
2021-09-14 16:14:50 +02:00
Buildpacks : [ ] string { "test" } ,
2021-11-29 12:32:32 +02:00
BuildEnvVars : map [ string ] interface { } {
"FOO" : "BAR" ,
} ,
AdditionalTags : [ ] string { "latest" } ,
2021-09-14 16:14:50 +02:00
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
2023-07-06 11:34:05 +02:00
addBuilderFiles ( & utils )
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
require . NoError ( t , err )
runner := utils . ExecMockRunner
assert . Contains ( t , runner . Env , "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}" )
2023-08-09 09:18:48 +02:00
assert . Equal ( t , creatorPath , runner . Calls [ 1 ] . Exec )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "/tmp/buildpacks" )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "/tmp/buildpacks/order.toml" )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , config . ContainerRegistryURL , config . ContainerImageName , config . ContainerImageTag ) )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:latest" , config . ContainerRegistryURL , config . ContainerImageName ) )
2023-07-06 11:34:05 +02:00
copiedFileExists , _ := utils . FileExists ( "/tmp/config.json" )
assert . True ( t , copiedFileExists )
} )
t . Run ( "success case (custom buildpacks, pre and post buildpacks and custom env variables, renaming docker conf file, additional tag)" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "0.0.1" ,
ContainerRegistryURL : imageRegistry ,
DockerConfigJSON : "/path/to/test.json" ,
PreBuildpacks : [ ] string { "pre-test" } ,
PostBuildpacks : [ ] string { "post-test" } ,
Buildpacks : [ ] string { "test" } ,
BuildEnvVars : map [ string ] interface { } {
"FOO" : "BAR" ,
} ,
AdditionalTags : [ ] string { "latest" } ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
addBuilderFiles ( & utils )
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
require . NoError ( t , err )
runner := utils . ExecMockRunner
assert . Contains ( t , runner . Env , "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}" )
2023-08-09 09:18:48 +02:00
assert . Equal ( t , creatorPath , runner . Calls [ 1 ] . Exec )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "/tmp/buildpacks" )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "/tmp/buildpacks/order.toml" )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , config . ContainerRegistryURL , config . ContainerImageName , config . ContainerImageTag ) )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:latest" , config . ContainerRegistryURL , config . ContainerImageName ) )
2023-07-06 11:34:05 +02:00
copiedFileExists , _ := utils . FileExists ( "/tmp/config.json" )
assert . True ( t , copiedFileExists )
} )
t . Run ( "success case (custom pre and post buildpacks and custom env variables, renaming docker conf file, additional tag)" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "0.0.1" ,
ContainerRegistryURL : imageRegistry ,
DockerConfigJSON : "/path/to/test.json" ,
PostBuildpacks : [ ] string { "post-test" } ,
PreBuildpacks : [ ] string { "pre-test" } ,
BuildEnvVars : map [ string ] interface { } {
"FOO" : "BAR" ,
} ,
AdditionalTags : [ ] string { "latest" } ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
2021-09-14 16:14:50 +02:00
addBuilderFiles ( & utils )
2022-06-14 15:14:44 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2021-08-26 14:26:54 +02:00
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2021-08-26 14:26:54 +02:00
runner := utils . ExecMockRunner
assert . Contains ( t , runner . Env , "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}" )
2023-08-09 09:18:48 +02:00
assert . Equal ( t , creatorPath , runner . Calls [ 1 ] . Exec )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "/tmp/buildpacks" )
assert . Contains ( t , runner . Calls [ 1 ] . Params , "/tmp/buildpacks/order.toml" )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , config . ContainerRegistryURL , config . ContainerImageName , config . ContainerImageTag ) )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:latest" , config . ContainerRegistryURL , config . ContainerImageName ) )
2022-01-11 11:01:15 +02:00
2023-07-04 14:19:02 +02:00
copiedFileExists , _ := utils . FileExists ( "/tmp/config.json" )
assert . True ( t , copiedFileExists )
2021-08-18 12:10:55 +02:00
} )
2021-10-01 13:48:24 +02:00
t . Run ( "success case (customTlsCertificates)" , func ( t * testing . T ) {
t . Parallel ( )
2021-10-11 11:33:51 +02:00
httpmock . Activate ( )
defer httpmock . DeactivateAndReset ( )
httpmock . RegisterResponder ( http . MethodGet , "https://test-cert.com/cert.crt" , httpmock . NewStringResponder ( 200 , "testCert" ) )
client := & piperhttp . Client { }
client . SetOptions ( piperhttp . ClientOptions { MaxRetries : - 1 , UseDefaultTransport : true } )
2021-10-01 13:48:24 +02:00
caCertsFile := "/etc/ssl/certs/ca-certificates.crt"
2021-10-07 16:04:20 +02:00
caCertsTmpFile := "/tmp/ca-certificates.crt"
2021-10-01 13:48:24 +02:00
registry := "some-registry"
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "0.0.1" ,
ContainerRegistryURL : registry ,
DockerConfigJSON : "/path/to/config.json" ,
2021-10-11 11:33:51 +02:00
CustomTLSCertificateLinks : [ ] string { "https://test-cert.com/cert.crt" , "https://test-cert.com/cert.crt" } ,
2021-10-01 13:48:24 +02:00
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( caCertsFile , [ ] byte ( "test\n" ) )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
addBuilderFiles ( & utils )
2022-06-14 15:14:44 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , client )
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2021-10-01 13:48:24 +02:00
2021-10-07 16:04:20 +02:00
result , err := utils . FilesMock . FileRead ( caCertsTmpFile )
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2021-10-01 13:48:24 +02:00
assert . Equal ( t , "test\ntestCert\ntestCert\n" , string ( result ) )
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2021-10-01 13:48:24 +02:00
runner := utils . ExecMockRunner
assert . Contains ( t , runner . Env , "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}" )
2021-10-07 16:04:20 +02:00
assert . Contains ( t , runner . Env , fmt . Sprintf ( "SSL_CERT_FILE=%s" , caCertsTmpFile ) )
2023-08-09 09:18:48 +02:00
assertLifecycleCalls ( t , runner , 2 )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , config . ContainerRegistryURL , config . ContainerImageName , config . ContainerImageTag ) )
2021-10-08 11:20:05 +02:00
} )
t . Run ( "success case (additionalTags)" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "3.1.5" ,
2022-01-14 17:49:45 +02:00
ContainerRegistryURL : imageRegistry ,
2021-10-08 11:20:05 +02:00
DockerConfigJSON : "/path/to/config.json" ,
AdditionalTags : [ ] string { "3" , "3.1" , "3.1" , "3.1.5" } ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
addBuilderFiles ( & utils )
2022-06-14 15:14:44 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2021-10-08 11:20:05 +02:00
runner := utils . ExecMockRunner
2023-08-09 09:18:48 +02:00
assertLifecycleCalls ( t , runner , 2 )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , config . ContainerRegistryURL , config . ContainerImageName , config . ContainerImageTag ) )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:3" , config . ContainerRegistryURL , config . ContainerImageName ) )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:3.1" , config . ContainerRegistryURL , config . ContainerImageName ) )
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:3.1.5" , config . ContainerRegistryURL , config . ContainerImageName ) )
2021-10-01 13:48:24 +02:00
} )
2022-10-25 14:35:24 +02:00
t . Run ( "success case: build environment variables" , func ( t * testing . T ) {
t . Parallel ( )
commonPipelineEnvironment := cnbBuildCommonPipelineEnvironment { }
config := cnbBuildOptions {
ContainerImageTag : "0.0.1" ,
ContainerRegistryURL : fmt . Sprintf ( "https://%s" , imageRegistry ) ,
ProjectDescriptor : "project.toml" ,
BuildEnvVars : map [ string ] interface { } {
"OPTIONS_KEY" : "OPTIONS_VALUE" ,
"OVERWRITE" : "this should win" ,
} ,
}
projectToml := ` [ project ]
id = "io.buildpacks.my-app"
[ [ build . env ] ]
name = "PROJECT_DESCRIPTOR_KEY"
value = "PROJECT_DESCRIPTOR_VALUE"
[ [ build . env ] ]
name = "OVERWRITE"
value = "this should be overwritten"
`
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( "project.toml" , [ ] byte ( projectToml ) )
addBuilderFiles ( & utils )
telemetryData := telemetry . CustomData { }
err := callCnbBuild ( & config , & telemetryData , & utils , & commonPipelineEnvironment , & piperhttp . Client { } )
require . NoError ( t , err )
2023-08-09 09:18:48 +02:00
assertLifecycleCalls ( t , utils . ExecMockRunner , 2 )
2022-10-25 14:35:24 +02:00
assetBuildEnv ( t , utils , "OPTIONS_KEY" , "OPTIONS_VALUE" )
assetBuildEnv ( t , utils , "PROJECT_DESCRIPTOR_KEY" , "PROJECT_DESCRIPTOR_VALUE" )
assetBuildEnv ( t , utils , "OVERWRITE" , "this should win" )
} )
2022-01-14 12:05:11 +02:00
t . Run ( "pom.xml exists (symlink for the target folder)" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "3.1.5" ,
2022-01-14 17:49:45 +02:00
ContainerRegistryURL : imageRegistry ,
2022-01-14 12:05:11 +02:00
DockerConfigJSON : "/path/to/config.json" ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . CurrentDir = "/jenkins"
utils . FilesMock . AddDir ( "/jenkins" )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
utils . FilesMock . AddFile ( "/workspace/pom.xml" , [ ] byte ( "test" ) )
addBuilderFiles ( & utils )
2022-02-16 14:28:51 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2022-01-14 12:05:11 +02:00
runner := utils . ExecMockRunner
2023-08-09 09:18:48 +02:00
assertLifecycleCalls ( t , runner , 2 )
2022-01-14 12:05:11 +02:00
assert . True ( t , utils . FilesMock . HasCreatedSymlink ( "/jenkins/target" , "/workspace/target" ) )
} )
t . Run ( "no pom.xml exists (no symlink for the target folder)" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "3.1.5" ,
2022-01-14 17:49:45 +02:00
ContainerRegistryURL : imageRegistry ,
2022-01-14 12:05:11 +02:00
DockerConfigJSON : "/path/to/config.json" ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . CurrentDir = "/jenkins"
utils . FilesMock . AddDir ( "/jenkins" )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
addBuilderFiles ( & utils )
2022-02-16 14:28:51 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2022-01-14 12:05:11 +02:00
runner := utils . ExecMockRunner
2023-08-09 09:18:48 +02:00
assertLifecycleCalls ( t , runner , 2 )
2022-01-14 12:05:11 +02:00
assert . False ( t , utils . FilesMock . HasCreatedSymlink ( "/jenkins/target" , "/workspace/target" ) )
} )
2021-08-18 12:10:55 +02:00
t . Run ( "error case: Invalid DockerConfigJSON file" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions {
2022-01-14 17:49:45 +02:00
ContainerImageTag : "0.0.1" ,
ContainerRegistryURL : imageRegistry ,
ContainerImageName : "my-image" ,
DockerConfigJSON : "/path/to/config.json" ,
2021-08-18 12:10:55 +02:00
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry":"dXNlcjpwYXNz"}} ` ) )
2021-09-14 15:38:58 +02:00
addBuilderFiles ( & utils )
2021-08-18 12:10:55 +02:00
2022-06-14 15:14:44 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2022-01-11 11:01:15 +02:00
assert . EqualError ( t , err , "failed to generate CNB_REGISTRY_AUTH: json: cannot unmarshal string into Go struct field ConfigFile.auths of type types.AuthConfig" )
2021-08-18 12:10:55 +02:00
} )
2021-10-01 10:05:15 +02:00
t . Run ( "error case: DockerConfigJSON file not there (config.json)" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions {
2022-01-14 17:49:45 +02:00
ContainerImageTag : "0.0.1" ,
ContainerRegistryURL : imageRegistry ,
ContainerImageName : "my-image" ,
DockerConfigJSON : "not-there/config.json" ,
2021-10-01 10:05:15 +02:00
}
utils := newCnbBuildTestsUtils ( )
addBuilderFiles ( & utils )
2022-06-14 15:14:44 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2023-07-04 14:19:02 +02:00
assert . EqualError ( t , err , "failed to create/rename DockerConfigJSON file: cannot copy 'not-there/config.json': file does not exist" )
2021-10-01 10:05:15 +02:00
} )
t . Run ( "error case: DockerConfigJSON file not there (not config.json)" , func ( t * testing . T ) {
2021-08-18 12:10:55 +02:00
t . Parallel ( )
config := cnbBuildOptions {
2022-01-14 17:49:45 +02:00
ContainerImageTag : "0.0.1" ,
ContainerRegistryURL : imageRegistry ,
ContainerImageName : "my-image" ,
DockerConfigJSON : "not-there" ,
2021-08-18 12:10:55 +02:00
}
utils := newCnbBuildTestsUtils ( )
2021-09-14 15:38:58 +02:00
addBuilderFiles ( & utils )
2022-06-14 15:14:44 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2023-07-04 14:19:02 +02:00
assert . EqualError ( t , err , "failed to create/rename DockerConfigJSON file: cannot copy 'not-there': file does not exist" )
2021-08-18 12:10:55 +02:00
} )
2021-09-14 15:38:58 +02:00
t . Run ( "error case: dockerImage is not a valid builder" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions { }
utils := newCnbBuildTestsUtils ( )
2022-06-14 15:14:44 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2021-12-07 15:06:35 +02:00
assert . EqualError ( t , err , "the provided dockerImage is not a valid builder: binary '/cnb/lifecycle/creator' not found" )
2021-09-14 15:38:58 +02:00
} )
2021-10-07 16:04:20 +02:00
t . Run ( "error case: builder image does not contain tls certificates" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "0.0.1" ,
2022-01-14 17:49:45 +02:00
ContainerRegistryURL : imageRegistry ,
2021-10-07 16:04:20 +02:00
DockerConfigJSON : "/path/to/config.json" ,
Buildpacks : [ ] string { "test" } ,
CustomTLSCertificateLinks : [ ] string { "http://example.com/certs.pem" } ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
addBuilderFiles ( & utils )
2022-06-14 15:14:44 +02:00
err := callCnbBuild ( & config , & telemetry . CustomData { } , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2021-10-07 16:04:20 +02:00
assert . EqualError ( t , err , "failed to copy certificates: cannot copy '/etc/ssl/certs/ca-certificates.crt': file does not exist" )
} )
2022-01-31 13:27:29 +02:00
t . Run ( "success case (telemetry was added)" , func ( t * testing . T ) {
t . Parallel ( )
registry := "some-registry"
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "3.1.5" ,
ContainerRegistryURL : registry ,
DockerConfigJSON : "/path/to/config.json" ,
ProjectDescriptor : "project.toml" ,
AdditionalTags : [ ] string { "latest" } ,
Buildpacks : [ ] string { "paketobuildpacks/java" , "gcr.io/paketo-buildpacks/node" } ,
Bindings : map [ string ] interface { } { "SECRET" : map [ string ] string { "key" : "KEY" , "file" : "a_file" } } ,
Path : "target" ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
2022-02-15 15:39:14 +02:00
utils . FilesMock . AddDir ( "target" )
utils . FilesMock . AddFile ( "target/project.toml" , [ ] byte ( ` [ project ]
2022-01-31 13:27:29 +02:00
id = "test"
name = "test"
version = "1.0.0"
[ build ]
include = [ ]
exclude = [ "*.tar" ]
[ [ build . buildpacks ] ]
uri = "some-buildpack" ` ) )
utils . FilesMock . AddFile ( "a_file" , [ ] byte ( ` { } ` ) )
utils . FilesMock . AddFile ( "target/somelib.jar" , [ ] byte ( ` FFFFFF ` ) )
addBuilderFiles ( & utils )
2023-08-04 13:31:33 +02:00
telemetryData := & telemetry . CustomData { }
err := callCnbBuild ( & config , telemetryData , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2022-01-31 13:27:29 +02:00
customDataAsString := telemetryData . Custom1
2023-08-04 13:31:33 +02:00
customData := & buildpacks . BuildpacksTelemetry { }
err = json . Unmarshal ( [ ] byte ( customDataAsString ) , customData )
2022-01-31 13:27:29 +02:00
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2022-02-16 14:28:51 +02:00
assert . Equal ( t , 3 , customData . Version )
2022-02-15 15:39:14 +02:00
require . Equal ( t , 1 , len ( customData . Data ) )
assert . Equal ( t , "3.1.5" , customData . Data [ 0 ] . ImageTag )
assert . Equal ( t , "folder" , string ( customData . Data [ 0 ] . Path ) )
assert . Contains ( t , customData . Data [ 0 ] . AdditionalTags , "latest" )
assert . Contains ( t , customData . Data [ 0 ] . BindingKeys , "SECRET" )
2022-04-12 16:01:38 +02:00
assert . Equal ( t , "paketobuildpacks/builder:base" , customData . Data [ 0 ] . Builder )
2022-02-15 15:39:14 +02:00
assert . Contains ( t , customData . Data [ 0 ] . Buildpacks . FromConfig , "paketobuildpacks/java" )
assert . NotContains ( t , customData . Data [ 0 ] . Buildpacks . FromProjectDescriptor , "paketobuildpacks/java" )
2023-04-18 09:10:38 +02:00
assert . Contains ( t , customData . Data [ 0 ] . Buildpacks . FromProjectDescriptor , "bcc73ab1f0a0d3fb0d1bf2b6df5510a25ccd14a761dbc0f5044ea24ead30452b" )
2022-02-15 15:39:14 +02:00
assert . Contains ( t , customData . Data [ 0 ] . Buildpacks . Overall , "paketobuildpacks/java" )
assert . True ( t , customData . Data [ 0 ] . ProjectDescriptor . Used )
assert . False ( t , customData . Data [ 0 ] . ProjectDescriptor . IncludeUsed )
assert . True ( t , customData . Data [ 0 ] . ProjectDescriptor . ExcludeUsed )
2022-01-31 13:27:29 +02:00
} )
2022-04-12 11:04:05 +02:00
t . Run ( "error case, multiple artifacts in path" , func ( t * testing . T ) {
t . Parallel ( )
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "3.1.5" ,
ContainerRegistryURL : fmt . Sprintf ( "https://%s" , imageRegistry ) ,
DockerConfigJSON : "/path/to/config.json" ,
ProjectDescriptor : "project.toml" ,
AdditionalTags : [ ] string { "latest" } ,
Buildpacks : [ ] string { "paketobuildpacks/java" , "gcr.io/paketo-buildpacks/node" } ,
Path : "target/*.jar" ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
utils . FilesMock . AddDir ( "target" )
utils . FilesMock . AddFile ( "target/app.jar" , [ ] byte ( ` FFFFFF ` ) )
utils . FilesMock . AddFile ( "target/app-src.jar" , [ ] byte ( ` FFFFFF ` ) )
addBuilderFiles ( & utils )
telemetryData := telemetry . CustomData { }
2022-06-14 15:14:44 +02:00
err := callCnbBuild ( & config , & telemetryData , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2022-04-12 11:04:05 +02:00
require . EqualError ( t , err , "could not resolve path: Failed to resolve glob for 'target/*.jar', matching 2 file(s)" )
} )
t . Run ( "success case, artifacts found by glob" , func ( t * testing . T ) {
t . Parallel ( )
commonPipelineEnvironment := cnbBuildCommonPipelineEnvironment { }
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "3.1.5" ,
ContainerRegistryURL : fmt . Sprintf ( "https://%s" , imageRegistry ) ,
DockerConfigJSON : "/path/to/config.json" ,
ProjectDescriptor : "project.toml" ,
AdditionalTags : [ ] string { "latest" } ,
Buildpacks : [ ] string { "paketobuildpacks/java" , "gcr.io/paketo-buildpacks/node" } ,
Path : "**/target" ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
utils . FilesMock . AddDir ( "target" )
utils . FilesMock . AddFile ( "target/app.jar" , [ ] byte ( ` FFFFFF ` ) )
addBuilderFiles ( & utils )
telemetryData := telemetry . CustomData { }
err := callCnbBuild ( & config , & telemetryData , & utils , & commonPipelineEnvironment , & piperhttp . Client { } )
require . NoError ( t , err )
runner := utils . ExecMockRunner
assert . Contains ( t , runner . Env , "CNB_REGISTRY_AUTH={\"my-registry\":\"Basic dXNlcjpwYXNz\"}" )
2023-08-09 09:18:48 +02:00
assert . Contains ( t , runner . Calls [ 1 ] . Params , fmt . Sprintf ( "%s/%s:%s" , imageRegistry , config . ContainerImageName , config . ContainerImageTag ) )
2022-04-12 11:04:05 +02:00
assert . Equal ( t , config . ContainerRegistryURL , commonPipelineEnvironment . container . registryURL )
assert . Equal ( t , "my-image:3.1.5" , commonPipelineEnvironment . container . imageNameTag )
} )
2022-01-31 13:27:29 +02:00
t . Run ( "success case (build env telemetry was added)" , func ( t * testing . T ) {
t . Parallel ( )
registry := "some-registry"
config := cnbBuildOptions {
ContainerImageName : "my-image" ,
ContainerImageTag : "3.1.5" ,
ContainerRegistryURL : registry ,
ProjectDescriptor : "project.toml" ,
BuildEnvVars : map [ string ] interface { } { "CONFIG_KEY" : "var" , "BP_JVM_VERSION" : "8" } ,
}
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( "project.toml" , [ ] byte ( ` [ project ]
id = "test"
[ build ]
include = [ ]
[ [ build . env ] ]
name = ' PROJECT_KEY '
value = ' var '
[ [ build . env ] ]
name = ' BP_NODE_VERSION '
value = ' 11 '
[ [ build . buildpacks ] ]
uri = "some-buildpack"
` ) )
addBuilderFiles ( & utils )
2023-08-04 13:31:33 +02:00
telemetryData := & telemetry . CustomData { }
err := callCnbBuild ( & config , telemetryData , & utils , & cnbBuildCommonPipelineEnvironment { } , & piperhttp . Client { } )
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
2022-01-31 13:27:29 +02:00
customDataAsString := telemetryData . Custom1
2023-08-04 13:31:33 +02:00
customData := & buildpacks . BuildpacksTelemetry { }
err = json . Unmarshal ( [ ] byte ( customDataAsString ) , customData )
2022-01-31 13:27:29 +02:00
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
require . Equal ( t , 1 , len ( customData . Data ) )
assert . Contains ( t , customData . Data [ 0 ] . BuildEnv . KeysFromConfig , "CONFIG_KEY" )
assert . NotContains ( t , customData . Data [ 0 ] . BuildEnv . KeysFromProjectDescriptor , "CONFIG_KEY" )
assert . Contains ( t , customData . Data [ 0 ] . BuildEnv . KeysOverall , "CONFIG_KEY" )
assert . NotContains ( t , customData . Data [ 0 ] . BuildEnv . KeysFromConfig , "PROJECT_KEY" )
assert . Contains ( t , customData . Data [ 0 ] . BuildEnv . KeysFromProjectDescriptor , "PROJECT_KEY" )
assert . Contains ( t , customData . Data [ 0 ] . BuildEnv . KeysOverall , "PROJECT_KEY" )
2022-01-31 13:27:29 +02:00
2022-02-15 15:39:14 +02:00
assert . Equal ( t , "8" , customData . Data [ 0 ] . BuildEnv . KeyValues [ "BP_JVM_VERSION" ] )
assert . Equal ( t , "11" , customData . Data [ 0 ] . BuildEnv . KeyValues [ "BP_NODE_VERSION" ] )
assert . NotContains ( t , customData . Data [ 0 ] . BuildEnv . KeyValues , "PROJECT_KEY" )
2022-01-31 13:27:29 +02:00
2023-04-18 09:10:38 +02:00
assert . Contains ( t , customData . Data [ 0 ] . Buildpacks . Overall , "bcc73ab1f0a0d3fb0d1bf2b6df5510a25ccd14a761dbc0f5044ea24ead30452b" )
2022-02-15 15:39:14 +02:00
} )
t . Run ( "success case (multiple images configured)" , func ( t * testing . T ) {
t . Parallel ( )
commonPipelineEnvironment := cnbBuildCommonPipelineEnvironment { }
config := cnbBuildOptions {
ContainerImageTag : "3.1.5" ,
ContainerRegistryURL : imageRegistry ,
DockerConfigJSON : "/path/to/my-config.json" ,
AdditionalTags : [ ] string { "3" , "3.1" , "3.1" , "3.1.5" } ,
2022-06-15 14:17:46 +02:00
MultipleImages : [ ] map [ string ] interface { } { { "ContainerImageName" : "my-image-0" , "ContainerImageAlias" : "simple" } , { "ContainerImageName" : "my-image-1" } } ,
2022-02-15 15:39:14 +02:00
}
expectedImageCount := len ( config . MultipleImages )
utils := newCnbBuildTestsUtils ( )
utils . FilesMock . AddFile ( config . DockerConfigJSON , [ ] byte ( ` { "auths": { "my-registry": { "auth":"dXNlcjpwYXNz"}}} ` ) )
addBuilderFiles ( & utils )
2023-08-04 13:31:33 +02:00
telemetryData := & telemetry . CustomData { }
err := callCnbBuild ( & config , telemetryData , & utils , & commonPipelineEnvironment , & piperhttp . Client { } )
2022-02-15 15:39:14 +02:00
require . NoError ( t , err )
customDataAsString := telemetryData . Custom1
2023-08-04 13:31:33 +02:00
customData := & buildpacks . BuildpacksTelemetry { }
err = json . Unmarshal ( [ ] byte ( customDataAsString ) , customData )
2022-03-02 17:26:45 +02:00
assert . NoError ( t , err )
2022-02-15 15:39:14 +02:00
require . Equal ( t , expectedImageCount , len ( customData . Data ) )
runner := utils . ExecMockRunner
2023-08-09 09:18:48 +02:00
require . Equal ( t , expectedImageCount , len ( runner . Calls ) - 1 )
2022-02-15 15:39:14 +02:00
for i , call := range runner . Calls {
2023-08-09 09:18:48 +02:00
if i == 0 { // first call is -version
continue
}
lifecycleCall := i - 1
assert . Equal ( t , 4 , len ( customData . Data [ lifecycleCall ] . AdditionalTags ) )
2022-02-15 15:39:14 +02:00
assertLifecycleCalls ( t , runner , i + 1 )
2023-08-09 09:18:48 +02:00
containerImageName := fmt . Sprintf ( "my-image-%d" , lifecycleCall )
2022-02-15 15:39:14 +02:00
assert . Contains ( t , call . Params , fmt . Sprintf ( "%s/%s:%s" , config . ContainerRegistryURL , containerImageName , config . ContainerImageTag ) )
assert . Contains ( t , call . Params , fmt . Sprintf ( "%s/%s:3" , config . ContainerRegistryURL , containerImageName ) )
assert . Contains ( t , call . Params , fmt . Sprintf ( "%s/%s:3.1" , config . ContainerRegistryURL , containerImageName ) )
assert . Contains ( t , call . Params , fmt . Sprintf ( "%s/%s:3.1.5" , config . ContainerRegistryURL , containerImageName ) )
}
2022-01-31 13:27:29 +02:00
2022-02-15 15:39:14 +02:00
assert . Equal ( t , "my-image-0:3.1.5" , commonPipelineEnvironment . container . imageNameTag )
2022-06-15 14:17:46 +02:00
assert . Equal ( t , [ ] string { "simple" , "my-image-1" } , commonPipelineEnvironment . container . imageNames )
2022-01-31 13:27:29 +02:00
} )
2021-08-18 12:10:55 +02:00
}