don t care

This commit is contained in:
Julien Cabillot 2021-12-29 17:08:12 -05:00
parent 5421846c1f
commit 228eb01db3
13 changed files with 15 additions and 1811 deletions

View File

@ -9,7 +9,7 @@ appVersion: "latest"
description: Helm chart for cv description: Helm chart for cv
name: cv name: cv
version: 0.0.1 version: 0.0.2
kubeVersion: ">=1.16.0-0" kubeVersion: ">=1.16.0-0"
keywords: keywords:
- cv - cv

View File

@ -1,136 +0,0 @@
package common
import (
"testing"
"github.com/Jeffail/gabs/v2"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type AddonCodeserverTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
baseValues string
}
func (suite *AddonCodeserverTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
suite.baseValues = `
addons:
codeserver:
enabled: true
volumeMounts:
- name: "config"
mountPath: "/data/config"
`
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestAddonCodeserver(t *testing.T) {
suite.Run(t, new(AddonCodeserverTestSuite))
}
func (suite *AddonCodeserverTestSuite) TestContainer() {
tests := map[string]struct {
values *string
expectedCodeserverContainer bool
}{
"Default": {values: nil, expectedCodeserverContainer: false},
"AddonEnabled": {values: &suite.baseValues, expectedCodeserverContainer: true},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, nil, tc.values)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
var codeserverContainer *gabs.Container
for _, container := range containers {
containerName := container.Path("name").Data().(string)
if containerName == "codeserver" {
codeserverContainer = container
break
}
}
if tc.expectedCodeserverContainer {
suite.Assertions.NotEmpty(codeserverContainer)
} else {
suite.Assertions.Empty(codeserverContainer)
}
})
}
}
func (suite *AddonCodeserverTestSuite) TestDeployKey() {
tests := map[string]struct {
values []string
expectSecret bool
expectedSecretName string
}{
"Inline": {values: []string{"addons.codeserver.git.deployKey=test"}, expectSecret: true, expectedSecretName: "common-test-deploykey"},
"InlineBase64": {values: []string{"addons.codeserver.git.deployKeyBase64=dGVzdEtleQ=="}, expectSecret: true, expectedSecretName: "common-test-deploykey"},
"ExistingSecret": {values: []string{"addons.codeserver.git.deployKeySecret=test-secret"}, expectSecret: false, expectedSecretName: "test-secret"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, &suite.baseValues)
if err != nil {
suite.FailNow(err.Error())
}
secretManifest := suite.Chart.Manifests.Get("Secret", tc.expectedSecretName)
if tc.expectSecret {
suite.Assertions.NotEmpty(secretManifest)
} else {
suite.Assertions.Empty(secretManifest)
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
var codeserverContainer *gabs.Container
for _, container := range containers {
containerName := container.Path("name").Data().(string)
if containerName == "codeserver" {
codeserverContainer = container
break
}
}
suite.Assertions.NotEmpty(codeserverContainer)
volumeMounts := codeserverContainer.Path("volumeMounts").Children()
var gitDeploykeyVolumeMount *gabs.Container
for _, volumeMount := range volumeMounts {
volumeMountName := volumeMount.Path("name").Data().(string)
if volumeMountName == "deploykey" {
gitDeploykeyVolumeMount = volumeMount
break
}
}
suite.Assertions.NotEmpty(gitDeploykeyVolumeMount)
suite.Assertions.EqualValues("/root/.ssh/id_rsa", gitDeploykeyVolumeMount.Path("mountPath").Data())
suite.Assertions.EqualValues("id_rsa", gitDeploykeyVolumeMount.Path("subPath").Data())
volumes := deploymentManifest.Path("spec.template.spec.volumes").Children()
var gitDeploykeyVolume *gabs.Container
for _, volume := range volumes {
volumeName := volume.Path("name").Data().(string)
if volumeName == "deploykey" {
gitDeploykeyVolume = volume
break
}
}
suite.Assertions.NotEmpty(gitDeploykeyVolume)
})
}
}

View File

@ -1,64 +0,0 @@
package common
import (
"testing"
"github.com/Jeffail/gabs/v2"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type AddonNetshootTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
baseValues []string
}
func (suite *AddonNetshootTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
suite.baseValues = []string{"addons.netshoot.enabled=true"}
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestAddonNetshoot(t *testing.T) {
suite.Run(t, new(AddonNetshootTestSuite))
}
func (suite *AddonNetshootTestSuite) TestContainer() {
tests := map[string]struct {
values []string
expectedNetshootContainer bool
}{
"Default": {values: nil, expectedNetshootContainer: false},
"AddonEnabled": {values: suite.baseValues, expectedNetshootContainer: true},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
var netshootContainer *gabs.Container
for _, container := range containers {
containerName := container.Path("name").Data().(string)
if containerName == "netshoot" {
netshootContainer = container
break
}
}
if tc.expectedNetshootContainer {
suite.Assertions.NotEmpty(netshootContainer)
} else {
suite.Assertions.Empty(netshootContainer)
}
})
}
}

View File

