~veebers/juju-ci-tools/explore-perf-bulk-model-destruction

« back to all changes in this revision

Viewing changes to tests/test_jujupy.py

  • Committer: Aaron Bentley
  • Date: 2016-05-25 16:12:07 UTC
  • mto: This revision was merged to the branch mainline in revision 1447.
  • Revision ID: aaron.bentley@canonical.com-20160525161207-h5z5uev3jkyewvxt
Update tests to use applications, not services.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
    EnvJujuClient2A2,
49
49
    EnvJujuClient2B2,
50
50
    EnvJujuClient2B3,
 
51
    EnvJujuClient2B7,
51
52
    ErroredUnit,
52
53
    GroupReporter,
53
54
    get_cache_path,
66
67
    make_safe_config,
67
68
    parse_new_state_server_from_error,
68
69
    SimpleEnvironment,
 
70
    ServiceStatus,
69
71
    Status,
70
72
    SYSTEM,
71
73
    tear_down,
88
90
__metaclass__ = type
89
91
 
90
92
 
 
93
def c_output(func):
 
94
    def wrapper(*args, **kwargs):
 
95
        result = func(*args, **kwargs)
 
96
        if 'service' in result:
 
97
            raise AssertionError('Result contained service')
 
98
        return result
 
99
    return wrapper
 
100
 
 
101
 
91
102
class AdminOperation(Exception):
92
103
 
93
104
    def __init__(self, operation):
270
281
                'relations': self.relations.get(service, {}),
271
282
                'exposed': service in self.exposed,
272
283
                }
273
 
        return {'machines': machines, 'services': services}
 
284
        return {'machines': machines, 'applications': services}
274
285
 
275
286
 
276
287
class FakeBackend:
392
403
 
393
404
    def juju(self, command, args, used_feature_flags,
394
405
             juju_home, model=None, check=True, timeout=None, extra_env=None):
 
406
        if 'service' in command:
 
407
            raise Exception('No service')
395
408
        if model is not None:
396
409
            model_state = self.controller_state.models[model]
397
410
            if command == 'enable-ha':
409
422
                parsed = parser.parse_args(args)
410
423
                self.deploy(model_state, parsed.charm_name,
411
424
                            parsed.service_name, parsed.series)
412
 
            if command == 'destroy-service':
413
 
                model_state.destroy_service(*args)
414
 
            if command == 'remove-service':
 
425
            if command == 'remove-application':
415
426
                model_state.destroy_service(*args)
416
427
            if command == 'add-relation':
417
428
                if args[0] == 'dummy-source':
481
492
        self.juju(command, args, used_feature_flags,
482
493
                  juju_home, model, timeout=timeout)
483
494
 
 
495
    @c_output
484
496
    def get_juju_output(self, command, args, used_feature_flags,
485
497
                        juju_home, model=None, timeout=None):
 
498
        if 'service' in command:
 
499
            raise Exception('No service')
486
500
        if model is not None:
487
501
            model_state = self.controller_state.models[model]
488
502
        if (command, args) == ('ssh', ('dummy-sink/0', GET_TOKEN_SCRIPT)):
524
538
        pass
525
539
 
526
540
 
 
541
class FakeBackend2B7(FakeBackend):
 
542
 
 
543
    def juju(self, command, args, used_feature_flags,
 
544
             juju_home, model=None, check=True, timeout=None, extra_env=None):
 
545
        if model is not None:
 
546
            model_state = self.controller_state.models[model]
 
547
        if command == 'destroy-service':
 
548
            model_state.destroy_service(*args)
 
549
        if command == 'remove-service':
 
550
            model_state.destroy_service(*args)
 
551
        return super(FakeBackend2B7).juju(command, args, used_feature_flags,
 
552
                                          juju_home, model, check, timeout,
 
553
                                          extra_env)
 
554
 
 
555
 
527
556
class FakeBackendOptionalJES(FakeBackend):
528
557
 
529
558
    def is_feature_enabled(self, feature):
629
658
        self.assertEqual('2.0', backend.version)
630
659
 
631
660
 
 
661
class TestEnvJujuClient2B7(ClientTest):
 
662
 
 
663
    def test_remove_service(self):
 
664
        env = EnvJujuClient2B7(
 
665
            JujuData('foo', {'type': 'local'}), '1.234-76', None)
 
666
        with patch.object(env, 'juju') as mock_juju:
 
667
            env.remove_service('mondogb')
 
668
        mock_juju.assert_called_with('remove-service', ('mondogb',))
 
669
 
 
670
 
632
671
class TestEnvJujuClient26(ClientTest, CloudSigmaTest):
633
672
 
634
673
    client_class = EnvJujuClient26
