Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart: Add unit tests for limits and resources #15621

Merged
merged 3 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions chart/tests/test_flower.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,27 @@ def test_should_create_valid_affinity_tolerations_and_node_selector(self):
"spec.template.spec.tolerations[0].key",
docs[0],
)

def test_flower_resources_are_configurable(self):
docs = render_chart(
values={
"flower": {
"resources": {
"limits": {"cpu": "200m", 'memory': "128Mi"},
"requests": {"cpu": "300m", 'memory': "169Mi"},
}
},
},
show_only=["templates/flower/flower-deployment.yaml"],
)
assert "128Mi" == jmespath.search("spec.template.spec.containers[0].resources.limits.memory", docs[0])
assert "169Mi" == jmespath.search(
"spec.template.spec.containers[0].resources.requests.memory", docs[0]
)
assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0])

def test_flower_resources_are_not_added_by_default(self):
docs = render_chart(
show_only=["templates/flower/flower-deployment.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {}
6 changes: 6 additions & 0 deletions chart/tests/test_kerberos.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ def test_kerberos_sidecar_resources(self):
)
assert jmespath.search("spec.template.spec.containers[2].resources.limits.cpu", docs[0]) == "201m"
assert jmespath.search("spec.template.spec.containers[2].resources.limits.memory", docs[0]) == "201Mi"

def test_keberos_sidecar_resources_are_not_added_by_default(self):
docs = render_chart(
show_only=["templates/workers/worker-deployment.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {}
36 changes: 36 additions & 0 deletions chart/tests/test_limit_ranges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import unittest

import jmespath

from chart.tests.helm_template_generator import render_chart


class LimitRangesTest(unittest.TestCase):
def test_limit_ranges_template(self):
docs = render_chart(
values={"limits": [{"max": {"cpu": "500m"}, "min": {"min": "200m"}, "type": "Container"}]},
show_only=["templates/limitrange.yaml"],
)
assert "LimitRange" == jmespath.search("kind", docs[0])
assert "500m" == jmespath.search("spec.limits[0].max.cpu", docs[0])

def test_limit_ranges_are_not_added_by_default(self):
docs = render_chart(show_only=["templates/limitrange.yaml"])
assert docs == []
28 changes: 28 additions & 0 deletions chart/tests/test_pgbouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,31 @@ def test_existing_secret(self):
"name": "pgbouncer-config",
"secret": {"secretName": "pgbouncer-config-secret"},
} == jmespath.search("spec.template.spec.volumes[0]", docs[0])

def test_pgbouncer_resources_are_configurable(self):
docs = render_chart(
values={
"pgbouncer": {
"enabled": True,
"resources": {
"limits": {"cpu": "200m", 'memory': "128Mi"},
"requests": {"cpu": "300m", 'memory': "169Mi"},
},
},
},
show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
)
assert "128Mi" == jmespath.search("spec.template.spec.containers[0].resources.limits.memory", docs[0])
assert "169Mi" == jmespath.search(
"spec.template.spec.containers[0].resources.requests.memory", docs[0]
)
assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0])

def test_pgbouncer_resources_are_not_added_by_default(self):
docs = render_chart(
values={
"pgbouncer": {"enabled": True},
},
show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {}
24 changes: 24 additions & 0 deletions chart/tests/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,27 @@ def test_should_create_valid_affinity_tolerations_and_node_selector(self):
"spec.template.spec.tolerations[0].key",
docs[0],
)

def test_redis_resources_are_configurable(self):
docs = render_chart(
values={
"redis": {
"resources": {
"limits": {"cpu": "200m", 'memory': "128Mi"},
"requests": {"cpu": "300m", 'memory': "169Mi"},
}
},
},
show_only=["templates/redis/redis-statefulset.yaml"],
)
assert "128Mi" == jmespath.search("spec.template.spec.containers[0].resources.limits.memory", docs[0])
assert "169Mi" == jmespath.search(
"spec.template.spec.containers[0].resources.requests.memory", docs[0]
)
assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0])

