Esempio n. 1
0
def test_services_are_enabled_and_stared():
    configuration = SimpleMachineConfiguration(services=[SimpleServiceConfig('sshd')])

    result = as_userdata_string(configuration)

    assert 'systemctl enable sshd' in result
    assert 'systemctl start sshd' in result
Esempio n. 2
0
    def first_boot(self, machine_configuration, size_slug):

        try:
            existing_id = self.find_image_id(machine_configuration)
            raise ValueError(
                'Snapshot already exists for \'{name}\' - id is \'{id}\''.
                format(name=machine_configuration.name, id=existing_id))
        except KeyError:
            pass

        configuration = str(userdata.as_userdata_string(machine_configuration))

        droplet = digitalocean.Droplet(
            token=self.authentication_token,
            name=machine_configuration.name,
            region=self.region_name,
            image=self.find_image_id(
                machine_configuration.base_image_configuration),
            size_slug=size_slug,
            backups=False,
            ssh_keys=[self.root_ssh_key_fingerprint],
            user_data=configuration)

        droplet.create()
        return Droplet(self, droplet, machine_configuration)
Esempio n. 3
0
def test_services_not_requiring_start_are_not_started():
    configuration = SimpleMachineConfiguration(
        services=[SimpleServiceConfig('sshd', requires_start=False)])

    result = as_userdata_string(configuration)

    assert 'systemctl start sshd' not in result
Esempio n. 4
0
def test_service_installation_come_after_raw_commands():
    configuration = SimpleMachineConfiguration(
        services=[SimpleServiceConfig('sshd')], raw_commands=['cmd1'])

    result = as_userdata_string(configuration)

    assert result.index('sshd') > result.index('cmd1')
Esempio n. 5
0
def test_outputs_raw_commands_as_yaml_items():
    assert '''runcmd:
  - cmd1 with args
  - cmd2
  - cmd3
''' in as_userdata_string(
        SimpleMachineConfiguration(raw_commands=['cmd1 with args', 'cmd2', 'cmd3'], required_packages=None))
Esempio n. 6
0
def test_services_requiring_restart_come_after_raw_commands():
    configuration = SimpleMachineConfiguration(services=[SimpleServiceConfig('sshd', requires_restart=True)],
                                               raw_commands=['cmd1'])

    result = as_userdata_string(configuration)

    assert 'systemctl restart sshd' in result
    assert result.index('restart') > result.index('cmd1')
Esempio n. 7
0
def test_services_are_enabled_and_stared():
    configuration = SimpleMachineConfiguration(
        services=[SimpleServiceConfig('sshd')])

    result = as_userdata_string(configuration)

    assert 'systemctl enable sshd' in result
    assert 'systemctl start sshd' in result
Esempio n. 8
0
def test_services_requiring_restart_come_after_raw_commands():
    configuration = SimpleMachineConfiguration(
        services=[SimpleServiceConfig('sshd', requires_restart=True)],
        raw_commands=['cmd1'])

    result = as_userdata_string(configuration)

    assert 'systemctl restart sshd' in result
    assert result.index('restart') > result.index('cmd1')
Esempio n. 9
0
def test_outputs_raw_commands_as_yaml_items():
    assert '''runcmd:
  - cmd1 with args
  - cmd2
  - cmd3
''' in as_userdata_string(
        SimpleMachineConfiguration(
            raw_commands=['cmd1 with args', 'cmd2', 'cmd3'],
            required_packages=None))
Esempio n. 10
0
def test_files_marked_with_executable_flags_if_they_were_configured_for_them():
    configuration = SimpleMachineConfiguration(raw_commands=['cmd1'],
                                               files=[
                                                   SimpleFileForCopyingRemote(b'content', '/usr/bin/runnable',
                                                                              flags=['executable']),
                                                   SimpleFileForCopyingRemote(b'content', '/usr/bin/not_runnable')
                                               ])
    result = as_userdata_string(configuration)

    assert 'chmod +x /usr/bin/runnable' in result
    assert 'chmod +x /usr/bin/not_runnable' not in result
Esempio n. 11
0
def test_file_copy_steps_are_gzip_and_b64_encoded():
    configuration = SimpleMachineConfiguration(raw_commands=['cmd1'],
                                               files=[SimpleFileForCopyingRemote(b'content', '/etc/something.conf')])
    result = as_userdata_string(configuration)
    line_with_file_copy = [l for l in result.split('\n') if '/etc/something.conf' in l][0]
    content = line_with_file_copy[len('  - echo \''):line_with_file_copy.find('\' |')]
    result = gzip.decompress(base64.standard_b64decode(content))

    assert result == b'content'
    assert 'gzip -d ' in line_with_file_copy
    assert 'base64 -d ' in line_with_file_copy
