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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
__metaclass__ = type

from contextlib import contextmanager
from datetime import timedelta
import os
import shutil
import subprocess
import tempfile
from textwrap import dedent
from unittest import TestCase

from mock import (
    MagicMock,
    patch,
)
import yaml

from jujupy import (
    CannotConnectEnv,
    Environment,
    ErroredUnit,
    format_listing,
    JujuClientDevel,
    Status,
    until_timeout,
)


class TestErroredUnit(TestCase):

    def test_output(self):
        e = ErroredUnit('bar', 'baz')
        self.assertEqual('bar is in state baz', str(e))


class TestUntilTimeout(TestCase):

    def test_no_timeout(self):

        iterator = until_timeout(0)

        def now_iter():
            yield iterator.start
            yield iterator.start
            assert False

        with patch.object(iterator, 'now', now_iter().next):
            for x in iterator:
                self.assertIs(None, x)
                break

    @contextmanager
    def patched_until(self, timeout, deltas):
        iterator = until_timeout(timeout)
        def now_iter():
            for d in deltas:
                yield iterator.start + d
            assert False
        with patch.object(iterator, 'now', now_iter().next):
            yield iterator

    def test_timeout(self):
        with self.patched_until(
            5, [timedelta(), timedelta(0, 4), timedelta(0, 5)]) as until:
            results = list(until)
        self.assertEqual([5, 1], results)

    def test_long_timeout(self):
        deltas = [timedelta(), timedelta(4, 0), timedelta(5, 0)]
        with self.patched_until(86400 * 5, deltas) as until:
            self.assertEqual([86400 * 5, 86400], list(until))

class JujuClientDevelFake(JujuClientDevel):

    output_iterator = None

    @classmethod
    def get_juju_output(cls, environment, command, *args):
        return cls.output_iterator.send((environment, command))

    @classmethod
    def set_output(cls, iterator):
        iterator.next()
        cls.output_iterator = iterator