1526
1565
            machines:
1527
1566
              "0":
1528
1567
                {0}: {1}
1529
 
            services:
 
1568
            applications:
1530
1569
              jenkins:
1531
1570
                units:
1532
1571
                  jenkins/0:
1640
1679
            JujuData('foo', {'type': 'local'}), '1.234-76', None)
1641
1680
        with patch.object(env, 'juju') as mock_juju:
1642
1681
            env.remove_service('mondogb')
1643
 
        mock_juju.assert_called_with('remove-service', ('mondogb',))
 
1682
        mock_juju.assert_called_with('remove-application', ('mondogb',))
1644
1683
 
1645
1684
    def test_status_until_always_runs_once(self):
1646
1685
        client = EnvJujuClient(
1874
1913
            machines:
1875
1914
              "0":
1876
1915
                agent-state: started
1877
 
            services:
 
1916
            applications:
1878
1917
              jenkins:
1879
1918
                units:
1880
1919
                  jenkins/0:
1897
1936
            machines:
1898
1937
              "0":
1899
1938
                agent-state: started
1900
 
            services:
 
1939
            applications:
1901
1940
              jenkins:
1902
1941
                units:
1903
1942
                  jenkins/0:
1915
1954
 
1916
1955
    def test_wait_for_workload(self):
1917
1956
        initial_status = Status.from_text("""\
1918
 
            services:
 
1957
            applications:
1919
1958
              jenkins:
1920
1959
                units:
1921
1960
                  jenkins/0:
1927
1966
                        current: unknown
1928
1967
        """)
1929
1968
        final_status = Status(copy.deepcopy(initial_status.status), None)
1930
 
        final_status.status['services']['jenkins']['units']['jenkins/0'][
 
1969
        final_status.status['applications']['jenkins']['units']['jenkins/0'][
1931
1970
            'workload-status']['current'] = 'active'
1932
1971
        client = EnvJujuClient(JujuData('local'), None, None)
1933
1972
        writes = []
2267
2306
            'machines': {
2268
2307
                '0': {'agent-state': 'started'},
2269
2308
            },
2270
 
            'services': {
 
2309
            'applications': {
2271
2310
                'jenkins': {
2272
2311
                    'units': {
2273
2312
                        'jenkins/1': {'baz': 'qux'}
2284
2323
            'machines': {
2285
2324
                '0': {'agent-state': 'started'},
2286
2325
            },
2287
 
            'services': {},
 
2326
            'applications': {},
2288
2327
        })
2289
2328
        client = EnvJujuClient(JujuData('local'), None, None)
2290
2329
        with patch('jujupy.until_timeout', lambda x: range(0)):
3650
3689
            result = client.get_status()
3651
3690
        gjo_mock.assert_called_once_with(
3652
3691
            'status', '--format', 'yaml', admin=False)
3653
 
        self.assertEqual(Status, type(result))
 
3692
        self.assertEqual(ServiceStatus, type(result))
3654
3693
        self.assertEqual(['a', 'b', 'c'], result.status)
3655
3694
 
3656
3695
    def test_get_status_retries_on_error(self):
4010
4049
                        'jenkins', 'sub1', start=now - timedelta(1200))
4011
4050
 
4012
4051
    def test_wait_for_workload(self):
4013
 
        initial_status = Status.from_text("""\
 
4052
        initial_status = ServiceStatus.from_text("""\
4014
4053
            services:
4015
4054
              jenkins:
4016
4055
                units:
5165
5204
            'machines': {
5166
5205
                '1': {'foo': 'bar', 'containers': {'1/lxc/0': {'baz': 'qux'}}}
5167
5206
            },
5168
 
            'services': {}}, '')
 
5207
            'applications': {}}, '')
5169
5208
        self.assertEqual(list(status.iter_machines()),
5170
5209
                         [('1', status.status['machines']['1'])])
5171
5210
 
5174
5213
            'machines': {
5175
5214
                '1': {'foo': 'bar', 'containers': {'1/lxc/0': {'baz': 'qux'}}}
5176
5215
            },
5177
 
            'services': {}}, '')
 
5216
            'applications': {}}, '')
5178
5217
        self.assertEqual(list(status.iter_machines(containers=True)), [
5179
5218
            ('1', status.status['machines']['1']),
5180
5219
            ('1/lxc/0', {'baz': 'qux'}),
5181
5220
        ])
5182
5221
 
5183
5222
    def test_agent_items_empty(self):
5184
 
        status = Status({'machines': {}, 'services': {}}, '')
 
5223
        status = Status({'machines': {}, 'applications': {}}, '')
5185
5224
        self.assertItemsEqual([], status.agent_items())
