~nskaggs/juju-ci-tools/add-assess-terms

« back to all changes in this revision

Viewing changes to tests/test_assess_terms.py

  • Committer: Nicholas Skaggs
  • Date: 2017-03-10 14:56:28 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20170310145628-phvpse2dq3479siv
Create assess terms

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Tests for assess_terms module."""
 
2
 
 
3
import logging
 
4
import StringIO
 
5
 
 
6
from mock import (
 
7
    Mock,
 
8
    patch,
 
9
    )
 
10
 
 
11
from assess_terms import (
 
12
    assess_terms,
 
13
    parse_args,
 
14
    main,
 
15
    )
 
16
from fakejuju import fake_juju_client
 
17
from tests import (
 
18
    parse_error,
 
19
    TestCase,
 
20
    )
 
21
 
 
22
 
 
23
class TestParseArgs(TestCase):
 
24
 
 
25
    def test_common_args(self):
 
26
        args = parse_args(["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
 
27
        self.assertEqual("an-env", args.env)
 
28
        self.assertEqual("/bin/juju", args.juju_bin)
 
29
        self.assertEqual("/tmp/logs", args.logs)
 
30
        self.assertEqual("an-env-mod", args.temp_env_name)
 
31
        self.assertEqual(False, args.debug)
 
32
 
 
33
    def test_help(self):
 
34
        fake_stdout = StringIO.StringIO()
 
35
        with parse_error(self) as fake_stderr:
 
36
            with patch("sys.stdout", fake_stdout):
 
37
                parse_args(["--help"])
 
38
        self.assertEqual("", fake_stderr.getvalue())
 
39
        self.assertNotIn("TODO", fake_stdout.getvalue())
 
40
 
 
41
 
 
42
class TestMain(TestCase):
 
43
 
 
44
    def test_main(self):
 
45
        argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
 
46
        env = object()
 
47
        client = Mock(spec=["is_jes_enabled"])
 
48
        with patch("assess_terms.configure_logging",
 
49
                   autospec=True) as mock_cl:
 
50
            with patch("assess_terms.BootstrapManager.booted_context",
 
51
                       autospec=True) as mock_bc:
 
52
                with patch('deploy_stack.client_from_config',
 
53
                           return_value=client) as mock_cfc:
 
54
                    with patch("assess_terms.assess_terms",
 
55
                               autospec=True) as mock_assess:
 
56
                        main(argv)
 
57
        mock_cl.assert_called_once_with(logging.DEBUG)
 
58
        mock_cfc.assert_called_once_with('an-env', "/bin/juju", debug=False,
 
59
                                         soft_deadline=None)
 
60
        self.assertEqual(mock_bc.call_count, 1)
 
61
        mock_assess.assert_called_once_with(client)
 
62
 
 
63
 
 
64
class TestAssess(TestCase):
 
65
 
 
66
    def test_terms(self):
 
67
        # Using fake_client means that deploy and get_status have plausible
 
68
        # results.  Wrapping it in a Mock causes every call to be recorded, and
 
69
        # allows assertions to be made about calls.  Mocks and the fake client
 
70
        # can also be used separately.
 
71
        fake_client = Mock(wraps=fake_juju_client())
 
72
        fake_client.bootstrap()
 
73
        assess_terms(fake_client)
 
74
        fake_client.deploy.assert_called_once_with(
 
75
            'local:trusty/my-charm')
 
76
        fake_client.wait_for_started.assert_called_once_with()
 
77
        self.assertEqual(
 
78
            1, fake_client.get_status().get_service_unit_count('my-charm'))
 
79
        self.assertNotIn("TODO", self.log_stream.getvalue())