Merge ~sajoupa/charm-k8s-telegraf:func_tests into charm-k8s-telegraf:master

Proposed by Laurent Sesquès
Status: Merged
Approved by: Laurent Sesquès
Approved revision: 50baf3f83a601462b2a05f3b7451fd12be557712
Merged at revision: f45cd5af2ded313a2eefd6543fa39dd5c04fce23
Proposed branch: ~sajoupa/charm-k8s-telegraf:func_tests
Merge into: charm-k8s-telegraf:master
Prerequisite: ~sajoupa/charm-k8s-telegraf:image-dockerhub
Diff against target: 216 lines (+119/-14)
8 files modified
.gitignore (+3/-0)
Makefile (+21/-7)
tests/functional/requirements.txt (+1/-0)
tests/functional/tests/bundles/base.yaml (+4/-0)
tests/functional/tests/bundles/overlays/local-charm-overlay.yaml.j2 (+3/-0)
tests/functional/tests/test_telegraf.py (+63/-0)
tests/functional/tests/tests.yaml (+8/-0)
tox.ini (+16/-7)
Reviewer Review Type Date Requested Status
Benjamin Allot Approve
John Lenton (community) Approve
Canonical IS Reviewers Pending
Review via email: mp+393340@code.launchpad.net

Commit message

Add functional tests

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.

Revision history for this message
John Lenton (chipaca) wrote :

Lovely, thank you!

review: Approve
Revision history for this message
Benjamin Allot (ballot) wrote :

Few comments, more for clarity than anything else !

Revision history for this message
Laurent Sesquès (sajoupa) wrote :

> Few comments, more for clarity than anything else !

Updated, except for the regex part, will reply inline.

Revision history for this message
Laurent Sesquès (sajoupa) wrote :

Replied to inline comments.

Revision history for this message
Benjamin Allot (ballot) wrote :

Small one I missed on Makefile, you can merge without it IMO, given our Makefile usage, but if it's a copy/paste from another, better to have the best approach.

Ok for the regexp if it takes milliseconds.

Bottom approved, feel free to fix the makefile or merge as is.

review: Approve
Revision history for this message
Benjamin Allot (ballot) :
Revision history for this message
Benjamin Allot (ballot) wrote :
Download full text (4.2 KiB)

In fact, I have the following error when I try to run the functional tests:

Executing functional tests in /home/ballot/charms/builds
functional create: /home/ballot/repos/charms/charm-k8s-telegraf/.tox/functional
functional installdeps: -r/home/ballot/repos/charms/charm-k8s-telegraf/tests/functional/requirements.txt
functional installed: appdirs==1.4.3,async-generator==1.10,bcrypt==3.2.0,CacheControl==0.12.6,certifi==2019.11.28,cffi==1.14.3,chardet==3.0.4,colorama==0.4.3,contextlib2==0.6.0,cryptography==3.2.1,debtcollector==2.2.0,distlib==0.3.0,distro==1.4.0,html5lib==1.0.1,hvac==0.6.4,idna==2.8,ipaddr==2.2.0,Jinja2==2.11.2,juju==2.8.4,juju-wait==2.8.4,jujubundlelib==0.5.6,lockfile==0.12.2,macaroonbakery==1.3.1,MarkupSafe==1.1.1,msgpack==0.6.2,mypy-extensions==0.4.3,netaddr==0.8.0,ops==1.0.1,oslo.config==6.11.2,oslo.i18n==5.0.1,packaging==20.3,paramiko==2.7.2,pbr==5.5.1,pep517==0.8.2,pkg-resources==0.0.0,progress==1.5,protobuf==3.13.0,pyasn1==0.4.8,pycparser==2.20,pymacaroons==0.13.0,PyNaCl==1.4.0,pyparsing==2.4.6,pyRFC3339==1.1,pytoml==0.1.21,pytz==2020.4,PyYAML==5.3.1,requests==2.22.0,retrying==1.3.3,rfc3986==1.4.0,six==1.14.0,stevedore==3.2.2,tenacity==6.2.0,theblues==0.5.2,toposort==1.5,typing-extensions==3.7.4.3,typing-inspect==0.6.0,urllib3==1.25.8,webencodings==0.5.1,websockets==7.0,wrapt==1.12.1,zaza==0.0.2.dev1
functional run-test-pre: PYTHONHASHSEED='3098041088'
functional run-test: commands[0] | functest-run-suite
2020-11-11 23:14:29 [WARNING] unknown delta type: id
2020-11-11 23:14:29 [INFO] Deploying bundle './tests/bundles/base.yaml' on to 'zaza-5567d93822fa' model
2020-11-11 23:14:29 [INFO] Rendered template '<Template 'local-charm-overlay.yaml.j2'>' to file '/tmp/tmp5z33c9e1/local-charm-overlay.yaml'
2020-11-11 23:14:29 [INFO] Deploying overlay '/tmp/tmp5z33c9e1/local-charm-overlay.yaml' on to 'zaza-5567d93822fa' model
2020-11-11 23:14:31 [INFO] ERROR cannot deploy bundle: the provided bundle has the following errors:
2020-11-11 23:14:31 [INFO] charm path in application "telegraf" does not exist: /home/ballot/charms/buildstelegraf.charm
2020-11-11 23:14:31 [ERROR] {'default_alias': 'zaza-5567d93822fa'}
2020-11-11 23:14:31 [ERROR] Model default_alias (zaza-5567d93822fa)
2020-11-11 23:14:31 [WARNING] unknown delta type: id
Traceback (most recent call last):
  File "/home/ballot/repos/charms/charm-k8s-telegraf/.tox/functional/bin/functest-run-suite", line 8, in <module>
    sys.exit(main())
  File "/home/ballot/repos/charms/charm-k8s-telegraf/.tox/functional/lib/python3.8/site-packages/zaza/charm_lifecycle/func_test_runner.py", line 259, in main
    func_test_runner(
  File "/home/ballot/repos/charms/charm-k8s-telegraf/.tox/functional/lib/python3.8/site-packages/zaza/charm_lifecycle/func_test_runner.py", line 200, in func_test_runner
    run_env_deployment(env_deployment, keep_model=preserve_model,
  File "/home/ballot/repos/charms/charm-k8s-telegraf/.tox/functional/lib/python3.8/site-packages/zaza/charm_lifecycle/func_test_runner.py", line 116, in run_env_deployment
    deploy.deploy(
  File "/home/ballot/repos/charms/charm-k8s-telegraf/.tox/functional/lib/python3.8/site-packages/zaza/charm_lifecycle/deploy.py", line 364, in de...

Read more...

review: Needs Fixing
Revision history for this message
Benjamin Allot (ballot) wrote :

LGTM, tested and working on my microk8s !

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision f45cd5af2ded313a2eefd6543fa39dd5c04fce23

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/.gitignore b/.gitignore
2index 490cc43..ae614f7 100644
3--- a/.gitignore
4+++ b/.gitignore
5@@ -4,3 +4,6 @@
6 .coverage
7 __pycache__
8 build
9+
10+# version data
11+repo-info
12diff --git a/Makefile b/Makefile
13index f24682f..7a71e0f 100644
14--- a/Makefile
15+++ b/Makefile
16@@ -1,5 +1,10 @@
17 VERSION_TO_BUILD ?= latest
18
19+PROJECTPATH=$(dir $(realpath $(lastword $(MAKEFILE_LIST))))
20+CHARM_BUILD_DIR ?= ${PROJECTPATH:%/=%}
21+METADATA_FILE="metadata.yaml"
22+CHARM_NAME=$(shell cat ${PROJECTPATH}/${METADATA_FILE} | awk '/^name:/ {print $$2".charm"}')
23+
24 blacken:
25 @echo "Normalising python layout with black."
26 @tox -e black
27@@ -11,17 +16,26 @@ lint: blacken
28
29 # We actually use the build directory created by charmcraft,
30 # but the .charm file makes a much more convenient sentinel.
31-unittest: telegraf.charm
32+unittests:
33 @tox -e unit
34
35-test: lint unittest
36+build:
37+ @echo "Building charm to ${CHARM_BUILD_DIR}/${CHARM_NAME}"
38+ @-git rev-parse HEAD > ./repo-info
39+ @cd ${CHARM_BUILD_DIR} && TERM=linux charmcraft build -f ${PROJECTPATH}
40+
41+test: lint unittests functional
42+ @echo "Tests completed for charm ${CHARM_NAME}."
43+
44+functional: build
45+ @echo "Executing functional tests in ${CHARM_BUILD_DIR}"
46+ @CHARM_BUILD_DIR=${CHARM_BUILD_DIR} tox -e functional
47
48 clean:
49 @echo "Cleaning files"
50- @git clean -fXd
51-
52-telegraf.charm: src/*.py requirements.txt
53- charmcraft build
54+ @git clean -fXd -e '!.idea'
55+ @echo "Cleaning existing build"
56+ @rm -rf ${CHARM_BUILD_DIR}/${CHARM_NAME}
57
58 image-build:
59 @echo "Building the image."
60@@ -31,4 +45,4 @@ image-build:
61 -t telegraf:$(VERSION_TO_BUILD) \
62 .
63
64-.PHONY: blacken lint unittest test clean image-build
65+.PHONY: blacken lint unittests test clean image-build build
66diff --git a/tests/functional/requirements.txt b/tests/functional/requirements.txt
67new file mode 100644
68index 0000000..b7c9112
69--- /dev/null
70+++ b/tests/functional/requirements.txt
71@@ -0,0 +1 @@
72+git+https://github.com/openstack-charmers/zaza.git#egg=zaza
73diff --git a/tests/functional/tests/bundles/base.yaml b/tests/functional/tests/bundles/base.yaml
74new file mode 100644
75index 0000000..f12ac73
76--- /dev/null
77+++ b/tests/functional/tests/bundles/base.yaml
78@@ -0,0 +1,4 @@
79+bundle: kubernetes
80+applications:
81+ telegraf:
82+ scale: 1
83diff --git a/tests/functional/tests/bundles/overlays/local-charm-overlay.yaml.j2 b/tests/functional/tests/bundles/overlays/local-charm-overlay.yaml.j2
84new file mode 100644
85index 0000000..d19c84a
86--- /dev/null
87+++ b/tests/functional/tests/bundles/overlays/local-charm-overlay.yaml.j2
88@@ -0,0 +1,3 @@
89+applications:
90+ {{ charm_name }}:
91+ charm: "{{ CHARM_BUILD_DIR }}/{{ charm_name }}.charm"
92diff --git a/tests/functional/tests/test_telegraf.py b/tests/functional/tests/test_telegraf.py
93new file mode 100644
94index 0000000..523067e
95--- /dev/null
96+++ b/tests/functional/tests/test_telegraf.py
97@@ -0,0 +1,63 @@
98+import logging
99+import requests
100+import re
101+import time
102+import unittest
103+
104+from zaza import model
105+
106+DEFAULT_HTTPGET_TIMEOUT = 10
107+DEFAULT_RETRIES = 12
108+DEFAULT_TELEGRAF_EXPORTER_PORT = "9103"
109+
110+
111+class BaseTelegrafTest(unittest.TestCase):
112+ """Base for Telegraf charm tests."""
113+
114+ @classmethod
115+ def setUpClass(cls):
116+ super(BaseTelegrafTest, cls).setUpClass()
117+ cls.model_name = model.get_juju_model()
118+ cls.application_name = "telegraf"
119+ cls.lead_unit_name = model.get_lead_unit_name(cls.application_name, model_name=cls.model_name)
120+ logging.debug("Leader unit is {}".format(cls.lead_unit_name))
121+
122+
123+class TestTelegraf(BaseTelegrafTest):
124+ def check_re_pattern(self, re_pattern, text):
125+ logging.info("checking metrics %s", re_pattern)
126+ # findall returns a list, [] when no match
127+ self.assertTrue(re.findall(re_pattern, text, flags=re.M))
128+
129+ def test_01_service(self):
130+ status = model.get_status()
131+ ip = status['applications']['telegraf']['public-address']
132+ url = "http://{}:{}/metrics".format(ip, DEFAULT_TELEGRAF_EXPORTER_PORT)
133+ # zaza.model.block_until_all_units_idle doesn't wait long enough in k8s models
134+ # https://github.com/openstack-charmers/zaza/issues/396
135+ time.sleep(15)
136+
137+ re_patterns = [
138+ r"^internal_agent_",
139+ r"^internal_memstats_",
140+ ]
141+
142+ for retry in range(DEFAULT_RETRIES):
143+ resp = requests.get(url, timeout=DEFAULT_HTTPGET_TIMEOUT)
144+ self.assertEqual(resp.status_code, 200)
145+
146+ logging.info("test_service: endpoint {}:{} is reachable".format(ip, DEFAULT_TELEGRAF_EXPORTER_PORT))
147+
148+ resp = requests.get(url)
149+ self.assertTrue(resp.ok)
150+ text = resp.text
151+
152+ # check that metrics are available
153+ for re_pattern in re_patterns:
154+ self.check_re_pattern(re_pattern, text)
155+
156+ break
157+ else:
158+ msg = "test_service: endpoint {}:{} is not reachable".format(ip, DEFAULT_TELEGRAF_EXPORTER_PORT)
159+ logging.error(msg)
160+ self.fail(msg)
161diff --git a/tests/functional/tests/tests.yaml b/tests/functional/tests/tests.yaml
162new file mode 100644
163index 0000000..5b7d050
164--- /dev/null
165+++ b/tests/functional/tests/tests.yaml
166@@ -0,0 +1,8 @@
167+charm_name: telegraf
168+gate_bundles:
169+ - base
170+target_deploy_status:
171+ telegraf:
172+ workload-status-message: ''
173+tests:
174+ - tests.test_telegraf.TestTelegraf
175diff --git a/tox.ini b/tox.ini
176index 48bf1ab..8935a0b 100644
177--- a/tox.ini
178+++ b/tox.ini
179@@ -7,6 +7,20 @@ skip_missing_interpreters = True
180 basepython = python3
181 setenv =
182 PYTHONPATH = {toxinidir}/build/lib:{toxinidir}/build/venv
183+passenv =
184+ HOME
185+ PATH
186+ CHARM_BUILD_DIR
187+ PYTEST_KEEP_MODEL
188+ PYTEST_CLOUD_NAME
189+ PYTEST_CLOUD_REGION
190+ PYTEST_MODEL
191+ MODEL_SETTINGS
192+ HTTP_PROXY
193+ HTTPS_PROXY
194+ NO_PROXY
195+ SNAP_HTTP_PROXY
196+ SNAP_HTTPS_PROXY
197
198 [testenv:unit]
199 commands =
200@@ -19,14 +33,9 @@ setenv =
201 TZ=UTC
202
203 [testenv:functional]
204-passenv =
205- HOME
206- JUJU_REPOSITORY
207- PATH
208-commands =
209- pytest -v --ignore mod --ignore {toxinidir}/tests/unit {posargs}
210+changedir = {toxinidir}/tests/functional
211+commands = functest-run-suite {posargs}
212 deps = -r{toxinidir}/tests/functional/requirements.txt
213- -r{toxinidir}/requirements.txt
214
215 [testenv:black]
216 commands = black --skip-string-normalization --line-length=120 src/ tests/

Subscribers

People subscribed via source and target branches