@ -1,129 +0,0 @@
package common
import (
"testing"
"github.com/Jeffail/gabs/v2"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type AddonVpnTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
baseValues []string
}
func (suite *AddonVpnTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
suite.baseValues = []string{"addons.vpn.enabled=true"}
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestAddonVpn(t *testing.T) {
suite.Run(t, new(AddonVpnTestSuite))
}
func (suite *AddonVpnTestSuite) TestContainer() {
tests := map[string]struct {
values []string
expectedVpnContainer bool
}{
"Default": {values: nil, expectedVpnContainer: false},
"AddonEnabled": {values: suite.baseValues, expectedVpnContainer: true},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
var vpnContainer *gabs.Container
for _, container := range containers {
containerName := container.Path("name").Data().(string)
if containerName == "openvpn" {
vpnContainer = container
break
}
}
if tc.expectedVpnContainer {
suite.Assertions.NotEmpty(vpnContainer)
} else {
suite.Assertions.Empty(vpnContainer)
}
})
}
}
func (suite *AddonVpnTestSuite) TestConfiguration() {
tests := map[string]struct {
values []string
expectSecret bool
expectedSecretName string
}{
"InlineConfig": {values: append(suite.baseValues, "addons.vpn.configFile=test"), expectSecret: true, expectedSecretName: "common-test-vpnconfig"},
"ExistingSecret": {values: append(suite.baseValues, "addons.vpn.configFileSecret=test-secret"), expectSecret: false, expectedSecretName: "test-secret"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
secretManifest := suite.Chart.Manifests.Get("Secret", tc.expectedSecretName)
if tc.expectSecret {
suite.Assertions.NotEmpty(secretManifest)
} else {
suite.Assertions.Empty(secretManifest)
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
var vpnContainer *gabs.Container
for _, container := range containers {
containerName := container.Path("name").Data().(string)
if containerName == "openvpn" {
vpnContainer = container
break
}
}
suite.Assertions.NotEmpty(vpnContainer)
volumeMounts := vpnContainer.Path("volumeMounts").Children()
var vpnConfigVolumeMount *gabs.Container
for _, volumeMount := range volumeMounts {
volumeMountName := volumeMount.Path("name").Data().(string)
if volumeMountName == "vpnconfig" {
vpnConfigVolumeMount = volumeMount
break
}
}
suite.Assertions.NotEmpty(vpnConfigVolumeMount)
suite.Assertions.EqualValues("/vpn/vpn.conf", vpnConfigVolumeMount.Path("mountPath").Data())
suite.Assertions.EqualValues("vpnConfigfile", vpnConfigVolumeMount.Path("subPath").Data())
volumes := deploymentManifest.Path("spec.template.spec.volumes").Children()
var vpnConfigVolume *gabs.Container
for _, volume := range volumes {
volumeName := volume.Path("name").Data().(string)
if volumeName == "vpnconfig" {
vpnConfigVolume = volume
break
}
}
suite.Assertions.NotEmpty(vpnConfigVolume)
})
}
}

View File

@ -1,98 +0,0 @@
package common
import (
// "fmt"
"testing"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type ConfigmapTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
}
func (suite *ConfigmapTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestConfigmap(t *testing.T) {
suite.Run(t, new(ConfigmapTestSuite))
}
func (suite *ConfigmapTestSuite) TestValues() {
tests := map[string]struct {
values []string
expectedConfigmap bool
}{
"Default": {
values: nil,
expectedConfigmap: false,
},
"Disabled": {
values: []string{"configmap.config.enabled=false"},
expectedConfigmap: false,
},
"Multiple": {
values: []string{
"configmap.config.enabled=true",
"configmap.config.data.foo=bar",
"configmap.secondary.enabled=true",
},
expectedConfigmap: true,
},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
configmapManifest := suite.Chart.Manifests.Get("ConfigMap", "common-test-config")
if tc.expectedConfigmap {
suite.Assertions.NotEmpty(configmapManifest)
} else {
suite.Assertions.Empty(configmapManifest)
}
})
}
}
func (suite *ConfigmapTestSuite) TestConfigmapName() {
tests := map[string]struct {
values []string
expectedName string
}{
"Default": {
values: []string{
"configmap.config.enabled=true",
"configmap.config.data.foo=bar",
},
expectedName: "common-test-config",
},
"CustomName": {
values: []string{
"configmap.config.enabled=true",
"configmap.config.data.foo=bar",
"configmap.config.nameOverride=http",
},
expectedName: "common-test-http",
},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
configmapManifest := suite.Chart.Manifests.Get("ConfigMap", tc.expectedName)
suite.Assertions.NotEmpty(configmapManifest)
})
}
}

View File

