def test_empty_customisation_script_doesnt_produce_write_files_stanza(
         self):
     for test_config in self.test_config.values():
         test_config['script_file'].remove()
         test_config['script_file'].ensure()
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     assert 'write_files' not in cloud_config
 def test_customisation_script_placed_in_correct_directory(self):
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     for stanza in cloud_config['write_files']:
         path = py.path.local(stanza['path'])
         assert ('/home/ubuntu/build-output/chroot-autobuild'
                 '/usr/share/livecd-rootfs/live-build/ubuntu-cpc/hooks' ==
                 path.dirname)
 def test_write_files_stanza_count_produced_for_customisation_script(self):
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     expected_count = 0
     if 'customisation_script' in self.kwargs:
         expected_count += 3
     if 'binary_customisation_script' in self.kwargs:
         expected_count += 1
     assert expected_count == len(cloud_config['write_files'])
 def test_customisation_script_is_an_appropriate_hook(self):
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     for stanza in cloud_config['write_files']:
         path = py.path.local(stanza['path'])
         content = base64.b64decode(stanza['content']).decode('utf-8')
         if ('-- chroot --' in content or '-- setup --' in content
                 or '-- teardown --' in content):
             assert '.chroot' == path.ext
         else:
             assert '.binary' == path.ext
 def test_customisation_script_is_included_in_template_as_base64(self):
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     for stanza in cloud_config['write_files']:
         if stanza['path'].endswith('9998-local-modifications.chroot'):
             expected_content = self.test_config['customisation_script'][
                 'content']
         elif stanza['path'].endswith('binary'):
             expected_content = self.test_config[
                 'binary_customisation_script']['content']
         else:
             continue
         assert expected_content == base64.b64decode(
             stanza['content']).decode('utf-8')
 def test_setup_teardown_content_matches_template(self, hook, monkeypatch):
     if list(self.kwargs.keys()) == ['binary_customisation_script']:
         pytest.skip('Test only applies to chroot hooks.')
     expected_string = '#!/bin/sh\n-- specific test content --'
     monkeypatch.setattr(generate_build_config,
                         "{}_CONTENT".format(hook.upper()), expected_string)
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     contents = [
         base64.b64decode(stanza['content'])
         for stanza in cloud_config['write_files']
     ]
     expected_bytes = expected_string.encode('utf-8')
     assert expected_bytes in contents
     assert 1 == len(
         [content for content in contents if expected_bytes == content])
 def test_setup_teardown_sequence_numbers(self):
     if list(self.kwargs.keys()) == ['binary_customisation_script']:
         pytest.skip('Test only applies to chroot hooks.')
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     sequence_numbers = {}
     for stanza in cloud_config['write_files']:
         sequence_number = stanza['path'].rsplit('/')[-1].split('-')[0]
         content = base64.b64decode(stanza['content']).decode('utf-8')
         if '-- chroot --' in content:
             sequence_numbers['chroot'] = sequence_number
         elif '-- setup --' in content:
             sequence_numbers['setup'] = sequence_number
         elif '-- teardown --' in content:
             sequence_numbers['teardown'] = sequence_number
     assert sequence_numbers['setup'] < sequence_numbers['chroot']
     assert sequence_numbers['chroot'] < sequence_numbers['teardown']
 def _write_cloud_config_to_tmpfile(*args, **kwargs):
     output_file = tmpdir.join('output.yaml')
     generate_build_config._write_cloud_config(output_file.strpath, *args,
                                               **kwargs)
     return output_file
 def test_customisation_script_marked_as_base64(self):
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     for stanza in cloud_config['write_files']:
         assert 'b64' == stanza['encoding']
 def test_customisation_script_is_executable_by_root(self):
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     for stanza in cloud_config['write_files']:
         assert '7' == stanza['permissions'][1]
 def test_customisation_script_owned_by_root(self):
     generate_build_config._write_cloud_config(self.output_file.strpath,
                                               **self.kwargs)
     cloud_config = yaml.load(self.output_file.open())
     for stanza in cloud_config['write_files']:
         assert 'root:root' == stanza['owner']