2023-05-03 18:02:11 +02:00
//go:build unit
// +build unit
2020-01-24 15:30:27 +02:00
package cmd
import (
"bytes"
"fmt"
"testing"
2020-04-24 09:37:11 +02:00
"github.com/SAP/jenkins-library/pkg/mock"
2022-01-28 09:31:34 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
2020-04-24 09:37:11 +02:00
2020-01-24 15:30:27 +02:00
"github.com/stretchr/testify/assert"
2022-02-24 13:01:36 +02:00
"github.com/stretchr/testify/require"
2020-01-24 15:30:27 +02:00
)
2021-12-02 13:18:21 +02:00
type kubernetesDeployMockUtils struct {
2022-07-21 09:04:21 +02:00
shouldFail bool
2021-12-02 13:18:21 +02:00
* mock . FilesMock
* mock . ExecMockRunner
2022-03-17 18:13:34 +02:00
* mock . HttpClientMock
2021-12-02 13:18:21 +02:00
}
func newKubernetesDeployMockUtils ( ) kubernetesDeployMockUtils {
utils := kubernetesDeployMockUtils {
shouldFail : false ,
FilesMock : & mock . FilesMock { } ,
ExecMockRunner : & mock . ExecMockRunner { } ,
}
return utils
}
2020-01-24 15:30:27 +02:00
func TestRunKubernetesDeploy ( t * testing . T ) {
t . Run ( "test helm" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
2022-01-11 12:30:40 +02:00
ContainerRegistryPassword : "dummy" ,
2020-08-14 17:16:25 +02:00
ContainerRegistrySecret : "testSecret" ,
2020-01-24 15:30:27 +02:00
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm" ,
2021-01-07 17:52:16 +02:00
ForceUpdates : true ,
2023-05-05 14:23:11 +02:00
RenderSubchartNotes : true ,
2020-01-24 15:30:27 +02:00
HelmDeployWaitSeconds : 400 ,
IngressHosts : [ ] string { "ingress.host1" , "ingress.host2" } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
2022-01-11 12:30:40 +02:00
DockerConfigJSON : ".pipeline/docker/config.json" ,
2020-01-24 15:30:27 +02:00
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
2022-01-11 12:30:40 +02:00
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
2020-01-24 15:30:27 +02:00
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
telemetryData := & telemetry . CustomData { }
runKubernetesDeploy ( opts , telemetryData , mockUtils , & stdout )
2020-01-24 15:30:27 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 0 ] . Exec , "Wrong init command" )
assert . Equal ( t , [ ] string { "init" , "--client-only" } , mockUtils . Calls [ 0 ] . Params , "Wrong init parameters" )
2020-01-24 15:30:27 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 1 ] . Exec , "Wrong secret creation command" )
2022-01-11 12:30:40 +02:00
assert . Equal ( t , [ ] string { "create" , "secret" , "generic" , "testSecret" , "--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" , "--insecure-skip-tls-verify=true" , "--dry-run=client" , "--output=json" } ,
mockUtils . Calls [ 1 ] . Params , "Wrong secret creation parameters" )
2021-06-11 10:41:03 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 2 ] . Exec , "Wrong upgrade command" )
2021-06-11 10:41:03 +02:00
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
"--install" ,
"--namespace" ,
"deploymentNamespace" ,
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret,ingress.hosts[0]=ingress.host1,ingress.hosts[1]=ingress.host2" ,
2021-06-11 10:41:03 +02:00
"--force" ,
"--wait" ,
"--timeout" ,
"400" ,
"--atomic" ,
"--kube-context" ,
"testCluster" ,
2023-05-05 14:23:11 +02:00
"--render-subchart-notes" ,
2021-06-11 10:41:03 +02:00
"--testParam" ,
"testValue" ,
2021-12-02 13:18:21 +02:00
} , mockUtils . Calls [ 2 ] . Params , "Wrong upgrade parameters" )
2022-01-28 09:31:34 +02:00
assert . Equal ( t , & telemetry . CustomData {
Custom1Label : "deployTool" ,
Custom1 : "helm" ,
} , telemetryData )
2021-06-11 10:41:03 +02:00
} )
2021-10-08 14:58:32 +02:00
t . Run ( "test helm - with containerImageName and containerImageTag instead of image" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
2022-01-11 12:30:40 +02:00
ContainerRegistryPassword : "dummy" ,
2021-10-08 14:58:32 +02:00
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
IngressHosts : [ ] string { "ingress.host1" , "ingress.host2" } ,
ContainerImageTag : "latest" ,
ContainerImageName : "path/to/Image" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
2022-01-11 12:30:40 +02:00
DockerConfigJSON : ".pipeline/docker/config.json" ,
2021-10-08 14:58:32 +02:00
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
2022-01-11 12:30:40 +02:00
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
2021-10-08 14:58:32 +02:00
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-10-08 14:58:32 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 0 ] . Exec , "Wrong init command" )
assert . Equal ( t , [ ] string { "init" , "--client-only" } , mockUtils . Calls [ 0 ] . Params , "Wrong init parameters" )
2021-10-08 14:58:32 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 1 ] . Exec , "Wrong secret creation command" )
2022-01-11 12:30:40 +02:00
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 1 ] . Params , "Wrong secret creation parameters" )
2021-10-08 14:58:32 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 2 ] . Exec , "Wrong upgrade command" )
2021-10-08 14:58:32 +02:00
2022-02-24 13:01:36 +02:00
assert . Contains ( t , mockUtils . Calls [ 2 ] . Params , "image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret,ingress.hosts[0]=ingress.host1,ingress.hosts[1]=ingress.host2" , "Wrong upgrade parameters" )
2021-10-08 14:58:32 +02:00
} )
2021-06-11 10:41:03 +02:00
t . Run ( "test helm - docker config.json path passed as parameter" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
2022-01-11 12:30:40 +02:00
ContainerRegistryURL : "https://my.registry:55555" ,
DockerConfigJSON : "/path/to/.docker/config.json" ,
ContainerRegistryUser : "registryUser" ,
ContainerRegistryPassword : "dummy" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
IngressHosts : [ ] string { "ingress.host1" , "ingress.host2" } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
2021-06-11 10:41:03 +02:00
}
k8sSecretSpec := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
2022-01-11 12:30:40 +02:00
mockUtils . AddFile ( "/path/to/.docker/config.json" , [ ] byte ( ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} ` ) )
2021-12-02 13:18:21 +02:00
mockUtils . StdoutReturn = map [ string ] string {
2022-01-11 12:30:40 +02:00
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=/path/to/.docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : k8sSecretSpec ,
2021-06-11 10:41:03 +02:00
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-06-11 10:41:03 +02:00
assert . NoError ( t , err )
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 0 ] . Exec , "Wrong init command" )
assert . Equal ( t , [ ] string { "init" , "--client-only" } , mockUtils . Calls [ 0 ] . Params , "Wrong init parameters" )
2021-06-11 10:41:03 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 1 ] . Exec , "Wrong secret creation command" )
2021-06-11 10:41:03 +02:00
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=/path/to/.docker/config.json" ,
2022-01-11 12:30:40 +02:00
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 1 ] . Params , "Wrong secret creation parameters" )
2020-01-24 15:30:27 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 2 ] . Exec , "Wrong upgrade command" )
2020-11-10 16:43:19 +02:00
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
"--install" ,
"--namespace" ,
"deploymentNamespace" ,
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret,ingress.hosts[0]=ingress.host1,ingress.hosts[1]=ingress.host2" ,
2021-01-07 17:52:16 +02:00
"--force" ,
2020-11-10 16:43:19 +02:00
"--wait" ,
"--timeout" ,
"400" ,
"--atomic" ,
"--kube-context" ,
"testCluster" ,
"--testParam" ,
"testValue" ,
2021-12-02 13:18:21 +02:00
} , mockUtils . Calls [ 2 ] . Params , "Wrong upgrade parameters" )
2020-11-10 16:43:19 +02:00
} )
t . Run ( "test helm -- keep failed deployment" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
2022-01-11 12:30:40 +02:00
ContainerRegistryPassword : "dummy" ,
2020-11-10 16:43:19 +02:00
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm" ,
2021-01-07 17:52:16 +02:00
ForceUpdates : true ,
2020-11-10 16:43:19 +02:00
HelmDeployWaitSeconds : 400 ,
IngressHosts : [ ] string { "ingress.host1" , "ingress.host2" } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
KeepFailedDeployments : true ,
2022-01-11 12:30:40 +02:00
DockerConfigJSON : ".pipeline/docker/config.json" ,
2020-11-10 16:43:19 +02:00
}
2021-06-11 10:41:03 +02:00
k8sSecretSpec := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
2020-11-10 16:43:19 +02:00
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
2022-01-11 12:30:40 +02:00
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : k8sSecretSpec ,
2020-11-10 16:43:19 +02:00
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2020-11-10 16:43:19 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 0 ] . Exec , "Wrong init command" )
assert . Equal ( t , [ ] string { "init" , "--client-only" } , mockUtils . Calls [ 0 ] . Params , "Wrong init parameters" )
2020-11-10 16:43:19 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 1 ] . Exec , "Wrong secret creation command" )
2022-01-11 12:30:40 +02:00
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 1 ] . Params , "Wrong secret creation parameters" )
2020-11-10 16:43:19 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 2 ] . Exec , "Wrong upgrade command" )
2020-01-24 15:30:27 +02:00
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
"--install" ,
"--namespace" ,
"deploymentNamespace" ,
2020-04-24 09:37:11 +02:00
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret,ingress.hosts[0]=ingress.host1,ingress.hosts[1]=ingress.host2" ,
2021-01-07 17:52:16 +02:00
"--force" ,
2020-01-24 15:30:27 +02:00
"--wait" ,
"--timeout" ,
"400" ,
2020-04-24 09:37:11 +02:00
"--kube-context" ,
"testCluster" ,
"--testParam" ,
"testValue" ,
2021-12-02 13:18:21 +02:00
} , mockUtils . Calls [ 2 ] . Params , "Wrong upgrade parameters" )
2020-04-24 09:37:11 +02:00
} )
2021-10-08 14:58:32 +02:00
t . Run ( "test helm - fails without image information" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
IngressHosts : [ ] string { } ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
}
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
2021-10-08 14:58:32 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2022-03-08 15:52:43 +02:00
assert . EqualError ( t , err , "failed to process deployment values: image information not given - please either set image or containerImageName and containerImageTag" )
2021-10-08 14:58:32 +02:00
} )
2020-04-24 09:37:11 +02:00
t . Run ( "test helm v3" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
2022-01-11 12:30:40 +02:00
ContainerRegistryPassword : "dummy" ,
2020-08-14 17:16:25 +02:00
ContainerRegistrySecret : "testSecret" ,
2020-04-24 09:37:11 +02:00
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
2021-01-07 17:52:16 +02:00
ForceUpdates : true ,
2020-04-24 09:37:11 +02:00
HelmDeployWaitSeconds : 400 ,
2020-08-18 07:32:36 +02:00
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
2020-04-24 09:37:11 +02:00
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
2022-01-11 12:30:40 +02:00
DockerConfigJSON : ".pipeline/docker/config.json" ,
2020-04-24 09:37:11 +02:00
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
2022-01-11 12:30:40 +02:00
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
2020-04-24 09:37:11 +02:00
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
telemetryData := & telemetry . CustomData { }
runKubernetesDeploy ( opts , telemetryData , mockUtils , & stdout )
2020-04-24 09:37:11 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong secret creation command" )
2022-01-11 12:30:40 +02:00
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
2020-04-24 09:37:11 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 1 ] . Exec , "Wrong upgrade command" )
2020-04-24 09:37:11 +02:00
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
2020-08-18 07:32:36 +02:00
"--values" ,
"values1.yaml" ,
"--values" ,
"values2.yaml" ,
2020-04-24 09:37:11 +02:00
"--install" ,
"--namespace" ,
"deploymentNamespace" ,
2020-01-24 15:30:27 +02:00
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret" ,
2021-01-07 17:52:16 +02:00
"--force" ,
2020-11-10 16:43:19 +02:00
"--wait" ,
"--timeout" ,
"400s" ,
2020-08-14 17:16:25 +02:00
"--atomic" ,
2020-11-10 16:43:19 +02:00
"--kube-context" ,
"testCluster" ,
"--testParam" ,
"testValue" ,
2021-12-02 13:18:21 +02:00
} , mockUtils . Calls [ 1 ] . Params , "Wrong upgrade parameters" )
2022-01-28 09:31:34 +02:00
assert . Equal ( t , & telemetry . CustomData {
Custom1Label : "deployTool" ,
Custom1 : "helm3" ,
} , telemetryData )
2020-11-10 16:43:19 +02:00
} )
2022-01-25 11:42:42 +02:00
t . Run ( "test helm v3 - runs helm tests" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
ContainerRegistryPassword : "dummy" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DockerConfigJSON : ".pipeline/docker/config.json" ,
RunHelmTests : true ,
2023-04-24 14:58:24 +02:00
HelmTestWaitSeconds : 400 ,
2022-01-25 11:42:42 +02:00
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2022-01-25 11:42:42 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong secret creation command" )
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
assert . Equal ( t , "helm" , mockUtils . Calls [ 1 ] . Exec , "Wrong upgrade command" )
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
"--values" ,
"values1.yaml" ,
"--values" ,
"values2.yaml" ,
"--install" ,
"--namespace" ,
"deploymentNamespace" ,
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret" ,
2022-01-25 11:42:42 +02:00
"--force" ,
"--wait" ,
"--timeout" ,
"400s" ,
"--atomic" ,
"--kube-context" ,
"testCluster" ,
"--testParam" ,
"testValue" ,
} , mockUtils . Calls [ 1 ] . Params , "Wrong upgrade parameters" )
assert . Equal ( t , "helm" , mockUtils . Calls [ 2 ] . Exec , "Wrong test command" )
assert . Equal ( t , [ ] string {
"test" ,
"deploymentName" ,
"--namespace" ,
"deploymentNamespace" ,
2023-05-25 13:03:33 +02:00
"--kube-context" ,
"testCluster" ,
2023-04-24 14:58:24 +02:00
"--timeout" ,
"400s" ,
2022-01-25 11:42:42 +02:00
} , mockUtils . Calls [ 2 ] . Params , "Wrong test parameters" )
} )
t . Run ( "test helm v3 - runs helm tests with logs" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
ContainerRegistryPassword : "dummy" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DockerConfigJSON : ".pipeline/docker/config.json" ,
RunHelmTests : true ,
ShowTestLogs : true ,
2023-04-24 14:58:24 +02:00
HelmTestWaitSeconds : 400 ,
2022-01-25 11:42:42 +02:00
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2022-01-25 11:42:42 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong secret creation command" )
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
assert . Equal ( t , "helm" , mockUtils . Calls [ 1 ] . Exec , "Wrong upgrade command" )
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
"--values" ,
"values1.yaml" ,
"--values" ,
"values2.yaml" ,
"--install" ,
"--namespace" ,
"deploymentNamespace" ,
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret" ,
2022-01-25 11:42:42 +02:00
"--force" ,
"--wait" ,
"--timeout" ,
"400s" ,
"--atomic" ,
"--kube-context" ,
"testCluster" ,
"--testParam" ,
"testValue" ,
} , mockUtils . Calls [ 1 ] . Params , "Wrong upgrade parameters" )
assert . Equal ( t , "helm" , mockUtils . Calls [ 2 ] . Exec , "Wrong test command" )
assert . Equal ( t , [ ] string {
"test" ,
"deploymentName" ,
"--namespace" ,
"deploymentNamespace" ,
2023-05-25 13:03:33 +02:00
"--kube-context" ,
"testCluster" ,
2023-04-24 14:58:24 +02:00
"--timeout" ,
"400s" ,
2022-01-25 11:42:42 +02:00
"--logs" ,
} , mockUtils . Calls [ 2 ] . Params , "Wrong test parameters" )
} )
t . Run ( "test helm v3 - should not run helm tests" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
ContainerRegistryPassword : "dummy" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DockerConfigJSON : ".pipeline/docker/config.json" ,
RunHelmTests : false ,
ShowTestLogs : true ,
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2022-01-25 11:42:42 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong secret creation command" )
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
assert . Equal ( t , "helm" , mockUtils . Calls [ 1 ] . Exec , "Wrong upgrade command" )
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
"--values" ,
"values1.yaml" ,
"--values" ,
"values2.yaml" ,
"--install" ,
"--namespace" ,
"deploymentNamespace" ,
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret" ,
2022-01-25 11:42:42 +02:00
"--force" ,
"--wait" ,
"--timeout" ,
"400s" ,
"--atomic" ,
"--kube-context" ,
"testCluster" ,
"--testParam" ,
"testValue" ,
} , mockUtils . Calls [ 1 ] . Params , "Wrong upgrade parameters" )
assert . Equal ( t , 2 , len ( mockUtils . Calls ) , "Too many helm calls" )
} )
2021-10-08 14:58:32 +02:00
t . Run ( "test helm v3 - with containerImageName and containerImageTag instead of image" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
2022-01-11 12:30:40 +02:00
ContainerRegistryPassword : "dummy" ,
2021-10-08 14:58:32 +02:00
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
ContainerImageName : "path/to/Image" ,
ContainerImageTag : "latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
2022-01-11 12:30:40 +02:00
DockerConfigJSON : ".pipeline/docker/config.json" ,
2021-10-08 14:58:32 +02:00
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
2022-01-11 12:30:40 +02:00
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
2021-10-08 14:58:32 +02:00
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-10-08 14:58:32 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong secret creation command" )
2022-01-11 12:30:40 +02:00
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
2021-10-08 14:58:32 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 1 ] . Exec , "Wrong upgrade command" )
2021-10-08 14:58:32 +02:00
2022-02-24 13:01:36 +02:00
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params , "image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret" , "Wrong upgrade parameters" )
2021-10-08 14:58:32 +02:00
} )
2022-02-24 13:01:36 +02:00
t . Run ( "test helm v3 - with multiple images" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
ContainerRegistryPassword : "dummy" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
ImageNames : [ ] string { "myImage" , "myImage.sub1" , "myImage.sub2" } ,
ImageNameTags : [ ] string { "myImage:myTag" , "myImage-sub1:myTag" , "myImage-sub2:myTag" } ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DockerConfigJSON : ".pipeline/docker/config.json" ,
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
}
var stdout bytes . Buffer
require . NoError ( t , runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout ) )
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong secret creation command" )
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
assert . Equal ( t , "helm" , mockUtils . Calls [ 1 ] . Exec , "Wrong upgrade command" )
2022-03-08 15:52:43 +02:00
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params , ` image.myImage.repository=my.registry:55555/myImage,image.myImage.tag=myTag,image.myImage_sub1.repository=my.registry:55555/myImage-sub1,image.myImage_sub1.tag=myTag,image.myImage_sub2.repository=my.registry:55555/myImage-sub2,image.myImage_sub2.tag=myTag,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret ` , "Wrong upgrade parameters" )
2022-02-24 13:01:36 +02:00
} )
t . Run ( "test helm v3 - with one image in multiple images array" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
ContainerRegistryPassword : "dummy" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
ImageNames : [ ] string { "myImage" } ,
ImageNameTags : [ ] string { "myImage:myTag" } ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DockerConfigJSON : ".pipeline/docker/config.json" ,
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
}
var stdout bytes . Buffer
require . NoError ( t , runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout ) )
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong secret creation command" )
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
assert . Equal ( t , "helm" , mockUtils . Calls [ 1 ] . Exec , "Wrong upgrade command" )
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params , ` image.myImage.repository=my.registry:55555/myImage,image.myImage.tag=myTag,image.repository=my.registry:55555/myImage,image.tag=myTag,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret ` , "Wrong upgrade parameters" )
} )
t . Run ( "test helm v3 - with multiple images - missing ImageNameTags" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
ContainerRegistryPassword : "dummy" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
ImageNames : [ ] string { "myImage" , "myImage.sub1" , "myImage.sub2" } ,
ImageNameTags : [ ] string { "myImage:myTag" } ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DockerConfigJSON : ".pipeline/docker/config.json" ,
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
}
var stdout bytes . Buffer
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2022-03-08 15:52:43 +02:00
assert . EqualError ( t , err , "failed to process deployment values: number of imageNames and imageNameTags must be equal" )
2022-02-24 13:01:36 +02:00
} )
2022-02-28 11:43:55 +02:00
t . Run ( "test helm v3 - with multiple images and valuesMapping" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
ContainerRegistryPassword : "dummy" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
ValuesMapping : map [ string ] interface { } {
"subchart.image.registry" : "image.myImage.repository" ,
"subchart.image.tag" : "image.myImage.tag" ,
} ,
ImageNames : [ ] string { "myImage" , "myImage.sub1" , "myImage.sub2" } ,
ImageNameTags : [ ] string { "myImage:myTag" , "myImage-sub1:myTag" , "myImage-sub2:myTag" } ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DockerConfigJSON : ".pipeline/docker/config.json" ,
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
}
var stdout bytes . Buffer
require . NoError ( t , runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout ) )
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong secret creation command" )
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
assert . Equal ( t , "helm" , mockUtils . Calls [ 1 ] . Exec , "Wrong upgrade command" )
2022-03-07 15:16:22 +02:00
assert . Equal ( t , len ( mockUtils . Calls [ 1 ] . Params ) , 21 , "Unexpected upgrade command" )
pos := 11
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params [ pos ] , "image.myImage.repository=my.registry:55555/myImage" , "Missing update parameter" )
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params [ pos ] , "image.myImage.tag=myTag" , "Wrong upgrade parameters" )
2022-03-08 15:52:43 +02:00
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params [ pos ] , "image.myImage_sub1.repository=my.registry:55555/myImage-sub1" , "Missing update parameter" )
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params [ pos ] , "image.myImage_sub1.tag=myTag" , "Missing update parameter" )
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params [ pos ] , "image.myImage_sub2.repository=my.registry:55555/myImage-sub2" , "Missing update parameter" )
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params [ pos ] , "image.myImage_sub2.tag=myTag,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==" , "Missing update parameter" )
2022-03-07 15:16:22 +02:00
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params [ pos ] , "imagePullSecrets[0].name=testSecret" , "Missing update parameter" )
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params [ pos ] , "subchart.image.registry=my.registry:55555/myImage" , "Missing update parameter" )
assert . Contains ( t , mockUtils . Calls [ 1 ] . Params [ pos ] , "subchart.image.tag=myTag" , "Missing update parameter" )
2022-02-28 11:43:55 +02:00
} )
t . Run ( "test helm v3 - with multiple images and incorrect valuesMapping" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
ContainerRegistryPassword : "dummy" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
ValuesMapping : map [ string ] interface { } {
"subchart.image.registry" : false ,
} ,
ImageNames : [ ] string { "myImage" , "myImage.sub1" , "myImage.sub2" } ,
ImageNameTags : [ ] string { "myImage:myTag" , "myImage-sub1:myTag" , "myImage-sub2:myTag" } ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DockerConfigJSON : ".pipeline/docker/config.json" ,
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
}
var stdout bytes . Buffer
require . Error ( t , runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout ) , "invalid path 'false' is used for valueMapping, only strings are supported" )
} )
2021-10-08 14:58:32 +02:00
t . Run ( "test helm3 - fails without image information" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
IngressHosts : [ ] string { } ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
}
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
2021-10-08 14:58:32 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2022-03-08 15:52:43 +02:00
assert . EqualError ( t , err , "failed to process deployment values: image information not given - please either set image or containerImageName and containerImageTag" )
2021-10-08 14:58:32 +02:00
} )
2020-11-10 16:43:19 +02:00
t . Run ( "test helm v3 - keep failed deployments" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
2022-01-11 12:30:40 +02:00
ContainerRegistryPassword : "dummy" ,
2020-11-10 16:43:19 +02:00
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
2021-01-07 17:52:16 +02:00
ForceUpdates : true ,
2020-11-10 16:43:19 +02:00
HelmDeployWaitSeconds : 400 ,
HelmValues : [ ] string { "values1.yaml" , "values2.yaml" } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
KeepFailedDeployments : true ,
2022-01-11 12:30:40 +02:00
DockerConfigJSON : ".pipeline/docker/config.json" ,
2020-11-10 16:43:19 +02:00
}
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . StdoutReturn = map [ string ] string {
2022-01-11 12:30:40 +02:00
` kubectl create secret generic testSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json ` : dockerConfigJSON ,
2020-11-10 16:43:19 +02:00
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2020-11-10 16:43:19 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong secret creation command" )
2022-01-11 12:30:40 +02:00
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
"testSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
"--insecure-skip-tls-verify=true" ,
"--dry-run=client" ,
"--output=json" } ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
2020-11-10 16:43:19 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "helm" , mockUtils . Calls [ 1 ] . Exec , "Wrong upgrade command" )
2020-11-10 16:43:19 +02:00
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
"--values" ,
"values1.yaml" ,
"--values" ,
"values2.yaml" ,
"--install" ,
"--namespace" ,
"deploymentNamespace" ,
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,secret.name=testSecret,secret.dockerconfigjson=ThisIsOurBase64EncodedSecret==,imagePullSecrets[0].name=testSecret" ,
2021-01-07 17:52:16 +02:00
"--force" ,
2020-11-10 16:43:19 +02:00
"--wait" ,
2020-08-14 17:16:25 +02:00
"--timeout" ,
"400s" ,
"--kube-context" ,
"testCluster" ,
"--testParam" ,
"testValue" ,
2021-12-02 13:18:21 +02:00
} , mockUtils . Calls [ 1 ] . Params , "Wrong upgrade parameters" )
2020-08-14 17:16:25 +02:00
} )
t . Run ( "test helm v3 - no container credentials" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ChartPath : "path/to/chart" ,
ContainerRegistrySecret : "testSecret" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
2021-01-07 17:52:16 +02:00
ForceUpdates : true ,
2020-08-14 17:16:25 +02:00
HelmDeployWaitSeconds : 400 ,
2020-08-18 07:32:36 +02:00
IngressHosts : [ ] string { } ,
2020-08-14 17:16:25 +02:00
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
}
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
2020-08-14 17:16:25 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2020-08-14 17:16:25 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , 1 , len ( mockUtils . Calls ) , "Wrong number of upgrade commands" )
assert . Equal ( t , "helm" , mockUtils . Calls [ 0 ] . Exec , "Wrong upgrade command" )
2020-08-14 17:16:25 +02:00
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
"--install" ,
2021-01-07 17:52:16 +02:00
"--namespace" ,
"deploymentNamespace" ,
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,imagePullSecrets[0].name=testSecret" ,
2020-08-14 17:16:25 +02:00
"--force" ,
2021-01-07 17:52:16 +02:00
"--wait" ,
"--timeout" ,
"400s" ,
"--atomic" ,
"--kube-context" ,
"testCluster" ,
"--testParam" ,
"testValue" ,
2021-12-02 13:18:21 +02:00
} , mockUtils . Calls [ 0 ] . Params , "Wrong upgrade parameters" )
2021-01-07 17:52:16 +02:00
} )
2022-06-29 12:00:37 +02:00
t . Run ( "test helm - use extensions" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ChartPath : "path/to/chart" ,
ContainerRegistrySecret : "testSecret" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
IngressHosts : [ ] string { } ,
Image : "path/to/Image:latest" ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
GithubToken : "testGHToken" ,
SetupScript : "https://github.com/my/test/setup_script.sh" ,
VerificationScript : "https://github.com/my/test/verification_script.sh" ,
TeardownScript : "https://github.com/my/test/teardown_script.sh" ,
}
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . HttpClientMock = & mock . HttpClientMock { HTTPFileUtils : mockUtils . FilesMock }
var stdout bytes . Buffer
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
assert . Equal ( t , 4 , len ( mockUtils . Calls ) )
assert . Equal ( t , ".pipeline/setup_script.sh" , mockUtils . Calls [ 0 ] . Exec )
assert . Equal ( t , ".pipeline/verification_script.sh" , mockUtils . Calls [ 2 ] . Exec )
assert . Equal ( t , ".pipeline/teardown_script.sh" , mockUtils . Calls [ 3 ] . Exec )
} )
2021-03-25 13:26:44 +02:00
t . Run ( "test helm v3 - fails without chart path" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "testSecret" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
IngressHosts : [ ] string { } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
}
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
2021-03-25 13:26:44 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-03-25 13:26:44 +02:00
assert . EqualError ( t , err , "chart path has not been set, please configure chartPath parameter" )
} )
t . Run ( "test helm v3 - fails without deployment name" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "testSecret" ,
ChartPath : "path/to/chart" ,
DeployTool : "helm3" ,
ForceUpdates : true ,
HelmDeployWaitSeconds : 400 ,
IngressHosts : [ ] string { } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
}
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
2021-03-25 13:26:44 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-03-25 13:26:44 +02:00
assert . EqualError ( t , err , "deployment name has not been set, please configure deploymentName parameter" )
} )
2021-01-07 17:52:16 +02:00
t . Run ( "test helm v3 - no force" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
ContainerRegistryURL : "https://my.registry:55555" ,
ChartPath : "path/to/chart" ,
ContainerRegistrySecret : "testSecret" ,
DeploymentName : "deploymentName" ,
DeployTool : "helm3" ,
HelmDeployWaitSeconds : 400 ,
IngressHosts : [ ] string { } ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
}
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
2021-01-07 17:52:16 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-01-07 17:52:16 +02:00
assert . Equal ( t , [ ] string {
"upgrade" ,
"deploymentName" ,
"path/to/chart" ,
"--install" ,
2020-08-14 17:16:25 +02:00
"--namespace" ,
"deploymentNamespace" ,
"--set" ,
2022-02-24 13:01:36 +02:00
"image.repository=my.registry:55555/path/to/Image,image.tag=latest,image.path/to/Image.repository=my.registry:55555/path/to/Image,image.path/to/Image.tag=latest,imagePullSecrets[0].name=testSecret" ,
2020-11-10 16:43:19 +02:00
"--wait" ,
2020-04-24 09:37:11 +02:00
"--timeout" ,
"400s" ,
2020-11-10 16:43:19 +02:00
"--atomic" ,
2020-01-24 15:30:27 +02:00
"--kube-context" ,
"testCluster" ,
"--testParam" ,
"testValue" ,
2021-12-02 13:18:21 +02:00
} , mockUtils . Calls [ 0 ] . Params , "Wrong upgrade parameters" )
2020-01-24 15:30:27 +02:00
} )
2021-06-11 10:41:03 +02:00
t . Run ( "test kubectl - create secret from docker config.json" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
2021-12-02 13:18:21 +02:00
AppTemplate : "path/to/test.yaml" ,
2021-06-11 10:41:03 +02:00
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistryUser : "registryUser" ,
2022-01-11 12:30:40 +02:00
ContainerRegistryPassword : "dummy" ,
2021-06-11 10:41:03 +02:00
ContainerRegistrySecret : "regSecret" ,
CreateDockerRegistrySecret : true ,
DeployTool : "kubectl" ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeConfig : "This is my kubeconfig" ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
2021-11-04 15:11:31 +02:00
DeployCommand : "apply" ,
2022-01-11 12:30:40 +02:00
DockerConfigJSON : ".pipeline/docker/config.json" ,
2021-06-11 10:41:03 +02:00
}
kubeYaml := ` kind : Deployment
2022-01-11 12:30:40 +02:00
metadata :
spec :
spec :
image : < image - name > `
dockerConfigJSON := ` { "kind": "Secret","data": { ".dockerconfigjson": "ThisIsOurBase64EncodedSecret=="}} `
2021-06-11 10:41:03 +02:00
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( opts . AppTemplate , [ ] byte ( kubeYaml ) )
2022-01-11 12:30:40 +02:00
mockUtils . StdoutReturn = map [ string ] string {
` kubectl create secret generic regSecret --from-file=.dockerconfigjson=.pipeline/docker/config.json --type=kubernetes.io/dockerconfigjson --insecure-skip-tls-verify=true --dry-run=client --output=json --insecure-skip-tls-verify=true --namespace=deploymentNamespace --context=testCluster ` : dockerConfigJSON ,
2021-06-11 10:41:03 +02:00
}
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-06-11 10:41:03 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , mockUtils . Env , [ ] string { "KUBECONFIG=This is my kubeconfig" } )
2021-06-11 10:41:03 +02:00
assert . Equal ( t , [ ] string {
"create" ,
"secret" ,
"generic" ,
2022-01-11 12:30:40 +02:00
"regSecret" ,
"--from-file=.dockerconfigjson=.pipeline/docker/config.json" ,
"--type=kubernetes.io/dockerconfigjson" ,
2020-01-24 15:30:27 +02:00
"--insecure-skip-tls-verify=true" ,
2022-01-11 12:30:40 +02:00
"--dry-run=client" ,
"--output=json" ,
2020-01-24 15:30:27 +02:00
"--insecure-skip-tls-verify=true" ,
2022-01-11 12:30:40 +02:00
"--namespace=deploymentNamespace" ,
"--context=testCluster" ,
} ,
mockUtils . Calls [ 0 ] . Params , "Wrong secret creation parameters" )
assert . Containsf ( t , mockUtils . Calls [ 1 ] . Params , "apply" , "Wrong secret creation parameters" )
assert . Containsf ( t , mockUtils . Calls [ 1 ] . Params , "-f" , "Wrong secret creation parameters" )
2020-01-24 15:30:27 +02:00
} )
t . Run ( "test kubectl - token only" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
2022-01-11 12:30:40 +02:00
APIServer : "https://my.api.server" ,
AppTemplate : "path/to/test.yaml" ,
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "regSecret" ,
DeployTool : "kubectl" ,
Image : "path/to/Image:latest" ,
KubeToken : "testToken" ,
Namespace : "deploymentNamespace" ,
DeployCommand : "apply" ,
2020-01-24 15:30:27 +02:00
}
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( opts . AppTemplate , [ ] byte ( "testYaml" ) )
mockUtils . ShouldFailOnCommand = map [ string ] error { }
2020-01-24 15:30:27 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2020-01-24 15:30:27 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong apply command" )
2020-01-24 15:30:27 +02:00
assert . Equal ( t , [ ] string {
"--insecure-skip-tls-verify=true" ,
fmt . Sprintf ( "--namespace=%v" , opts . Namespace ) ,
fmt . Sprintf ( "--server=%v" , opts . APIServer ) ,
fmt . Sprintf ( "--token=%v" , opts . KubeToken ) ,
"apply" ,
"--filename" ,
opts . AppTemplate ,
2021-12-02 13:18:21 +02:00
} , mockUtils . Calls [ 0 ] . Params , "kubectl parameters incorrect" )
2020-01-24 15:30:27 +02:00
} )
2021-10-08 14:58:32 +02:00
t . Run ( "test kubectl - with containerImageName and containerImageTag instead of image" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
2022-01-11 12:30:40 +02:00
APIServer : "https://my.api.server" ,
AppTemplate : "test.yaml" ,
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "regSecret" ,
DeployTool : "kubectl" ,
ContainerImageTag : "latest" ,
ContainerImageName : "path/to/Image" ,
KubeConfig : "This is my kubeconfig" ,
Namespace : "deploymentNamespace" ,
DeployCommand : "apply" ,
2021-10-08 14:58:32 +02:00
}
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( "test.yaml" , [ ] byte ( "image: <image-name>" ) )
2021-10-08 14:58:32 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-10-08 14:58:32 +02:00
2021-12-02 13:18:21 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong apply command" )
2021-10-08 14:58:32 +02:00
2021-12-02 13:18:21 +02:00
appTemplateFileContents , err := mockUtils . FileRead ( opts . AppTemplate )
assert . NoError ( t , err )
2021-10-08 14:58:32 +02:00
assert . Contains ( t , string ( appTemplateFileContents ) , "image: my.registry:55555/path/to/Image:latest" , "kubectl parameters incorrect" )
} )
2022-03-08 15:52:43 +02:00
t . Run ( "test kubectl - with containerImageName and containerImageTag instead of image using go template" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
APIServer : "https://my.api.server" ,
AppTemplate : "test.yaml" ,
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "regSecret" ,
DeployTool : "kubectl" ,
ContainerImageTag : "latest" ,
ContainerImageName : "path/to/Image" ,
KubeConfig : "This is my kubeconfig" ,
Namespace : "deploymentNamespace" ,
DeployCommand : "apply" ,
}
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( "test.yaml" , [ ] byte ( "image: {{ .Values.image.repository }}:{{ .Values.image.tag }}" ) )
var stdout bytes . Buffer
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong apply command" )
appTemplateFileContents , err := mockUtils . FileRead ( opts . AppTemplate )
assert . NoError ( t , err )
assert . Contains ( t , string ( appTemplateFileContents ) , "image: my.registry:55555/path/to/Image:latest" , "kubectl parameters incorrect" )
} )
t . Run ( "test kubectl - with multiple images using go template" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
APIServer : "https://my.api.server" ,
AppTemplate : "test.yaml" ,
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "regSecret" ,
DeployTool : "kubectl" ,
KubeConfig : "This is my kubeconfig" ,
Namespace : "deploymentNamespace" ,
DeployCommand : "apply" ,
ValuesMapping : map [ string ] interface { } {
"subchart.image.repository" : "image.myImage.repository" ,
"subchart.image.tag" : "image.myImage.tag" ,
} ,
ImageNames : [ ] string { "myImage" , "myImage-sub1" , "myImage-sub2" } ,
ImageNameTags : [ ] string { "myImage:myTag" , "myImage-sub1:myTag" , "myImage-sub2:myTag" } ,
}
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( "test.yaml" , [ ] byte ( ` image : { { . Values . image . myImage . repository } } : { { . Values . image . myImage . tag } }
image2 : { { . Values . subchart . image . repository } } : { { . Values . subchart . image . tag } }
image3 : { { . Values . image . myImage_sub1 . repository } } : { { . Values . image . myImage_sub1 . tag } } ` ) )
var stdout bytes . Buffer
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong apply command" )
appTemplateFileContents , err := mockUtils . FileRead ( opts . AppTemplate )
assert . NoError ( t , err )
assert . Contains ( t , string ( appTemplateFileContents ) , "image: my.registry:55555/myImage:myTag\nimage2: my.registry:55555/myImage:myTag\nimage3: my.registry:55555/myImage-sub1:myTag" , "kubectl parameters incorrect" )
} )
2022-03-09 10:48:19 +02:00
t . Run ( "test kubectl - with multiple images and digests" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
APIServer : "https://my.api.server" ,
AppTemplate : "test.yaml" ,
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "regSecret" ,
DeployTool : "kubectl" ,
KubeConfig : "This is my kubeconfig" ,
Namespace : "deploymentNamespace" ,
DeployCommand : "apply" ,
ValuesMapping : map [ string ] interface { } {
"subchart.image.repository" : "image.myImage.repository" ,
"subchart.image.tag" : "image.myImage.tag" ,
} ,
ImageNames : [ ] string { "myImage" , "myImage-sub1" , "myImage-sub2" } ,
ImageNameTags : [ ] string { "myImage:myTag" , "myImage-sub1:myTag" , "myImage-sub2:myTag" } ,
ImageDigests : [ ] string { "sha256:111" , "sha256:222" , "sha256:333" } ,
}
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( "test.yaml" , [ ] byte ( ` image : { { . Values . image . myImage . repository } } : { { . Values . image . myImage . tag } }
image2 : { { . Values . subchart . image . repository } } : { { . Values . subchart . image . tag } }
image3 : { { . Values . image . myImage_sub1 . repository } } : { { . Values . image . myImage_sub1 . tag } }
image4 : { { . Values . image . myImage_sub2 . repository } } : { { . Values . image . myImage_sub2 . tag } } ` ) )
var stdout bytes . Buffer
runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong apply command" )
appTemplateFileContents , err := mockUtils . FileRead ( opts . AppTemplate )
assert . NoError ( t , err )
assert . Contains ( t , string ( appTemplateFileContents ) , ` image : my . registry : 55555 / myImage : myTag @ sha256 : 111
image2 : my . registry : 55555 / myImage : myTag @ sha256 : 111
image3 : my . registry : 55555 / myImage - sub1 : myTag @ sha256 : 222
image4 : my . registry : 55555 / myImage - sub2 : myTag @ sha256 : 333 ` , "kubectl parameters incorrect" )
} )
2022-03-08 15:52:43 +02:00
t . Run ( "test kubectl - fail with multiple images using placeholder" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
APIServer : "https://my.api.server" ,
AppTemplate : "test.yaml" ,
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "regSecret" ,
DeployTool : "kubectl" ,
KubeConfig : "This is my kubeconfig" ,
Namespace : "deploymentNamespace" ,
DeployCommand : "apply" ,
ImageNames : [ ] string { "myImage" , "myImage-sub1" , "myImage-sub2" } ,
ImageNameTags : [ ] string { "myImage:myTag" , "myImage-sub1:myTag" , "myImage-sub2:myTag" } ,
}
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( "test.yaml" , [ ] byte ( "image: <image-name>" ) )
var stdout bytes . Buffer
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
assert . EqualError ( t , err , "multi-image replacement not supported for single image placeholder" )
} )
2021-10-08 14:58:32 +02:00
t . Run ( "test kubectl - fails without image information" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
2022-01-11 12:30:40 +02:00
APIServer : "https://my.api.server" ,
AppTemplate : "test.yaml" ,
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "regSecret" ,
DeployTool : "kubectl" ,
KubeConfig : "This is my kubeconfig" ,
Namespace : "deploymentNamespace" ,
DeployCommand : "apply" ,
2021-10-08 14:58:32 +02:00
}
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( "test.yaml" , [ ] byte ( "testYaml" ) )
2021-10-08 14:58:32 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2022-03-08 15:52:43 +02:00
assert . EqualError ( t , err , "failed to process deployment values: image information not given - please either set image or containerImageName and containerImageTag" )
2021-10-08 14:58:32 +02:00
} )
2021-11-04 15:11:31 +02:00
t . Run ( "test kubectl - use replace deploy command" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
2021-12-02 13:18:21 +02:00
AppTemplate : "test.yaml" ,
2021-11-04 15:11:31 +02:00
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "regSecret" ,
CreateDockerRegistrySecret : true ,
DeployTool : "kubectl" ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeConfig : "This is my kubeconfig" ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DeployCommand : "replace" ,
}
kubeYaml := ` kind : Deployment
2022-01-11 12:30:40 +02:00
metadata :
spec :
spec :
image : < image - name > `
2021-11-04 15:11:31 +02:00
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( "test.yaml" , [ ] byte ( kubeYaml ) )
2021-11-04 15:11:31 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-11-04 15:11:31 +02:00
assert . NoError ( t , err , "Command should not fail" )
2021-12-02 13:18:21 +02:00
assert . Equal ( t , mockUtils . Env , [ ] string { "KUBECONFIG=This is my kubeconfig" } )
2021-11-04 15:11:31 +02:00
2022-01-11 12:30:40 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong replace command" )
2021-11-04 15:11:31 +02:00
assert . Equal ( t , [ ] string {
"--insecure-skip-tls-verify=true" ,
fmt . Sprintf ( "--namespace=%v" , opts . Namespace ) ,
fmt . Sprintf ( "--context=%v" , opts . KubeContext ) ,
"replace" ,
"--filename" ,
opts . AppTemplate ,
"--testParam" ,
"testValue" ,
2022-01-11 12:30:40 +02:00
} , mockUtils . Calls [ 0 ] . Params , "kubectl parameters incorrect" )
2021-11-04 15:11:31 +02:00
2021-12-02 13:18:21 +02:00
appTemplate , err := mockUtils . FileRead ( opts . AppTemplate )
2021-11-04 15:11:31 +02:00
assert . Contains ( t , string ( appTemplate ) , "my.registry:55555/path/to/Image:latest" )
} )
t . Run ( "test kubectl - use replace --force deploy command" , func ( t * testing . T ) {
opts := kubernetesDeployOptions {
2021-12-02 13:18:21 +02:00
AppTemplate : "test.yaml" ,
2021-11-04 15:11:31 +02:00
ContainerRegistryURL : "https://my.registry:55555" ,
ContainerRegistrySecret : "regSecret" ,
CreateDockerRegistrySecret : true ,
DeployTool : "kubectl" ,
Image : "path/to/Image:latest" ,
AdditionalParameters : [ ] string { "--testParam" , "testValue" } ,
KubeConfig : "This is my kubeconfig" ,
KubeContext : "testCluster" ,
Namespace : "deploymentNamespace" ,
DeployCommand : "replace" ,
ForceUpdates : true ,
}
kubeYaml := ` kind : Deployment
2022-01-11 12:30:40 +02:00
metadata :
spec :
spec :
image : < image - name > `
2021-11-04 15:11:31 +02:00
2021-12-02 13:18:21 +02:00
mockUtils := newKubernetesDeployMockUtils ( )
mockUtils . AddFile ( "test.yaml" , [ ] byte ( kubeYaml ) )
2021-11-04 15:11:31 +02:00
var stdout bytes . Buffer
2022-01-28 09:31:34 +02:00
err := runKubernetesDeploy ( opts , & telemetry . CustomData { } , mockUtils , & stdout )
2021-11-04 15:11:31 +02:00
assert . NoError ( t , err , "Command should not fail" )
2021-12-02 13:18:21 +02:00
assert . Equal ( t , mockUtils . Env , [ ] string { "KUBECONFIG=This is my kubeconfig" } )
2021-11-04 15:11:31 +02:00
2022-01-11 12:30:40 +02:00
assert . Equal ( t , "kubectl" , mockUtils . Calls [ 0 ] . Exec , "Wrong replace command" )
2021-11-04 15:11:31 +02:00
assert . Equal ( t , [ ] string {
"--insecure-skip-tls-verify=true" ,
fmt . Sprintf ( "--namespace=%v" , opts . Namespace ) ,
fmt . Sprintf ( "--context=%v" , opts . KubeContext ) ,
"replace" ,
"--filename" ,
opts . AppTemplate ,
"--force" ,
"--testParam" ,
"testValue" ,
2022-01-11 12:30:40 +02:00
} , mockUtils . Calls [ 0 ] . Params , "kubectl parameters incorrect" )
2021-11-04 15:11:31 +02:00
2021-12-02 13:18:21 +02:00
appTemplate , err := mockUtils . FileRead ( opts . AppTemplate )
2021-11-04 15:11:31 +02:00
assert . Contains ( t , string ( appTemplate ) , "my.registry:55555/path/to/Image:latest" )
} )
2020-01-24 15:30:27 +02:00
}
func TestSplitRegistryURL ( t * testing . T ) {
tt := [ ] struct {
in string
outProtocol string
outRegistry string
outError error
} {
{ in : "https://my.registry.com" , outProtocol : "https" , outRegistry : "my.registry.com" , outError : nil } ,
{ in : "https://" , outProtocol : "" , outRegistry : "" , outError : fmt . Errorf ( "Failed to split registry url 'https://'" ) } ,
{ in : "my.registry.com" , outProtocol : "" , outRegistry : "" , outError : fmt . Errorf ( "Failed to split registry url 'my.registry.com'" ) } ,
{ in : "" , outProtocol : "" , outRegistry : "" , outError : fmt . Errorf ( "Failed to split registry url ''" ) } ,
{ in : "https://https://my.registry.com" , outProtocol : "" , outRegistry : "" , outError : fmt . Errorf ( "Failed to split registry url 'https://https://my.registry.com'" ) } ,
}
for _ , test := range tt {
p , r , err := splitRegistryURL ( test . in )
assert . Equal ( t , test . outProtocol , p , "Protocol value unexpected" )
assert . Equal ( t , test . outRegistry , r , "Registry value unexpected" )
assert . Equal ( t , test . outError , err , "Error value not as expected" )
}
}
func TestSplitImageName ( t * testing . T ) {
tt := [ ] struct {
in string
outImage string
outTag string
outError error
} {
{ in : "" , outImage : "" , outTag : "" , outError : fmt . Errorf ( "Failed to split image name ''" ) } ,
{ in : "path/to/image" , outImage : "path/to/image" , outTag : "" , outError : nil } ,
{ in : "path/to/image:tag" , outImage : "path/to/image" , outTag : "tag" , outError : nil } ,
{ in : "https://my.registry.com/path/to/image:tag" , outImage : "" , outTag : "" , outError : fmt . Errorf ( "Failed to split image name 'https://my.registry.com/path/to/image:tag'" ) } ,
}
for _ , test := range tt {
i , tag , err := splitFullImageName ( test . in )
assert . Equal ( t , test . outImage , i , "Image value unexpected" )
assert . Equal ( t , test . outTag , tag , "Tag value unexpected" )
assert . Equal ( t , test . outError , err , "Error value not as expected" )
}
}