Skip to content

Commit

Permalink
add-chart-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinBisson committed Sep 4, 2024
1 parent cbac966 commit 654bf08
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .ats/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
app-tests-app-config-file: tests/test-values.yaml
app-tests-deploy-namespace: kube-system

smoke-tests-cluster-type: kind

upgrade-tests-cluster-type: kind
upgrade-tests-app-catalog-url: https://giantswarm.github.io/giantswarm-catalog
upgrade-tests-app-config-file: tests/test-values.yaml

skip-steps: [functional]

# --------
# Uncomment values below this line to be able to execute app-test-suite against an externally created
# Kubernetes cluster.
# Comment out everything above

# If you want to create a kind cluster, use the line below
# kind create cluster --config tests/kind-config.yaml --kubeconfig kube.config
# --------

# app-tests-app-config-file: tests/test-values.yaml
# app-tests-deploy-namespace: loki

# upgrade-tests-app-catalog-url: https://giantswarm.github.io/giantswarm-catalog
# upgrade-tests-app-config-file: tests/test-values.yaml

# skip-steps: [functional, upgrade]

# smoke-tests-cluster-type: external
# functional-tests-cluster-type: external
# upgrade-tests-cluster-type: external
# external-cluster-kubeconfig-path: kube.config
# external-cluster-type: kind
# external-cluster-version: 1.24.0

# app-tests-skip-app-delete: "Yes"
28 changes: 28 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,46 @@ version: 2.1
orbs:
architect: giantswarm/[email protected]

jobs:
template-chart:
docker:
- image: giantswarm/helm-chart-testing:v3.11.0
steps:
- checkout
- run: |-
cd helm/alloy
helm dep up
helm template .
workflows:
package-and-push-chart-on-tag:
jobs:
- template-chart:
name: "template-chart-test"
filters:
branches:
ignore:
- main

- architect/push-to-app-catalog:
context: architect
executor: app-build-suite
name: package-and-push-chart
app_catalog: giantswarm-catalog
app_catalog_test: giantswarm-test-catalog
chart: "alloy"
persist_chart_archive: true
ct_config: ".circleci/ct-config.yaml"
# Trigger job on git tag.
filters:
tags:
only: /^v.*/

- architect/run-tests-with-ats:
name: run-tests-with-ats
requires:
- "package-and-push-chart"
filters:
branches:
ignore:
- main
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add helm chart templating test in ci pipeline.
- Add tests with ats in ci pipeline.

## [0.5.1] - 2024-09-03

### Fixed
Expand Down
12 changes: 12 additions & 0 deletions tests/ats/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pytest-helm-charts = ">=1.0.2"
pytest = ">=6.2.5"
pykube-ng = ">=22.1.0"

[requires]
python_version = "3.9"
359 changes: 359 additions & 0 deletions tests/ats/Pipfile.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/ats/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[pytest]
markers =
smoke
functional
upgrade
flaky
69 changes: 69 additions & 0 deletions tests/ats/test_alloy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import logging
from typing import List, Tuple

import pytest
import pykube
from pytest_helm_charts.clusters import Cluster
from pytest_helm_charts.k8s.stateful_set import wait_for_stateful_sets_to_run


logger = logging.getLogger(__name__)

namespace_name = "kube-system"
statefulset_name = "alloy"

timeout: int = 900

@pytest.mark.smoke
def test_api_working(kube_cluster: Cluster) -> None:
"""Very minimalistic example of using the [kube_cluster](pytest_helm_charts.fixtures.kube_cluster)
fixture to get an instance of [Cluster](pytest_helm_charts.clusters.Cluster) under test
and access its [kube_client](pytest_helm_charts.clusters.Cluster.kube_client) property
to get access to Kubernetes API of cluster under test.
Please refer to [pykube](https://pykube.readthedocs.io/en/latest/api/pykube.html) to get docs
for [HTTPClient](https://pykube.readthedocs.io/en/latest/api/pykube.html#pykube.http.HTTPClient).
"""
assert kube_cluster.kube_client is not None
assert len(pykube.Node.objects(kube_cluster.kube_client)) >= 1

# scope "module" means this is run only once, for the first test case requesting! It might be tricky
# if you want to assert this multiple times
# -- Checking that mimir's statefulset is present on the cluster
@pytest.fixture(scope="module")
def components(kube_cluster: Cluster) -> List[pykube.StatefulSet]:
logger.info("Waiting for alloy statefulset component to be deployed..")

components_ready = wait_for_components(kube_cluster)

logger.info("alloy component are deployed..")

return components_ready

def wait_for_components(kube_cluster: Cluster) -> List[pykube.StatefulSet]:
statefulsets = wait_for_stateful_sets_to_run(
kube_cluster.kube_client,
[statefulset_name],
namespace_name,
timeout,
)
return (statefulsets)

@pytest.fixture(scope="module")
def pods(kube_cluster: Cluster) -> List[pykube.Pod]:
pods = pykube.Pod.objects(kube_cluster.kube_client)

pods = pods.filter(namespace=namespace_name, selector={
'app.kubernetes.io/name': 'alloy', 'app.kubernetes.io/instance': 'alloy'})

return pods

# when we start the tests on circleci, we have to wait for pods to be available, hence
# this additional delay and retries
# -- Checking that all pods from alloy's statefulset are available (i.e in "Ready" state)
@pytest.mark.smoke
@pytest.mark.upgrade
@pytest.mark.flaky(reruns=5, reruns_delay=10)
def test_pods_available(components: List[pykube.StatefulSet]):
# loop over the list of deployments
for d in components:
assert int(d.obj["status"]["readyReplicas"]) == int(d.obj["spec"]["replicas"])
3 changes: 3 additions & 0 deletions tests/test-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alloy:
controller:
type: statefulset

0 comments on commit 654bf08

Please sign in to comment.