1
"""Tests for assess_min_version module."""
12
from assess_min_version import (
25
from utility import JujuAssertionError
28
class TestParseArgs(TestCase):
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)
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())
46
class TestMain(TestCase):
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:
60
mock_cl.assert_called_once_with(logging.DEBUG)
61
mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False,
63
self.assertEqual(mock_bc.call_count, 1)
64
mock_assess.assert_called_once_with(client)
67
class TestAssess(TestCase):
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")
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")
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")
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")
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')
97
mock_client.version = '1.25.4-trusty-amd64'
98
ver = get_current_version(mock_client)
99
self.assertEqual(ver, '1.25.4')
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:
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")
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)
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',
127
call(mock_client, assert_pass, '1.2-beta1', '2.0.0',
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',
132
call(mock_client, assert_pass, '2.0.0', '2.0.0', 'current')]
133
self.assertEqual(mock_ad.mock_calls, ad_calls)