def test_redis_resources_are_not_added_by_default(self):
docs = render_chart(
show_only=["templates/redis/redis-statefulset.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {}
47 changes: 47 additions & 0 deletions chart/tests/test_resource_quota.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import unittest

import jmespath

from chart.tests.helm_template_generator import render_chart


class ResourceQuotaTest(unittest.TestCase):
def test_resource_quota_template(self):
docs = render_chart(
values={
"quotas": {
"configmaps": "10",
"persistentvolumeclaims": "4",
"pods": "4",
"replicationcontrollers": "20",
"secrets": "10",
"services": "10",
}
},
show_only=["templates/resourcequota.yaml"],
)
assert "ResourceQuota" == jmespath.search("kind", docs[0])
assert "20" == jmespath.search("spec.hard.replicationcontrollers", docs[0])

def test_resource_quota_are_not_added_by_default(self):
docs = render_chart(
show_only=["templates/resourcequota.yaml"],
)
assert docs == []
24 changes: 24 additions & 0 deletions chart/tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,27 @@ def test_logs_persistence_changes_volume(self, log_persistence_values, expected_
assert {"name": "logs", **expected_volume} == jmespath.search(
"spec.template.spec.volumes[1]", docs[0]
)

def test_scheduler_resources_are_configurable(self):
kaxil marked this conversation as resolved.
Show resolved Hide resolved
docs = render_chart(
values={
"scheduler": {
"resources": {
"limits": {"cpu": "200m", 'memory': "128Mi"},
"requests": {"cpu": "300m", 'memory': "169Mi"},
}
},
},
show_only=["templates/scheduler/scheduler-deployment.yaml"],
)
assert "128Mi" == jmespath.search("spec.template.spec.containers[0].resources.limits.memory", docs[0])
assert "169Mi" == jmespath.search(
"spec.template.spec.containers[0].resources.requests.memory", docs[0]
)
assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0])

def test_scheduler_resources_are_not_added_by_default(self):
docs = render_chart(
show_only=["templates/scheduler/scheduler-deployment.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {}
24 changes: 24 additions & 0 deletions chart/tests/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,27 @@ def test_should_create_valid_affinity_tolerations_and_node_selector(self):
"spec.template.spec.tolerations[0].key",
docs[0],
)

def test_stastd_resources_are_configurable(self):
docs = render_chart(
values={
"statsd": {
"resources": {
"limits": {"cpu": "200m", 'memory': "128Mi"},
"requests": {"cpu": "300m", 'memory': "169Mi"},
}
},
},
show_only=["templates/statsd/statsd-deployment.yaml"],
)
assert "128Mi" == jmespath.search("spec.template.spec.containers[0].resources.limits.memory", docs[0])
assert "169Mi" == jmespath.search(
"spec.template.spec.containers[0].resources.requests.memory", docs[0]
)
assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0])

def test_statsd_resources_are_not_added_by_default(self):
docs = render_chart(
show_only=["templates/statsd/statsd-deployment.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {}
24 changes: 24 additions & 0 deletions chart/tests/test_webserver_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,27 @@ def test_logs_persistence_adds_volume_and_mount(self, log_persistence_values, ex
assert "logs" not in [
v["name"] for v in jmespath.search("spec.template.spec.containers[0].volumeMounts", docs[0])
]

def test_webserver_resources_are_configurable(self):
docs = render_chart(
values={
"webserver": {
"resources": {
"limits": {"cpu": "200m", 'memory': "128Mi"},
"requests": {"cpu": "300m", 'memory': "169Mi"},
}
},
},
show_only=["templates/webserver/webserver-deployment.yaml"],
)
assert "128Mi" == jmespath.search("spec.template.spec.containers[0].resources.limits.memory", docs[0])
assert "169Mi" == jmespath.search(
"spec.template.spec.containers[0].resources.requests.memory", docs[0]
)
assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0])

def test_webserver_resources_are_not_added_by_default(self):
docs = render_chart(
show_only=["templates/webserver/webserver-deployment.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {}
24 changes: 24 additions & 0 deletions chart/tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,27 @@ def test_logs_persistence_changes_volume(self, log_persistence_values, expected_
assert {"name": "logs", **expected_volume} == jmespath.search(
"spec.template.spec.volumes[1]", docs[0]
)

def test_worker_resources_are_configurable(self):
docs = render_chart(
values={
"workers": {
"resources": {
"limits": {"cpu": "200m", 'memory': "128Mi"},
"requests": {"cpu": "300m", 'memory': "169Mi"},
}
},
},
show_only=["templates/workers/worker-deployment.yaml"],
)
assert "128Mi" == jmespath.search("spec.template.spec.containers[0].resources.limits.memory", docs[0])
assert "169Mi" == jmespath.search(
"spec.template.spec.containers[0].resources.requests.memory", docs[0]
)
assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0])

def test_worker_resources_are_not_added_by_default(self):
docs = render_chart(
show_only=["templates/workers/worker-deployment.yaml"],
)
assert jmespath.search("spec.template.spec.containers[0].resources", docs[0]) == {}