@ -1,304 +0,0 @@
package common
import (
"testing"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type ContainerTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
}
func (suite *ContainerTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestContainer(t *testing.T) {
suite.Run(t, new(ContainerTestSuite))
}
func (suite *ContainerTestSuite) TestCommand() {
tests := map[string]struct {
values []string
expectedCommand []string
}{
"Default": {values: nil, expectedCommand: nil},
"SingleString": {values: []string{"command=/bin/sh"}, expectedCommand: []string{"/bin/sh"}},
"StringList": {values: []string{"command={/bin/sh,-c}"}, expectedCommand: []string{"/bin/sh", "-c"}},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
containerCommand := containers[0].Path("command")
if tc.expectedCommand == nil {
suite.Assertions.Empty(containerCommand)
} else {
var actualDataList []string
actualData := containerCommand.Children()
for _, key := range actualData {
actualDataList = append(actualDataList, key.Data().(string))
}
suite.Assertions.EqualValues(tc.expectedCommand, actualDataList)
}
})
}
}
func (suite *ContainerTestSuite) TestArgs() {
tests := map[string]struct {
values []string
expectedArgs []string
}{
"Default": {values: nil, expectedArgs: nil},
"SingleString": {values: []string{"args=sleep infinity"}, expectedArgs: []string{"sleep infinity"}},
"StringList": {values: []string{"args={sleep,infinity}"}, expectedArgs: []string{"sleep", "infinity"}},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
containerArgs := containers[0].Path("args")
if tc.expectedArgs == nil {
suite.Assertions.Empty(containerArgs)
} else {
var actualDataList []string
actualData := containerArgs.Children()
for _, key := range actualData {
actualDataList = append(actualDataList, key.Data().(string))
}
suite.Assertions.EqualValues(tc.expectedArgs, actualDataList)
}
})
}
}
func (suite *ContainerTestSuite) TestEnv() {
tests := map[string]struct {
values []string
expectedEnv map[string]string
}{
"Default": {values: nil, expectedEnv: nil},
"KeyValueString": {values: []string{"env.string=value_of_env"}, expectedEnv: map[string]string{"string": "value_of_env"}},
"KeyValueFloat": {values: []string{"env.float=4.2"}, expectedEnv: map[string]string{"float": "4.2"}},
"KeyValueBool": {values: []string{"env.bool=false"}, expectedEnv: map[string]string{"bool": "false"}},
"KeyValueInt": {values: []string{"env.int=42"}, expectedEnv: map[string]string{"int": "42"}},
"List": {values: []string{"env[0].name=STATIC_ENV_FROM_LIST", "env[0].value=STATIC_ENV_VALUE_FROM_LIST"}, expectedEnv: map[string]string{"STATIC_ENV_FROM_LIST": "STATIC_ENV_VALUE_FROM_LIST"}},
"ValueFrom": {values: []string{"env[0].name=STATIC_ENV_FROM_LIST", "env[0].valueFrom.fieldRef.fieldPath=spec.nodeName"}, expectedEnv: map[string]string{"STATIC_ENV_FROM_LIST": "spec.nodeName"}},
"KeyValue+ExplicitValueFrom": {values: []string{"env.STATIC_ENV=value_of_env", "env.STATIC_ENV_FROM.valueFrom.fieldRef.fieldPath=spec.nodeName"}, expectedEnv: map[string]string{"STATIC_ENV": "value_of_env", "STATIC_ENV_FROM": "spec.nodeName"}},
"ImplicitValueFrom": {values: []string{"env.NODE_NAME.fieldRef.fieldPath=spec.nodeName"}, expectedEnv: map[string]string{"NODE_NAME": "spec.nodeName"}},
"Templated": {values: []string{`env.DYN_ENV=\{\{ .Release.Name \}\}-admin`}, expectedEnv: map[string]string{"DYN_ENV": "common-test-admin"}},
"Mixed": {
values: []string{
`env.DYN_ENV=\{\{ .Release.Name \}\}-admin`,
"env.STATIC_ENV=value_of_env",
"env.STATIC_EXPLICIT_ENV_FROM.valueFrom.fieldRef.fieldPath=spec.nodeName",
"env.STATIC_IMPLICIT_ENV_FROM.fieldRef.fieldPath=spec.nodeName",
},
expectedEnv: map[string]string{
"DYN_ENV": "common-test-admin",
"STATIC_ENV": "value_of_env",
"STATIC_EXPLICIT_ENV_FROM": "spec.nodeName",
"STATIC_IMPLICIT_ENV_FROM": "spec.nodeName",
},
},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
containerEnv := containers[0].Path("env")
if tc.expectedEnv == nil {
suite.Assertions.Empty(containerEnv)
} else {
actualDataMap := make(map[string]string)
actualData := containerEnv.Children()
for _, value := range actualData {
envVar := value.ChildrenMap()
envName := envVar["name"].Data().(string)
var envValue string
if _, ok := envVar["valueFrom"]; ok {
envValue = value.Path("valueFrom.fieldRef.fieldPath").Data().(string)
} else {
envValue = value.Path("value").Data().(string)
}
actualDataMap[envName] = envValue
}
suite.Assertions.EqualValues(tc.expectedEnv, actualDataMap)
}
})
}
}
func (suite *ContainerTestSuite) TestEnvFrom() {
tests := map[string]struct {
values []string
expectSecret bool
expectedSecretName string
}{
"Default": {values: nil, expectSecret: false, expectedSecretName: ""},
"FromSecret": {values: []string{"secret.STATIC_SECRET=value_of_secret"}, expectSecret: true, expectedSecretName: "common-test"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
secretManifest := suite.Chart.Manifests.Get("Secret", tc.expectedSecretName)
if tc.expectSecret {
suite.Assertions.NotEmpty(secretManifest)
} else {
suite.Assertions.Empty(secretManifest)
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
containerEnvFrom := containers[0].Path("envFrom").Children()
if !tc.expectSecret {
suite.Assertions.Empty(containerEnvFrom)
} else {
suite.Assertions.EqualValues(tc.expectedSecretName, containerEnvFrom[0].Path("secretRef.name").Data().(string))
}
})
}
}
func (suite *ContainerTestSuite) TestPorts() {
tests := map[string]struct {
values []string
expectedPortName string
expectedPort int
expectedProtocol string
}{
"Default": {values: nil, expectedPortName: "http", expectedPort: 0, expectedProtocol: "TCP"},
"CustomName": {values: []string{"service.main.ports.http.enabled=false", "service.main.ports.server.enabled=true", "service.main.ports.server.port=8080"}, expectedPortName: "server", expectedPort: 8080, expectedProtocol: "TCP"},
"ProtocolHTTP": {values: []string{"service.main.ports.http.protocol=HTTP"}, expectedPortName: "http", expectedPort: 0, expectedProtocol: "TCP"},
"ProtocolHTTPS": {values: []string{"service.main.ports.http.protocol=HTTP"}, expectedPortName: "http", expectedPort: 0, expectedProtocol: "TCP"},
"ProtocolUDP": {values: []string{"service.main.ports.http.protocol=UDP"}, expectedPortName: "http", expectedPort: 0, expectedProtocol: "UDP"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
containerPorts := containers[0].Path("ports").Children()
suite.Assertions.NotEmpty(containerPorts[0])
suite.Assertions.EqualValues(tc.expectedPortName, containerPorts[0].Path("name").Data())
suite.Assertions.EqualValues(tc.expectedProtocol, containerPorts[0].Path("protocol").Data())
if tc.expectedPort == 0 {
suite.Assertions.Empty(containerPorts[0].Path("containerPort").Data())
} else {
suite.Assertions.EqualValues(tc.expectedPort, containerPorts[0].Path("containerPort").Data())
}
})
}
}
func (suite *ContainerTestSuite) TestPersistenceVolumeMounts() {
values := `
persistence:
config:
enabled: true
cache:
enabled: true
type: emptyDir
claimWithCustomMountPath:
enabled: true
mountPath: /custom
accessMode: ReadWriteMany
size: 1G
claimWithSubPath:
enabled: true
existingClaim: myClaim
subPath: "mySubPath"
hostpath-data:
enabled: true
type: hostPath
mountPath: /data
hostPath: /tmp
hostpath-dev:
enabled: true
type: hostPath
hostPath: /dev
subPath: mySubPath
`
tests := map[string]struct {
values *string
volumeToTest string
expectedMountPath string
expectedSubPath string
}{
"MountWithoutMountPath": {values: &values, volumeToTest: "config", expectedMountPath: "/config"},
"EmptyDir": {values: &values, volumeToTest: "cache", expectedMountPath: "/cache"},
"MountWithCustomMountPath": {values: &values, volumeToTest: "claimWithCustomMountPath", expectedMountPath: "/custom"},
"MountWithSubPath": {values: &values, volumeToTest: "claimWithSubPath", expectedMountPath: "/claimWithSubPath", expectedSubPath: "mySubPath"},
"HostPathMount": {values: &values, volumeToTest: "hostpath-data", expectedMountPath: "/data"},
"HostPathMountWithSubPath": {values: &values, volumeToTest: "hostpath-dev", expectedMountPath: "/dev", expectedSubPath: "mySubPath"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, nil, tc.values)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
containers := deploymentManifest.Path("spec.template.spec.containers").Children()
containerVolumeMounts := containers[0].Path("volumeMounts").Children()
suite.Assertions.NotEmpty(containerVolumeMounts)
for _, volumeMount := range containerVolumeMounts {
volumeMountName := volumeMount.Path("name").Data().(string)
if volumeMountName == tc.volumeToTest {
suite.Assertions.EqualValues(tc.expectedMountPath, volumeMount.Path("mountPath").Data())
if tc.expectedSubPath == "" {
suite.Assertions.Empty(volumeMount.Path("subPath").Data())
} else {
suite.Assertions.EqualValues(tc.expectedSubPath, volumeMount.Path("subPath").Data())
}
break
}
}
})
}
}