Esempio n. 12
0
def test_files_marked_with_executable_flags_if_they_were_configured_for_them():
    configuration = SimpleMachineConfiguration(
        raw_commands=['cmd1'],
        files=[
            SimpleFileForCopyingRemote(b'content',
                                       '/usr/bin/runnable',
                                       flags=['executable']),
            SimpleFileForCopyingRemote(b'content', '/usr/bin/not_runnable')
        ])
    result = as_userdata_string(configuration)

    assert 'chmod +x /usr/bin/runnable' in result
    assert 'chmod +x /usr/bin/not_runnable' not in result
Esempio n. 13
0
def test_file_copy_steps_are_gzip_and_b64_encoded():
    configuration = SimpleMachineConfiguration(
        raw_commands=['cmd1'],
        files=[SimpleFileForCopyingRemote(b'content', '/etc/something.conf')])
    result = as_userdata_string(configuration)
    line_with_file_copy = [
        l for l in result.split('\n') if '/etc/something.conf' in l
    ][0]
    content = line_with_file_copy[len('  - echo \''):line_with_file_copy.
                                  find('\' |')]
    result = gzip.decompress(base64.standard_b64decode(content))

    assert result == b'content'
    assert 'gzip -d ' in line_with_file_copy
    assert 'base64 -d ' in line_with_file_copy
Esempio n. 14
0
    def first_boot(self, machine_configuration, size_slug):

        try:
            existing_id = self.find_image_id(machine_configuration)
            raise ValueError(
                'Snapshot already exists for \'{name}\' - id is \'{id}\''.format(name=machine_configuration.name,
                                                                                 id=existing_id))
        except KeyError:
            pass

        configuration = str(userdata.as_userdata_string(machine_configuration))

        droplet = digitalocean.Droplet(token=self.authentication_token,
                                       name=machine_configuration.name,
                                       region=self.region_name,
                                       image=self.find_image_id(machine_configuration.base_image_configuration),
                                       size_slug=size_slug,
                                       backups=False,
                                       ssh_keys=[self.root_ssh_key_fingerprint],
                                       user_data=configuration)

        droplet.create()
        return Droplet(self, droplet, machine_configuration)
Esempio n. 15
0
def test_service_installation_come_after_raw_commands():
    configuration = SimpleMachineConfiguration(services=[SimpleServiceConfig('sshd')], raw_commands=['cmd1'])

    result = as_userdata_string(configuration)

    assert result.index('sshd') > result.index('cmd1')
Esempio n. 16
0
def test_adds_file_copy_steps_before_other_runcmds():
    configuration = SimpleMachineConfiguration(
        raw_commands=['cmd1'],
        files=[SimpleFileForCopyingRemote(b'content', '/etc/something.conf')])
    result = as_userdata_string(configuration)
    assert result.index('cmd1') > result.index('/etc/something.conf')
Esempio n. 17
0
def test_adds_required_packages_before_runcmd_section():
    result = as_userdata_string(
        SimpleMachineConfiguration(raw_commands=['cmd1'],
                                   required_packages=['package1']))
    assert result.index('packages:') < result.index('runcmd:')
Esempio n. 18
0
def test_adds_required_packages():
    assert '''packages:
  - super_service
''' in as_userdata_string(
        SimpleMachineConfiguration(raw_commands=[],
                                   required_packages=['super_service']))
Esempio n. 19
0
def test_starts_with_cloud_config_header():
    result = as_userdata_string(SimpleMachineConfiguration(None, None))
    assert result.startswith("#cloud-config")
Esempio n. 20
0
def test_shuts_down_machine_when_done():
    empty_config = SimpleMachineConfiguration()
    result = as_userdata_string(empty_config)
    assert result.endswith('shutdown +1')
Esempio n. 21
0
def test_services_not_requiring_start_are_not_started():
    configuration = SimpleMachineConfiguration(services=[SimpleServiceConfig('sshd', requires_start=False)])

    result = as_userdata_string(configuration)

    assert 'systemctl start sshd' not in result
Esempio n. 22
0
def test_shuts_down_machine_when_done():
    empty_config = SimpleMachineConfiguration()
    result = as_userdata_string(empty_config)
    assert result.endswith('shutdown +1')
Esempio n. 23
0
def test_starts_with_cloud_config_header():
    result = as_userdata_string(SimpleMachineConfiguration(None, None))
    assert result.startswith("#cloud-config")
Esempio n. 24
0
def test_adds_required_packages():
    assert '''packages:
  - super_service
''' in as_userdata_string(SimpleMachineConfiguration(raw_commands=[], required_packages=['super_service']))
Esempio n. 25
0
def test_adds_required_packages_before_runcmd_section():
    result = as_userdata_string(SimpleMachineConfiguration(raw_commands=['cmd1'], required_packages=['package1']))
    assert result.index('packages:') < result.index('runcmd:')
Esempio n. 26
0
def test_adds_file_copy_steps_before_other_runcmds():
    configuration = SimpleMachineConfiguration(raw_commands=['cmd1'],
                                               files=[SimpleFileForCopyingRemote(b'content', '/etc/something.conf')])
    result = as_userdata_string(configuration)
    assert result.index('cmd1') > result.index('/etc/something.conf')