Exemple #1
0
 def test_can_install_barebones_packages(self):
     lxd_container = unittest.mock.Mock()
     lxd_container.execute.return_value = ('ok', 'ok', '')
     guest = DebianGuest(lxd_container)
     guest.install_barebones_packages()
     assert lxd_container.execute.call_count == 1
     assert lxd_container.execute.call_args[0] == \
         (['apt-get', 'install', '-y'] + DebianGuest.barebones_packages, )
Exemple #2
0
 def test_can_install_packages(self):
     lxd_container = unittest.mock.Mock()
     lxd_container.execute.return_value = ('ok', 'ok', '')
     guest = DebianGuest(lxd_container)
     guest.install_packages([
         'python',
         'openssh',
     ])
     assert lxd_container.execute.call_count == 1
     assert lxd_container.execute.call_args[0] == \
         (['apt-get', 'install', '-y', 'python', 'openssh', ], )
Exemple #3
0
 def test_can_install_packages(self):
     guest = DebianGuest(FakeContainer())
     guest.install_packages([
         'python',
         'openssh',
     ])
     assert guest.lxd_container.execute.call_count == 2
     assert guest.lxd_container.execute.call_args_list[0][0] == \
         (['apt-get', 'update', ], )
     assert guest.lxd_container.execute.call_args_list[1][0] == \
         (['apt-get', 'install', '-y', 'python', 'openssh', ], )
Exemple #4
0
    def test_trigger_packages_installation_on_the_guest_if_the_related_attr_is_defined(
            self):
        class DummyProvisioner(Provisioner):
            name = 'myprovisioner'
            schema = {
                'test': 'test',
            }

            guest_required_packages_debian = [
                'test01',
                'test02',
            ]

        container = FakeContainer()
        lxd_container = container._container
        lxd_container.execute.return_value = ('ok', 'ok', '')
        host = unittest.mock.Mock()
        guest = DebianGuest(container)
        provisioner = DummyProvisioner('./', host, [guest], {})
        provisioner.setup()
        assert lxd_container.execute.call_count == 2
        assert lxd_container.execute.call_args_list[0][0] == \
            (['apt-get', 'update'], )
        assert lxd_container.execute.call_args_list[1][0] == \
            (['apt-get', 'install', '-y', 'test01', 'test02', ], )
Exemple #5
0
 def test_inventory_with_lxd_transport(self):
     c = FakeContainer(name='c1')
     provisioner = AnsibleProvisioner('./', Host(), [DebianGuest(c)], {
         'playbook': 'deploy.yml',
         'lxd_transport': True
     })
     inv = provisioner.get_inventory()
     assert 'ansible_host={}'.format(c.lxd_name) in inv
Exemple #6
0
 def test_can_run_commands_on_the_host_side(self, mock_popen):
     host = Host(unittest.mock.Mock())
     guest = DebianGuest(unittest.mock.Mock())
     provisioner = ShellProvisioner('./', host, guest, {
         'inline': 'touch f',
         'side': 'host',
     })
     provisioner.provision()
     assert mock_popen.call_args[0] == ('touch f', )
Exemple #7
0
 def test_can_run_a_script_on_the_host_side(self, mock_popen):
     host = Host(unittest.mock.Mock())
     guest = DebianGuest(unittest.mock.Mock())
     provisioner = ShellProvisioner('./', host, guest, {
         'script': 'test.sh',
         'side': 'host',
     })
     provisioner.provision()
     assert mock_popen.call_args[0] == ('./test.sh', )
Exemple #8
0
 def test_can_run_commands_on_the_guest_side(self):
     lxd_container = unittest.mock.Mock()
     lxd_container.execute.return_value = ('ok', 'ok', '')
     host = Host(unittest.mock.Mock())
     guest = DebianGuest(lxd_container)
     provisioner = ShellProvisioner('./', host, guest,
                                    {'inline': 'echo TEST'})
     provisioner.provision()
     assert lxd_container.execute.call_count == 1
     assert lxd_container.execute.call_args_list[0][0] == (['echo',
                                                            'TEST'], )
Exemple #9
0
 def test_inventory_contains_groups(self):
     c1 = FakeContainer(name='c1')
     c2 = FakeContainer(name='c2')
     # c3 is deliberately not part of our guests list. This is to test that it doesn't end up
     # in the inventory and result in spurious unreachable hosts. These situations can happen
     # in two ways: errors in the config file, or guest filtering in the command line ("lxdock
     # provision c1, c2" for example).
     provisioner = AnsibleProvisioner(
         './', Host(), [DebianGuest(c1), DebianGuest(c2)], {
             'playbook': 'deploy.yml',
             'groups': {
                 'g1': ['c1', 'c2'],
                 'g2': ['c1', 'c3']
             }
         })
     inv = provisioner.get_inventory()
     # group order is not guaranteed. our tests have to be written with that in mind.
     groups = re.findall(r'\[(g1|g2)\]([^[]+)', inv, re.MULTILINE)
     # we sort so that g1 will be first all the time
     groups = sorted([(gname, hosts.strip()) for gname, hosts in groups])
     assert sorted(groups) == [('g1', 'c1\nc2'), ('g2', 'c1')]
