def test_create_meta_with_declared_icon_and_setup(self): gui_path = os.path.join('setup', 'gui') os.makedirs(gui_path) icon_content = b'this is the icon' with open(os.path.join(gui_path, 'icon.png'), 'wb') as f: f.write(icon_content) open(os.path.join(os.curdir, 'my-icon.png'), 'w').close() self.config_data['icon'] = 'my-icon.png' meta.create(self.config_data) expected_icon = os.path.join(self.meta_dir, 'gui', 'icon.png') self.assertTrue(os.path.exists(expected_icon), 'icon.png was not setup correctly') with open(expected_icon, 'rb') as f: self.assertEqual(f.read(), icon_content) self.assertTrue( os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) self.assertFalse('icon' in y, 'icon found in snap.yaml {}'.format(y))
def test_create_meta_without_config(self, mock_the_open, mock_wrap_exe): del self.config_data["config"] meta.create(self.config_data, ["amd64"]) self.mock_makedirs.assert_called_once_with(self.meta_dir, exist_ok=True) mock_the_open.assert_has_calls(self.expected_open_calls)
def test_create_meta_with_app_with_security_policy(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'app1.sh'), 'w').close() open(os.path.join(os.curdir, 'stub-sec'), 'w').close() self.config_data['apps'] = { 'app1': { 'command': 'app1.sh', 'uses': ['migration'], }, } self.config_data['uses'] = { 'migration': { 'type': 'migration-skill', 'security-policy': { 'apparmor': 'stub-sec', 'seccomp': 'stub-sec', }, }, } meta.create(self.config_data) app1_wrapper_path = os.path.join(self.snap_dir, 'command-app1.wrapper') self.assertTrue( os.path.exists(app1_wrapper_path), 'the wrapper for app1 was not setup correctly') sec_path = os.path.join(self.meta_dir, 'stub-sec') self.assertTrue( os.path.exists(sec_path), 'the security-policies for app1 were not setup correctly') self.assertTrue( os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) expected = {'architectures': ['amd64'], 'apps': { 'app1': { 'command': 'command-app1.wrapper', 'uses': ['migration'], }, }, 'uses': { 'migration': { 'type': 'migration-skill', 'security-policy': { 'apparmor': 'meta/stub-sec', 'seccomp': 'meta/stub-sec', }, } }, 'description': 'my description', 'summary': 'my summary', 'name': 'my-package', 'version': '1.0'} self.assertEqual(y, expected)
def test_create_meta(self, mock_the_open, mock_wrap_exe): meta.create(self.config_data, ['amd64']) self.mock_makedirs.assert_has_calls([ call(self.meta_dir, exist_ok=True), call(self.hooks_dir), ]) mock_the_open.assert_has_calls(self.expected_open_calls) mock_wrap_exe.assert_has_calls([ call( '$SNAP_APP_PATH/bin/bash', os.path.join(os.path.abspath(os.curdir), 'snap/bin/bash.wrapper'), ), call( 'bin/config', os.path.join(self.hooks_dir, 'config'), args=[], cwd='$SNAP_APP_PATH', ), ]) self.mock_copyfile.assert_has_calls([ call('my-icon.png', os.path.join(self.meta_dir, 'my-icon.png')), call('file.apparmor', os.path.join(self.meta_dir, 'file.apparmor')), call('file.seccomp', os.path.join(self.meta_dir, 'file.seccomp')), ])
def test_create_meta(self, mock_the_open, mock_wrap_exe): meta.create(self.config_data, ['amd64']) self.mock_makedirs.assert_has_calls([ call(self.meta_dir, exist_ok=True), call(self.hooks_dir), ]) self.mock_rename.assert_has_calls([ call(os.path.join(self.snap_dir, 'bin', 'config.wrapper'), os.path.join(self.hooks_dir, 'config')), ]) mock_the_open.assert_has_calls(self.expected_open_calls) mock_wrap_exe.assert_has_calls([ call( '$SNAP_APP_PATH/bin/bash', os.path.join(self.snap_dir, 'bin', 'bash.wrapper'), args=None, shebang=None ), call( '$SNAP_APP_PATH/bin/config', os.path.join(self.snap_dir, 'bin', 'config.wrapper'), args=[], shebang=None, ), ]) self.mock_copyfile.assert_has_calls([ call('my-icon.png', os.path.join(self.meta_dir, 'my-icon.png')), call('file.apparmor', os.path.join(self.meta_dir, 'file.apparmor')), call('file.seccomp', os.path.join(self.meta_dir, 'file.seccomp')), ])
def test_create_meta_with_vararg_config(self, mock_the_open, mock_wrap_exe): self.config_data['config'] = 'python3 my.py --config' meta.create(self.config_data, ['amd64']) self.mock_makedirs.assert_has_calls([ call(self.meta_dir, exist_ok=True), call(self.hooks_dir), ]) self.mock_rename.assert_has_calls([ call(os.path.join(self.snap_dir, 'python3.wrapper'), os.path.join(self.hooks_dir, 'config')), ]) mock_the_open.assert_has_calls(self.expected_open_calls) mock_wrap_exe.assert_has_calls([ call( '$SNAP_APP_PATH/bin/bash', os.path.join(os.path.abspath(os.curdir), 'snap/bin/bash.wrapper'), args=None, shebang=None, ), call( '$SNAP_APP_PATH/python3', os.path.join(self.snap_dir, 'python3.wrapper'), args=['my.py', '--config'], shebang=None, ), ])
def test_create_meta_with_declared_icon_and_setup(self): fake_logger = fixtures.FakeLogger(level=logging.INFO) self.useFixture(fake_logger) gui_path = os.path.join('setup', 'gui') os.makedirs(gui_path) icon_content = b'this is the icon' with open(os.path.join(gui_path, 'icon.png'), 'wb') as f: f.write(icon_content) open(os.path.join(os.curdir, 'my-icon.png'), 'w').close() self.config_data['icon'] = 'my-icon.png' meta.create(self.config_data) expected_icon = os.path.join(self.meta_dir, 'gui', 'icon.png') self.assertTrue(os.path.exists(expected_icon), 'icon.png was not setup correctly') with open(expected_icon, 'rb') as f: self.assertEqual(f.read(), icon_content) self.assertTrue( os.path.exists(self.snap_yaml), 'snap.yaml was not created') self.assertTrue( "DEPRECATED: 'icon' defined in snapcraft.yaml" in fake_logger.output, 'Missing deprecation message for icon') with open(self.snap_yaml) as f: y = yaml.load(f) self.assertFalse('icon' in y, 'icon found in snap.yaml {}'.format(y))
def test_create_meta_with_app(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'app1.sh'), 'w').close() self.config_data['apps'] = { 'app1': { 'command': 'app1.sh' }, } meta.create(self.config_data) app1_wrapper_path = os.path.join(self.snap_dir, 'command-app1.wrapper') self.assertTrue(os.path.exists(app1_wrapper_path), 'the wrapper for app1 was not setup correctly') self.assertTrue(os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) expected = { 'architectures': ['amd64'], 'apps': { 'app1': { 'command': 'command-app1.wrapper', }, }, 'description': 'my description', 'summary': 'my summary', 'name': 'my-package', 'version': '1.0' } self.assertEqual(y, expected)
def test_create_meta_with_vararg_config(self, mock_the_open, mock_wrap_exe): self.config_data['config'] = 'python3 my.py --config' meta.create(self.config_data, ['amd64']) self.mock_makedirs.assert_has_calls([ call(self.meta_dir, exist_ok=True), call(self.hooks_dir), ]) mock_the_open.assert_has_calls(self.expected_open_calls) mock_wrap_exe.assert_has_calls([ call( '$SNAP_APP_PATH/bin/bash', os.path.join(os.path.abspath(os.curdir), 'snap/bin/bash.wrapper'), ), call( 'python3', os.path.join(self.hooks_dir, 'config'), args=['my.py', '--config'], cwd='$SNAP_APP_PATH', ), ])
def test_create_meta_with_app(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'app1.sh'), 'w').close() self.config_data['apps'] = { 'app1': {'command': 'app1.sh'}, } meta.create(self.config_data) app1_wrapper_path = os.path.join(self.snap_dir, 'command-app1.wrapper') self.assertTrue( os.path.exists(app1_wrapper_path), 'the wrapper for app1 was not setup correctly') self.assertTrue( os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) expected = {'architectures': ['amd64'], 'apps': { 'app1': { 'command': 'command-app1.wrapper', }, }, 'description': 'my description', 'summary': 'my summary', 'name': 'my-package', 'version': '1.0'} self.assertEqual(y, expected)
def test_create_meta_with_implicit_migration_skill(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'app1.sh'), 'w').close() open(os.path.join(os.curdir, 'stub-sec'), 'w').close() self.config_data['apps'] = { 'app1': { 'command': 'app1.sh', 'uses': ['migration-skill'], }, } self.config_data['uses'] = { 'migration-skill': { 'security-policy': { 'apparmor': 'stub-sec', 'seccomp': 'stub-sec', }, }, } meta.create(self.config_data) app1_wrapper_path = os.path.join(self.snap_dir, 'command-app1.wrapper') self.assertTrue(os.path.exists(app1_wrapper_path), 'the wrapper for app1 was not setup correctly') sec_path = os.path.join(self.meta_dir, 'stub-sec') self.assertTrue( os.path.exists(sec_path), 'the security-policies for app1 were not setup correctly') self.assertTrue(os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) expected = { 'architectures': ['amd64'], 'apps': { 'app1': { 'command': 'command-app1.wrapper', 'uses': ['migration-skill'], }, }, 'uses': { 'migration-skill': { 'security-policy': { 'apparmor': 'meta/stub-sec', 'seccomp': 'meta/stub-sec', }, } }, 'description': 'my description', 'summary': 'my summary', 'name': 'my-package', 'version': '1.0' } self.assertEqual(y, expected)
def test_create_meta_without_config(self, mock_the_open, mock_wrap_exe): del self.config_data['config'] meta.create(self.config_data, ['amd64']) self.mock_makedirs.assert_called_once_with(self.meta_dir, exist_ok=True) mock_the_open.assert_has_calls(self.expected_open_calls)
def test_create_meta_with_config(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'config.sh'), 'w').close() self.config_data['config'] = 'config.sh' meta.create(self.config_data) self.assertTrue(os.path.exists(os.path.join(self.hooks_dir, 'config')), 'the config was not setup correctly')
def test_create_meta_with_config(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'config.sh'), 'w').close() self.config_data['config'] = 'config.sh' meta.create(self.config_data) self.assertTrue( os.path.exists(os.path.join(self.hooks_dir, 'config')), 'the config was not setup correctly')
def test_create_no_change_if_not_migration_skill(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'app1.sh'), 'w').close() open(os.path.join(os.curdir, 'stub-sec'), 'w').close() self.config_data['apps'] = { 'app1': { 'command': 'app1.sh', 'uses': ['migration'], }, } self.config_data['uses'] = { 'migration': { 'type': 'not-a-migration-skillz', 'security-policy': { 'apparmor': 'stub-sec', 'seccomp': 'stub-sec', }, }, } meta.create(self.config_data) self.assertTrue(os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) expected = { 'architectures': ['amd64'], 'apps': { 'app1': { 'command': 'command-app1.wrapper', 'uses': ['migration'], }, }, 'uses': { 'migration': { 'type': 'not-a-migration-skillz', 'security-policy': { 'apparmor': 'stub-sec', 'seccomp': 'stub-sec', }, } }, 'description': 'my description', 'summary': 'my summary', 'name': 'my-package', 'version': '1.0' } self.assertEqual(y, expected)
def snap(args): cmd(args) # This check is to support manual assembly. if not os.path.exists(os.path.join(common.get_snapdir(), 'meta')): arches = [snapcraft.common.get_arch(), ] config = _load_config() # FIXME this should be done in a more contained manner common.env = config.snap_env() meta.create(config.data, arches)
def test_create_meta_with_declared_icon_and_setup_ran_twice_ok(self): gui_path = os.path.join('setup', 'gui') os.makedirs(gui_path) icon_content = b'this is the icon' with open(os.path.join(gui_path, 'icon.png'), 'wb') as f: f.write(icon_content) open(os.path.join(os.curdir, 'my-icon.png'), 'w').close() self.config_data['icon'] = 'my-icon.png' meta.create(self.config_data) # Running again should be good meta.create(self.config_data)
def test_create_no_change_if_not_migration_skill(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'app1.sh'), 'w').close() open(os.path.join(os.curdir, 'stub-sec'), 'w').close() self.config_data['apps'] = { 'app1': { 'command': 'app1.sh', 'uses': ['migration'], }, } self.config_data['uses'] = { 'migration': { 'type': 'migration-skillz', 'security-policy': { 'apparmor': 'stub-sec', 'seccomp': 'stub-sec', }, }, } meta.create(self.config_data) self.assertTrue( os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) expected = {'architectures': ['amd64'], 'apps': { 'app1': { 'command': 'command-app1.wrapper', 'uses': ['migration'], }, }, 'uses': { 'migration': { 'type': 'migration-skillz', 'security-policy': { 'apparmor': 'stub-sec', 'seccomp': 'stub-sec', }, } }, 'description': 'my description', 'summary': 'my summary', 'name': 'my-package', 'version': '1.0'} self.assertEqual(y, expected)
def snap(args): cmd(args) config = _load_config() # TODO move all this to meta.create if 'architectures' in config.data: arches = config.data['architectures'] else: arches = [snapcraft.common.get_arch(), ] # FIXME this should be done in a more contained manner common.env = config.snap_env() meta.create(config.data, arches)
def test_create_meta_with_vararg_config(self, mock_the_open, mock_wrap_exe): self.config_data["config"] = "python3 my.py --config" meta.create(self.config_data, ["amd64"]) self.mock_makedirs.assert_has_calls([call(self.meta_dir, exist_ok=True), call(self.hooks_dir)]) mock_the_open.assert_has_calls(self.expected_open_calls) mock_wrap_exe.assert_has_calls( [ call("$SNAP_APP_PATH/bin/bash", os.path.join(os.path.abspath(os.curdir), "snap/bin/bash.wrapper")), call( "python3", os.path.join(self.hooks_dir, "config"), args=["my.py", "--config"], cwd="$SNAP_APP_PATH" ), ] )
def test_create_meta_with_license(self, mock_the_open, mock_wrap_exe): self.config_data.pop('config') self.config_data['license'] = 'LICENSE' meta.create(self.config_data) self.mock_makedirs.assert_has_calls([ call(self.meta_dir, exist_ok=True), call(self.hooks_dir), ]) self.mock_rename.assert_not_called() mock_the_open.assert_has_calls(self.expected_open_calls) self.mock_copyfile.assert_any_call( 'LICENSE', os.path.join(self.hooks_dir, 'license'))
def test_create_meta_with_icon(self): open(os.path.join(os.curdir, 'my-icon.png'), 'w').close() self.config_data['icon'] = 'my-icon.png' meta.create(self.config_data) self.assertTrue( os.path.exists(os.path.join(self.meta_dir, 'icon.png')), 'icon.png was not setup correctly') self.assertTrue(os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) self.assertFalse('icon' in y, 'icon found in snap.yaml {}'.format(y))
def test_create_meta(self): meta.create(self.config_data) self.assertTrue( os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) expected = {'architectures': ['amd64'], 'description': 'my description', 'summary': 'my summary', 'name': 'my-package', 'version': '1.0'} self.assertEqual(y, expected)
def test_create_meta_with_icon(self): open(os.path.join(os.curdir, 'my-icon.png'), 'w').close() self.config_data['icon'] = 'my-icon.png' meta.create(self.config_data) self.assertTrue( os.path.exists(os.path.join(self.meta_dir, 'icon.png')), 'icon.png was not setup correctly') self.assertTrue( os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) self.assertFalse('icon' in y, 'icon found in snap.yaml {}'.format(y))
def test_create_meta_with_license(self): open(os.path.join(os.curdir, 'LICENSE'), 'w').close() self.config_data['license'] = 'LICENSE' meta.create(self.config_data) self.assertTrue( os.path.exists(os.path.join(self.meta_dir, 'license.txt')), 'license.txt was not setup correctly') self.assertTrue( os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) self.assertFalse('license' in y, 'license found in snap.yaml {}'.format(y))
def test_create_meta_with_license(self): open(os.path.join(os.curdir, 'LICENSE'), 'w').close() self.config_data['license'] = 'LICENSE' meta.create(self.config_data) self.assertTrue( os.path.exists(os.path.join(self.meta_dir, 'license.txt')), 'license.txt was not setup correctly') self.assertTrue(os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) self.assertFalse('license' in y, 'license found in snap.yaml {}'.format(y))
def test_create_meta_with_config_with_args(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'config.sh'), 'w').close() self.config_data['config'] = 'config.sh something' meta.create(self.config_data) config_hook = os.path.join(self.hooks_dir, 'config') self.assertTrue(os.path.exists(config_hook), 'the config was not setup correctly') with open(config_hook) as f: config_wrapper = f.readlines() expected_wrapper = [ '#!/bin/sh\n', '\n', '\n', 'exec "$SNAP/config.sh" something $*\n' ] self.assertEqual(config_wrapper, expected_wrapper)
def test_create_meta_with_config_with_args(self): os.makedirs(self.snap_dir) open(os.path.join(self.snap_dir, 'config.sh'), 'w').close() self.config_data['config'] = 'config.sh something' meta.create(self.config_data) config_hook = os.path.join(self.hooks_dir, 'config') self.assertTrue( os.path.exists(config_hook), 'the config was not setup correctly') with open(config_hook) as f: config_wrapper = f.readlines() expected_wrapper = [ '#!/bin/sh\n', '\n', '\n', 'exec "$SNAP/config.sh" something $*\n'] self.assertEqual(config_wrapper, expected_wrapper)
def test_create_meta(self): meta.create(self.config_data) self.assertTrue(os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) expected = { 'architectures': ['amd64'], 'description': 'my description', 'summary': 'my summary', 'name': 'my-package', 'version': '1.0' } self.assertEqual(y, expected)
def test_create_meta(self, mock_the_open, mock_wrap_exe): meta.create(self.config_data, ["amd64"]) self.mock_makedirs.assert_has_calls([call(self.meta_dir, exist_ok=True), call(self.hooks_dir)]) mock_the_open.assert_has_calls(self.expected_open_calls) mock_wrap_exe.assert_has_calls( [ call("$SNAP_APP_PATH/bin/bash", os.path.join(os.path.abspath(os.curdir), "snap/bin/bash.wrapper")), call("bin/config", os.path.join(self.hooks_dir, "config"), args=[], cwd="$SNAP_APP_PATH"), ] ) self.mock_copyfile.assert_has_calls( [ call("my-icon.png", os.path.join(self.meta_dir, "my-icon.png")), call("file.apparmor", os.path.join(self.meta_dir, "file.apparmor")), call("file.seccomp", os.path.join(self.meta_dir, "file.seccomp")), ] )
def test_create_meta_with_license_in_setup(self): os.mkdir('setup') license_text = 'this is the license' with open(os.path.join('setup', 'license.txt'), 'w') as f: f.write(license_text) meta.create(self.config_data) expected_license = os.path.join(self.meta_dir, 'license.txt') self.assertTrue(os.path.exists(expected_license), 'license.txt was not setup correctly') with open(expected_license) as f: self.assertEqual(f.read(), license_text) self.assertTrue( os.path.exists(self.snap_yaml), 'snap.yaml was not created') with open(self.snap_yaml) as f: y = yaml.load(f) self.assertFalse('license' in y, 'license found in snap.yaml {}'.format(y))
def _create_meta(self, step, part_names): if step == 'strip' and part_names == self.config.part_names: common.env = self.config.snap_env() meta.create(self.config.data)