View File

@ -1,59 +0,0 @@
package common
import (
"testing"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type ControllerTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
}
func (suite *ControllerTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestController(t *testing.T) {
suite.Run(t, new(ControllerTestSuite))
}
func (suite *ControllerTestSuite) TestTypes() {
tests := map[string]struct {
values []string
expectedRenderFailure bool
expectedController string
}{
"Default": {values: nil, expectedRenderFailure: false, expectedController: "deployment"},
"DaemonSet": {values: []string{"controller.type=daemonset"}, expectedRenderFailure: false, expectedController: "daemonset"},
"Deployment": {values: []string{"controller.type=deployment"}, expectedRenderFailure: false, expectedController: "deployment"},
"StatefulSet": {values: []string{"controller.type=statefulset"}, expectedRenderFailure: false, expectedController: "statefulset"},
"Custom": {values: []string{"controller.type=custom"}, expectedRenderFailure: true, expectedController: ""},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if tc.expectedRenderFailure {
suite.Assertions.Error(err)
return
}
if err != nil {
suite.FailNow(err.Error())
}
manifest := suite.Chart.Manifests.Get(tc.expectedController, "common-test")
suite.Assertions.NotEmpty(manifest)
types := map[string]interface{}{"deployment": nil, "statefulset": nil, "daemonset": nil}
delete(types, tc.expectedController)
for k := range types {
suite.Assertions.Empty(suite.Chart.Manifests.Get(k, "common-test"))
}
})
}
}

View File

@ -1,107 +0,0 @@
package common
import (
"testing"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type HorizontalPodAutoscalerTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
}
func (suite *HorizontalPodAutoscalerTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestHorizontalPodAutoscaler(t *testing.T) {
suite.Run(t, new(HorizontalPodAutoscalerTestSuite))
}
func (suite *HorizontalPodAutoscalerTestSuite) TestValues() {
baseValues := []string{"autoscaling.enabled=true"}
tests := map[string]struct {
values []string
expectedHorizontalPodAutoscaler bool
expectedTarget string
expectedMinReplicas int
expectedMaxReplicas int
}{
"Default": {
values: nil,
expectedHorizontalPodAutoscaler: false,
},
"Enabled": {
values: baseValues,
expectedHorizontalPodAutoscaler: true, expectedTarget: "common-test", expectedMinReplicas: 1, expectedMaxReplicas: 3,
},
"CustomSettings": {
values: append(baseValues, "autoscaling.target=common-custom", "autoscaling.minReplicas=4", "autoscaling.maxReplicas=8"),
expectedHorizontalPodAutoscaler: true, expectedTarget: "common-custom", expectedMinReplicas: 4, expectedMaxReplicas: 8,
},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
manifest := suite.Chart.Manifests.Get("HorizontalPodAutoscaler", "common-test")
if tc.expectedHorizontalPodAutoscaler {
suite.Assertions.NotEmpty(manifest)
suite.Assertions.EqualValues(tc.expectedTarget, manifest.Path("spec.scaleTargetRef.name").Data())
suite.Assertions.EqualValues(tc.expectedMinReplicas, manifest.Path("spec.minReplicas").Data())
suite.Assertions.EqualValues(tc.expectedMaxReplicas, manifest.Path("spec.maxReplicas").Data())
} else {
suite.Assertions.Empty(manifest)
}
})
}
}
func (suite *HorizontalPodAutoscalerTestSuite) TestMetrics() {
baseValues := []string{"autoscaling.enabled=true"}
tests := map[string]struct {
values []string
expectedResources map[string]int
}{
"targetCPUUtilizationPercentage": {
values: append(baseValues, "autoscaling.targetCPUUtilizationPercentage=60"),
expectedResources: map[string]int{"cpu": 60},
},
"targetMemoryUtilizationPercentage": {
values: append(baseValues, "autoscaling.targetMemoryUtilizationPercentage=70"),
expectedResources: map[string]int{"memory": 70},
},
"Combined": {
values: append(baseValues, "autoscaling.targetCPUUtilizationPercentage=60", "autoscaling.targetMemoryUtilizationPercentage=70"),
expectedResources: map[string]int{"cpu": 60, "memory": 70},
},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
manifest := suite.Chart.Manifests.Get("HorizontalPodAutoscaler", "common-test")
suite.Assertions.NotEmpty(manifest)
manifestMetrics := manifest.Path("spec.metrics").Children()
metricsMap := make(map[string]int)
for _, manifestMetric := range manifestMetrics {
metricsMap[manifestMetric.Path("resource.name").Data().(string)] = int(manifestMetric.Path("resource.targetAverageUtilization").Data().(float64))
}
suite.Assertions.EqualValues(tc.expectedResources, metricsMap)
})
}
}

View File

