Skip to content
parameters_test.go 7.65 KiB
Newer Older
Timothee Gosselin's avatar
Timothee Gosselin committed
package parameters_test
Timothee Gosselin's avatar
Timothee Gosselin committed

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

Timothee Gosselin's avatar
Timothee Gosselin committed
	. "k8s.libre.sh/application/settings/parameters"
Timothee Gosselin's avatar
Timothee Gosselin committed

	corev1 "k8s.io/api/core/v1"
)

var _ = Describe("Parameters", func() {
	var (
Timothee Gosselin's avatar
Timothee Gosselin committed
		paramEnvVar     Parameter
		paramEnvVarFrom Parameter
		//	paramEnvFrom          Parameter
		//	paramEnvFromSecret    Parameter
Timothee Gosselin's avatar
Timothee Gosselin committed
		paramEnvVarFromSecret Parameter
Timothee Gosselin's avatar
Timothee Gosselin committed
		paramEnvVarFromObject Parameter
Timothee Gosselin's avatar
Timothee Gosselin committed
		paramTemplateLiteral  Parameter
		paramTemplateFrom     Parameter
		paramMountFile        Parameter
Timothee Gosselin's avatar
Timothee Gosselin committed
		valueFrom             ValueFrom
		valueFromSecret       ValueFrom
Timothee Gosselin's avatar
Timothee Gosselin committed
		valueFromObject       ValueFrom
Timothee Gosselin's avatar
Timothee Gosselin committed
		parameters            *Parameters
Timothee Gosselin's avatar
Timothee Gosselin committed
	//	settings              *Settings
	//	envFrom               *EnvFrom
Timothee Gosselin's avatar
Timothee Gosselin committed
	)

	BeforeEach(func() {
Timothee Gosselin's avatar
Timothee Gosselin committed
		paramTemplateLiteral = Parameter{
			Key:       "TEMPLATE_PARAMETER",
			Value:     "{{ .LITERAL_PARAMETER }}",
Timothee Gosselin's avatar
Timothee Gosselin committed
			Type:      ConfigParameter,
			Generate:  GenerateTemplate,
			MountType: MountLiteral,
		}

Timothee Gosselin's avatar
Timothee Gosselin committed
		paramTemplateFrom = Parameter{
			Key:       "TEMPLATE_PARAMETER_FROM",
			Value:     "{{ .LITERAL_PARAMETER }}",
			Type:      ConfigParameter,
			Generate:  GenerateTemplate,
Timothee Gosselin's avatar
Timothee Gosselin committed
			MountType: MountEnvFile,
Timothee Gosselin's avatar
Timothee Gosselin committed
		}

Timothee Gosselin's avatar
Timothee Gosselin committed
		paramEnvVar = Parameter{
Timothee Gosselin's avatar
Timothee Gosselin committed
			Key:       "LITERAL_PARAMETER",
			Value:     "LiteralParameter",
Timothee Gosselin's avatar
Timothee Gosselin committed
			Type:      ConfigParameter,
			MountType: MountLiteral,
Timothee Gosselin's avatar
Timothee Gosselin committed
		}

		valueFrom = ValueFrom{
Timothee Gosselin's avatar
Timothee Gosselin committed
			FromKey: "FROM_KEY_CONFIG_PARAMETER",
			Ref:     "configRef",
Timothee Gosselin's avatar
Timothee Gosselin committed
		}

		valueFromSecret = ValueFrom{
Timothee Gosselin's avatar
Timothee Gosselin committed
			FromKey: "FROM_KEY_SECRET_PARAMETER",
			Ref:     "secretRef",
Timothee Gosselin's avatar
Timothee Gosselin committed
		}

Timothee Gosselin's avatar
Timothee Gosselin committed
		valueFromObject = ValueFrom{
			FromKey: "status.podIP",
Timothee Gosselin's avatar
Timothee Gosselin committed
			Ref:     "v1",
Timothee Gosselin's avatar
Timothee Gosselin committed
		paramEnvVarFrom = Parameter{
Timothee Gosselin's avatar
Timothee Gosselin committed
			Key:       "VALUE_FROM_CONFIG_PARAMETER",
Timothee Gosselin's avatar
Timothee Gosselin committed
			ValueFrom: valueFrom,
Timothee Gosselin's avatar
Timothee Gosselin committed
			Type:      ConfigParameter,
			MountType: MountFrom,
Timothee Gosselin's avatar
Timothee Gosselin committed
		}

		paramEnvVarFromSecret = Parameter{
Timothee Gosselin's avatar
Timothee Gosselin committed
			Key:       "VALUE_FROM_SECRET_PARAMETER",
Timothee Gosselin's avatar
Timothee Gosselin committed
			ValueFrom: valueFromSecret,
Timothee Gosselin's avatar
Timothee Gosselin committed
			Type:      SecretParameter,
			MountType: MountFrom,
Timothee Gosselin's avatar
Timothee Gosselin committed
		}

Timothee Gosselin's avatar
Timothee Gosselin committed
		paramEnvVarFromObject = Parameter{
Timothee Gosselin's avatar
Timothee Gosselin committed
			Key:       "PARAMETER_FROM_OBJECT",
Timothee Gosselin's avatar
Timothee Gosselin committed
			Type:      ObjectFieldParameter,
			ValueFrom: valueFromObject,
			MountType: MountFrom,
Timothee Gosselin's avatar
Timothee Gosselin committed
		}

		paramMountFile = Parameter{
			Key:       "nginx.conf",
			Type:      ConfigParameter,
			Value:     "example",
			MountType: MountFile,
			MountPath: MountPath{
				Path:    "/ect/nginx/nginx.conf",
				SubPath: "nginx.conf",
			},
		}

Timothee Gosselin's avatar
Timothee Gosselin committed
		parameters = &Parameters{
			&paramEnvVar,
			&paramEnvVarFrom,
			&paramEnvVarFromSecret,
Timothee Gosselin's avatar
Timothee Gosselin committed
			//	&paramEnvFrom,
			//	&paramEnvFromSecret,
Timothee Gosselin's avatar
Timothee Gosselin committed
			&paramEnvVarFromObject,
Timothee Gosselin's avatar
Timothee Gosselin committed
			&paramTemplateLiteral,
			&paramTemplateFrom,
			&paramMountFile,
Timothee Gosselin's avatar
Timothee Gosselin committed
		/* 		envFrom = &EnvFrom{
		   			SecretRefs: []string{
		   				"mysecret",
		   				"myothersecret",
		   			},
		   			ConfigRefs: []string{
		   				"myconfigmap",
		   			},
		   		}

		   		settings = &Settings{
		   			EnvFrom:    envFrom,
		   			Parameters: parameters,
		   		} */
	Describe("Generating Envirnonment Variables for containers from parameter", func() {
		Context("MountType is Literal", func() {
			It("should generate envVar", func() {
Timothee Gosselin's avatar
Timothee Gosselin committed

				expectedObj := corev1.EnvVar{
Timothee Gosselin's avatar
Timothee Gosselin committed
					Name:  "LITERAL_PARAMETER",
					Value: "LiteralParameter",
Timothee Gosselin's avatar
Timothee Gosselin committed
				}

				res, err := paramEnvVar.GetEnvVar()

				Expect(res).To(Equal(expectedObj))
				Expect(err).NotTo(HaveOccurred())
			})
		})

		Context("MountType is From", func() {
			It("should generate envVar with valueFrom", func() {
Timothee Gosselin's avatar
Timothee Gosselin committed
				expectedObj := corev1.EnvVar{
Timothee Gosselin's avatar
Timothee Gosselin committed
					Name: "VALUE_FROM_CONFIG_PARAMETER",
Timothee Gosselin's avatar
Timothee Gosselin committed
					ValueFrom: &corev1.EnvVarSource{
						ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
							LocalObjectReference: corev1.LocalObjectReference{
Timothee Gosselin's avatar
Timothee Gosselin committed
								Name: "configRef",
Timothee Gosselin's avatar
Timothee Gosselin committed
							},
Timothee Gosselin's avatar
Timothee Gosselin committed
							Key: "FROM_KEY_CONFIG_PARAMETER",
Timothee Gosselin's avatar
Timothee Gosselin committed
				// TODO ADD SECRET

Timothee Gosselin's avatar
Timothee Gosselin committed
				res, err := paramEnvVarFrom.GetEnvVar()

				Expect(res).To(Equal(expectedObj))
				Expect(err).NotTo(HaveOccurred())
			})
		})

Timothee Gosselin's avatar
Timothee Gosselin committed
		Context("MountType is ObjectField", func() {
			It("should generate envVar with valueFrom", func() {
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
				expectedObj := corev1.EnvVar{
Timothee Gosselin's avatar
Timothee Gosselin committed
					Name: "PARAMETER_FROM_OBJECT",
Timothee Gosselin's avatar
Timothee Gosselin committed
					ValueFrom: &corev1.EnvVarSource{
						FieldRef: &corev1.ObjectFieldSelector{
							APIVersion: "v1",
							FieldPath:  "status.podIP",
Timothee Gosselin's avatar
Timothee Gosselin committed
				res, err := paramEnvVarFromObject.GetEnvVar()
Timothee Gosselin's avatar
Timothee Gosselin committed

				Expect(res).To(Equal(expectedObj))
				Expect(err).NotTo(HaveOccurred())
			})
		})
	})

Timothee Gosselin's avatar
Timothee Gosselin committed
	/* 	Describe("Generating Env for containers with EnvFrom", func() {
Timothee Gosselin's avatar
Timothee Gosselin committed
		Context("Generating env", func() {
			It("should generate envFrom", func() {
Timothee Gosselin's avatar
Timothee Gosselin committed

				expectedObj := []corev1.EnvFromSource{
Timothee Gosselin's avatar
Timothee Gosselin committed
					{
						ConfigMapRef: &corev1.ConfigMapEnvSource{
							LocalObjectReference: corev1.LocalObjectReference{
								Name: "myconfigmap",
						SecretRef: &corev1.SecretEnvSource{
							LocalObjectReference: corev1.LocalObjectReference{
								Name: "mysecret",
Timothee Gosselin's avatar
Timothee Gosselin committed
							},
						},
					},
Timothee Gosselin's avatar
Timothee Gosselin committed
					{
						SecretRef: &corev1.SecretEnvSource{
							LocalObjectReference: corev1.LocalObjectReference{
								Name: "myothersecret",
Timothee Gosselin's avatar
Timothee Gosselin committed
							},
						},
					},
				res := settings.GetEnvFrom()
Timothee Gosselin's avatar
Timothee Gosselin committed

				Expect(res).To(Equal(expectedObj))
				//	Expect(err).NotTo(HaveOccurred())
			})
Timothee Gosselin's avatar
Timothee Gosselin committed
	}) */

	Describe("Generating Env for containers with Parameters", func() {
		Context("Generating envFrom", func() {
			It("should generate envVar", func() {

				expectedObj := []corev1.EnvVar{
					{
						Name:  "LITERAL_PARAMETER",
						Value: "LiteralParameter",
					},
					{
						Name: "VALUE_FROM_CONFIG_PARAMETER",
						ValueFrom: &corev1.EnvVarSource{
							ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
								LocalObjectReference: corev1.LocalObjectReference{
									Name: "configRef",
Timothee Gosselin's avatar
Timothee Gosselin committed
								Key: "FROM_KEY_CONFIG_PARAMETER",
Timothee Gosselin's avatar
Timothee Gosselin committed
							},
						},
Timothee Gosselin's avatar
Timothee Gosselin committed
					},
					{
						Name: "VALUE_FROM_SECRET_PARAMETER",
						ValueFrom: &corev1.EnvVarSource{
							SecretKeyRef: &corev1.SecretKeySelector{
								LocalObjectReference: corev1.LocalObjectReference{
									Name: "secretRef",
Timothee Gosselin's avatar
Timothee Gosselin committed
								Key: "FROM_KEY_SECRET_PARAMETER",
Timothee Gosselin's avatar
Timothee Gosselin committed
							},
						},
Timothee Gosselin's avatar
Timothee Gosselin committed
					},
					{
						Name: "PARAMETER_FROM_OBJECT",
						ValueFrom: &corev1.EnvVarSource{
							FieldRef: &corev1.ObjectFieldSelector{
								APIVersion: "v1",
								FieldPath:  "status.podIP",
Timothee Gosselin's avatar
Timothee Gosselin committed
					},
					{
						Name:  "TEMPLATE_PARAMETER",
						Value: "LiteralParameter",
					},
				}
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
				parameters.InitValues()
				res := parameters.GetEnvVar()
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
				Expect(res).To(Equal(expectedObj))
				//	Expect(err).NotTo(HaveOccurred())
Timothee Gosselin's avatar
Timothee Gosselin committed
			})
Timothee Gosselin's avatar
Timothee Gosselin committed
		})
Timothee Gosselin's avatar
Timothee Gosselin committed
	})
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
	Describe("Generating data for configmap and secret with Parameters", func() {
		Context("Generating data", func() {
			It("should generate data", func() {
				parameters.InitValues()
Timothee Gosselin's avatar
Timothee Gosselin committed

Timothee Gosselin's avatar
Timothee Gosselin committed
				expectedObj := map[string]string{
					"TEMPLATE_PARAMETER_FROM": "LiteralParameter",
				}

				Expect(parameters.GetData()).To(Equal(expectedObj))
				//	Expect(err).NotTo(HaveOccurred())
Timothee Gosselin's avatar
Timothee Gosselin committed
			})
		})
Timothee Gosselin's avatar
Timothee Gosselin committed
	})
	/*
		Describe("Generating mount files", func() {
			Context("Generating volumes & volume mount", func() {
				It("should generate volume for pod", func() {

					expectedObj := corev1.Volume{
						Name: "nginx-conf",
						VolumeSource: corev1.VolumeSource{
							ConfigMap: &corev1.ConfigMapVolumeSource{
								LocalObjectReference: corev1.LocalObjectReference{
									Name: "",
								},
							},
						},
					}
					Expect(paramMountFile.GetPodVolume()).To(Equal(expectedObj))
					//	Expect(err).NotTo(HaveOccurred())
				})
				It("should generate volumeMount for container", func() {
					expectedObj := corev1.VolumeMount{
						Name:      "nginx-conf",
						ReadOnly:  true,
						MountPath: "",
						SubPath:   "",
					}

					Expect(paramMountFile.GetVolumeMount()).To(Equal(expectedObj))
					//	Expect(err).NotTo(HaveOccurred())
				})
			})
		})
Timothee Gosselin's avatar
Timothee Gosselin committed
	*/

Timothee Gosselin's avatar
Timothee Gosselin committed
})