Abstract

Documentation of the unit tests for this repository.

PostgreSQL Unit Tests#

Tests Usage#

To run these tests you need to install the unittest plugin.

  1. Install the plugin.

    helm plugin install https://github.com/helm-unittest/helm-unittest
    
  2. Run the tests.

    helm unittest -f 'tests/*.yaml' .
    

Service Test#

suite

PostgreSQL Service Test Suite


Tests for the Service template.

tests/service_test_suite.yaml#
suite: PostgreSQL Service Test Suite
release:
  name: postgresql
templates:
- "templates/service.yaml"
values:
- ../values.yaml

tests:
- it: "should render Service with correct metadata"
  asserts:
  - isKind:
      of: Service
  - matchRegex:
      path: metadata.name
      pattern: "^postgresql.*$"
  - equal:
      path: metadata.labels
      value:
        app.kubernetes.io/component: database
        app.kubernetes.io/instance: postgresql
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/name: postgresql
        app.kubernetes.io/version: "16.3"
        helm.sh/chart: postgresql-0.0.3

- it: "should render Service with correct spec configurations"
  asserts:
  - equal:
      path: spec.type
      value: "LoadBalancer"
  - equal:
      path: spec.ports[0].port
      value: 5432
  - equal:
      path: spec.ports[0].targetPort
      value: 5432
  - equal:
      path: spec.ports[0].protocol
      value: "TCP"
  - equal:
      path: spec.ports[0].name
      value: "postgresql"
  - equal:
      path: spec.selector
      value:
        app.kubernetes.io/component: database
        app.kubernetes.io/instance: postgresql
        app.kubernetes.io/name: postgresql

ServiceAccount Test#

suite

ServiceAccount Test Suite


Tests for the PostgreSQL ServiceAccount.

tests/serviceaccount_test_suite.yaml#
# :start-at: "suite: PostgreSQL ServiceAccount Test Suite"
# ```
suite: PostgreSQL ServiceAccount Test Suite
release:
  name: postgresql
templates:
- "templates/serviceaccount.yaml"
values:
- ../values.yaml

tests:
- it: "should render ServiceAccount with correct metadata"
  asserts:
  - isKind:
      of: ServiceAccount
  - equal:
      path: metadata.name
      value: postgresql
  - equal:
      path: metadata.labels
      value:
        app.kubernetes.io/component: database
        app.kubernetes.io/instance: postgresql
        app.kubernetes.io/name: postgresql
  - equal:
      path: metadata.annotations
      value:
        app.kubernetes.io/name: postgresql

- it: "should render ServiceAccount with correct spec configurations"
  asserts:
  - equal:
      path: automountServiceAccountToken
      value: true

StatefulSet Test#

suite

StatefulSet Test


Tests the StatefulSet template values.

tests/statefulset_test.yaml#
# :start-at: "suite: PostgreSQL StatefulSet Test Suite"
# ```
suite: PostgreSQL StatefulSet Test Suite
release:
  name: postgresql
templates:
- "templates/statefulset.yaml"
set:
  type: StatefulSet
values:
- ../values.yaml

tests:
- it: "should render StatefulSet with correct metadata"
  asserts:
  - isKind:
      of: StatefulSet
  - equal:
      path: metadata.name
      value: postgresql

- it: "should render StatefulSet with correct selector and serviceName"
  asserts:
  - equal:
      path: spec.serviceName
      value: postgresql
  - equal:
      path: spec.replicas
      value: 1
  - equal:
      path: spec.selector.matchLabels
      value:
        app.kubernetes.io/component: database
        app.kubernetes.io/instance: postgresql
        app.kubernetes.io/name: postgresql

- it: "should render StatefulSet with correct pod template metadata"
  asserts:
  - equal:
      path: spec.template.metadata.labels
      value:
        app.kubernetes.io/component: database
        app.kubernetes.io/instance: postgresql
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/name: postgresql
        app.kubernetes.io/version: "16.3"
        helm.sh/chart: postgresql-0.0.3
  documentSelector:
    path: kind
    value: StatefulSet

- it: "should render StatefulSet with correct container configurations"
  asserts:
  - equal:
      path: spec.template.spec.containers[0].name
      value: "postgres"
  - equal:
      path: spec.template.spec.containers[0].image
      value: "postgres:16-alpine"
  - equal:
      path: spec.template.spec.containers[0].imagePullPolicy
      value: "IfNotPresent"
  - equal:
      path: spec.template.spec.containers[0].ports[0].containerPort
      value: 5432
  - equal:
      path: spec.template.spec.containers[0].ports[0].name
      value: "postgres"
  - equal:
      path: spec.template.spec.containers[0].env[0].name
      value: "POSTGRES_DB"
  - equal:
      path: spec.template.spec.containers[0].env[0].value
      value: "postgres"
  - equal:
      path: spec.template.spec.containers[0].env[1].name
      value: "POSTGRES_USER"
  - equal:
      path: spec.template.spec.containers[0].env[1].value
      value: "postgres"
  - equal:
      path: spec.template.spec.containers[0].env[2].name
      value: "POSTGRES_PASSWORD"
  - equal:
      path: spec.template.spec.containers[0].env[2].valueFrom.secretKeyRef.name
      value: "postgresql"
  - equal:
      path: spec.template.spec.containers[0].env[2].valueFrom.secretKeyRef.key
      value: "password"
  - equal:
      path: spec.template.spec.containers[0].volumeMounts[0].name
      value: "postgresql"
  - equal:
      path: spec.template.spec.containers[0].volumeMounts[0].mountPath
      value: "/var/lib/postgresql/"

- it: "should render StatefulSet with correct volumeClaimTemplates"
  asserts:
  - equal:
      path: spec.volumeClaimTemplates[0].metadata.name
      value: postgresql
  - equal:
      path: spec.volumeClaimTemplates[0].spec.accessModes[0]
      value: "ReadWriteMany"
  - equal:
      path: spec.volumeClaimTemplates[0].spec.resources.requests.storage
      value: "50Gi"
  - equal:
      path: spec.volumeClaimTemplates[0].spec.storageClassName
      value: "csi-lvm-linear"
  - equal:
      path: spec.volumeClaimTemplates[0].spec.volumeMode
      value: "Filesystem"
  documentSelector:
    path: kind
    value: StatefulSet