5186
5225
 
5187
5226
    def test_agent_items(self):
5189
5228
            'machines': {
5190
5229
                '1': {'foo': 'bar'}
5191
5230
            },
5192
 
            'services': {
 
5231
            'applications': {
5193
5232
                'jenkins': {
5194
5233
                    'units': {
5195
5234
                        'jenkins/1': {
5214
5253
                    '2': {'qux': 'baz'},
5215
5254
                }}
5216
5255
            },
5217
 
            'services': {}
 
5256
            'applications': {}
5218
5257
        }, '')
5219
5258
        expected = [
5220
5259
            ('1', {'foo': 'bar', 'containers': {'2': {'qux': 'baz'}}}),
5237
5276
                '1': {'agent-state': 'good'},
5238
5277
                '2': {},
5239
5278
            },
5240
 
            'services': {
 
5279
            'applications': {
5241
5280
                'jenkins': {
5242
5281
                    'units': {
5243
5282
                        'jenkins/1': {'agent-state': 'bad'},
5272
5311
                '1': {'agent-state': 'good'},
5273
5312
                '2': {},
5274
5313
            },
5275
 
            'services': {
 
5314
            'applications': {
5276
5315
                'jenkins': {
5277
5316
                    'units': {
5278
5317
                        'jenkins/1': {'agent-state': 'bad'},
5289
5328
            'machines': {
5290
5329
                '1': {},
5291
5330
            },
5292
 
            'services': {
 
5331
            'applications': {
5293
5332
                'jenkins': {
5294
5333
                    'units': {
5295
5334
                        'jenkins/1': {'agent-state': 'bad'},
5314
5353
            'machines': {
5315
5354
                '1': {},
5316
5355
            },
5317
 
            'services': {
 
5356
            'applications': {
5318
5357
                'ubuntu': {},
5319
5358
                'jenkins': {
5320
5359
                    'units': {
5358
5397
            'machines': {
5359
5398
                '1': {},
5360
5399
            },
5361
 
            'services': {
 
5400
            'applications': {
5362
5401
                'jenkins': {
5363
5402
                    'units': {
5364
5403
                        'jenkins/1': {'agent-state': 'bad'},
5380
5419
                '1': {'agent-state': 'good'},
5381
5420
                '2': {},
5382
5421
            },
5383
 
            'services': {
 
5422
            'applications': {
5384
5423
                'jenkins': {
5385
5424
                    'units': {
5386
5425
                        'jenkins/1': {'agent-state': 'bad'},
5402
5441
                '1': {'agent-state': 'good'},
5403
5442
                '2': {},
5404
5443
            },
5405
 
            'services': {
 
5444
            'applications': {
5406
5445
                'jenkins': {
5407
5446
                    'units': {
5408
5447
                        'jenkins/1': {'agent-status': {'current': 'bad'}},
5425
5464
                '1': {'juju-status': {'current': 'good'}},
5426
5465
                '2': {},
5427
5466
            },
5428
 
            'services': {
 
5467
            'applications': {
5429
5468
                'jenkins': {
5430
5469
                    'units': {
5431
5470
                        'jenkins/1': {'juju-status': {'current': 'bad'}},
5448
5487
                '1': {'agent-state': 'good'},
5449
5488
                '2': {},
5450
5489
            },
5451
 
            'services': {
 
5490
            'applications': {
5452
5491
                'jenkins': {
5453
5492
                    'units': {
5454
5493
                        'jenkins/1': {'agent-state': 'bad'},
5466
5505
                '1': {'agent-state': 'started'},
5467
5506
                '2': {'agent-state': 'started'},
5468
5507
            },
5469
 
            'services': {
 
5508
            'applications': {
5470
5509
                'jenkins': {
5471
5510
                    'units': {
5472
5511
                        'jenkins/1': {
5490
5529
                '1': {'agent-state': 'started'},
5491
5530
                '2': {'agent-state': 'started'},
5492
5531
            },
5493
 
            'services': {
 
5532
            'applications': {
5494
5533
                'jenkins': {
5495
5534
                    'units': {
5496
5535
                        'jenkins/1': {
5513
5552
            'machines': {
5514
5553
                '1': {'agent-state': 'any-error'},
5515
5554
            },
5516
 
            'services': {}
 
5555
            'applications': {}
5517
5556
        }, '')
5518
5557
        with self.assertRaisesRegexp(ErroredUnit,
5519
5558
                                     '1 is in state any-error'):
5523
5562
        status = Status({
5524
5563
            'machines': {'0': {
5525
5564
                'agent-state-info': failure}},
5526
 
            'services': {},
 
5565
            'applications': {},
5527
5566
        }, '')
5528
5567
        with self.assertRaises(ErroredUnit) as e_cxt:
5529
5568
            status.check_agents_started()
5552
5591
            'machines': {
5553
5592
                '1': {'agent-state-info': 'any-error'},
5554
5593
            },
5555
 
            'services': {}
 
5594
            'applications': {}
5556
5595
        }, '')
5557
5596
        with self.assertRaisesRegexp(ErroredUnit,
5558
5597
                                     '1 is in state any-error'):
5564
5603
                '1': {'agent-version': '1.6.2'},
5565
5604
                '2': {'agent-version': '1.6.1'},
5566
5605
            },
5567
 
            'services': {
 
5606
            'applications': {
5568
5607
                'jenkins': {
5569
5608
                    'units': {
5570
5609
                        'jenkins/0': {
5586
5625
                '1': {'juju-status': {'version': '1.6.2'}},
5587
5626
                '2': {'juju-status': {'version': '1.6.1'}},
5588
5627
            },
5589
 
            'services': {
 
5628
            'applications': {
5590
5629
                'jenkins': {
5591
5630
                    'units': {
5592
5631
                        'jenkins/0': {
5637
5676
        self.assertEqual(status.status_text, text)
5638
5677
        self.assertEqual(status.status, {
5639
5678
            'machines': {'0': {'agent-state': 'pending'}},
 
5679
            'applications': {'jenkins': {'units': {'jenkins/0': {
 
5680
                'agent-state': 'horsefeathers'}}}}
 
5681
        })
 
5682
 
 
5683
    def test_iter_units(self):
 
5684
        started_unit = {'agent-state': 'started'}
 
5685
        unit_with_subordinates = {
 
5686
            'agent-state': 'started',
 
5687
            'subordinates': {
 
5688
                'ntp/0': started_unit,
 
5689
                'nrpe/0': started_unit,
 
5690
            },
 
5691
        }
 
5692
        status = Status({
 
5693
            'machines': {
 
5694
                '1': {'agent-state': 'started'},
 
5695
            },
 
5696
            'applications': {
 
5697
                'jenkins': {
 
5698
                    'units': {
 
5699
                        'jenkins/0': unit_with_subordinates,
 
5700
                    }
 
5701
                },
 
5702
                'application': {
 
5703
                    'units': {
 
5704
                        'application/0': started_unit,
 
5705
                        'application/1': started_unit,
 
5706
                    }
 
5707
                },
 
5708
            }
 
5709
        }, '')
 
5710
        expected = [
 
5711
            ('application/0', started_unit),
 
5712
            ('application/1', started_unit),
 
5713
            ('jenkins/0', unit_with_subordinates),
 
5714
            ('nrpe/0', started_unit),
 
5715
            ('ntp/0', started_unit),
 
5716
        ]
 
5717
        gen = status.iter_units()
 
5718
        self.assertIsInstance(gen, types.GeneratorType)
 
5719
        self.assertEqual(expected, list(gen))
 
5720
 
 
5721
 
 
5722
class TestServiceStatus(FakeHomeTestCase):
 
5723
 
 
5724
    def test_iter_machines_no_containers(self):
 
5725
        status = ServiceStatus({
 
5726
            'machines': {
 
5727
                '1': {'foo': 'bar', 'containers': {'1/lxc/0': {'baz': 'qux'}}}
 
5728
            },
 
5729
            'services': {}}, '')
 
5730
        self.assertEqual(list(status.iter_machines()),
 
5731
                         [('1', status.status['machines']['1'])])
 
5732
 
 
5733
    def test_iter_machines_containers(self):
 
5734
        status = ServiceStatus({
 
5735
            'machines': {
 
5736
                '1': {'foo': 'bar', 'containers': {'1/lxc/0': {'baz': 'qux'}}}
 
5737
            },
 
5738
            'services': {}}, '')
 
5739
        self.assertEqual(list(status.iter_machines(containers=True)), [
 
5740
            ('1', status.status['machines']['1']),
 
5741
            ('1/lxc/0', {'baz': 'qux'}),
 
5742
        ])
 
5743
 
 
5744
    def test_agent_items_empty(self):
 
5745
        status = ServiceStatus({'machines': {}, 'services': {}}, '')
 
5746
        self.assertItemsEqual([], status.agent_items())
 
5747
 
 
5748
    def test_agent_items(self):
 
5749
        status = ServiceStatus({
 
5750
            'machines': {
 
5751
                '1': {'foo': 'bar'}
 
5752
            },
 
5753
            'services': {
 
5754
                'jenkins': {
 
5755
                    'units': {
 
5756
                        'jenkins/1': {
 
5757
                            'subordinates': {
 
5758
                                'sub': {'baz': 'qux'}
 
5759
                            }
 
5760
                        }
 
5761
                    }
 
5762
                }
 
5763
            }
 
5764
        }, '')
 
5765
        expected = [
 
5766
            ('1', {'foo': 'bar'}),
 
5767
            ('jenkins/1', {'subordinates': {'sub': {'baz': 'qux'}}}),
 
5768
            ('sub', {'baz': 'qux'})]
 
5769
        self.assertItemsEqual(expected, status.agent_items())
 
5770
 
 
5771
    def test_agent_items_containers(self):
 
5772
        status = ServiceStatus({
 
5773
            'machines': {
 
5774
                '1': {'foo': 'bar', 'containers': {
 
5775
                    '2': {'qux': 'baz'},
 
5776
                }}
 
5777
            },
 
5778
            'services': {}
 
5779
        }, '')
 
5780
        expected = [
 
5781
            ('1', {'foo': 'bar', 'containers': {'2': {'qux': 'baz'}}}),
 
5782
            ('2', {'qux': 'baz'})
 
5783
        ]
 
5784
        self.assertItemsEqual(expected, status.agent_items())
 
5785
 
 
5786
    def test_get_service_count_zero(self):
 
5787
        status = ServiceStatus({
 
5788
            'machines': {
 
5789
                '1': {'agent-state': 'good'},
 
5790
                '2': {},
 
5791
            },
 
5792
        }, '')
 
5793
        self.assertEqual(0, status.get_service_count())
 
5794
 
 
5795
    def test_get_service_count(self):
 
5796
        status = ServiceStatus({
 
5797
            'machines': {
 
5798
                '1': {'agent-state': 'good'},
 
5799
                '2': {},
 
5800
            },
 
5801
            'services': {
 
5802
                'jenkins': {
 
5803
                    'units': {
 
5804
                        'jenkins/1': {'agent-state': 'bad'},
 
5805
                    }
 
5806
                },
 
5807
                'dummy-sink': {
 
5808
                    'units': {
 
5809
                        'dummy-sink/0': {'agent-state': 'started'},
 
5810
                    }
 
5811
                },
 
5812
                'juju-reports': {
 
5813
                    'units': {
 
5814
                        'juju-reports/0': {'agent-state': 'pending'},
 
5815
                    }
 
5816
                }
 
5817
            }
 
5818
        }, '')
 
5819
        self.assertEqual(3, status.get_service_count())
 
5820
 
 
5821
    def test_get_service_unit_count_zero(self):
 
5822
        status = ServiceStatus({
 
5823
            'machines': {
 
5824
                '1': {'agent-state': 'good'},
 
5825
                '2': {},
 
5826
            },
 
5827
        }, '')
 
5828
        self.assertEqual(0, status.get_service_unit_count('jenkins'))
 
5829
 
 
5830
    def test_get_service_unit_count(self):
 
5831
        status = ServiceStatus({
 
5832
            'machines': {
 
5833
                '1': {'agent-state': 'good'},
 
5834
                '2': {},
 
5835
            },
 
5836
            'services': {
 
5837
                'jenkins': {
 
5838
                    'units': {
 
5839
                        'jenkins/1': {'agent-state': 'bad'},
 
5840
                        'jenkins/2': {'agent-state': 'bad'},
 
5841
                        'jenkins/3': {'agent-state': 'bad'},
 
5842
                    }
 
5843
                }
 
5844
            }
 
5845
        }, '')
 
5846
        self.assertEqual(3, status.get_service_unit_count('jenkins'))
 
5847
 
 
5848
    def test_get_unit(self):
 
5849
        status = ServiceStatus({
 
5850
            'machines': {
 
5851
                '1': {},
 
5852
            },
 
5853
            'services': {
 
5854
                'jenkins': {
 
5855
                    'units': {
 
5856
                        'jenkins/1': {'agent-state': 'bad'},
 
5857
                    }
 
5858
                },
 
5859
                'dummy-sink': {
 
5860
                    'units': {
 
5861
                        'jenkins/2': {'agent-state': 'started'},
 
5862
                    }
 
5863
                },
 
5864
            }
 
5865
        }, '')
 
5866
        self.assertEqual(
 
5867
            status.get_unit('jenkins/1'), {'agent-state': 'bad'})
 
5868
        self.assertEqual(
 
5869
            status.get_unit('jenkins/2'), {'agent-state': 'started'})
 
5870
        with self.assertRaisesRegexp(KeyError, 'jenkins/3'):
 
5871
            status.get_unit('jenkins/3')
 
5872
 
 
5873
    def test_service_subordinate_units(self):
 
5874
        status = ServiceStatus({
 
5875
            'machines': {
 
5876
                '1': {},
 
5877
            },
 
5878
            'services': {
 
5879
                'ubuntu': {},
 
5880
                'jenkins': {
 
5881
                    'units': {
 
5882
                        'jenkins/1': {
 
5883
                            'subordinates': {
 
5884
                                'chaos-monkey/0': {'agent-state': 'started'},
 
5885
                            }
 
5886
                        }
 
5887
                    }
 
5888
                },
 
5889
                'dummy-sink': {
 
5890
                    'units': {
 
5891
                        'jenkins/2': {
 
5892
                            'subordinates': {
 
5893
                                'chaos-monkey/1': {'agent-state': 'started'}
 
5894
                            }
 
5895
                        },
 
5896
                        'jenkins/3': {
 
5897
                            'subordinates': {
 
5898
                                'chaos-monkey/2': {'agent-state': 'started'}
 
5899
                            }
 
5900
                        }
 
5901
                    }
 
5902
                }
 
5903
            }
 
5904
        }, '')
 
5905
        self.assertItemsEqual(
 
5906
            status.service_subordinate_units('ubuntu'),
 
5907
            [])
 
5908
        self.assertItemsEqual(
 
5909
            status.service_subordinate_units('jenkins'),
 
5910
            [('chaos-monkey/0', {'agent-state': 'started'},)])
 
5911
        self.assertItemsEqual(
 
5912
            status.service_subordinate_units('dummy-sink'), [
 
5913
                ('chaos-monkey/1', {'agent-state': 'started'}),
 
5914
                ('chaos-monkey/2', {'agent-state': 'started'})]
 
5915
            )
 
5916
 
 
5917
    def test_get_open_ports(self):
 
5918
        status = ServiceStatus({
 
5919
            'machines': {
 
5920
                '1': {},
 
5921
            },
 
5922
            'services': {
 
5923
                'jenkins': {
 
5924
                    'units': {
 
5925
                        'jenkins/1': {'agent-state': 'bad'},
 
5926
                    }
 
5927
                },
 
5928
                'dummy-sink': {
 
5929
                    'units': {
 
5930
                        'jenkins/2': {'open-ports': ['42/tcp']},
 
5931
                    }
 
5932
                },
 
5933
            }
 
5934
        }, '')
 
5935
        self.assertEqual(status.get_open_ports('jenkins/1'), [])
 
5936
        self.assertEqual(status.get_open_ports('jenkins/2'), ['42/tcp'])
 
5937
 
 
5938
    def test_agent_states_with_agent_state(self):
 
5939
        status = ServiceStatus({
 
5940
            'machines': {
 
5941
                '1': {'agent-state': 'good'},
 
5942
                '2': {},
 
5943
            },
 
5944
            'services': {
 
5945
                'jenkins': {
 
5946
                    'units': {
 
5947
                        'jenkins/1': {'agent-state': 'bad'},
 
5948
                        'jenkins/2': {'agent-state': 'good'},
 
5949
                    }
 
5950
                }
 
5951
            }
 
5952
        }, '')
 
5953
        expected = {
 
5954
            'good': ['1', 'jenkins/2'],
 
5955
            'bad': ['jenkins/1'],
 
5956
            'no-agent': ['2'],
 
5957
        }
 
5958
        self.assertEqual(expected, status.agent_states())
 
5959
 
 
5960
    def test_agent_states_with_agent_status(self):
 
5961
        status = ServiceStatus({
 
5962
            'machines': {
 
5963
                '1': {'agent-state': 'good'},
 
5964
                '2': {},
 
5965
            },
 
5966
            'services': {
 
5967
                'jenkins': {
 
5968
                    'units': {
 
5969
                        'jenkins/1': {'agent-status': {'current': 'bad'}},
 
5970
                        'jenkins/2': {'agent-status': {'current': 'good'}},
 
5971
                        'jenkins/3': {},
 
5972
                    }
 
5973
                }
 
5974
            }
 
5975
        }, '')
 
5976
        expected = {
 
5977
            'good': ['1', 'jenkins/2'],
 
5978
            'bad': ['jenkins/1'],
 
5979
            'no-agent': ['2', 'jenkins/3'],
 
5980
        }
 
5981
        self.assertEqual(expected, status.agent_states())
 
5982
 
 
5983
    def test_agent_states_with_juju_status(self):
 
5984
        status = ServiceStatus({
 
5985
            'machines': {
 
5986
                '1': {'juju-status': {'current': 'good'}},
 
5987
                '2': {},
 
5988
            },
 
5989
            'services': {
 
5990
                'jenkins': {
 
5991
                    'units': {
 
5992
                        'jenkins/1': {'juju-status': {'current': 'bad'}},
 
5993
                        'jenkins/2': {'juju-status': {'current': 'good'}},
 
5994
                        'jenkins/3': {},
 
5995
                    }
 
5996
                }
 
5997
            }
 
5998
        }, '')
 
5999
        expected = {
 
6000
            'good': ['1', 'jenkins/2'],
 
6001
            'bad': ['jenkins/1'],
 
6002
            'no-agent': ['2', 'jenkins/3'],
 
6003
        }
 
6004
        self.assertEqual(expected, status.agent_states())
 
6005
 
 
6006
    def test_check_agents_started_not_started(self):
 
6007
        status = ServiceStatus({
 
6008
            'machines': {
 
6009
                '1': {'agent-state': 'good'},
 
6010
                '2': {},
 
6011
            },
 
6012
            'services': {
 
6013
                'jenkins': {
 
6014
                    'units': {
 
6015
                        'jenkins/1': {'agent-state': 'bad'},
 
6016
                        'jenkins/2': {'agent-state': 'good'},
 
6017
                    }
 
6018
                }
 
6019
            }
 
6020
        }, '')
 
6021
        self.assertEqual(status.agent_states(),
 
6022
                         status.check_agents_started('env1'))
 
6023
 
 
6024
    def test_check_agents_started_all_started_with_agent_state(self):
 
6025
        status = ServiceStatus({
 
6026
            'machines': {
 
6027
                '1': {'agent-state': 'started'},
 
6028
                '2': {'agent-state': 'started'},
 
6029
            },
 
6030
            'services': {
 
6031
                'jenkins': {
 
6032
                    'units': {
 
6033
                        'jenkins/1': {
 
6034
                            'agent-state': 'started',
 
6035
                            'subordinates': {
 
6036
                                'sub1': {
 
6037
                                    'agent-state': 'started'
 
6038
                                }
 
6039
                            }
 
6040
                        },
 
6041
                        'jenkins/2': {'agent-state': 'started'},
 
6042
                    }
 
6043
                }
 
6044
            }
 
6045
        }, '')
 
6046
        self.assertIs(None, status.check_agents_started('env1'))
 
6047
 
 
6048
    def test_check_agents_started_all_started_with_agent_status(self):
 
6049
        status = ServiceStatus({
 
6050
            'machines': {
 
6051
                '1': {'agent-state': 'started'},
 
6052
                '2': {'agent-state': 'started'},
 
6053
            },
 
6054
            'services': {
 
6055
                'jenkins': {
 
6056
                    'units': {
 
6057
                        'jenkins/1': {
 
6058
                            'agent-status': {'current': 'idle'},
 
6059
                            'subordinates': {
 
6060
                                'sub1': {
 
6061
                                    'agent-status': {'current': 'idle'}
 
6062
                                }
 
6063
                            }
 
6064
                        },
 
6065
                        'jenkins/2': {'agent-status': {'current': 'idle'}},
 
6066
                    }
 
6067
                }
 
6068
            }
 
6069
        }, '')
 
6070
        self.assertIs(None, status.check_agents_started('env1'))
 
6071
 
 
6072
    def test_check_agents_started_agent_error(self):
 
6073
        status = ServiceStatus({
 
6074
            'machines': {
 
6075
                '1': {'agent-state': 'any-error'},
 
6076
            },
 
6077
            'services': {}
 
6078
        }, '')
 
6079
        with self.assertRaisesRegexp(ErroredUnit,
 
6080
                                     '1 is in state any-error'):
 
6081
            status.check_agents_started('env1')
 
6082
 
 
6083
    def do_check_agents_started_failure(self, failure):
 
6084
        status = ServiceStatus({
 
6085
            'machines': {'0': {
 
6086
                'agent-state-info': failure}},
 
6087
            'services': {},
 
6088
        }, '')
 
6089
        with self.assertRaises(ErroredUnit) as e_cxt:
 
6090
            status.check_agents_started()
 
6091
        e = e_cxt.exception
 
6092
        self.assertEqual(
 
6093
            str(e), '0 is in state {}'.format(failure))
 
6094
        self.assertEqual(e.unit_name, '0')
 
6095
        self.assertEqual(e.state, failure)
 
6096
 
 
6097
    def test_check_agents_cannot_set_up_groups(self):
 
6098
        self.do_check_agents_started_failure('cannot set up groups foobar')
 
6099
 
 
6100
    def test_check_agents_error(self):
 
6101
        self.do_check_agents_started_failure('error executing "lxc-start"')
 
6102
 
 
6103
    def test_check_agents_cannot_run_instances(self):
 
6104
        self.do_check_agents_started_failure('cannot run instances')
 
6105
 
 
6106
    def test_check_agents_cannot_run_instance(self):
 
6107
        self.do_check_agents_started_failure('cannot run instance')
 
6108
 
 
6109
    def test_check_agents_started_agent_info_error(self):
 
6110
        # Sometimes the error is indicated in a special 'agent-state-info'
 
6111
        # field.
 
6112
        status = ServiceStatus({
 
6113
            'machines': {
 
6114
                '1': {'agent-state-info': 'any-error'},
 
6115
            },
 
6116
            'services': {}
 
6117
        }, '')
 
6118
        with self.assertRaisesRegexp(ErroredUnit,
 
6119
                                     '1 is in state any-error'):
 
6120
            status.check_agents_started('env1')
 
6121
 
 
6122
    def test_get_agent_versions_1x(self):
 
6123
        status = ServiceStatus({
 
6124
            'machines': {
 
6125
                '1': {'agent-version': '1.6.2'},
 
6126
                '2': {'agent-version': '1.6.1'},
 
6127
            },
 
6128
            'services': {
 
6129
                'jenkins': {
 
6130
                    'units': {
 
6131
                        'jenkins/0': {
 
6132
                            'agent-version': '1.6.1'},
 
6133
                        'jenkins/1': {},
 
6134
                    },
 
6135
                }
 
6136
            }
 
6137
        }, '')
 
6138
        self.assertEqual({
 
6139
            '1.6.2': {'1'},
 
6140
            '1.6.1': {'jenkins/0', '2'},
 
6141
            'unknown': {'jenkins/1'},
 
6142
        }, status.get_agent_versions())
 
6143
 
 
6144
    def test_get_agent_versions_2x(self):
 
6145
        status = ServiceStatus({
 
6146
            'machines': {
 
6147
                '1': {'juju-status': {'version': '1.6.2'}},
 
6148
                '2': {'juju-status': {'version': '1.6.1'}},
 
6149
            },
 
6150
            'services': {
 
6151
                'jenkins': {
 
6152
                    'units': {
 
6153
                        'jenkins/0': {
 
6154
                            'juju-status': {'version': '1.6.1'}},
 
6155
                        'jenkins/1': {},
 
6156
                    },
 
6157
                }
 
6158
            }
 
6159
        }, '')
 
6160
        self.assertEqual({
 
6161
            '1.6.2': {'1'},
 
6162
            '1.6.1': {'jenkins/0', '2'},
 
6163
            'unknown': {'jenkins/1'},
 
6164
        }, status.get_agent_versions())
 
6165
 
 
6166
    def test_iter_new_machines(self):
 
6167
        old_status = ServiceStatus({
 
6168
            'machines': {
 
6169
                'bar': 'bar_info',
 
6170
            }
 
6171
        }, '')
 
6172
        new_status = ServiceStatus({
 
6173
            'machines': {
 
6174
                'foo': 'foo_info',
 
6175
                'bar': 'bar_info',
 
6176
            }
 
6177
        }, '')
 
6178
        self.assertItemsEqual(new_status.iter_new_machines(old_status),
 
6179
                              [('foo', 'foo_info')])
 
6180
 
 
6181
    def test_get_instance_id(self):
 
6182
        status = ServiceStatus({
 
6183
            'machines': {
 
6184
                '0': {'instance-id': 'foo-bar'},
 
6185
                '1': {},
 
6186
            }
 
6187
        }, '')
 
6188
        self.assertEqual(status.get_instance_id('0'), 'foo-bar')
 
6189
        with self.assertRaises(KeyError):
 
6190
            status.get_instance_id('1')
 
6191
        with self.assertRaises(KeyError):
 
6192
            status.get_instance_id('2')
 
6193
 
 
6194
    def test_from_text(self):
 
6195
        text = TestEnvJujuClient1X.make_status_yaml(
 
6196
            'agent-state', 'pending', 'horsefeathers')
 
6197
        status = ServiceStatus.from_text(text)
 
6198
        self.assertEqual(status.status_text, text)
 
6199
        self.assertEqual(status.status, {
 
6200
            'machines': {'0': {'agent-state': 'pending'}},
5640
6201
            'services': {'jenkins': {'units': {'jenkins/0': {
5641
6202
                'agent-state': 'horsefeathers'}}}}
5642
6203
        })
5650
6211
                'nrpe/0': started_unit,
5651
6212
            },
5652
6213
        }
5653
 
        status = Status({
 
6214
        status = ServiceStatus({
5654
6215
            'machines': {
5655
6216
                '1': {'agent-state': 'started'},
5656
6217
            },