@ -1,222 +0,0 @@
package common
import (
"testing"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type IngressTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
}
func (suite *IngressTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestIngress(t *testing.T) {
suite.Run(t, new(IngressTestSuite))
}
func (suite *IngressTestSuite) TestValues() {
tests := map[string]struct {
values []string
expectedIngress bool
expectedHostName string
expectedPath string
}{
"Default": {
values: nil,
expectedIngress: false,
},
"Disabled": {
values: []string{"ingress.main.enabled=false"},
expectedIngress: false,
},
"CustomHostAndPath": {
values: []string{
"ingress.main.enabled=true",
"ingress.main.hosts[0].host=chart-test.local",
"ingress.main.hosts[0].paths[0].path=/test",
},
expectedIngress: true,
expectedHostName: "chart-test.local",
expectedPath: "/test",
},
"Multiple": {
values: []string{
"ingress.main.enabled=true",
"ingress.secondary.enabled=true",
},
expectedIngress: true,
},
"PathTemplate": {
values: []string{
"ingress.main.enabled=true",
"ingress.main.hosts[0].host=chart-example.local",
`ingress.main.hosts[0].paths[0].path=\{\{ .Release.Name \}\}.path`,
},
expectedIngress: true,
expectedPath: "common-test.path",
},
"HostTemplate": {
values: []string{
"ingress.main.enabled=true",
`ingress.main.hosts[0].host=\{\{ .Release.Name \}\}.hostname`,
"ingress.main.hosts[0].paths[0].path=/",
},
expectedIngress: true,
expectedHostName: "common-test.hostname",
},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
ingressManifest := suite.Chart.Manifests.Get("Ingress", "common-test")
if tc.expectedIngress {
suite.Assertions.NotEmpty(ingressManifest)
ingressRules := ingressManifest.Path("spec.rules").Children()
if tc.expectedHostName != "" {
suite.Assertions.EqualValues(tc.expectedHostName, ingressRules[0].Path("host").Data())
}
if tc.expectedPath != "" {
paths := ingressRules[0].Path("http.paths").Children()
suite.Assertions.EqualValues(tc.expectedPath, paths[0].Path("path").Data())
}
} else {
suite.Assertions.Empty(ingressManifest)
}
})
}
}
func (suite *IngressTestSuite) TestPathServices() {
tests := map[string]struct {
values []string
expectedServiceName string
expectedServicePort int
}{
"Default": {
values: []string{"ingress.main.enabled=true"},
expectedServiceName: "common-test",
},
"CustomService": {
values: []string{
"service.main.ports.http.targetPort=80",
"ingress.main.enabled=true",
"ingress.main.hosts[0].host=test.local",
"ingress.main.hosts[0].paths[0].path=/second/",
"ingress.main.hosts[0].paths[0].service.name=pathService",
"ingress.main.hosts[0].paths[0].service.port=1234",
},
expectedServiceName: "pathService",
expectedServicePort: 1234,
},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
ingressManifest := suite.Chart.Manifests.Get("Ingress", "common-test")
suite.Assertions.NotEmpty(ingressManifest)
ingressRules := ingressManifest.Path("spec.rules").Children()
paths := ingressRules[0].Path("http.paths").Children()
primaryPath := paths[0]
if tc.expectedServiceName == "" {
suite.Assertions.Empty(primaryPath.Path("backend.service.name").Data())
} else {
suite.Assertions.EqualValues(tc.expectedServiceName, primaryPath.Path("backend.service.name").Data())
}
if tc.expectedServicePort == 0 {
suite.Assertions.Empty(primaryPath.Path("backend.service.port.number").Data())
} else {
suite.Assertions.EqualValues(tc.expectedServicePort, primaryPath.Path("backend.service.port.number").Data())
}
})
}
}
func (suite *IngressTestSuite) TestTLS() {
tests := map[string]struct {
values []string
expectedTLS bool
expectedHostName string
expectedSecretName string
}{
"Default": {
values: nil,
expectedTLS: false,
},
"Provided": {
values: []string{
"ingress.main.enabled=true",
"ingress.main.tls[0].hosts[0]=hostname",
"ingress.main.tls[0].secretName=secret-name",
},
expectedTLS: true,
expectedHostName: "hostname",
expectedSecretName: "secret-name",
},
"NoSecret": {
values: []string{
"ingress.main.enabled=true",
"ingress.main.tls[0].hosts[0]=hostname",
},
expectedTLS: true,
expectedHostName: "hostname",
expectedSecretName: "",
},
"SecretTemplate": {
values: []string{
"ingress.main.enabled=true",
"ingress.main.tls[0].hosts[0]=hostname",
`ingress.main.tls[0].secretName=\{\{ .Release.Name \}\}-secret`,
},
expectedTLS: true,
expectedHostName: "hostname",
expectedSecretName: "common-test-secret",
},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
ingressManifest := suite.Chart.Manifests.Get("Ingress", "common-test")
if tc.expectedTLS {
suite.Assertions.NotEmpty(ingressManifest.Path("spec.tls").Data())
tlsSpec := ingressManifest.Path("spec.tls").Children()
tlsHostsSpec := tlsSpec[0].Path("hosts").Children()
suite.Assertions.EqualValues(tc.expectedHostName, tlsHostsSpec[0].Data())
if tc.expectedSecretName == "" {
suite.Assertions.Empty(tlsSpec[0].Path("secretName").Data())
} else {
suite.Assertions.EqualValues(tc.expectedSecretName, tlsSpec[0].Path("secretName").Data())
}
} else {
suite.Assertions.Empty(ingressManifest.Path("spec.tls").Data())
}
})
}
}

View File