Exemple #10
0
 def test_can_run_ansible_playbooks(self, mock_popen):
     host = Host(unittest.mock.Mock())
     guest = DebianGuest(unittest.mock.Mock())
     lxd_state = unittest.mock.Mock()
     lxd_state.network.__getitem__ = unittest.mock.MagicMock(
         return_value={'addresses': [{'family': 'init', 'address': '0.0.0.0', }, ]})
     guest.lxd_container.state.return_value = lxd_state
     provisioner = AnsibleProvisioner('./', host, guest, {'playbook': 'deploy.yml'})
     provisioner.provision()
     assert re.match(
         'ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook --inventory-file /[/\w]+ '
         './deploy.yml', mock_popen.call_args[0][0])
Exemple #11
0
 def test_can_run_commands_on_the_guest_side(self):
     container = FakeContainer()
     lxd_container = container._container
     host = Host()
     guest = DebianGuest(container)
     cmd = """touch f && echo "Here's the PATH" $PATH >> /tmp/test.txt"""
     provisioner = ShellProvisioner('./', host, [guest], {'inline': cmd})
     provisioner.provision()
     assert lxd_container.execute.call_count == 1
     assert lxd_container.execute.call_args_list[0][0] == ([
         'sh', '-c', cmd
     ], )
Exemple #12
0
 def test_can_run_commands_on_the_host_side(self, mock_popen):
     host = Host()
     guest = DebianGuest(FakeContainer())
     provisioner = ShellProvisioner(
         './', host, [guest], {
             'inline':
             """touch f && echo "Here's the PATH" $PATH >> /tmp/test.txt""",
             'side': 'host',
         })
     provisioner.provision()
     assert mock_popen.call_args[0] == (
         """sh -c 'touch f && echo "Here'"'"'s the PATH" $PATH >> /tmp/test.txt'""",
     )
Exemple #13
0
 def test_can_run_a_script_on_the_guest_side(self, mock_open):
     lxd_container = unittest.mock.Mock()
     lxd_container.execute.return_value = ('ok', 'ok', '')
     host = Host(unittest.mock.Mock())
     guest = DebianGuest(lxd_container)
     provisioner = ShellProvisioner('./', host, guest, {
         'script': 'test.sh',
     })
     provisioner.provision()
     assert lxd_container.execute.call_count == 2
     assert lxd_container.execute.call_args_list[0][0] == ([
         'chmod',
         '+x',
         '/tmp/test.sh',
     ], )
     assert lxd_container.execute.call_args_list[1][0] == ([
         '/tmp/test.sh',
     ], )
Exemple #14
0
 def test_can_run_a_script_on_the_guest_side(self, mock_open):
     container = FakeContainer()
     lxd_container = container._container
     host = Host()
     guest = DebianGuest(container)
     provisioner = ShellProvisioner('./', host, [guest], {
         'script': 'test.sh',
     })
     provisioner.provision()
     assert lxd_container.execute.call_count == 2
     assert lxd_container.execute.call_args_list[0][0] == ([
         'chmod',
         '+x',
         '/tmp/test.sh',
     ], )
     assert lxd_container.execute.call_args_list[1][0] == ([
         '/tmp/test.sh',
     ], )
Exemple #15
0
    def test_trigger_specific_setup_on_the_guest_if_the_related_method_is_defined(
            self):
        class DummyProvisioner(Provisioner):
            name = 'myprovisioner'
            schema = {
                'test': 'test',
            }
            called = False

            def setup_guest_debian(self):
                self.called = True

        lxd_container = unittest.mock.Mock()
        lxd_container.execute.return_value = ('ok', 'ok', '')
        host = unittest.mock.Mock()
        guest = DebianGuest(lxd_container)
        provisioner = DummyProvisioner('./', host, guest, {})
        provisioner.setup()
        assert provisioner.called
Exemple #16
0
 def test_can_run_ansible_playbooks(self, mock_popen, options,
                                    expected_cmdargs):
     host = Host()
     guest = DebianGuest(FakeContainer())
     lxd_state = unittest.mock.Mock()
     lxd_state.network.__getitem__ = unittest.mock.MagicMock(return_value={
         'addresses': [
             {
                 'family': 'init',
                 'address': '0.0.0.0',
             },
         ]
     })
     guest.lxd_container.state.return_value = lxd_state
     options['playbook'] = 'deploy.yml'
     provisioner = AnsibleProvisioner('./', host, [guest], options)
     provisioner.provision()
     m = re.match(
         r'ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook --inventory-file /[/\w]+ '
         r'(.*)\s?./deploy.yml', mock_popen.call_args[0][0])
     assert m
     assert m.group(1).strip() == expected_cmdargs