def get_configuration(self): """Returns the job configuration represented by this JSON :returns: The job configuration :rtype: :class:`job.configuration.configuration.JobConfiguration`: """ config = JobConfiguration() for name, mount_dict in self._config['mounts'].items(): if mount_dict['type'] == 'host': config.add_mount(HostMountConfig(name, mount_dict['host_path'])) elif mount_dict['type'] == 'volume': config.add_mount( VolumeMountConfig(name, mount_dict['driver'], mount_dict['driver_opts'])) default_workspace = self._config['output_workspaces']['default'] if default_workspace: config.default_output_workspace = default_workspace for output, workspace in self._config['output_workspaces'][ 'outputs'].items(): config.add_output_workspace(output, workspace) config.priority = self._config['priority'] for name, value in self._config['settings'].items(): config.add_setting(name, value) return config
def test_convert_config_to_v2_json(self): """Tests calling convert_config_to_v2_json()""" # Try configuration with nothing set config = JobConfiguration() json = convert_config_to_v2_json(config) JobConfigurationV2(configuration=json.get_dict(), do_validate=True) # Revalidate # Try configuration with a variety of values config = JobConfiguration() config.add_mount(HostMountConfig('mount_1', '/the/host/path')) config.add_mount( VolumeMountConfig('mount_2', 'driver', { 'opt_1': 'foo', 'opt_2': 'bar' })) config.default_output_workspace = 'workspace_1' config.add_output_workspace('output_2', 'workspace_2') config.priority = 999 config.add_setting('setting_1', 'Hello') config.add_setting('setting_2', 'Scale!') json = convert_config_to_v2_json(config) JobConfigurationV2(configuration=json.get_dict(), do_validate=True) # Revalidate self.assertSetEqual(set(json.get_dict()['mounts'].keys()), {'mount_1', 'mount_2'}) self.assertSetEqual(set(json.get_dict()['settings'].keys()), {'setting_1', 'setting_2'})
def test_add_mount(self): """Tests calling JobConfiguration.add_mount()""" configuration = JobConfiguration() host_mount = HostMountConfig('mount_1', '/the/host/path') configuration.add_mount(host_mount) vol_mount = VolumeMountConfig('mount_1', driver='driver', driver_opts={}) with self.assertRaises(InvalidJobConfiguration) as context: configuration.add_mount(vol_mount) self.assertEqual(context.exception.error.name, 'DUPLICATE_MOUNT')
def test_merge_recipe_config(self): """Tests calling JobConfiguration.merge_recipe_config()""" configuration = JobConfiguration() host_mount = HostMountConfig('mount_1', '/the/host/path') configuration.add_mount(host_mount) configuration.add_output_workspace('output_1', 'workspace_1') configuration.add_setting('setting_1', 'value_1') recipe_config = { 'version': '6', 'mounts': { 'mount_1': { 'type': 'host', 'host_path': '/the/host/path2' }, 'mount_2': { 'type': 'volume', 'driver': 'driver', 'driver_opts': { 'opt_1': 'foo', 'opt_2': 'bar' } } }, 'output_workspaces': { 'default': 'workspace_1', 'outputs': { 'output': 'workspace_2' } }, 'priority': 999, 'settings': {} } configuration.merge_recipe_config(recipe_config) self.assertEqual(configuration.default_output_workspace, 'workspace_1') self.assertEqual(configuration.priority, 999) self.assertDictEqual(configuration.output_workspaces, { 'output': 'workspace_2', 'output_1': 'workspace_1' }) self.assertItemsEqual(configuration.mounts.keys(), ['mount_1', 'mount_2']) self.assertEqual(configuration.mounts['mount_1'].host_path, '/the/host/path2') self.assertDictEqual(configuration.mounts['mount_2'].driver_opts, { 'opt_1': 'foo', 'opt_2': 'bar' }) self.assertDictEqual(configuration.settings, {'setting_1': 'value_1'})
def test_validate_mounts(self): """Tests calling JobConfiguration.validate() to validate mount configuration""" manifest_dict = { 'seedVersion': '1.0.0', 'job': { 'name': 'random-number-gen', 'jobVersion': '0.1.0', 'packageVersion': '0.1.0', 'title': 'Random Number Generator', 'description': 'Generates a random number and outputs on stdout', 'maintainer': { 'name': 'John Doe', 'email': '*****@*****.**' }, 'timeout': 10, 'interface': { 'mounts': [{ 'name': 'mount_a', 'path': '/the/a/path' }, { 'name': 'mount_b', 'path': '/the/b/path' }, { 'name': 'mount_c', 'path': '/the/c/path' }] } } } manifest = SeedManifest(manifest_dict) configuration = JobConfiguration() configuration.add_mount(HostMountConfig('mount_a', '/the/host/a/path')) configuration.add_mount(HostMountConfig('mount_b', '/the/host/b/path')) configuration.add_mount(HostMountConfig('mount_c', '/the/host/c/path')) configuration.add_mount(HostMountConfig('mount_d', '/the/host/d/path')) warnings = configuration.validate(manifest) self.assertEqual(len(warnings), 1) self.assertEqual(warnings[0].name, 'UNKNOWN_MOUNT') manifest_dict = { 'seedVersion': '1.0.0', 'job': { 'name': 'random-number-gen', 'jobVersion': '0.1.0', 'packageVersion': '0.1.0', 'title': 'Random Number Generator', 'description': 'Generates a random number and outputs on stdout', 'maintainer': { 'name': 'John Doe', 'email': '*****@*****.**' }, 'timeout': 10, 'interface': { 'mounts': [{ 'name': 'mount_a', 'path': '/the/a/path' }, { 'name': 'mount_b', 'path': '/the/b/path' }, { 'name': 'mount_c', 'path': '/the/c/path' }] } } } manifest = SeedManifest(manifest_dict) configuration = JobConfiguration() configuration.add_mount(HostMountConfig('mount_a', '/the/host/a/path')) configuration.add_mount(HostMountConfig('mount_b', '/the/host/b/path')) warnings = configuration.validate(manifest) self.assertEqual(len(warnings), 1) self.assertEqual(warnings[0].name, 'MISSING_MOUNT')