Merge lp:~nskaggs/juju-ci-tools/add-assess-terms into lp:juju-ci-tools

Proposed by Nicholas Skaggs on 2017-03-29
Status: Work in progress
Proposed branch: lp:~nskaggs/juju-ci-tools/add-assess-terms
Merge into: lp:juju-ci-tools
Prerequisite: lp:~nskaggs/juju-ci-tools/add-term-charms
Diff against target: 182 lines (+173/-0)
2 files modified
assess_terms.py (+94/-0)
tests/test_assess_terms.py (+79/-0)
To merge this branch: bzr merge lp:~nskaggs/juju-ci-tools/add-assess-terms
Reviewer Review Type Date Requested Status
Juju Release Engineering 2017-03-29 Pending
Review via email: mp+321313@code.launchpad.net

Description of the change

This implements a basic test for various charm types that contain metrics, terms and plans

To post a comment you must log in.

Unmerged revisions

1934. By Nicholas Skaggs on 2017-03-22

working PoC

1933. By Nicholas Skaggs on 2017-03-22

update deploy

1932. By Nicholas Skaggs on 2017-03-10

fix repo

1931. By Nicholas Skaggs on 2017-03-10

fix location

1930. By Nicholas Skaggs on 2017-03-10

Create assess terms

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'assess_terms.py'
2--- assess_terms.py 1970-01-01 00:00:00 +0000
3+++ assess_terms.py 2017-03-29 15:30:53 +0000
4@@ -0,0 +1,94 @@
5+#!/usr/bin/env python
6+"""
7+This test will test a suite of charms that require combinations of terms,
8+metrics, and commerical features.
9+
10+"""
11+from __future__ import print_function
12+
13+import argparse
14+from collections import namedtuple
15+import logging
16+import subprocess
17+import sys
18+
19+from deploy_stack import BootstrapManager
20+from jujucharm import (
21+ local_charm_path,
22+)
23+from utility import (
24+ add_basic_testing_arguments,
25+ configure_logging,
26+ JujuAssertionError,
27+)
28+
29+__metaclass__ = type
30+
31+log = logging.getLogger("assess_terms")
32+
33+
34+Test = namedtuple("Test", ["charm_name", "series", "success"])
35+
36+
37+def assess_terms(client):
38+ """Assess charm combinations
39+
40+ :param client: Juju client.
41+ :type client: jujupy.ModelClient
42+ :return: None
43+ """
44+ tests = [
45+ Test(charm_name="canonical-terms-example",
46+ series="xenial", success=False),
47+ ]
48+ for test in tests:
49+ test_charm = local_charm_path(
50+ charm=test.charm_name, juju_ver=client.version, series=test.series)
51+ log.info(
52+ "Assessing charm: test: {} charm_dir:{}".format(
53+ test, charm_path))
54+ assert_deploy(client, test, test_charm)
55+
56+
57+def assert_deploy(client, test, test_charm):
58+ """Deploy a charm and assert a success or fail.
59+
60+ :param client: Juju client
61+ :type client: jujupy.ModelClient
62+ :param test: Deploy test data.
63+ :type test: Test
64+ :param charm_dir:
65+ :type charm_dir: str
66+ :return: None
67+ """
68+ if test.success:
69+ client.deploy(charm=test_charm, series=test.series)
70+ client.wait_for_started()
71+ else:
72+ try:
73+ client.deploy(charm=test_charm, series=test.series)
74+ except subprocess.CalledProcessError:
75+ return
76+ raise JujuAssertionError('Assert deploy failed for {}'.format(test))
77+
78+
79+def parse_args(argv):
80+ """Parse all arguments."""
81+ parser = argparse.ArgumentParser(
82+ description=("Test charms with combinations of terms, "
83+ "plans and commercial support"))
84+ add_basic_testing_arguments(parser)
85+ return parser.parse_args(argv)
86+
87+
88+def main(argv=None):
89+ args = parse_args(argv)
90+ configure_logging(args.verbose)
91+ bs_manager = BootstrapManager.from_args(args)
92+ with bs_manager.booted_context(args.upload_tools):
93+ assess_terms(bs_manager.client)
94+ return 0
95+
96+
97+if __name__ == '__main__':
98+ sys.exit(main())
99
100=== added file 'tests/test_assess_terms.py'
101--- tests/test_assess_terms.py 1970-01-01 00:00:00 +0000
102+++ tests/test_assess_terms.py 2017-03-29 15:30:53 +0000
103@@ -0,0 +1,79 @@
104+"""Tests for assess_terms module."""
105+
106+import logging
107+import StringIO
108+
109+from mock import (
110+ Mock,
111+ patch,
112+ )
113+
114+from assess_terms import (
115+ assess_terms,
116+ parse_args,
117+ main,
118+ )
119+from fakejuju import fake_juju_client
120+from tests import (
121+ parse_error,
122+ TestCase,
123+ )
124+
125+
126+class TestParseArgs(TestCase):
127+
128+ def test_common_args(self):
129+ args = parse_args(["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
130+ self.assertEqual("an-env", args.env)
131+ self.assertEqual("/bin/juju", args.juju_bin)
132+ self.assertEqual("/tmp/logs", args.logs)
133+ self.assertEqual("an-env-mod", args.temp_env_name)
134+ self.assertEqual(False, args.debug)
135+
136+ def test_help(self):
137+ fake_stdout = StringIO.StringIO()
138+ with parse_error(self) as fake_stderr:
139+ with patch("sys.stdout", fake_stdout):
140+ parse_args(["--help"])
141+ self.assertEqual("", fake_stderr.getvalue())
142+ self.assertNotIn("TODO", fake_stdout.getvalue())
143+
144+
145+class TestMain(TestCase):
146+
147+ def test_main(self):
148+ argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
149+ env = object()
150+ client = Mock(spec=["is_jes_enabled"])
151+ with patch("assess_terms.configure_logging",
152+ autospec=True) as mock_cl:
153+ with patch("assess_terms.BootstrapManager.booted_context",
154+ autospec=True) as mock_bc:
155+ with patch('deploy_stack.client_from_config',
156+ return_value=client) as mock_cfc:
157+ with patch("assess_terms.assess_terms",
158+ autospec=True) as mock_assess:
159+ main(argv)
160+ mock_cl.assert_called_once_with(logging.DEBUG)
161+ mock_cfc.assert_called_once_with('an-env', "/bin/juju", debug=False,
162+ soft_deadline=None)
163+ self.assertEqual(mock_bc.call_count, 1)
164+ mock_assess.assert_called_once_with(client)
165+
166+
167+class TestAssess(TestCase):
168+
169+ def test_terms(self):
170+ # Using fake_client means that deploy and get_status have plausible
171+ # results. Wrapping it in a Mock causes every call to be recorded, and
172+ # allows assertions to be made about calls. Mocks and the fake client
173+ # can also be used separately.
174+ fake_client = Mock(wraps=fake_juju_client())
175+ fake_client.bootstrap()
176+ assess_terms(fake_client)
177+ fake_client.deploy.assert_called_once_with(
178+ 'local:trusty/my-charm')
179+ fake_client.wait_for_started.assert_called_once_with()
180+ self.assertEqual(
181+ 1, fake_client.get_status().get_service_unit_count('my-charm'))
182+ self.assertNotIn("TODO", self.log_stream.getvalue())

Subscribers

People subscribed via source and target branches