class TestJujuClientDevel(TestCase):

    def test_get_version(self):

        def juju_cmd_iterator():
            params = yield
            self.assertEqual((None, '--version'), params)
            yield ' 5.6 \n'

        JujuClientDevelFake.set_output(juju_cmd_iterator())
        effect = lambda x: ' 5.6 \n'
        with patch('subprocess.check_output', side_effect=effect) as vsn:
            version = JujuClientDevelFake.get_version()
        self.assertEqual('5.6', version)
        vsn.assert_called_with(('juju', '--version'))

    def test_by_version(self):
        def juju_cmd_iterator():
            yield '1.17'
            yield '1.16'
            yield '1.16.1'
            yield '1.15'

        context = patch.object(
            JujuClientDevel, 'get_version',
            side_effect=juju_cmd_iterator().next)
        with context:
            self.assertIs(JujuClientDevel,
                          type(JujuClientDevel.by_version()))
            with self.assertRaisesRegexp(Exception, 'Unsupported juju: 1.16'):
                JujuClientDevel.by_version()
            with self.assertRaisesRegexp(Exception,
                                         'Unsupported juju: 1.16.1'):
                JujuClientDevel.by_version()
            client = JujuClientDevel.by_version()
            self.assertIs(JujuClientDevel, type(client))
            self.assertEqual('1.15', client.version)

    def test_full_args(self):
        env = Environment('foo', '')
        client = JujuClientDevel(None, 'my/juju/bin')
        full = client._full_args(env, 'bar', False, ('baz', 'qux'))
        self.assertEqual(('juju', '--show-log', 'bar', '-e', 'foo', 'baz',
                          'qux'), full)
        full = client._full_args(env, 'bar', True, ('baz', 'qux'))
        self.assertEqual((
            'juju', '--show-log', 'bar', '-e', 'foo',
            'baz', 'qux'), full)
        full = client._full_args(None, 'bar', False, ('baz', 'qux'))
        self.assertEqual(('juju', '--show-log', 'bar', 'baz', 'qux'), full)

    def test_full_args_debug(self):
        env = Environment('foo', '')
        client = JujuClientDevel(None, 'my/juju/bin')
        client.debug = True
        full = client._full_args(env, 'bar', False, ('baz', 'qux'))
        self.assertEqual((
            'juju', '--debug', 'bar', '-e', 'foo', 'baz', 'qux'), full)

    def test_bootstrap_hpcloud(self):
        env = Environment('hp', '')
        with patch.object(env, 'hpcloud', lambda: True):
            with patch.object(JujuClientDevel, 'juju') as mock:
                JujuClientDevel(None, None).bootstrap(env)
            mock.assert_called_with(
                env, 'bootstrap', ('--constraints', 'mem=2G'), False)

    def test_bootstrap_non_sudo(self):
        env = Environment('foo', '')
        with patch.object(env, 'needs_sudo', lambda: False):
            with patch.object(JujuClientDevel, 'juju') as mock:
                JujuClientDevel(None, None).bootstrap(env)
            mock.assert_called_with(
                env, 'bootstrap', ('--constraints', 'mem=2G'), False)

    def test_bootstrap_sudo(self):
        env = Environment('foo', '')
        client = JujuClientDevel(None, None)
        with patch.object(env, 'needs_sudo', lambda: True):
            with patch.object(JujuClientDevel, 'juju') as mock:
                client.bootstrap(env)
            mock.assert_called_with(
                env, 'bootstrap', ('--constraints', 'mem=2G'), True)

    def test_destroy_environment_non_sudo(self):
        env = Environment('foo', '')
        client = JujuClientDevel(None, None)
        with patch.object(env, 'needs_sudo', lambda: False):
            with patch.object(JujuClientDevel, 'juju') as mock:
                client.destroy_environment(env)
            mock.assert_called_with(
                None, 'destroy-environment', ('foo', '--force', '-y'),
                False, check=False)

    def test_destroy_environment_sudo(self):
        env = Environment('foo', '')
        client = JujuClientDevel(None, None)
        with patch.object(env, 'needs_sudo', lambda: True):
            with patch.object(JujuClientDevel, 'juju') as mock:
                client.destroy_environment(env)
            mock.assert_called_with(
                None, 'destroy-environment', ('foo', '--force', '-y'),
                True, check=False)

    def test_get_juju_output(self):
        env = Environment('foo', '')
        asdf = lambda x, stderr: 'asdf'
        client = JujuClientDevel(None, None)
        with patch('subprocess.check_output', side_effect=asdf) as mock:
            result = client.get_juju_output(env, 'bar')
        self.assertEqual('asdf', result)
        self.assertEqual((('juju', '--show-log', 'bar', '-e', 'foo'),),
                         mock.call_args[0])

    def test_get_juju_output_accepts_varargs(self):
        env = Environment('foo', '')
        asdf = lambda x, stderr: 'asdf'
        client = JujuClientDevel(None, None)
        with patch('subprocess.check_output', side_effect=asdf) as mock:
            result = client.get_juju_output(env, 'bar', 'baz', '--qux')
        self.assertEqual('asdf', result)
        self.assertEqual((('juju', '--show-log', 'bar', '-e', 'foo', 'baz',
                           '--qux'),), mock.call_args[0])

    def test_get_juju_output_stderr(self):
        def raise_without_stderr(args, stderr):
            stderr.write('Hello!')
            raise subprocess.CalledProcessError('a', 'b')
        env = Environment('foo', '')
        client = JujuClientDevel(None, None)
        with self.assertRaises(subprocess.CalledProcessError) as exc:
            with patch('subprocess.check_output', raise_without_stderr):
                client.get_juju_output(env, 'bar')
        self.assertEqual(exc.exception.stderr, 'Hello!')

    def test_get_juju_output_accepts_timeout(self):
        env = Environment('foo', '')
        client = JujuClientDevel(None, None)
        with patch('subprocess.check_output') as sco_mock:
            client.get_juju_output(env, 'bar', timeout=5)
        self.assertEqual(sco_mock.call_args[0][0],
            ('timeout', '5.00s', 'juju', '--show-log', 'bar', '-e', 'foo'))

    def test_get_status(self):
        def output_iterator():
            yield
            yield dedent("""\
                - a
                - b
                - c
                """)
        client = JujuClientDevelFake(None, None)
        client.set_output(output_iterator())
        env = Environment('foo', '')
        result = client.get_status(env)
        self.assertEqual(Status, type(result))
        self.assertEqual(['a', 'b', 'c'], result.status)

    def test_get_status_retries_on_error(self):
        client = JujuClientDevelFake(None, None)
        client.attempt = 0
        def get_juju_output(environment, command):
            if client.attempt == 1:
                return '"hello"'
            client.attempt += 1
            raise subprocess.CalledProcessError(1, command)

        env = Environment('foo', '')
        with patch.object(client, 'get_juju_output', get_juju_output):
            client.get_status(env)

    def test_get_status_raises_on_timeout_1(self):
        client = JujuClientDevelFake(None, None)
        def get_juju_output(environment, command):
            raise subprocess.CalledProcessError(1, command)

        env = Environment('foo', '')
        with patch.object(client, 'get_juju_output', get_juju_output):
            with patch('jujupy.until_timeout', lambda x: iter([None, None])):
                with self.assertRaisesRegexp(
                        Exception, 'Timed out waiting for juju status'):
                    client.get_status(env)

    def test_get_status_raises_on_timeout_2(self):
        client = JujuClientDevelFake(None, None)
        env = Environment('foo', '')
        with patch('jujupy.until_timeout', return_value=iter([1])) as mock_ut:
            with self.assertRaises(StopIteration):
                client.get_status(env, 500)
        mock_ut.assert_called_with(500)

    def test_get_env_option(self):
        client = JujuClientDevel(None, None)
        env = Environment('foo', '')
        with patch('subprocess.check_output') as mock:
            mock.return_value = 'https://example.org/juju/tools'
            result = client.get_env_option(env, 'tools-metadata-url')
        self.assertEqual(
            mock.call_args[0][0],
            ('juju', '--show-log', 'get-env', '-e', 'foo',
             'tools-metadata-url'))
        self.assertEqual('https://example.org/juju/tools', result)

    def test_set_env_option(self):
        client = JujuClientDevel(None, None)
        env = Environment('foo', '')
        with patch('subprocess.check_call') as mock:
            client.set_env_option(
                env, 'tools-metadata-url', 'https://example.org/juju/tools')
        mock.assert_called_with(
            ('juju', '--show-log', 'set-env', '-e', 'foo',
             'tools-metadata-url=https://example.org/juju/tools'))

    def test_juju(self):
        env = Environment('qux', '')
        client = JujuClientDevel(None, None)
        with patch('sys.stdout') as stdout_mock:
            with patch('subprocess.check_call') as mock:
                client.juju(env, 'foo', ('bar', 'baz'))
        mock.assert_called_with(('juju', '--show-log', 'foo', '-e', 'qux',
                                 'bar', 'baz'))
        stdout_mock.flush.assert_called_with()

    def test_juju_no_check(self):
        env = Environment('qux', '')
        client = JujuClientDevel(None, None)
        with patch('sys.stdout') as stdout_mock:
            with patch('subprocess.call') as mock:
                client.juju(env, 'foo', ('bar', 'baz'), check=False)
        mock.assert_called_with(('juju', '--show-log', 'foo', '-e', 'qux',
                                 'bar', 'baz'))
        stdout_mock.flush.assert_called_with()


