Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

[meta] format with black python scripts #475

Merged
merged 4 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[meta] format with black python scripts
  • Loading branch information
jmlrt committed Feb 10, 2020
commit 778aa0915de7e3f69529e5b1da3699dc9c37f33e
258 changes: 155 additions & 103 deletions apm-server/tests/apmserver_test.py
Original file line number Diff line number Diff line change
@@ -1,130 +1,150 @@
import os
import sys
sys.path.insert(1, os.path.join(sys.path[0], '../../helpers'))

sys.path.insert(1, os.path.join(sys.path[0], "../../helpers"))
from helpers import helm_template

project = 'apm-server'
name = 'release-name-' + project
project = "apm-server"
name = "release-name-" + project


def test_defaults():
config = ''
config = ""

r = helm_template(config)
assert name in r['deployment']
assert name in r['service']
assert name in r["deployment"]
assert name in r["service"]

s = r['service'][name]['spec']
assert s['ports'][0]['port'] == 8200
assert s['ports'][0]['name'] == 'http'
assert s['ports'][0]['protocol'] == 'TCP'
assert s['ports'][0]['targetPort'] == 8200
s = r["service"][name]["spec"]
assert s["ports"][0]["port"] == 8200
assert s["ports"][0]["name"] == "http"
assert s["ports"][0]["protocol"] == "TCP"
assert s["ports"][0]["targetPort"] == 8200

c = r['deployment'][name]['spec']['template']['spec']['containers'][0]
assert c['name'] == 'apm-server'
assert c['image'].startswith('docker.elastic.co/apm/apm-server:')
assert c['ports'][0]['containerPort'] == 8200
c = r["deployment"][name]["spec"]["template"]["spec"]["containers"][0]
assert c["name"] == "apm-server"
assert c["image"].startswith("docker.elastic.co/apm/apm-server:")
assert c["ports"][0]["containerPort"] == 8200


def test_adding_a_extra_container():
config = '''
config = """
extraContainers: |
- name: do-something
image: busybox
command: ['do', 'something']
'''
"""
r = helm_template(config)
extraContainer = r['deployment'][name]['spec']['template']['spec']['containers']
assert {'name': 'do-something', 'image': 'busybox', 'command': ['do', 'something'], } in extraContainer
extraContainer = r["deployment"][name]["spec"]["template"]["spec"]["containers"]
assert {
"name": "do-something",
"image": "busybox",
"command": ["do", "something"],
} in extraContainer


def test_adding_a_extra_init_container():
config = '''
config = """
extraInitContainers: |
- name: do-something
image: busybox
command: ['do', 'something']
'''
"""
r = helm_template(config)
extraInitContainer = r['deployment'][name]['spec']['template']['spec']['initContainers']
assert {'name': 'do-something', 'image': 'busybox', 'command': ['do', 'something'], } in extraInitContainer
extraInitContainer = r["deployment"][name]["spec"]["template"]["spec"][
"initContainers"
]
assert {
"name": "do-something",
"image": "busybox",
"command": ["do", "something"],
} in extraInitContainer


def test_adding_envs():
config = '''
config = """
extraEnvs:
- name: LOG_LEVEL
value: DEBUG
'''
"""
r = helm_template(config)
envs = r['deployment'][name]['spec']['template']['spec']['containers'][0]['env']
assert {'name': 'LOG_LEVEL', 'value': 'DEBUG'} in envs
envs = r["deployment"][name]["spec"]["template"]["spec"]["containers"][0]["env"]
assert {"name": "LOG_LEVEL", "value": "DEBUG"} in envs


def test_adding_image_pull_secrets():
config = '''
config = """
imagePullSecrets:
- name: test-registry
'''
"""
r = helm_template(config)
assert r['deployment'][name]['spec']['template']['spec']['imagePullSecrets'][0]['name'] == 'test-registry'
assert (
r["deployment"][name]["spec"]["template"]["spec"]["imagePullSecrets"][0]["name"]
== "test-registry"
)


def test_adding_tolerations():
config = '''
config = """
tolerations:
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoExecute"
tolerationSeconds: 3600
'''
"""
r = helm_template(config)
assert r['deployment'][name]['spec']['template']['spec']['tolerations'][0]['key'] == 'key1'
assert (
r["deployment"][name]["spec"]["template"]["spec"]["tolerations"][0]["key"]
== "key1"
)


def test_override_the_default_update_strategy():
config = '''
config = """
updateStrategy:
type: "OnDelete"
'''
"""

