~dooferlad/juju-ci-tools/juju-ci-tools-addressable-containers

« back to all changes in this revision

Viewing changes to test_jujupy.py

  • Committer: Aaron Bentley
  • Date: 2015-09-02 16:25:47 UTC
  • mto: (997.1.84 trunk)
  • mto: This revision was merged to the branch mainline in revision 1004.
  • Revision ID: aaron.bentley@canonical.com-20150902162547-xmw26uwqasn7ui47
Remove JujuClientDevel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from jujuconfig import (
25
25
    get_environments_path,
26
26
    get_jenv_path,
27
 
    get_juju_home,
28
27
    NoSuchEnvironment,
29
28
)
30
29
from jujupy import (
42
41
    jes_home_path,
43
42
    JESByDefault,
44
43
    JESNotSupported,
45
 
    JujuClientDevel,
46
44
    JUJU_DEV_FEATURE_FLAGS,
47
45
    make_client,
48
46
    make_jes_home,
1525
1523
        self.assertEqual(client.juju_home, tb_home)
1526
1524
 
1527
1525
 
1528
 
class TestJujuClientDevel(TestCase):
1529
 
 
1530
 
    def test_get_version(self):
1531
 
        value = ' 5.6 \n'
1532
 
        with patch('subprocess.check_output', return_value=value) as vsn:
1533
 
            version = JujuClientDevel.get_version()
1534
 
        self.assertEqual('5.6', version)
1535
 
        vsn.assert_called_with(('juju', '--version'))
1536
 
 
1537
 
    def test_by_version(self):
1538
 
        def juju_cmd_iterator():
1539
 
            yield '1.17'
1540
 
            yield '1.16'
1541
 
            yield '1.16.1'
1542
 
            yield '1.15'
1543
 
 
1544
 
        context = patch.object(
1545
 
            JujuClientDevel, 'get_version',
1546
 
            side_effect=juju_cmd_iterator().next)
1547
 
        with context:
1548
 
            self.assertIs(JujuClientDevel,
1549
 
                          type(JujuClientDevel.by_version()))
1550
 
            with self.assertRaisesRegexp(Exception, 'Unsupported juju: 1.16'):
1551
 
                JujuClientDevel.by_version()
1552
 
            with self.assertRaisesRegexp(Exception,
1553
 
                                         'Unsupported juju: 1.16.1'):
1554
 
                JujuClientDevel.by_version()
1555
 
            client = JujuClientDevel.by_version()
1556
 
            self.assertIs(JujuClientDevel, type(client))
1557
 
            self.assertEqual('1.15', client.version)
1558
 
 
1559
 
    def test_bootstrap_hpcloud(self):
1560
 
        env = SimpleEnvironment('hp')
1561
 
        with patch.object(env, 'hpcloud', lambda: True):
1562
 
            with patch.object(EnvJujuClient, 'juju') as mock:
1563
 
                JujuClientDevel(None, None).bootstrap(env)
1564
 
            mock.assert_called_with(
1565
 
                'bootstrap', ('--constraints', 'mem=2G'), False)
1566
 
 
1567
 
    def test_bootstrap_non_sudo(self):
1568
 
        env = SimpleEnvironment('foo')
1569
 
        with patch.object(SimpleEnvironment, 'needs_sudo', return_value=False):
1570
 
            with patch.object(EnvJujuClient, 'juju') as mock:
1571
 
                JujuClientDevel(None, None).bootstrap(env)
1572
 
            mock.assert_called_with(
1573
 
                'bootstrap', ('--constraints', 'mem=2G'), False)
1574
 
 
1575
 
    def test_bootstrap_sudo(self):
1576
 
        env = SimpleEnvironment('foo')
1577
 
        client = JujuClientDevel(None, None)
1578
 
        with patch.object(SimpleEnvironment, 'needs_sudo', return_value=True):
1579
 
            with patch.object(EnvJujuClient, 'juju') as mock:
1580
 
                client.bootstrap(env)
1581
 
            mock.assert_called_with(
1582
 
                'bootstrap', ('--constraints', 'mem=2G'), True)
1583
 
 
1584
 
    def test_destroy_environment_non_sudo(self):
1585
 
        env = SimpleEnvironment('foo')
1586
 
        client = JujuClientDevel(None, None)
1587
 
        with patch.object(SimpleEnvironment, 'needs_sudo', return_value=False):
1588
 
            with patch.object(EnvJujuClient, 'juju') as mock:
1589
 
                client.destroy_environment(env)
1590
 
            mock.assert_called_with(
1591
 
                'destroy-environment', ('foo', '--force', '-y'),
1592
 
                False, check=False, include_e=False, timeout=600)
1593
 
 
1594
 
    def test_destroy_environment_sudo(self):
1595
 
        env = SimpleEnvironment('foo')
1596
 
        client = JujuClientDevel(None, None)
1597
 
        with patch.object(SimpleEnvironment, 'needs_sudo', return_value=True):
1598
 
            with patch.object(EnvJujuClient, 'juju') as mock:
1599
 
                client.destroy_environment(env)
1600
 
            mock.assert_called_with(
1601
 
                'destroy-environment', ('foo', '--force', '-y'),
1602
 
                True, check=False, include_e=False, timeout=600.0)
1603
 
 
1604
 
    def test_get_juju_output(self):
1605
 
        env = SimpleEnvironment('foo')
1606
 
        asdf = lambda x, stderr, env: 'asdf'
1607
 
        client = JujuClientDevel(None, None)
1608
 
        with patch('subprocess.check_output', side_effect=asdf) as mock:
1609
 
            result = client.get_juju_output(env, 'bar')
1610
 
        self.assertEqual('asdf', result)
1611
 
        self.assertEqual((('juju', '--show-log', 'bar', '-e', 'foo'),),
1612
 
                         mock.call_args[0])
1613
 
 
1614
 
    def test_get_juju_output_accepts_varargs(self):
1615
 
        env = SimpleEnvironment('foo')
1616
 
        asdf = lambda x, stderr, env: 'asdf'
1617
 
        client = JujuClientDevel(None, None)
1618
 
        with patch('subprocess.check_output', side_effect=asdf) as mock:
1619
 
            result = client.get_juju_output(env, 'bar', 'baz', '--qux')
1620
 
        self.assertEqual('asdf', result)
1621
 
        self.assertEqual((('juju', '--show-log', 'bar', '-e', 'foo', 'baz',
1622
 
                           '--qux'),), mock.call_args[0])
1623
 
 
1624
 
    def test_get_juju_output_stderr(self):
1625
 
        def raise_without_stderr(args, stderr, env):
1626
 
            stderr.write('Hello!')
1627
 
            raise subprocess.CalledProcessError('a', 'b')
1628
 
        env = SimpleEnvironment('foo')
1629
 
        client = JujuClientDevel(None, None)
1630
 
        with self.assertRaises(subprocess.CalledProcessError) as exc:
1631
 
            with patch('subprocess.check_output', raise_without_stderr):
1632
 
                client.get_juju_output(env, 'bar')
1633
 
        self.assertEqual(exc.exception.stderr, 'Hello!')
1634
 
 
1635
 
    def test_get_juju_output_accepts_timeout(self):
1636
 
        env = SimpleEnvironment('foo')
1637
 
        client = JujuClientDevel(None, None)
1638
 
        with patch('subprocess.check_output') as sco_mock:
1639
 
            client.get_juju_output(env, 'bar', timeout=5)
1640
 
        self.assertEqual(
1641
 
            sco_mock.call_args[0][0],
1642
 
            (sys.executable, get_timeout_path(), '5.00', '--', 'juju',
1643
 
             '--show-log', 'bar', '-e', 'foo'))
1644
 
 
1645
 
    def test_juju_output_supplies_path(self):
1646
 
        env = SimpleEnvironment('foo')
1647
 
        client = JujuClientDevel(None, '/foobar/bar')
1648
 
        with patch('subprocess.check_output') as sco_mock:
1649
 
            client.get_juju_output(env, 'baz')
1650
 
        self.assertRegexpMatches(sco_mock.call_args[1]['env']['PATH'],
1651
 
                                 r'/foobar\:')
1652
 
 
1653
 
    def test_get_status(self):
1654
 
        output_text = yield dedent("""\
1655
 
                - a
1656
 
                - b
1657
 
                - c
1658
 
                """)
1659
 
        client = JujuClientDevel(None, None)
1660
 
        env = SimpleEnvironment('foo')
1661
 
        with patch.object(EnvJujuClient, 'get_juju_output',
1662
 
                          return_value=output_text):
1663
 
            result = client.get_status(env)
1664
 
        self.assertEqual(Status, type(result))
1665
 
        self.assertEqual(['a', 'b', 'c'], result.status)
1666
 
 
1667
 
    def test_get_status_retries_on_error(self):
1668
 
        client = JujuClientDevel(None, None)
1669
 
        client.attempt = 0
1670
 
 
1671
 
        def get_juju_output(command, *args):
1672
 
            if client.attempt == 1:
1673
 
                return '"hello"'
1674
 
            client.attempt += 1
1675
 
            raise subprocess.CalledProcessError(1, command)
1676
 
 
1677
 
        env = SimpleEnvironment('foo')
1678
 
        with patch.object(EnvJujuClient, 'get_juju_output', get_juju_output):
1679
 
            client.get_status(env)
1680
 
 
1681
 
    def test_get_status_raises_on_timeout_1(self):
1682
 
        client = JujuClientDevel(None, None)
1683
 
 
1684
 
        def get_juju_output(command):
1685
 
            raise subprocess.CalledProcessError(1, command)
1686
 
 
1687
 
        env = SimpleEnvironment('foo')
1688
 
        with patch.object(EnvJujuClient, 'get_juju_output',
1689
 
                          side_effect=get_juju_output):
1690
 
            with patch('jujupy.until_timeout', lambda x: iter([None, None])):
1691
 
                with self.assertRaisesRegexp(
1692
 
                        Exception, 'Timed out waiting for juju status'):
1693
 
                    client.get_status(env)
1694
 
 
1695
 
    def test_get_status_raises_on_timeout_2(self):
1696
 
        client = JujuClientDevel(None, None)
1697
 
        env = SimpleEnvironment('foo')
1698
 
        with patch('jujupy.until_timeout', return_value=iter([1])) as mock_ut:
1699
 
            with patch.object(EnvJujuClient, 'get_juju_output',
1700
 
                              side_effect=StopIteration):
1701
 
                with self.assertRaises(StopIteration):
1702
 
                    client.get_status(env, 500)
1703
 
        mock_ut.assert_called_with(500)
1704
 
 
1705
 
    def test_get_env_option(self):
1706
 
        client = JujuClientDevel(None, None)
1707
 
        env = SimpleEnvironment('foo')
1708
 
        with patch('subprocess.check_output') as mock:
1709
 
            mock.return_value = 'https://example.org/juju/tools'
1710
 
            result = client.get_env_option(env, 'tools-metadata-url')
1711
 
        self.assertEqual(
1712
 
            mock.call_args[0][0],
1713
 
            ('juju', '--show-log', 'get-env', '-e', 'foo',
1714
 
             'tools-metadata-url'))
1715
 
        self.assertEqual('https://example.org/juju/tools', result)
1716
 
 
1717
 
    def test_set_env_option(self):
1718
 
        client = JujuClientDevel(None, None)
1719
 
        env = SimpleEnvironment('foo')
1720
 
        with patch('subprocess.check_call') as mock:
1721
 
            client.set_env_option(
1722
 
                env, 'tools-metadata-url', 'https://example.org/juju/tools')
1723
 
        environ = dict(os.environ)
1724
 
        environ['JUJU_HOME'] = get_juju_home()
1725
 
        mock.assert_called_with(
1726
 
            ('juju', '--show-log', 'set-env', '-e', 'foo',
1727
 
             'tools-metadata-url=https://example.org/juju/tools'),
1728
 
            env=environ)
1729
 
 
1730
 
    def test_juju(self):
1731
 
        env = SimpleEnvironment('qux')
1732
 
        client = JujuClientDevel(None, None)
1733
 
        with patch('sys.stdout') as stdout_mock:
1734
 
            with patch('subprocess.check_call') as mock:
1735
 
                client.juju(env, 'foo', ('bar', 'baz'))
1736
 
        environ = dict(os.environ)
1737
 
        environ['JUJU_HOME'] = get_juju_home()
1738
 
        mock.assert_called_with(('juju', '--show-log', 'foo', '-e', 'qux',
1739
 
                                 'bar', 'baz'), env=environ)
1740
 
        stdout_mock.flush.assert_called_with()
1741
 
 
1742
 
    def test_juju_env(self):
1743
 
        env = SimpleEnvironment('qux')
1744
 
        client = JujuClientDevel(None, '/foobar/baz')
1745
 
        with patch('subprocess.check_call') as cc_mock:
1746
 
            client.juju(env, 'foo', ('bar', 'baz'))
1747
 
        self.assertRegexpMatches(cc_mock.call_args[1]['env']['PATH'],
1748
 
                                 r'/foobar\:')
1749
 
 
1750
 
    def test_juju_no_check(self):
1751
 
        env = SimpleEnvironment('qux')
1752
 
        client = JujuClientDevel(None, None)
1753
 
        with patch('sys.stdout') as stdout_mock:
1754
 
            with patch('subprocess.call') as mock:
1755
 
                client.juju(env, 'foo', ('bar', 'baz'), check=False)
1756
 
        environ = dict(os.environ)
1757
 
        environ['JUJU_HOME'] = get_juju_home()
1758
 
        mock.assert_called_with(('juju', '--show-log', 'foo', '-e', 'qux',
1759
 
                                 'bar', 'baz'), env=environ)
1760
 
        stdout_mock.flush.assert_called_with()
1761
 
 
1762
 
    def test_juju_no_check_env(self):
1763
 
        env = SimpleEnvironment('qux')
1764
 
        client = JujuClientDevel(None, '/foobar/baz')
1765
 
        with patch('subprocess.call') as call_mock:
1766
 
            client.juju(env, 'foo', ('bar', 'baz'), check=False)
1767
 
        self.assertRegexpMatches(call_mock.call_args[1]['env']['PATH'],
1768
 
                                 r'/foobar\:')
1769
 
 
1770
 
 
1771
1526
class TestStatus(TestCase):
1772
1527
 
1773
1528
    def test_iter_machines_no_containers(self):