@ -1,166 +0,0 @@
package common
import (
"testing"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type PersistenceVolumeClaimTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
}
func (suite *PersistenceVolumeClaimTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestPersistenceVolumeClaim(t *testing.T) {
suite.Run(t, new(PersistenceVolumeClaimTestSuite))
}
func (suite *PersistenceVolumeClaimTestSuite) TestName() {
tests := map[string]struct {
values []string
expectedName string
}{
"Default": {values: []string{"persistence.config.enabled=true"}, expectedName: "common-test-config"},
"WithoutSuffix": {values: []string{"persistence.config.enabled=true", "persistence.config.nameOverride=-"}, expectedName: "common-test"},
"WithNameOverride": {values: []string{"persistence.config.enabled=true", "persistence.config.nameOverride=custom"}, expectedName: "common-test-custom"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
pvcManifest := suite.Chart.Manifests.Get("PersistentVolumeClaim", tc.expectedName)
suite.Assertions.NotEmpty(pvcManifest)
})
}
}
func (suite *PersistenceVolumeClaimTestSuite) TestStorageClass() {
tests := map[string]struct {
values []string
expectedStorageClass string
}{
"Default": {values: []string{"persistence.config.enabled=true"}, expectedStorageClass: "-"},
"CustomClass": {values: []string{"persistence.config.enabled=true", "persistence.config.storageClass=custom"}, expectedStorageClass: "custom"},
"Empty": {values: []string{"persistence.config.enabled=true", "persistence.config.storageClass=-"}, expectedStorageClass: ""},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
pvcManifest := suite.Chart.Manifests.Get("PersistentVolumeClaim", "common-test-config")
suite.Assertions.NotEmpty(pvcManifest)
if tc.expectedStorageClass == "-" {
suite.Assertions.Empty(pvcManifest.Path("spec.storageClassName").Data())
} else {
suite.Assertions.EqualValues(tc.expectedStorageClass, pvcManifest.Path("spec.storageClassName").Data())
}
})
}
}
func (suite *PersistenceVolumeClaimTestSuite) TestMetaData() {
defaultChartAnnotations := make(map[string]interface{})
defaultChartLabels := map[string]interface{}{
"app.kubernetes.io/instance": "common-test",
"app.kubernetes.io/managed-by": "Helm",
"app.kubernetes.io/name": "common-test",
"helm.sh/chart": "common-test-0.1.0",
}
tests := map[string]struct {
values []string
expectedAnnotations map[string]interface{}
expectedLabels map[string]interface{}
}{
"Default": {
values: []string{
"persistence.config.enabled=true",
},
expectedAnnotations: nil,
expectedLabels: nil,
},
"NoRetain": {
values: []string{
"persistence.config.enabled=true",
"persistence.config.labels.test_label=test",
"persistence.config.annotations.test_annotation=test",
},
expectedAnnotations: map[string]interface{}{
"test_annotation": "test",
},
expectedLabels: map[string]interface{}{
"test_label": "test",
},
},
"Retain": {
values: []string{
"persistence.config.enabled=true",
"persistence.config.retain=true",
"persistence.config.labels.test_label=test",
"persistence.config.annotations.test_annotation=test",
},
expectedAnnotations: map[string]interface{}{
"helm.sh/resource-policy": "keep",
"test_annotation": "test",
},
expectedLabels: map[string]interface{}{
"test_label": "test",
},
},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
pvcManifest := suite.Chart.Manifests.Get("PersistentVolumeClaim", "common-test-config")
suite.Assertions.NotEmpty(pvcManifest)
pvcAnnotations := pvcManifest.Path("metadata.annotations").Data()
testAnnotations := make(map[string]interface{})
for index, element := range defaultChartAnnotations {
testAnnotations[index] = element
}
for index, element := range tc.expectedAnnotations {
testAnnotations[index] = element
}
if len(testAnnotations) == 0 {
suite.Assertions.Equal(nil, pvcAnnotations)
} else {
suite.Assertions.EqualValues(testAnnotations, pvcAnnotations)
}
pvcLabels := pvcManifest.Path("metadata.labels").Data()
testLabels := make(map[string]interface{})
for index, element := range defaultChartLabels {
testLabels[index] = element
}
for index, element := range tc.expectedLabels {
testLabels[index] = element
}
if len(testLabels) == 0 {
suite.Assertions.Equal(nil, pvcLabels)
} else {
suite.Assertions.EqualValues(testLabels, pvcLabels)
}
})
}
}

View File

