Skip to content

Commit 99e0b7c

Browse files
committed
[WIP] Code refactor
1 parent ff39176 commit 99e0b7c

File tree

2 files changed

+148
-99
lines changed

2 files changed

+148
-99
lines changed

charts/test/unit/deployment_test.go

Lines changed: 102 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func defaultDeployment() appsv1.Deployment {
4242
Containers: []corev1.Container{
4343
{
4444
Name: "manager",
45-
Image: "hashicorp/hcp-terraform-operator:2.7.0",
45+
Image: fmt.Sprintf("hashicorp/hcp-terraform-operator:%s", helmChartVersion),
4646
Command: []string{"/manager"},
4747
Args: []string{
4848
"--sync-period=1h",
@@ -171,51 +171,6 @@ func TestDeploymentNamespace(t *testing.T) {
171171
assert.Equal(t, d, deployment)
172172
}
173173

174-
func TestDeploymentReplicas(t *testing.T) {
175-
options := &helm.Options{
176-
SetValues: map[string]string{
177-
"replicaCount": "5",
178-
},
179-
Version: helmChartVersion,
180-
}
181-
deployment := renderDeploymentManifest(t, options)
182-
d := defaultDeployment()
183-
d.Spec.Replicas = ptr.To(int32(5))
184-
185-
assert.Equal(t, d, deployment)
186-
}
187-
188-
func TestDeploymentPriorityClassName(t *testing.T) {
189-
priorityClassName := "high-priority"
190-
options := &helm.Options{
191-
SetValues: map[string]string{
192-
"priorityClassName": priorityClassName,
193-
},
194-
Version: helmChartVersion,
195-
}
196-
deployment := renderDeploymentManifest(t, options)
197-
d := defaultDeployment()
198-
d.Spec.Template.Spec.PriorityClassName = priorityClassName
199-
200-
assert.Equal(t, d, deployment)
201-
}
202-
203-
func TestDeploymentServiceAccountName(t *testing.T) {
204-
serviceAccountName := "this"
205-
options := &helm.Options{
206-
SetValues: map[string]string{
207-
"serviceAccount.create": "true",
208-
"serviceAccount.name": serviceAccountName,
209-
},
210-
Version: helmChartVersion,
211-
}
212-
deployment := renderDeploymentManifest(t, options)
213-
d := defaultDeployment()
214-
d.Spec.Template.Spec.ServiceAccountName = serviceAccountName
215-
216-
assert.Equal(t, d, deployment)
217-
}
218-
219174
func TestDeploymentImagePullSecrets(t *testing.T) {
220175
options := &helm.Options{
221176
SetJsonValues: map[string]string{
@@ -251,83 +206,46 @@ func TestDeploymentPodLabels(t *testing.T) {
251206
assert.Equal(t, d, deployment)
252207
}
253208

254-
func TestDeploymentSecurityContext(t *testing.T) {
209+
func TestDeploymentReplicaCount(t *testing.T) {
255210
options := &helm.Options{
256-
Version: helmChartVersion,
257211
SetValues: map[string]string{
258-
"securityContext.runAsNonRoot": "false",
212+
"replicaCount": "5",
259213
},
214+
Version: helmChartVersion,
260215
}
261216
deployment := renderDeploymentManifest(t, options)
262217
d := defaultDeployment()
263-
264-
d.Spec.Template.Spec.SecurityContext.RunAsNonRoot = ptr.To(false)
218+
d.Spec.Replicas = ptr.To(int32(5))
265219

266220
assert.Equal(t, d, deployment)
267221
}
268222

269-
func TestDeploymentCustomCAcertificates(t *testing.T) {
223+
func TestDeploymentSecurityContext(t *testing.T) {
270224
options := &helm.Options{
271225
Version: helmChartVersion,
272226
SetValues: map[string]string{
273-
"customCAcertificates": "SGVsbG8gV29ybGQ=",
227+
"securityContext.runAsNonRoot": "false",
274228
},
275229
}
276230
deployment := renderDeploymentManifest(t, options)
277231
d := defaultDeployment()
278232

279-
d.Spec.Template.Spec.Volumes = []corev1.Volume{
280-
{
281-
Name: "ca-certificates",
282-
VolumeSource: corev1.VolumeSource{
283-
ConfigMap: &corev1.ConfigMapVolumeSource{
284-
LocalObjectReference: corev1.LocalObjectReference{
285-
Name: fmt.Sprintf("%s-ca-certificates", helmReleaseName),
286-
},
287-
},
288-
},
289-
},
290-
}
291-
d.Spec.Template.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{
292-
{
293-
Name: "ca-certificates",
294-
ReadOnly: true,
295-
MountPath: "/etc/ssl/certs/custom-ca-certificates.crt",
296-
SubPath: "ca-certificates",
297-
},
298-
}
233+
d.Spec.Template.Spec.SecurityContext.RunAsNonRoot = ptr.To(false)
299234

300235
assert.Equal(t, d, deployment)
301236
}
302237

303-
func TestDeploymentControllers(t *testing.T) {
238+
func TestDeploymentPriorityClassName(t *testing.T) {
239+
priorityClassName := "high-priority"
304240
options := &helm.Options{
305-
Version: helmChartVersion,
306241
SetValues: map[string]string{
307-
"controllers.agentPool.workers": "5",
308-
"controllers.agentPool.syncPeriod": "15m",
309-
"controllers.module.workers": "5",
310-
"controllers.module.syncPeriod": "15m",
311-
"controllers.project.workers": "5",
312-
"controllers.project.syncPeriod": "15m",
313-
"controllers.workspace.workers": "5",
314-
"controllers.workspace.syncPeriod": "15m",
242+
"priorityClassName": priorityClassName,
315243
},
244+
Version: helmChartVersion,
316245
}
317246
deployment := renderDeploymentManifest(t, options)
318247
d := defaultDeployment()
319-
320-
d.Spec.Template.Spec.Containers[0].Args = []string{
321-
"--sync-period=1h",
322-
"--agent-pool-workers=5",
323-
"--agent-pool-sync-period=15m",
324-
"--module-workers=5",
325-
"--module-sync-period=15m",
326-
"--project-workers=5",
327-
"--project-sync-period=15m",
328-
"--workspace-workers=5",
329-
"--workspace-sync-period=15m",
330-
}
248+
d.Spec.Template.Spec.PriorityClassName = priorityClassName
331249

332250
assert.Equal(t, d, deployment)
333251
}
@@ -470,5 +388,94 @@ func TestDeploymentOperatorSkipTLSVerify(t *testing.T) {
470388
assert.Equal(t, d, deployment)
471389
}
472390

391+
// KUBERBAC PROXY >>>
392+
// KUBERBAC PROXY <<<
393+
394+
func TestDeploymentControllers(t *testing.T) {
395+
options := &helm.Options{
396+
Version: helmChartVersion,
397+
SetValues: map[string]string{
398+
"controllers.agentPool.workers": "5",
399+
"controllers.agentPool.syncPeriod": "15m",
400+
"controllers.module.workers": "5",
401+
"controllers.module.syncPeriod": "15m",
402+
"controllers.project.workers": "5",
403+
"controllers.project.syncPeriod": "15m",
404+
"controllers.workspace.workers": "5",
405+
"controllers.workspace.syncPeriod": "15m",
406+
},
407+
}
408+
deployment := renderDeploymentManifest(t, options)
409+
d := defaultDeployment()
410+
411+
d.Spec.Template.Spec.Containers[0].Args = []string{
412+
"--sync-period=1h",
413+
"--agent-pool-workers=5",
414+
"--agent-pool-sync-period=15m",
415+
"--module-workers=5",
416+
"--module-sync-period=15m",
417+
"--project-workers=5",
418+
"--project-sync-period=15m",
419+
"--workspace-workers=5",
420+
"--workspace-sync-period=15m",
421+
}
422+
423+
assert.Equal(t, d, deployment)
424+
}
425+
426+
func TestDeploymentCustomCAcertificates(t *testing.T) {
427+
options := &helm.Options{
428+
Version: helmChartVersion,
429+
SetValues: map[string]string{
430+
"customCAcertificates": "SGVsbG8gV29ybGQ=",
431+
},
432+
}
433+
deployment := renderDeploymentManifest(t, options)
434+
d := defaultDeployment()
435+
436+
d.Spec.Template.Spec.Volumes = []corev1.Volume{
437+
{
438+
Name: "ca-certificates",
439+
VolumeSource: corev1.VolumeSource{
440+
ConfigMap: &corev1.ConfigMapVolumeSource{
441+
LocalObjectReference: corev1.LocalObjectReference{
442+
Name: fmt.Sprintf("%s-ca-certificates", helmReleaseName),
443+
},
444+
},
445+
},
446+
},
447+
}
448+
d.Spec.Template.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{
449+
{
450+
Name: "ca-certificates",
451+
ReadOnly: true,
452+
MountPath: "/etc/ssl/certs/custom-ca-certificates.crt",
453+
SubPath: "ca-certificates",
454+
},
455+
}
456+
457+
assert.Equal(t, d, deployment)
458+
}
459+
460+
func TestDeploymentServiceAccountName(t *testing.T) {
461+
serviceAccountName := "this"
462+
options := &helm.Options{
463+
SetValues: map[string]string{
464+
"serviceAccount.create": "true",
465+
"serviceAccount.name": serviceAccountName,
466+
},
467+
Version: helmChartVersion,
468+
}
469+
deployment := renderDeploymentManifest(t, options)
470+
d := defaultDeployment()
471+
d.Spec.Template.Spec.ServiceAccountName = serviceAccountName
472+
473+
assert.Equal(t, d, deployment)
474+
}
475+
476+
// RBAC >>>
477+
// RBAC <<<
478+
473479
// TODO:
474480
// - kubeRbacProxy.*
481+
// - RBAC

charts/test/unit/helpers.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ package unit
55

66
import (
77
"fmt"
8+
"log"
9+
"os"
810
"testing"
911

1012
"github.com/gruntwork-io/terratest/modules/helm"
1113
"github.com/stretchr/testify/assert"
14+
"gopkg.in/yaml.v2"
1215
appsv1 "k8s.io/api/apps/v1"
1316
corev1 "k8s.io/api/core/v1"
1417
rbacv1 "k8s.io/api/rbac/v1"
1518
)
1619

1720
const (
1821
// Helm chart.
19-
helmChartName = "hcp-terraform-operator"
20-
helmChartVersion = "2.7.0"
21-
helmChartPath = "../../hcp-terraform-operator"
22-
helmReleaseName = "this"
22+
helmChartName = "hcp-terraform-operator"
23+
helmChartPath = "../../hcp-terraform-operator"
24+
helmReleaseName = "this"
2325

2426
// Defaults.
2527
defaultNamespace = "default"
@@ -37,6 +39,10 @@ const (
3739
)
3840

3941
var (
42+
// Generic variables.
43+
helmChartVersion = "0.0.0"
44+
45+
// Deployment variables.
4046
defaultDeploymentName = fmt.Sprintf("%s-%s", helmReleaseName, helmChartName)
4147
defaultDeploymentLabels = map[string]string{
4248
"helm.sh/chart": fmt.Sprintf("%s-%s", helmChartName, helmChartVersion),
@@ -59,6 +65,7 @@ var (
5965
defaultDeploymentTemplateVolumeName = "manager-config"
6066
defaultDeploymentTemplateVolumeConfigMapName = fmt.Sprintf("%s-manager-config", helmReleaseName)
6167

68+
// RBAC variables.
6269
defaultRBACRoleName = fmt.Sprintf("%s-leader-election-role", helmReleaseName)
6370
defaultRBACRoleBindingName = fmt.Sprintf("%s-leader-election-rolebinding", helmReleaseName)
6471

@@ -68,6 +75,7 @@ var (
6875
defaultRBACClusterRoleBindingManagerName = fmt.Sprintf("%s-manager-rolebinding", helmReleaseName)
6976
defaultRBACClusterRoleBindingProxyName = fmt.Sprintf("%s-proxy-rolebinding", helmReleaseName)
7077

78+
// Service account variables.
7179
defaultServiceAccountName = fmt.Sprintf("%s-%s", helmReleaseName, helmChartName)
7280
defaultServiceAccountLabels = map[string]string{
7381
"helm.sh/chart": fmt.Sprintf("%s-%s", helmChartName, helmChartVersion),
@@ -78,6 +86,40 @@ var (
7886
}
7987
)
8088

89+
func init() {
90+
var err error
91+
helmChartVersion, err = getChartVersion()
92+
fmt.Println("INIT:", helmChartVersion)
93+
if err != nil {
94+
log.Fatal(err)
95+
os.Exit(1)
96+
}
97+
98+
defaultDeploymentLabels["helm.sh/chart"] = fmt.Sprintf("%s-%s", helmChartName, helmChartVersion)
99+
defaultDeploymentLabels["app.kubernetes.io/version"] = helmChartVersion
100+
101+
defaultServiceAccountLabels["helm.sh/chart"] = fmt.Sprintf("%s-%s", helmChartName, helmChartVersion)
102+
defaultServiceAccountLabels["app.kubernetes.io/version"] = helmChartVersion
103+
}
104+
105+
type Chart struct {
106+
Version string `yaml:"version"`
107+
}
108+
109+
func getChartVersion() (string, error) {
110+
file, err := os.ReadFile(fmt.Sprintf("%s/Chart.yaml", helmChartPath))
111+
if err != nil {
112+
log.Fatalf("Error reading Chart.yaml: %v", err)
113+
}
114+
115+
var chart Chart
116+
if err := yaml.Unmarshal(file, &chart); err != nil {
117+
return "", fmt.Errorf("Error unmarshalling YAML: %v", err)
118+
}
119+
120+
return chart.Version, nil
121+
}
122+
81123
func renderDeploymentManifest(t *testing.T, options *helm.Options) appsv1.Deployment {
82124
output, err := helm.RenderTemplateE(t, options, helmChartPath, helmReleaseName, []string{deploymentTemplate})
83125
assert.NoError(t, err)

0 commit comments

Comments
 (0)