class TestStatus(TestCase):

    def test_agent_items_empty(self):
        status = Status({'machines': {}, 'services': {}})
        self.assertItemsEqual([], status.agent_items())

    def test_agent_items(self):
        status = Status({
            'machines': {
                '1': {'foo': 'bar'}
            },
            'services': {
                'jenkins': {
                    'units': {
                        'jenkins/1': {'baz': 'qux'}
                    }
                }
            }
        })
        expected = [
            ('1', {'foo': 'bar'}), ('jenkins/1', {'baz': 'qux'})]
        self.assertItemsEqual(expected, status.agent_items())

    def test_agent_states(self):
        status = Status({
            'machines': {
                '1': {'agent-state': 'good'},
                '2': {},
            },
            'services': {
                'jenkins': {
                    'units': {
                        'jenkins/1': {'agent-state': 'bad'},
                        'jenkins/2': {'agent-state': 'good'},
                    }
                }
            }
        })
        expected = {
            'good': ['1', 'jenkins/2'],
            'bad': ['jenkins/1'],
            'no-agent': ['2'],
        }
        self.assertEqual(expected, status.agent_states())

    def test_check_agents_started_not_started(self):
        status = Status({
            'machines': {
                '1': {'agent-state': 'good'},
                '2': {},
            },
            'services': {
                'jenkins': {
                    'units': {
                        'jenkins/1': {'agent-state': 'bad'},
                        'jenkins/2': {'agent-state': 'good'},
                    }
                }
            }
        })
        self.assertEqual(status.agent_states(),
                         status.check_agents_started('env1'))

    def test_check_agents_started_all_started(self):
        status = Status({
            'machines': {
                '1': {'agent-state': 'started'},
                '2': {'agent-state': 'started'},
            },
            'services': {
                'jenkins': {
                    'units': {
                        'jenkins/1': {'agent-state': 'started'},
                        'jenkins/2': {'agent-state': 'started'},
                    }
                }
            }
        })
        self.assertIs(None, status.check_agents_started('env1'))

    def test_check_agents_started_agent_error(self):
        status = Status({
            'machines': {
                '1': {'agent-state': 'any-error'},
            },
            'services': {}
        })
        with self.assertRaisesRegexp(ErroredUnit,
                                     '1 is in state any-error'):
            status.check_agents_started('env1')

    def test_check_agents_started_agent_info_error(self):
        # Sometimes the error is indicated in a special 'agent-state-info'
        # field.
        status = Status({
            'machines': {
                '1': {'agent-state-info': 'any-error'},
            },
            'services': {}
        })
        with self.assertRaisesRegexp(ErroredUnit,
                                     '1 is in state any-error'):
            status.check_agents_started('env1')

    def test_get_agent_versions(self):
        status = Status({
            'machines': {
                '1': {'agent-version': '1.6.2'},
                '2': {'agent-version': '1.6.1'},
            },
            'services': {
                'jenkins': {
                    'units': {
                        'jenkins/0': {
                            'agent-version': '1.6.1'},
                        'jenkins/1': {},
                    },
                }
            }
        })
        self.assertEqual({
            '1.6.2': {'1'},
            '1.6.1': {'jenkins/0', '2'},
            'unknown': {'jenkins/1'},
        }, status.get_agent_versions())