@ -1,386 +0,0 @@
package common
import (
"testing"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type PodTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
}
func (suite *PodTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestPod(t *testing.T) {
suite.Run(t, new(PodTestSuite))
}
func (suite *PodTestSuite) TestReplicas() {
tests := map[string]struct {
values []string
expectedValue interface{}
}{
"Default": {values: nil, expectedValue: 1},
"Specified": {values: []string{"controller.replicas=3"}, expectedValue: 3},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
suite.Assertions.EqualValues(tc.expectedValue, deploymentManifest.Path("spec.replicas").Data())
})
}
}
func (suite *PodTestSuite) TestHostNetwork() {
tests := map[string]struct {
values []string
expectedValue interface{}
}{
"Default": {values: nil, expectedValue: nil},
"SpecifiedTrue": {values: []string{"hostNetwork=true"}, expectedValue: true},
"SpecifiedFalse": {values: []string{"hostNetwork=false"}, expectedValue: nil},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
suite.Assertions.EqualValues(tc.expectedValue, deploymentManifest.Path("spec.template.spec.hostNetwork").Data())
})
}
}
func (suite *PodTestSuite) TestDnsPolicy() {
tests := map[string]struct {
values []string
expectedValue interface{}
}{
"Default": {values: nil, expectedValue: "ClusterFirst"},
"HostnetworkFalse": {values: []string{"hostNetwork=false"}, expectedValue: "ClusterFirst"},
"HostnetworkTrue": {values: []string{"hostNetwork=true"}, expectedValue: "ClusterFirstWithHostNet"},
"ManualOverride": {values: []string{"dnsPolicy=None"}, expectedValue: "None"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
suite.Assertions.NotEmpty(deploymentManifest)
suite.Assertions.EqualValues(tc.expectedValue, deploymentManifest.Path("spec.template.spec.dnsPolicy").Data())
})
}
}
func (suite *PodTestSuite) TestInitContainers() {
tests := map[string]struct {
values []string
expectedContainer interface{}
}{
"StaticWithName": {values: []string{"initContainers.init1.name=template-test"}, expectedContainer: "template-test"},
"StaticWithoutName": {values: []string{"initContainers.init1.image=template-test"}, expectedContainer: "init1"},
"DynamicTemplate": {values: []string{`initContainers.init1.name=\{\{ .Release.Name \}\}-container`}, expectedContainer: "common-test-container"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
initcontainers := deploymentManifest.Path("spec.template.spec.initContainers")
suite.Assertions.Contains(initcontainers.Search("*", "name").Data(), tc.expectedContainer)
})
}
}
func (suite *PodTestSuite) TestAdditionalContainers() {
tests := map[string]struct {
values []string
expectedContainer interface{}
}{
"StaticWithName": {values: []string{"additionalContainers.additional1.name=template-test"}, expectedContainer: "template-test"},
"StaticWithoutName": {values: []string{"additionalContainers.additional1.image=template-test"}, expectedContainer: "additional1"},
"DynamicTemplate": {values: []string{`additionalContainers.additional1.name=\{\{ .Release.Name \}\}-container`}, expectedContainer: "common-test-container"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
containers := deploymentManifest.Path("spec.template.spec.containers")
suite.Assertions.Contains(containers.Search("*", "name").Data(), tc.expectedContainer)
})
}
}
func (suite *PodTestSuite) TestPersistenceItems() {
values := `
persistence:
cache:
enabled: true
type: emptyDir
config:
enabled: true
data:
enabled: true
existingClaim: dataClaim
custom-mount:
enabled: true
type: custom
volumeSpec:
downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
`
tests := map[string]struct {
values *string
expectedVolumes []string
}{
"Default": {values: nil, expectedVolumes: nil},
"MultipleItems": {values: &values, expectedVolumes: []string{"config", "cache", "data", "custom-mount"}},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, nil, tc.values)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
volumes := deploymentManifest.Path("spec.template.spec.volumes")
if tc.expectedVolumes == nil {
suite.Assertions.EqualValues(nil, volumes.Data())
} else {
suite.Assertions.NotEmpty(volumes)
searchVolumes := volumes.Search("*", "name").Data()
for _, expectedVolume := range tc.expectedVolumes {
suite.Assertions.Contains(searchVolumes, expectedVolume)
}
}
})
}
}
func (suite *PodTestSuite) TestPersistenceClaimNames() {
values := `
persistence:
config:
enabled: true
existingClaim:
enabled: true
existingClaim: myClaim
claimWithoutSuffix:
enabled: true
nameOverride: "-"
accessMode: ReadWriteMany
size: 1G
claimWithNameOverride:
enabled: true
nameOverride: suffix
accessMode: ReadWriteMany
size: 1G
`
tests := map[string]struct {
values *string
volumeToTest string
expectedClaimName string
}{
"DefaultClaimName": {values: &values, volumeToTest: "config", expectedClaimName: "common-test-config"},
"ClaimNameWithoutSuffix": {values: &values, volumeToTest: "claimWithoutSuffix", expectedClaimName: "common-test"},
"ClaimNameWithNameOverride": {values: &values, volumeToTest: "claimWithNameOverride", expectedClaimName: "common-test-suffix"},
"ExistingClaim": {values: &values, volumeToTest: "existingClaim", expectedClaimName: "myClaim"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, nil, tc.values)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
volumes := deploymentManifest.Path("spec.template.spec.volumes").Children()
for _, volume := range volumes {
volumeName := volume.Path("name").Data().(string)
if volumeName == tc.volumeToTest {
suite.Assertions.EqualValues(tc.expectedClaimName, volume.Path("persistentVolumeClaim.claimName").Data())
break
}
}
})
}
}
func (suite *PodTestSuite) TestPersistenceEmptyDir() {
baseValues := `
persistence:
config:
enabled: true
type: emptyDir
`
tests := map[string]struct {
values []string
expectedMedium string
expectedSizeLimit string
}{
"Enabled": {values: nil, expectedMedium: "", expectedSizeLimit: ""},
"WithMedium": {values: []string{"persistence.config.medium=memory"}, expectedMedium: "memory", expectedSizeLimit: ""},
"WithSizeLimit": {values: []string{"persistence.config.medium=memory", "persistence.config.sizeLimit=1Gi"}, expectedMedium: "memory", expectedSizeLimit: "1Gi"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, &baseValues)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
volumes := deploymentManifest.Path("spec.template.spec.volumes").Children()
volume := volumes[0]
suite.Assertions.NotEmpty(volume.Data())
if tc.expectedMedium == "" {
suite.Assertions.Nil(volume.Path("emptyDir.medium"))
} else {
suite.Assertions.EqualValues(tc.expectedMedium, volume.Path("emptyDir.medium").Data())
}
if tc.expectedSizeLimit == "" {
suite.Assertions.Nil(volume.Path("emptyDir.sizeLimit"))
} else {
suite.Assertions.EqualValues(tc.expectedSizeLimit, volume.Path("emptyDir.sizeLimit").Data())
}
})
}
}
func (suite *PodTestSuite) TestHostPathVolumes() {
values := `
persistence:
hostpathmounts-data:
enabled: true
type: hostPath
hostPath: "/tmp1"
mountPath: "/data"
hostpathmounts-with-type:
enabled: true
type: hostPath
hostPath: "/tmp2"
hostPathType: "Directory"
mountPath: "/data2"
`
tests := map[string]struct {
values *string
expectedVolumes []string
}{
"Default": {values: nil, expectedVolumes: nil},
"MultipleItems": {values: &values, expectedVolumes: []string{"hostpathmounts-data", "hostpathmounts-with-type"}},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, nil, tc.values)
if err != nil {
suite.FailNow(err.Error())
}
deploymentManifest := suite.Chart.Manifests.Get("Deployment", "common-test")
volumes := deploymentManifest.Path("spec.template.spec.volumes")
if tc.expectedVolumes == nil {
suite.Assertions.EqualValues(nil, volumes.Data())
} else {
suite.Assertions.NotEmpty(volumes)
searchVolumes := volumes.Search("*", "name").Data()
for _, expectedVolume := range tc.expectedVolumes {
suite.Assertions.Contains(searchVolumes, expectedVolume)
}
}
})
}
}
func (suite *PodTestSuite) TestVolumeClaimTemplates() {
values := `
volumeClaimTemplates:
- name: 'storage'
accessMode: 'ReadWriteOnce'
size: '10Gi'
storageClass: 'storage'
`
tests := map[string]struct {
values []string
volumeClaimToTest string
expectedAccessMode string
expectedSize string
expectedStorageClassName string
}{
"StatefulSet": {values: []string{"controller.type=statefulset"}, volumeClaimToTest: "storage", expectedAccessMode: "ReadWriteOnce", expectedSize: "10Gi", expectedStorageClassName: "storage"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, &values)
if err != nil {
suite.FailNow(err.Error())
}
controllerManifest := suite.Chart.Manifests.Get("StatefulSet", "common-test")
suite.Assertions.NotEmpty(controllerManifest)
volumeClaimTemplates := controllerManifest.Path("spec.volumeClaimTemplates").Children()
suite.Assertions.NotEmpty(volumeClaimTemplates)
for _, volumeClaimTemplate := range volumeClaimTemplates {
volumeClaimName := volumeClaimTemplate.Path("metadata.name").Data()
if volumeClaimName == tc.volumeClaimToTest {
if tc.expectedAccessMode == "" {
suite.Assertions.Empty(controllerManifest)
} else {
accessModes := volumeClaimTemplate.Path("spec.accessModes").Children()
suite.Assertions.EqualValues(tc.expectedAccessMode, accessModes[0].Data())
}
if tc.expectedSize == "" {
suite.Assertions.Empty(controllerManifest)
} else {
suite.Assertions.EqualValues(tc.expectedSize, volumeClaimTemplate.Path("spec.resources.requests.storage").Data())
}
if tc.expectedStorageClassName == "" {
suite.Assertions.Empty(controllerManifest)
} else {
suite.Assertions.EqualValues(tc.expectedStorageClassName, volumeClaimTemplate.Path("spec.storageClassName").Data())
}
break
}
}
})
}
}

