~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to tests/test_assess_min_version.py

  • Committer: Aaron Bentley
  • Date: 2015-01-19 15:32:33 UTC
  • mto: This revision was merged to the branch mainline in revision 804.
  • Revision ID: aaron.bentley@canonical.com-20150119153233-jjcvikwiw1dx2lak
Print error on missing environment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Tests for assess_min_version module."""
2
 
 
3
 
import logging
4
 
from mock import (
5
 
    Mock,
6
 
    patch,
7
 
    call,
8
 
)
9
 
import StringIO
10
 
import subprocess
11
 
 
12
 
from assess_min_version import (
13
 
    assess_deploy,
14
 
    assert_fail,
15
 
    assess_min_version,
16
 
    assert_pass,
17
 
    get_current_version,
18
 
    main,
19
 
    parse_args,
20
 
)
21
 
from tests import (
22
 
    parse_error,
23
 
    TestCase,
24
 
)
25
 
from utility import JujuAssertionError
26
 
 
27
 
 
28
 
class TestParseArgs(TestCase):
29
 
 
30
 
    def test_parse_args(self):
31
 
        args = parse_args(["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
32
 
        self.assertEqual("an-env", args.env)
33
 
        self.assertEqual("/bin/juju", args.juju_bin)
34
 
        self.assertEqual("/tmp/logs", args.logs)
35
 
        self.assertEqual("an-env-mod", args.temp_env_name)
36
 
        self.assertEqual(False, args.debug)
37
 
 
38
 
    def test_help(self):
39
 
        fake_stdout = StringIO.StringIO()
40
 
        with parse_error(self) as fake_stderr:
41
 
            with patch("sys.stdout", fake_stdout):
42
 
                parse_args(["--help"])
43
 
        self.assertEqual("", fake_stderr.getvalue())
44
 
 
45
 
 
46
 
class TestMain(TestCase):
47
 
 
48
 
    def test_main(self):
49
 
        argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
50
 
        client = Mock(spec=["is_jes_enabled"])
51
 
        with patch("assess_min_version.configure_logging",
52
 
                   autospec=True) as mock_cl:
53
 
            with patch("assess_min_version.BootstrapManager.booted_context",
54
 
                       autospec=True) as mock_bc:
55
 
                with patch("deploy_stack.client_from_config",
56
 
                           return_value=client) as mock_c:
57
 
                    with patch("assess_min_version.assess_min_version",
58
 
                               autospec=True) as mock_assess:
59
 
                        main(argv)
60
 
        mock_cl.assert_called_once_with(logging.DEBUG)
61
 
        mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False,
62
 
                                       soft_deadline=None)
63
 
        self.assertEqual(mock_bc.call_count, 1)
64
 
        mock_assess.assert_called_once_with(client)
65
 
 
66
 
 
67
 
class TestAssess(TestCase):
68
 
 
69
 
    def test_assert_fail(self):
70
 
        mock_client = Mock(spec=["deploy"])
71
 
        mock_client.deploy.side_effect = subprocess.CalledProcessError('', '')
72
 
        assert_fail(mock_client, "dummpy", "2.0", "2.0", "name")
73
 
 
74
 
    def test_assert_fail_exception(self):
75
 
        mock_client = Mock(spec=["deploy"])
76
 
        with self.assertRaisesRegexp(
77
 
                JujuAssertionError, 'assert_fail failed min: 2.0 cur: 2.0'):
78
 
            assert_fail(mock_client, "dummpy", "2.0", "2.0", "name")
79
 
 
80
 
    def test_assert_pass(self):
81
 
        mock_client = Mock(spec=["deploy", "wait_for_started"])
82
 
        assert_pass(mock_client, "dummpy", "2.0", "2.0", "name")
83
 
 
84
 
    def test_assert_pass_exception(self):
85
 
        mock_client = Mock(spec=["deploy", "wait_for_started"])
86
 
        mock_client.deploy.side_effect = subprocess.CalledProcessError('', '')
87
 
        with self.assertRaisesRegexp(
88
 
                JujuAssertionError, 'assert_pass failed min: 2.0 cur: 2.0'):
89
 
            assert_pass(mock_client, "dummpy", "2.0", "2.0", "name")
90
 
 
91
 
    def test_get_current_version(self):
92
 
        mock_client = Mock(spec=["version"])
93
 
        mock_client.version = '2.0-beta4-trusty-amd64'
94
 
        ver = get_current_version(mock_client)
95
 
        self.assertEqual(ver, '2.0-beta4')
96
 
 
97
 
        mock_client.version = '1.25.4-trusty-amd64'
98
 
        ver = get_current_version(mock_client)
99
 
        self.assertEqual(ver, '1.25.4')
100
 
 
101
 
    def test_assess_deploy(self):
102
 
        mock_client = Mock(spec=["deploy", "wait_for_started"])
103
 
        mock_assertion = Mock(spec=[])
104
 
        with patch("assess_min_version.temp_dir", autospec=True) as mock_td:
105
 
            with patch("assess_min_version.make_minver_charm",
106
 
                       autospec=True) as mock_mc:
107
 
                assess_deploy(
108
 
                    mock_client, mock_assertion, "2.1", "2.0", "dummy")
109
 
        temp_dir = mock_td.return_value.__enter__.return_value
110
 
        mock_assertion.assert_called_once_with(
111
 
            mock_client, temp_dir, "2.1", "2.0", "dummy")
112
 
        mock_mc.assert_called_once_with(temp_dir, "2.1")
113
 
 
114
 
    def test_assess_min_version(self):
115
 
        mock_client = Mock(spec=["juju", "wait_for_started"])
116
 
        with patch("assess_min_version.get_current_version",
117
 
                   autospec=True, return_value="2.0.0") as mock_gcv:
118
 
            with patch("assess_min_version.assess_deploy",
119
 
                       autospec=True) as mock_ad:
120
 
                assess_min_version(mock_client)
121
 
        mock_gcv.assert_called_once_with(mock_client)
122
 
        ad_calls = [
123
 
            call(mock_client, assert_pass, '1.25.0', '2.0.0', 'name1250'),
124
 
            call(mock_client, assert_fail, '99.9.9', '2.0.0', 'name9999'),
125
 
            call(mock_client, assert_fail, '99.9-alpha1', '2.0.0',
126
 
                 'name999alpha1'),
127
 
            call(mock_client, assert_pass, '1.2-beta1', '2.0.0',
128
 
                 'name12beta1'),
129
 
            call(mock_client, assert_pass, '1.25.5.1', '2.0.0', 'name12551'),
130
 
            call(mock_client, assert_pass, '2.0-alpha1', '2.0.0',
131
 
                 'name20alpha1'),
132
 
            call(mock_client, assert_pass, '2.0.0', '2.0.0', 'current')]
133
 
        self.assertEqual(mock_ad.mock_calls, ad_calls)