r = helm_template(config)
assert r['deployment'][name]['spec']['strategy']['type'] == 'OnDelete'
assert r["deployment"][name]["spec"]["strategy"]["type"] == "OnDelete"


def test_setting_a_custom_service_account():
config = '''
config = """
serviceAccount: notdefault
'''
"""
r = helm_template(config)
assert r['deployment'][name]['spec']['template']['spec']['serviceAccountName'] == 'notdefault'
assert (
r["deployment"][name]["spec"]["template"]["spec"]["serviceAccountName"]
== "notdefault"
)


def test_self_managing_rbac_resources():
config = '''
config = """
managedServiceAccount: false
'''
"""
r = helm_template(config)
assert 'serviceaccount' not in r
assert 'clusterrole' not in r
assert 'clusterrolebinding' not in r
assert "serviceaccount" not in r
assert "clusterrole" not in r
assert "clusterrolebinding" not in r


def test_setting_pod_security_context():
config = '''
config = """
podSecurityContext:
runAsUser: 1001
privileged: false
'''
"""
r = helm_template(config)
c = r['deployment'][name]['spec']['template']['spec']['containers'][0]
assert c['securityContext']['runAsUser'] == 1001
assert c['securityContext']['privileged'] is False
c = r["deployment"][name]["spec"]["template"]["spec"]["containers"][0]
assert c["securityContext"]["runAsUser"] == 1001
assert c["securityContext"]["privileged"] is False


def test_adding_in_apm_config():
config = '''
config = """
apmConfig:
apm-server.yml: |
key:
Expand All @@ -133,86 +153,112 @@ def test_adding_in_apm_config():

other-config.yml: |
hello = world
'''
"""
r = helm_template(config)
print(r['configmap'].keys())
c = r['configmap'][name + '-config']['data']

assert 'apm-server.yml' in c
assert 'other-config.yml' in c

assert 'nestedkey: value' in c['apm-server.yml']
assert 'dot.notation: test' in c['apm-server.yml']

assert 'hello = world' in c['other-config.yml']

d = r['deployment'][name]['spec']['template']['spec']

assert {'configMap': {'name': name + '-config', 'defaultMode': 0o600}, 'name': project + '-config'} in d['volumes']
assert {'mountPath': '/usr/share/apm-server/apm-server.yml', 'name': project + '-config', 'subPath': 'apm-server.yml', 'readOnly': True} in d['containers'][0]['volumeMounts']
assert {'mountPath': '/usr/share/apm-server/other-config.yml', 'name': project + '-config', 'subPath': 'other-config.yml', 'readOnly': True} in d['containers'][0]['volumeMounts']

assert 'configChecksum' in r['deployment'][name]['spec']['template']['metadata']['annotations'].keys()
print(r["configmap"].keys())
c = r["configmap"][name + "-config"]["data"]

assert "apm-server.yml" in c
assert "other-config.yml" in c

assert "nestedkey: value" in c["apm-server.yml"]
assert "dot.notation: test" in c["apm-server.yml"]

assert "hello = world" in c["other-config.yml"]

d = r["deployment"][name]["spec"]["template"]["spec"]

assert {
"configMap": {"name": name + "-config", "defaultMode": 0o600},
"name": project + "-config",
} in d["volumes"]
assert {
"mountPath": "/usr/share/apm-server/apm-server.yml",
"name": project + "-config",
"subPath": "apm-server.yml",
"readOnly": True,
} in d["containers"][0]["volumeMounts"]
assert {
"mountPath": "/usr/share/apm-server/other-config.yml",
"name": project + "-config",
"subPath": "other-config.yml",
"readOnly": True,
} in d["containers"][0]["volumeMounts"]

assert (
"configChecksum"
in r["deployment"][name]["spec"]["template"]["metadata"]["annotations"].keys()
)


def test_adding_a_secret_mount():
config = '''
config = """
secretMounts:
- name: elastic-certificates
secretName: elastic-certs
path: /usr/share/apm-server/config/certs
'''
"""
r = helm_template(config)
s = r['deployment'][name]['spec']['template']['spec']
assert s['containers'][0]['volumeMounts'][0] == {
'mountPath': '/usr/share/apm-server/config/certs',
'name': 'elastic-certificates'
s = r["deployment"][name]["spec"]["template"]["spec"]
assert s["containers"][0]["volumeMounts"][0] == {
"mountPath": "/usr/share/apm-server/config/certs",
"name": "elastic-certificates",
}
assert s['volumes'][0] == {
'name': 'elastic-certificates',
'secret': {
'secretName': 'elastic-certs'
}
assert s["volumes"][0] == {
"name": "elastic-certificates",
"secret": {"secretName": "elastic-certs"},
}


def test_adding_a_extra_volume_with_volume_mount():
config = '''
config = """
extraVolumes:
- name: extras
emptyDir: {}
extraVolumeMounts:
- name: extras
mountPath: /usr/share/extras
readOnly: true
'''
"""
r = helm_template(config)
extraVolume = r['deployment'][name]['spec']['template']['spec']['volumes']
assert {'name': 'extras', 'emptyDir': {}} in extraVolume
extraVolumeMounts = r['deployment'][name]['spec']['template']['spec']['containers'][0]['volumeMounts']
assert {'name': 'extras', 'mountPath': '/usr/share/extras', 'readOnly': True} in extraVolumeMounts
extraVolume = r["deployment"][name]["spec"]["template"]["spec"]["volumes"]
assert {"name": "extras", "emptyDir": {}} in extraVolume
extraVolumeMounts = r["deployment"][name]["spec"]["template"]["spec"]["containers"][
0
]["volumeMounts"]
assert {
"name": "extras",
"mountPath": "/usr/share/extras",
"readOnly": True,
} in extraVolumeMounts


