Skip to content

Commit

Permalink
feat(runner-config): 🔥 add some unit test to capture original behavio…
Browse files Browse the repository at this point in the history
…ur and validate field value override
  • Loading branch information
Yannick Roffin committed Aug 21, 2024
1 parent c373809 commit e4ac25b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
3 changes: 1 addition & 2 deletions api/v1alpha2/reference_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/fluxcd/pkg/apis/meta"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -166,7 +165,7 @@ type RunnerPodSpec struct {

// Runner pod ImagePullPolicy to use other than default
// +optional
ImagePullPolicy v1.PullPolicy `json:"imagePullPolicy,omitempty"`
ImagePullPolicy string `json:"imagePullPolicy,omitempty"`

// List of sources to populate environment variables in the container.
// The keys defined within a source must be a C_IDENTIFIER. All invalid keys
Expand Down
71 changes: 71 additions & 0 deletions controllers/tc000260_runner_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func Test_000260_runner_pod_test(t *testing.T) {
spec := reconciler.runnerPodSpec(helloWorldTF, "runner.tls-123")
g.Expect(spec.ServiceAccountName).To(Equal(serviceAccountName))
g.Expect(spec.Containers[0].Image).To(Equal(runnerPodImage))
g.Expect(spec.Containers[0].ImagePullPolicy).To(Equal(corev1.PullIfNotPresent))
g.Expect(spec.HostAliases[0].Hostnames).To(Equal([]string{"foo", "bar"}))

podTemplate, err := runnerPodTemplate(helloWorldTF, "runner.tls-123", revision)
Expand Down Expand Up @@ -178,6 +179,76 @@ func Test_000260_runner_pod_test_env_vars(t *testing.T) {
}()).To(BeTrue())
}

func Test_000260_runner_pod_a_test_image_pull_policy(t *testing.T) {
Spec("This spec describes a runner pod creation process")

const (
terraformName = "runner-pod-test"
sourceName = "runner-pod-test"
serviceAccountName = "helloworld-tf-runner"
runnerPodImage = "ghcr.io/weaveworks/tf-runner:test"
revision = "v2.6@sha256:c7fd0cc69b924aa5f9a6928477311737e439ca1b9e444855b0377e8a8ec65bb5"
)

var stringMap = map[string]string{
"company.com/abc": "xyz",
"company.com/xyz": "abc",
"tf.weave.works/tls-secret-name": "runner.tls-123",
}

g := NewWithT(t)

It("generate a runner pod template")
By("passing a terraform object, the runner pod template should be accurate")
helloWorldTF := infrav1.Terraform{
ObjectMeta: metav1.ObjectMeta{
Name: terraformName,
Namespace: "flux-system",
},
Spec: infrav1.TerraformSpec{
ApprovePlan: "auto",
Path: "./terraform-hello-world-example",
SourceRef: infrav1.CrossNamespaceSourceReference{
Kind: "GitRepository",
Name: sourceName,
Namespace: "flux-system",
},
ServiceAccountName: serviceAccountName,
RunnerPodTemplate: infrav1.RunnerPodTemplate{
Metadata: infrav1.RunnerPodMetadata{
Labels: stringMap,
Annotations: stringMap,
},
Spec: infrav1.RunnerPodSpec{
Image: runnerPodImage,
ImagePullPolicy: "Always",
},
},
},
}

spec := reconciler.runnerPodSpec(helloWorldTF, "runner.tls-123")
g.Expect(spec.ServiceAccountName).To(Equal(serviceAccountName))
g.Expect(spec.Containers[0].Image).To(Equal(runnerPodImage))
g.Expect(spec.Containers[0].ImagePullPolicy).To(Equal(corev1.PullAlways))

podTemplate, err := runnerPodTemplate(helloWorldTF, "runner.tls-123", revision)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(func() bool {
for k, v := range stringMap {
if v != podTemplate.ObjectMeta.Labels[k] {
return false
}
}
for k, v := range stringMap {
if v != podTemplate.ObjectMeta.Annotations[k] {
return false
}
}
return true
}()).To(BeTrue())
}

func Test_000260_runner_pod_test_env_vars_proxy(t *testing.T) {
Spec("This spec describes a runner pod creation process")

Expand Down
4 changes: 2 additions & 2 deletions controllers/tf_controller_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func getRunnerPodImage(image string) string {
return runnerPodImage
}

func getRunnerImagePullPolicy(imagePullPolicy v1.PullPolicy) v1.PullPolicy {
runnerImagePullPolicy := imagePullPolicy
func getRunnerImagePullPolicy(imagePullPolicy string) v1.PullPolicy {
runnerImagePullPolicy := v1.PullPolicy(imagePullPolicy)
if runnerImagePullPolicy == "" {
runnerImagePullPolicy = v1.PullIfNotPresent
}
Expand Down
8 changes: 2 additions & 6 deletions docs/References/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -870,9 +870,7 @@ string
<td>
<code>imagePullPolicy</code><br>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#pullpolicy-v1-core">
Kubernetes core/v1.PullPolicy
</a>
string
</em>
</td>
<td>
Expand Down Expand Up @@ -1113,9 +1111,7 @@ string
<td>
<code>imagePullPolicy</code><br>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.23/#pullpolicy-v1-core">
Kubernetes core/v1.PullPolicy
</a>
string
</em>
</td>
<td>
Expand Down

0 comments on commit e4ac25b

Please sign in to comment.