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

« back to all changes in this revision

Viewing changes to tests/test_joyent.py

  • Committer: John George
  • Date: 2015-01-14 22:03:47 UTC
  • mto: This revision was merged to the branch mainline in revision 798.
  • Revision ID: john.george@canonical.com-20150114220347-e8q5wezs1qg9a00u
Added support for setting the juju path, series and agent_url.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from datetime import (
2
 
    datetime,
3
 
    timedelta,
4
 
)
5
 
import json
6
 
from mock import patch
7
 
from unittest import TestCase
8
 
 
9
 
from joyent import (
10
 
    Client,
11
 
    ISO_8601_FORMAT,
12
 
    parse_args,
13
 
)
14
 
 
15
 
 
16
 
def make_machine(state='running', hours=2):
17
 
    then = datetime.utcnow() - timedelta(hours=hours)
18
 
    return {
19
 
        'id': 'id',
20
 
        'state': state,
21
 
        'created': then.strftime(ISO_8601_FORMAT)}
22
 
 
23
 
 
24
 
def fake_list_machines(machine):
25
 
    def list_machines(machine_id=None):
26
 
        if machine_id:
27
 
            return machine
28
 
        else:
29
 
            return [machine]
30
 
    return list_machines
31
 
 
32
 
 
33
 
class JoyentTestCase(TestCase):
34
 
 
35
 
    def test_parse_args(self):
36
 
        args = parse_args(
37
 
            ['-d', '-v', '-u', 'sdc_url', '-a', 'account', '-k', 'key_id',
38
 
             '-p', 'key/path', 'list-machines'])
39
 
        self.assertEqual('sdc_url', args.sdc_url)
40
 
        self.assertEqual('account', args.account)
41
 
        self.assertEqual('key_id', args.key_id)
42
 
        self.assertEqual('key/path', args.key_path)
43
 
        self.assertTrue(args.dry_run)
44
 
        self.assertTrue(args.verbose)
45
 
 
46
 
 
47
 
class ClientTestCase(TestCase):
48
 
 
49
 
    def test_init(self):
50
 
        client = Client(
51
 
            'sdc_url', 'account', 'key_id', './key', 'manta_url',
52
 
            dry_run=True, verbose=True)
53
 
        self.assertEqual('sdc_url', client.sdc_url)
54
 
        self.assertEqual('account', client.account)
55
 
        self.assertEqual('key_id', client.key_id)
56
 
        self.assertEqual('./key', client.key_path)
57
 
        self.assertEqual(3, client.pause)
58
 
        self.assertTrue(client.dry_run)
59
 
        self.assertTrue(client.verbose)
60
 
 
61
 
    def test_list_machine_tags(self):
62
 
        client = Client(
63
 
            'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
64
 
        headers = {}
65
 
        content = json.dumps({'env': 'foo'})
66
 
        with patch.object(client, '_request', autospec=True,
67
 
                          return_value=(headers, content)) as mock:
68
 
            tags = client._list_machine_tags('bar')
69
 
        mock.assert_called_once_with('/machines/bar/tags')
70
 
        self.assertEqual({'env': 'foo'}, tags)
71
 
 
72
 
    def test_delete_old_machines(self):
73
 
        machine = make_machine('stopped')
74
 
        client = Client(
75
 
            'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
76
 
        with patch.object(client, '_list_machines',
77
 
                          side_effect=fake_list_machines(machine)) as lm_mock:
78
 
            with patch.object(client, '_list_machine_tags', autospec=True,
79
 
                              return_value={}) as lmt_mock:
80
 
                with patch.object(client, '_delete_running_machine',
81
 
                                  autospec=True) as drm_mock:
82
 
                    with patch.object(client, 'attempt_deletion',
83
 
                                      autospec=True) as rd_mock:
84
 
                        client.delete_old_machines(1)
85
 
        lm_mock.assert_called_once_with()
86
 
        lmt_mock.assert_called_once_with('id')
87
 
        drm_mock.assert_called_once_with('id')
88
 
        self.assertEqual(0, rd_mock.call_count)
89
 
 
90
 
    def test_delete_old_machines_stuck_provisioning(self):
91
 
        machine = make_machine('provisioning')
92
 
        client = Client(
93
 
            'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
94
 
        with patch.object(client, '_list_machines', autospec=True,
95
 
                          side_effect=fake_list_machines(machine)):
96
 
            with patch.object(client, '_list_machine_tags', autospec=True):
97
 
                with patch.object(client, '_delete_running_machine',
98
 
                                  autospec=True) as drm_mock:
99
 
                    with patch.object(client, 'attempt_deletion',
100
 
                                      autospec=True) as rd_mock:
101
 
                        client.delete_old_machines(1)
102
 
        self.assertEqual(0, drm_mock.call_count)
103
 
        rd_mock.assert_called_once_with([machine])
104
 
 
105
 
    def test_delete_old_machines_permanent(self):
106
 
        machine = make_machine('provisioning')
107
 
        client = Client(
108
 
            'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
109
 
        with patch.object(client, '_list_machines', autospec=True,
110
 
                          side_effect=fake_list_machines(machine)):
111
 
            with patch.object(client, '_list_machine_tags', autospec=True,
112
 
                              return_value={'permanent': 'true'}) as lmt_mock:
113
 
                with patch.object(client, '_delete_running_machine',
114
 
                                  autospec=True) as drm_mock:
115
 
                    with patch.object(client, 'attempt_deletion',
116
 
                                      autospec=True) as rd_mock:
117
 
                        client.delete_old_machines(1)
118
 
        lmt_mock.assert_called_once_with('id')
119
 
        self.assertEqual(0, drm_mock.call_count)
120
 
        self.assertEqual(0, rd_mock.call_count)
121
 
 
122
 
    def test_attempt_deletion(self):
123
 
        client = Client(
124
 
            'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
125
 
        with patch.object(client, 'delete_machine', autospec=True) as dm_func:
126
 
            all_success = client.attempt_deletion(['a', 'b'])
127
 
        self.assertIs(True, all_success)
128
 
        dm_func.assert_any_call('a')
129
 
        dm_func.assert_any_call('b')
130
 
        with patch.object(client, 'delete_machine', autospec=True,
131
 
                          side_effect=[Exception, None]) as dm_func:
132
 
            all_success = client.attempt_deletion(['a', 'b'])
133
 
        self.assertIs(False, all_success)
134
 
        dm_func.assert_any_call('a')
135
 
        dm_func.assert_any_call('b')