~abentley/juju-ci-tools/client-from-config-4

« back to all changes in this revision

Viewing changes to tests/test_assess_model_migration.py

  • Committer: Aaron Bentley
  • Date: 2016-03-18 14:47:06 UTC
  • mto: This revision was merged to the branch mainline in revision 1324.
  • Revision ID: aaron.bentley@canonical.com-20160318144706-z7wy9c21m3psi6g5
Introduce set_model_name, update tests, check controller on bootstrap.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Tests for assess_model_migration module."""
2
 
 
3
 
import argparse
4
 
import logging
5
 
from mock import call, Mock, patch
6
 
import os
7
 
import StringIO
8
 
 
9
 
import assess_model_migration as amm
10
 
from deploy_stack import BootstrapManager
11
 
from tests import (
12
 
    parse_error,
13
 
    TestCase,
14
 
)
15
 
from utility import (
16
 
    temp_dir,
17
 
    until_timeout,
18
 
)
19
 
 
20
 
 
21
 
class TestParseArgs(TestCase):
22
 
 
23
 
    def test_default_args(self):
24
 
        args = amm.parse_args(
25
 
            ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
26
 
        self.assertEqual(
27
 
            args,
28
 
            argparse.Namespace(
29
 
                env="an-env",
30
 
                juju_bin='/bin/juju',
31
 
                logs='/tmp/logs',
32
 
                temp_env_name='an-env-mod',
33
 
                debug=False,
34
 
                agent_stream=None,
35
 
                agent_url=None,
36
 
                bootstrap_host=None,
37
 
                keep_env=False,
38
 
                machine=[],
39
 
                region=None,
40
 
                series=None,
41
 
                upload_tools=False,
42
 
                verbose=20))
43
 
 
44
 
    def test_help(self):
45
 
        fake_stdout = StringIO.StringIO()
46
 
        with parse_error(self) as fake_stderr:
47
 
            with patch("sys.stdout", fake_stdout):
48
 
                amm.parse_args(["--help"])
49
 
        self.assertEqual("", fake_stderr.getvalue())
50
 
        self.assertIn(
51
 
            "Test model migration feature", fake_stdout.getvalue())
52
 
 
53
 
 
54
 
class TestGetBootstrapManagers(TestCase):
55
 
    def test_returns_two_bs_managers(self):
56
 
        ret_bs = [Mock(), Mock()]
57
 
        with temp_dir() as log_dir:
58
 
            args = argparse.Namespace(logs=log_dir)
59
 
            with patch.object(
60
 
                    BootstrapManager, 'from_args', side_effect=ret_bs):
61
 
                bs1, bs2 = amm.get_bootstrap_managers(args)
62
 
                self.assertEqual(bs1, ret_bs[0])
63
 
                self.assertEqual(bs2, ret_bs[1])
64
 
 
65
 
    def test_gives_second_manager_unique_env(self):
66
 
        mock_bs1 = Mock()
67
 
        mock_bs1.temp_env_name = 'testing-env-name'
68
 
        ret_bs = [mock_bs1, Mock()]
69
 
        with temp_dir() as log_dir:
70
 
            args = argparse.Namespace(logs=log_dir)
71
 
            with patch.object(BootstrapManager, 'from_args',
72
 
                              side_effect=ret_bs):
73
 
                bs1, bs2 = amm.get_bootstrap_managers(args)
74
 
                self.assertEqual(bs2.temp_env_name, 'testing-env-name-b')
75
 
 
76
 
    def test_creates_unique_log_dirs(self):
77
 
        ret_bs = [Mock(), Mock()]
78
 
        args = argparse.Namespace(logs='/some/path')
79
 
        with patch.object(BootstrapManager, 'from_args', side_effect=ret_bs):
80
 
            with patch.object(amm, '_new_log_dir') as log_dir:
81
 
                bs1, bs2 = amm.get_bootstrap_managers(args)
82
 
                self.assertEqual(2, log_dir.call_count)
83
 
                self.assertEqual(
84
 
                    log_dir.mock_calls,
85
 
                    [call(args.logs, 'a'), call(args.logs, 'b')])
86
 
 
87
 
 
88
 
class TestNewLogDir(TestCase):
89
 
 
90
 
    def test_returns_created_log_path(self):
91
 
        with temp_dir() as log_dir_path:
92
 
            post_fix = 'testing'
93
 
            expected_path = '{}/env-testing'.format(log_dir_path)
94
 
            log_dir = amm._new_log_dir(log_dir_path, post_fix)
95
 
            self.assertEqual(log_dir, expected_path)
96
 
 
97
 
    def test_creates_new_log_dir(self):
98
 
        with temp_dir() as log_dir_path:
99
 
            post_fix = 'testing'
100
 
            expected_path = '{}/env-testing'.format(log_dir_path)
101
 
            amm._new_log_dir(log_dir_path, post_fix)
102
 
            self.assertTrue(os.path.exists(expected_path))
103
 
 
104
 
 
105
 
class TestWaitForModel(TestCase):
106
 
    # Check that it returns an error if the model never comes up.
107
 
    # Pass in a timeout for the model check
108
 
    def test_raises_exception_when_timeout_occurs(self):
109
 
        with patch.object(until_timeout, 'next', side_effect=StopIteration()):
110
 
            with self.assertRaises(AssertionError):
111
 
                amm.wait_for_model(Mock(), 'TestModelName')
112
 
 
113
 
    def test_returns_when_model_found(self):
114
 
        mock_client = Mock()
115
 
        mock_client.get_models.return_value = dict(
116
 
            models=[
117
 
                dict(name='TestModelName')])
118
 
        amm.wait_for_model(mock_client, 'TestModelName')
119
 
 
120
 
    def test_pauses_between_failed_matches(self):
121
 
        mock_client = Mock()
122
 
        mock_client.get_models.side_effect = [
123
 
            dict(models=[]),    # Failed check
124
 
            dict(models=[dict(name='TestModelName')]),  # Successful check
125
 
            ]
126
 
        with patch.object(amm, 'sleep') as mock_sleep:
127
 
            amm.wait_for_model(mock_client, 'TestModelName')
128
 
            mock_sleep.assert_called_once_with(1)
129
 
 
130
 
 
131
 
class TestMain(TestCase):
132
 
 
133
 
    def test_main(self):
134
 
        argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
135
 
        bs_1 = Mock()
136
 
        bs_2 = Mock()
137
 
        with patch.object(amm, "configure_logging", autospec=True) as mock_cl:
138
 
            with patch.object(amm, "assess_model_migration",
139
 
                              autospec=True) as mock_assess:
140
 
                with patch.object(amm, "get_bootstrap_managers",
141
 
                                  return_value=[bs_1, bs_2]):
142
 
                    amm.main(argv)
143
 
        mock_cl.assert_called_once_with(logging.DEBUG)
144
 
        mock_assess.assert_called_once_with(bs_1, bs_2, False)