View File

@ -1,138 +0,0 @@
package common
import (
"fmt"
"testing"
"github.com/k8s-at-home/library-charts/test/helmunit"
"github.com/stretchr/testify/suite"
)
type ServiceTestSuite struct {
suite.Suite
Chart helmunit.HelmChart
}
func (suite *ServiceTestSuite) SetupSuite() {
suite.Chart = helmunit.New("common-test", "../../../../helper-charts/common-test")
suite.Chart.UpdateDependencies()
}
// We need this function to kick off the test suite, otherwise
// "go test" won't know about our tests
func TestService(t *testing.T) {
suite.Run(t, new(ServiceTestSuite))
}
func (suite *ServiceTestSuite) TestServiceName() {
tests := map[string]struct {
values []string
expectedName string
}{
"Default": {values: nil, expectedName: "common-test"},
"CustomName": {values: []string{"service.main.nameOverride=http"}, expectedName: "common-test-http"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
serviceManifest := suite.Chart.Manifests.Get("Service", tc.expectedName)
suite.Assertions.NotEmpty(serviceManifest)
})
}
}
func (suite *ServiceTestSuite) TestPortNames() {
tests := map[string]struct {
values []string
expectedRenderFailure bool
expectedName string
expectedTargetPort interface{}
}{
"Default": {values: nil, expectedRenderFailure: false, expectedName: "http", expectedTargetPort: "http"},
"CustomName": {values: []string{"service.main.ports.http.enabled=false", "service.main.ports.server.enabled=true", "service.main.ports.server.port=8080"}, expectedRenderFailure: false, expectedName: "server", expectedTargetPort: "server"},
"CustomTargetPort": {values: []string{"service.main.ports.http.targetPort=80"}, expectedRenderFailure: false, expectedName: "http", expectedTargetPort: 80},
"NamedTargetPort": {values: []string{"service.main.ports.http.targetPort=name"}, expectedRenderFailure: true, expectedName: "", expectedTargetPort: nil},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if tc.expectedRenderFailure {
suite.Assertions.Error(err)
return
}
if err != nil {
suite.FailNow(err.Error())
}
serviceManifest := suite.Chart.Manifests.Get("Service", "common-test")
suite.Assertions.NotEmpty(serviceManifest)
servicePorts := serviceManifest.Path("spec.ports").Children()
suite.Assertions.EqualValues(tc.expectedName, servicePorts[0].Path("name").Data())
suite.Assertions.EqualValues(tc.expectedTargetPort, servicePorts[0].Path("targetPort").Data())
})
}
}
func (suite *ServiceTestSuite) TestPortProtocol() {
tests := map[string]struct {
values []string
expectedProtocol string
}{
"Default": {values: nil, expectedProtocol: "TCP"},
"ExplicitTCP": {values: []string{"service.main.ports.http.protocol=TCP"}, expectedProtocol: "TCP"},
"ExplicitHTTP": {values: []string{"service.main.ports.http.protocol=HTTP"}, expectedProtocol: "TCP"},
"ExplicitHTTPS": {values: []string{"service.main.ports.http.protocol=HTTPS"}, expectedProtocol: "TCP"},
"ExplicitUDP": {values: []string{"service.main.ports.http.protocol=UDP"}, expectedProtocol: "UDP"},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
serviceManifest := suite.Chart.Manifests.Get("Service", "common-test")
suite.Assertions.NotEmpty(serviceManifest)
servicePorts := serviceManifest.Path("spec.ports").Children()
suite.Assertions.EqualValues(tc.expectedProtocol, servicePorts[0].Path("protocol").Data())
})
}
}
func (suite *ServiceTestSuite) TestAnnotations() {
tests := map[string]struct {
values []string
expectedAnnotations map[string]string
}{
"Default": {values: nil, expectedAnnotations: nil},
"ExplicitTCP": {values: []string{"service.main.ports.http.protocol=TCP"}, expectedAnnotations: nil},
"ExplicitHTTP": {values: []string{"service.main.ports.http.protocol=HTTP"}, expectedAnnotations: nil},
"ExplicitHTTPS": {values: []string{"service.main.ports.http.protocol=HTTPS"}, expectedAnnotations: map[string]string{"traefik.ingress.kubernetes.io/service.serversscheme": "https"}},
"ExplicitUDP": {values: []string{"service.main.ports.http.protocol=UDP"}, expectedAnnotations: nil},
}
for name, tc := range tests {
suite.Suite.Run(name, func() {
err := suite.Chart.Render(nil, tc.values, nil)
if err != nil {
suite.FailNow(err.Error())
}
serviceManifest := suite.Chart.Manifests.Get("Service", "common-test")
suite.Assertions.NotEmpty(serviceManifest)
serviceAnnotations := serviceManifest.Path("metadata.annotations").Children()
if tc.expectedAnnotations == nil {
suite.Assertions.Empty(serviceAnnotations)
} else {
for annotation, value := range tc.expectedAnnotations {
serviceAnnotation := serviceManifest.Path("metadata.annotations").Search(annotation)
suite.Assertions.NotEmpty(serviceAnnotation, fmt.Sprintf("Annotation %s not found", annotation))
suite.Assertions.EqualValues(value, serviceAnnotation.Data(), fmt.Sprintf("Invalid value for annotation %s", annotation))
}
}
})
}
}

View File

@ -25,16 +25,29 @@ service:
main: main:
ports: ports:
http: http:
port: 80 port: 8080
ingress: ingress:
# -- Enable and configure ingress settings for the chart under this key. # -- Enable and configure ingress settings for the chart under this key.
# @default -- See values.yaml # @default -- See values.yaml
main: main:
enabled: false enabled: false
# hosts:
# - host: "cv.opti.cabillot.eu"
# paths:
# - path: "/"
# tls:
# - hosts:
# - "cv.opti.cabillot.eu"
# -- Configures the probes for the main Pod. # -- Configures the probes for the main Pod.
# @default -- See values.yaml # @default -- See values.yaml
probes: probes:
liveness: liveness:
enabled: false enabled: false
readiness:
enabled: false
startup:
enabled: false
automountServiceAccountToken: false