def test_adding_pod_labels():
config = '''
config = """
labels:
app.kubernetes.io/name: apm-server
'''
"""
r = helm_template(config)
assert r['deployment'][name]['metadata']['labels']['app.kubernetes.io/name'] == 'apm-server'
assert (
r["deployment"][name]["metadata"]["labels"]["app.kubernetes.io/name"]
== "apm-server"
)


def test_adding_a_node_selector():
config = '''
config = """
nodeSelector:
disktype: ssd
'''
"""
r = helm_template(config)
assert r['deployment'][name]['spec']['template']['spec']['nodeSelector']['disktype'] == 'ssd'
assert (
r["deployment"][name]["spec"]["template"]["spec"]["nodeSelector"]["disktype"]
== "ssd"
)


def test_adding_an_affinity_rule():
config = '''
config = """
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
Expand All @@ -223,24 +269,30 @@ def test_adding_an_affinity_rule():
values:
- apm-server
topologyKey: kubernetes.io/hostname
'''
"""

r = helm_template(config)
assert r['deployment'][name]['spec']['template']['spec']['affinity']['podAntiAffinity'][
'requiredDuringSchedulingIgnoredDuringExecution'][0]['topologyKey'] == 'kubernetes.io/hostname'
assert (
r["deployment"][name]["spec"]["template"]["spec"]["affinity"][
"podAntiAffinity"
]["requiredDuringSchedulingIgnoredDuringExecution"][0]["topologyKey"]
== "kubernetes.io/hostname"
)


def test_priority_class_name():
config = '''
config = """
priorityClassName: ""
'''
"""
r = helm_template(config)
spec = r['deployment'][name]['spec']['template']['spec']
assert 'priorityClassName' not in spec
spec = r["deployment"][name]["spec"]["template"]["spec"]
assert "priorityClassName" not in spec

config = '''
config = """
priorityClassName: "highest"
'''
"""
r = helm_template(config)
priority_class_name = r['deployment'][name]['spec']['template']['spec']['priorityClassName']
priority_class_name = r["deployment"][name]["spec"]["template"]["spec"][
"priorityClassName"
]
assert priority_class_name == "highest"
Loading