def fast_timeout(count):
    if False:
        yield


class TestEnvironment(TestCase):

    @staticmethod
    def make_status_yaml(key, machine_value, unit_value):
        return dedent("""\
            machines:
              "0":
                {0}: {1}
            services:
              jenkins:
                units:
                  jenkins/0:
                    {0}: {2}
        """.format(key, machine_value, unit_value))

    def test_wait_for_started(self):
        def output_iterator():
            yield
            yield self.make_status_yaml('agent-state', 'started', 'started')
        JujuClientDevelFake.set_output(output_iterator())
        env = Environment('local', JujuClientDevelFake(None, None))
        env.wait_for_started()

    def test_wait_for_started_timeout(self):
        def output_iterator():
            yield
            while True:
                yield self.make_status_yaml(
                    'agent-state', 'pending', 'started')
        JujuClientDevelFake.set_output(output_iterator())
        env = Environment('local', JujuClientDevelFake)
        with patch('jujupy.until_timeout', lambda x: range(0)):
            with self.assertRaisesRegexp(
                    Exception,
                    'Timed out waiting for agents to start in local'):
                env.wait_for_started()

    def test_wait_for_version(self):
        def output_iterator():
            yield
            yield self.make_status_yaml('agent-version', '1.17.2', '1.17.2')
        JujuClientDevelFake.set_output(output_iterator())
        env = Environment('local', JujuClientDevelFake(None, None))
        env.wait_for_version('1.17.2')

    def test_wait_for_version_timeout(self):
        def output_iterator():
            yield
            yield self.make_status_yaml('agent-version', '1.17.2', '1.17.1')
        JujuClientDevelFake.set_output(output_iterator())
        env = Environment('local', JujuClientDevelFake)
        with patch('jujupy.until_timeout', lambda x: range(0)):
            with self.assertRaisesRegexp(
                    Exception, 'Some versions did not update'):
                env.wait_for_version('1.17.2')

    def test_wait_for_version_handles_connection_error(self):
        err = subprocess.CalledProcessError(2, 'foo')
        err.stderr = 'Unable to connect to environment'
        err = CannotConnectEnv(err)
        status = self.make_status_yaml('agent-version', '1.17.2', '1.17.2')
        actions = [err, status]

        def get_juju_output_fake(*args):
            action = actions.pop(0)
            if isinstance(action, Exception):
                raise action
            else:
                return action

        env = Environment('local', JujuClientDevelFake(None, None))
        output_real = 'test_jujupy.JujuClientDevelFake.get_juju_output'
        devnull = open(os.devnull, 'w')
        with patch('sys.stdout', devnull):
            with patch(output_real, get_juju_output_fake):
                env.wait_for_version('1.17.2')

    def test_wait_for_version_raises_non_connection_error(self):
        err = Exception('foo')
        status = self.make_status_yaml('agent-version', '1.17.2', '1.17.2')
        actions = [err, status]

        def get_juju_output_fake(*args):
            action = actions.pop(0)
            if isinstance(action, Exception):
                raise action
            else:
                return action

        env = Environment('local', JujuClientDevelFake(None, None))
        output_real = 'test_jujupy.JujuClientDevelFake.get_juju_output'
        devnull = open(os.devnull, 'w')
        with patch('sys.stdout', devnull):
            with patch(output_real, get_juju_output_fake):
                with self.assertRaisesRegexp(Exception, 'foo'):
                    env.wait_for_version('1.17.2')

    def test_local_from_config(self):
        env = Environment('local', '', {'type': 'openstack'})
        self.assertFalse(env.local, 'Does not respect config type.')
        env = Environment('local', '', {'type': 'local'})
        self.assertTrue(env.local, 'Does not respect config type.')

    def test_kvm_from_config(self):
        env = Environment('local', '', {'type': 'local'})
        self.assertFalse(env.kvm, 'Does not respect config type.')
        env = Environment('local', '', {'type': 'local', 'container': 'kvm'})
        self.assertTrue(env.kvm, 'Does not respect config type.')

    def test_hpcloud_from_config(self):
        env = Environment('cloud', '', {'auth-url': 'before.keystone.after'})
        self.assertFalse(env.hpcloud, 'Does not respect config type.')
        env = Environment('hp', '', {'auth-url': 'before.hpcloudsvc.after/'})
        self.assertTrue(env.hpcloud, 'Does not respect config type.')

    def test_from_config(self):
        home = tempfile.mkdtemp()
        try:
            environments_path = os.path.join(home, 'environments.yaml')
            old_home = os.environ.get('JUJU_HOME')
            os.environ['JUJU_HOME'] = home
            try:
                with open(environments_path, 'w') as environments:
                    yaml.dump({'environments': {
                        'foo': {'type': 'local'}
                    }}, environments)
                env = Environment.from_config('foo')
                self.assertIs(Environment, type(env))
                self.assertEqual({'type': 'local'}, env.config)
            finally:
                if old_home is None:
                    del os.environ['JUJU_HOME']
                else:
                    os.environ['JUJU_HOME'] = old_home
        finally:
            shutil.rmtree(home)

    def test_upgrade_juju_nonlocal(self):
        env = Environment('foo', MagicMock(), {'type': 'nonlocal'})
        env.client.version = '1.234-76'
        env.upgrade_juju()
        env.client.juju.assert_called_with(env, 'upgrade-juju',
                                           ('--version', '1.234'))

    def test_get_matching_agent_version(self):
        env = Environment('foo', MagicMock(), {'type': 'local'})
        env.client.version = '1.23-series-arch'
        self.assertEqual('1.23.1', env.get_matching_agent_version())
        self.assertEqual('1.23', env.get_matching_agent_version(
                         no_build=True))
        env.client.version = '1.20-beta1-series-arch'
        self.assertEqual('1.20-beta1.1', env.get_matching_agent_version())

    def test_upgrade_juju_local(self):
        env = Environment('foo', MagicMock(), {'type': 'local'})
        env.client.version = '1.234-76'
        env.upgrade_juju()
        env.client.juju.assert_called_with(
            env, 'upgrade-juju', ('--version', '1.234', '--upload-tools',))

    def test_deploy_non_joyent(self):
        env = Environment('foo', MagicMock(), {'type': 'local'})
        env.client.version = '1.234-76'
        env.deploy('mondogb')
        env.client.juju.assert_called_with(env, 'deploy', ('mondogb',))

    def test_deploy_joyent(self):
        env = Environment('foo', MagicMock(), {'type': 'joyent'})
        env.client.version = '1.234-76'
        env.deploy('mondogb')
        env.client.juju.assert_called_with(
            env, 'deploy', ('mondogb',))

    def test_set_testing_tools_metadata_url(self):
        client = JujuClientDevel(None, None)
        env = Environment('foo', client)
        with patch.object(client, 'get_env_option') as mock_get:
            mock_get.return_value = 'https://example.org/juju/tools'
            with patch.object(client, 'set_env_option') as mock_set:
                env.set_testing_tools_metadata_url()
        mock_get.assert_called_with(env, 'tools-metadata-url')
        mock_set.assert_called_with(
            env, 'tools-metadata-url', 'https://example.org/juju/testing/tools')

    def test_set_testing_tools_metadata_url_noop(self):
        client = JujuClientDevel(None, None)
        env = Environment('foo', client)
        with patch.object(client, 'get_env_option') as mock_get:
            mock_get.return_value = 'https://example.org/juju/testing/tools'
            with patch.object(client, 'set_env_option') as mock_set:
                env.set_testing_tools_metadata_url()
        mock_get.assert_called_with(env, 'tools-metadata-url')
        self.assertEqual(0, mock_set.call_count)


class TestFormatListing(TestCase):

    def test_format_listing(self):
        result = format_listing(
            {'1': ['a', 'b'], '2': ['c'], 'expected': ['d']}, 'expected')
        self.assertEqual('1: a, b | 2: c', result)