diff --git a/rapyuta_io/clients/device.py b/rapyuta_io/clients/device.py index df96af2b..ff153d1b 100644 --- a/rapyuta_io/clients/device.py +++ b/rapyuta_io/clients/device.py @@ -253,7 +253,8 @@ class Device(PartialMixin, ObjDict): def __init__(self, name, runtime=None, runtime_docker=False, runtime_preinstalled=False, ros_distro=None, rosbag_mount_path=None, - ros_workspace=None, description=None, python_version=DevicePythonVersion.PYTHON2): + ros_workspace=None, description=None, python_version=DevicePythonVersion.PYTHON2, + config_variables=None, labels=None): self.validate(name, runtime, runtime_docker, runtime_preinstalled, ros_distro, rosbag_mount_path, ros_workspace, description, python_version) self.name = name @@ -272,6 +273,8 @@ def __init__(self, name, runtime=None, runtime_docker=False, runtime_preinstalle self._ros_workspace = ros_workspace self.description = description self.python_version = python_version + self.config_variables = config_variables if config_variables else {} + self.labels = labels if labels else {} @staticmethod def validate(name, runtime, runtime_docker, runtime_preinstalled, ros_distro, rosbag_mount_path, @@ -320,6 +323,9 @@ def _serialize(self): '_ros_workspace']: if getattr(self, field): device['config_variables'][field[1:]] = getattr(self, field) + for key, value in self.config_variables.items(): + device['config_variables'][key] = value + device['labels'] = self.labels return device @classmethod diff --git a/sdk_test/device/create_device_test.py b/sdk_test/device/create_device_test.py index dd5bf192..de84e18a 100644 --- a/sdk_test/device/create_device_test.py +++ b/sdk_test/device/create_device_test.py @@ -17,8 +17,24 @@ def setUp(self): def test_create_device_docker_compose(self): self.logger.info('creating a device with dockercompose runtime') - device_object = Device(name='test-docker-device', runtime=DeviceRuntime.DOCKER, ros_distro=ROSDistro.MELODIC, - rosbag_mount_path='test/path', description='test-description') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-docker-device', + runtime=DeviceRuntime.DOCKER, + ros_distro=ROSDistro.MELODIC, + rosbag_mount_path='test/path', + description='test-description', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) self.assertEqual(device.name, 'test-docker-device') self.assertEqual(device.description, 'test-description') @@ -26,20 +42,42 @@ def test_create_device_docker_compose(self): 'runtime_docker': 'True', 'runtime_preinstalled': 'False', 'ros_distro': 'melodic', - 'rosbag_mount_path': 'test/path' + 'rosbag_mount_path': 'test/path', + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' } self.logger.info('validating the config variables of the created device') for config in device.config_variables: if config.key in expected_configs: self.assertEqual(expected_configs[config.key], config.value) + self.logger.info('validating the labels of the created device') + for label in device.labels: + if label.key in custom_labels: + self.assertEqual(custom_labels[label.key], label.value) self.logger.info('deleting the device') self.config.client.delete_device(device.deviceId) # The below test uses the new runtime config key def test_create_device_docker_compose_v2(self): self.logger.info('creating a device with dockercompose runtime') - device_object = Device(name='test-docker-device', runtime_docker=True, ros_distro=ROSDistro.MELODIC, - rosbag_mount_path='test/path', description='test-description') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-docker-device', + runtime_docker=True, + ros_distro=ROSDistro.MELODIC, + rosbag_mount_path='test/path', + description='test-description', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) self.assertEqual(device.name, 'test-docker-device') self.assertEqual(device.description, 'test-description') @@ -47,19 +85,41 @@ def test_create_device_docker_compose_v2(self): 'runtime_docker': 'True', 'runtime_preinstalled': 'False', 'ros_distro': 'melodic', - 'rosbag_mount_path': 'test/path' + 'rosbag_mount_path': 'test/path', + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' } self.logger.info('validating the config variables of the created device') for config in device.config_variables: if config.key in expected_configs: self.assertEqual(expected_configs[config.key], config.value) + self.logger.info('validating the labels of the created device') + for label in device.labels: + if label.key in custom_labels: + self.assertEqual(custom_labels[label.key], label.value) self.logger.info('deleting the device') self.config.client.delete_device(device.deviceId) def test_create_device_preinstalled(self): self.logger.info('creating a device with preinstalled runtime') - device_object = Device(name='test-preinstalled-device', runtime=DeviceRuntime.PREINSTALLED, - ros_distro=ROSDistro.MELODIC, ros_workspace='test/path', description='test-description') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-preinstalled-device', + runtime=DeviceRuntime.PREINSTALLED, + ros_distro=ROSDistro.MELODIC, + ros_workspace='test/path', + description='test-description', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) self.assertEqual(device.name, 'test-preinstalled-device') self.assertEqual(device.description, 'test-description') @@ -67,12 +127,18 @@ def test_create_device_preinstalled(self): 'runtime_docker': 'False', 'runtime_preinstalled': 'True', 'ros_distro': 'melodic', - 'ros_workspace': 'test/path' + 'ros_workspace': 'test/path', + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' } self.logger.info('validating the config variables of the created device') for config in device.config_variables: if config.key in expected_configs: self.assertEqual(expected_configs[config.key], config.value) + self.logger.info('validating the labels of the created device') + for label in device.labels: + if label.key in custom_labels: + self.assertEqual(custom_labels[label.key], label.value) self.logger.info('deleting the device') self.config.client.delete_device(device.deviceId) @@ -82,8 +148,24 @@ def test_onboard_device_print(self): "{url} && " \ "sudo bash start -r preinstalled -w test/path".format(url=url) self.logger.info('creating a device') - device_object = Device(name='test-onboard-device-print', runtime=DeviceRuntime.PREINSTALLED, - ros_distro=ROSDistro.MELODIC, ros_workspace='test/path', description='test-description') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-onboard-device-print', + runtime=DeviceRuntime.PREINSTALLED, + ros_distro=ROSDistro.MELODIC, + ros_workspace='test/path', + description='test-description', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) onboard_script = device.onboard_script() self.assertIsNotNone(re.match(onboard_script_regex, onboard_script.full_command())) @@ -97,8 +179,24 @@ def test_onboard_device_print_v2(self): "{url} && " \ "sudo bash start -r preinstalled -w test/path".format(url=url) self.logger.info('creating a device') - device_object = Device(name='test-onboard-device-print-2', runtime_preinstalled=True, - ros_distro=ROSDistro.MELODIC, ros_workspace='test/path', description='test-description') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-onboard-device-print-2', + runtime_preinstalled=True, + ros_distro=ROSDistro.MELODIC, + ros_workspace='test/path', + description='test-description', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) onboard_script = device.onboard_script() self.assertIsNotNone(re.match(onboard_script_regex, onboard_script.full_command())) @@ -111,9 +209,26 @@ def test_onboard_device_print_both_runtimes(self): "{url} && " \ "sudo bash start -r preinstalled -w test/path -r dockercompose -b test/path".format(url=url) self.logger.info('creating a device') - device_object = Device(name='test-onboard-device-print-both-runtimes', runtime_docker=True, runtime_preinstalled=True, - ros_distro=ROSDistro.MELODIC, ros_workspace='test/path', description='test-description', - rosbag_mount_path='test/path') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-onboard-device-print-both-runtimes', + runtime_docker=True, + runtime_preinstalled=True, + ros_distro=ROSDistro.MELODIC, + ros_workspace='test/path', + description='test-description', + rosbag_mount_path='test/path', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) onboard_script = device.onboard_script() self.assertIsNotNone(re.match(onboard_script_regex, onboard_script.full_command())) @@ -126,10 +241,25 @@ def test_onboard_script_run(self): def test_create_device_python3_preinstalled(self): self.logger.info('creating a device with python3 preinstalled runtime') - device_object = Device(name='test-preinstalled-device', runtime=DeviceRuntime.PREINSTALLED, - ros_distro=ROSDistro.MELODIC, ros_workspace='test/path', - python_version=DevicePythonVersion.PYTHON3, - description='test-description') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-preinstalled-device', + runtime=DeviceRuntime.PREINSTALLED, + ros_distro=ROSDistro.MELODIC, + ros_workspace='test/path', + python_version=DevicePythonVersion.PYTHON3, + description='test-description', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) self.assertEqual(device.name, 'test-preinstalled-device') self.assertEqual(device.description, 'test-description') @@ -138,22 +268,43 @@ def test_create_device_python3_preinstalled(self): 'runtime_docker': 'False', 'runtime_preinstalled': 'True', 'ros_distro': 'melodic', - 'ros_workspace': 'test/path' + 'ros_workspace': 'test/path', + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' } self.logger.info('validating the config variables of the created device') for config in device.config_variables: if config.key in expected_configs: self.assertEqual(expected_configs[config.key], config.value) + self.logger.info('validating the labels of the created device') + for label in device.labels: + if label.key in custom_labels: + self.assertEqual(custom_labels[label.key], label.value) self.logger.info('deleting the device') self.config.client.delete_device(device.deviceId) # The below test uses the new runtime config key def test_create_device_python3_preinstalled_v2(self): self.logger.info('creating a device with python3 preinstalled runtime v2') - device_object = Device(name='test-preinstalled-device-2', runtime_preinstalled=True, - ros_distro=ROSDistro.MELODIC, ros_workspace='test/path', - python_version=DevicePythonVersion.PYTHON3, - description='test-description') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-preinstalled-device-2', + runtime_preinstalled=True, + ros_distro=ROSDistro.MELODIC, + ros_workspace='test/path', + python_version=DevicePythonVersion.PYTHON3, + description='test-description', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) self.assertEqual(device.name, 'test-preinstalled-device-2') self.assertEqual(device.description, 'test-description') @@ -162,20 +313,42 @@ def test_create_device_python3_preinstalled_v2(self): 'runtime_preinstalled': 'True', 'runtime_docker': 'False', 'ros_distro': 'melodic', - 'ros_workspace': 'test/path' + 'ros_workspace': 'test/path', + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' } self.logger.info('validating the config variables of the created device') for config in device.config_variables: if config.key in expected_configs: self.assertEqual(expected_configs[config.key], config.value) + self.logger.info('validating the labels of the created device') + for label in device.labels: + if label.key in custom_labels: + self.assertEqual(custom_labels[label.key], label.value) self.logger.info('deleting the device') self.config.client.delete_device(device.deviceId) def test_upgrade_python2_to_python3_device(self): self.logger.info('creating a device on python2 with dockercompose runtime') - device_object = Device(name='test-docker-device', runtime=DeviceRuntime.DOCKER, ros_distro=ROSDistro.MELODIC, - rosbag_mount_path='test/path', python_version=DevicePythonVersion.PYTHON2, - description='test-description') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-docker-device', + runtime=DeviceRuntime.DOCKER, + ros_distro=ROSDistro.MELODIC, + rosbag_mount_path='test/path', + python_version=DevicePythonVersion.PYTHON2, + description='test-description', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) self.assertEqual(device.name, 'test-docker-device') self.assertEqual(device.description, 'test-description') @@ -184,12 +357,18 @@ def test_upgrade_python2_to_python3_device(self): 'runtime_docker': 'True', 'runtime_preinstalled': 'False', 'ros_distro': 'melodic', - 'rosbag_mount_path': 'test/path' + 'rosbag_mount_path': 'test/path', + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' } self.logger.info('validating the config variables of the created device') for config in device.config_variables: if config.key in expected_configs: self.assertEqual(expected_configs[config.key], config.value) + self.logger.info('validating the labels of the created device') + for label in device.labels: + if label.key in custom_labels: + self.assertEqual(custom_labels[label.key], label.value) device.upgrade() device = self.config.client.get_device(device.deviceId) self.assertEqual(device.python_version, DevicePythonVersion.PYTHON3) @@ -198,11 +377,27 @@ def test_upgrade_python2_to_python3_device(self): def test_create_device_python3_both_runtimes(self): self.logger.info('creating a python3 device with both the runtimes enabled runtime') - device_object = Device(name='test-both-runtimes-device', runtime_docker=True, - runtime_preinstalled=True, rosbag_mount_path='test/path', - ros_distro=ROSDistro.MELODIC, ros_workspace='test/path', - python_version=DevicePythonVersion.PYTHON3, - description='test-description') + custom_config_variables = { + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' + } + custom_labels = { + 'custom-label-1': 'label1', + 'custom-label-2': 'label2', + 'custom-label-3': 'label3' + } + device_object = Device( + name='test-both-runtimes-device', + runtime_docker=True, + runtime_preinstalled=True, + rosbag_mount_path='test/path', + ros_distro=ROSDistro.MELODIC, + ros_workspace='test/path', + python_version=DevicePythonVersion.PYTHON3, + description='test-description', + config_variables=custom_config_variables, + labels=custom_labels + ) device = self.config.client.create_device(device_object) self.assertEqual(device.name, 'test-both-runtimes-device') self.assertEqual(device.description, 'test-description') @@ -212,12 +407,18 @@ def test_create_device_python3_both_runtimes(self): 'runtime_preinstalled': 'True', 'ros_distro': 'melodic', 'ros_workspace': 'test/path', - 'rosbag_mount_path': 'test/path' + 'rosbag_mount_path': 'test/path', + 'custom-config-variable-1': 'value1', + 'custom-config-variable-2': 'value2' } self.logger.info('validating the config variables of the created device') for config in device.config_variables: if config.key in expected_configs: self.assertEqual(expected_configs[config.key], config.value) + self.logger.info('validating the labels of the created device') + for label in device.labels: + if label.key in custom_labels: + self.assertEqual(custom_labels[label.key], label.value) self.logger.info('deleting the device') self.config.client.delete_device(device.deviceId)