def test_load_dna(self): # test with a valid DNA file dna_to_test = DnaLoader(self.dna_test_file)._load_dna() self.assertTrue(isinstance(dna_to_test, Dna)) # test with a non valid DNA file dna_invalid_test_file = get_test_path("modules/test_invalid_dna.yml") self.assertIsNone(DnaLoader(dna_invalid_test_file)._load_dna())
def test_get_dna(self): expected_result = Dna() expected_result.name = "neuron_test" expected_result.module_type = "neuron" expected_result.tags = ['test'] expected_result.author = 'Kalliope project team' expected_result.kalliope_supported_version = [0.4] dna_to_test = DnaLoader(self.dna_test_file).get_dna() self.assertTrue(dna_to_test.__eq__(expected_result))
def test_load_dna(self): # test with a valid DNA file dna_to_test = DnaLoader(self.dna_test_file)._load_dna() self.assertTrue(isinstance(dna_to_test, Dna)) # test with a non valid DNA file if "/Tests" in os.getcwd(): dna_invalid_test_file = "modules/test_invalid_dna.yml" else: dna_invalid_test_file = "Tests/modules/test_invalid_dna.yml" self.assertIsNone(DnaLoader(dna_invalid_test_file)._load_dna())
def test_get_yaml_config(self): expected_result = { 'kalliope_supported_version': [0.4], 'author': 'Kalliope project team', 'type': 'neuron', 'name': 'neuron_test', 'tags': ['test'] } dna_file_content = DnaLoader(self.dna_test_file).get_yaml_config() self.assertEqual(dna_file_content, expected_result)
def install(self): """ Module installation method. """ # first, we clone the repo self._clone_repo(path=self.tmp_path, git_url=self.git_url) # check the content of the cloned repo if self.is_repo_ok(dna_file_path=self.dna_file_path, install_file_path=self.install_file_path): # Load the dna.yml file self.dna = DnaLoader(self.dna_file_path).get_dna() if self.dna is not None: logger.debug("[ResourcesManager] DNA file content: " + str(self.dna)) if self.is_settings_ok(resources=self.settings.resources, dna=self.dna): # the dna file is ok, check the supported version if self._check_supported_version( current_version=self.settings.kalliope_version, supported_versions=self.dna. kalliope_supported_version): # Let's find the target folder depending the type module_type = self.dna.module_type.lower() target_folder = self._get_target_folder( resources=self.settings.resources, module_type=module_type) if target_folder is not None: # let's move the tmp folder in the right folder and get a new path for the module module_name = self.dna.name.lower() target_path = self._rename_temp_folder( name=self.dna.name.lower(), target_folder=target_folder, tmp_path=self.tmp_path) # if the target_path exists, then run the install file within the new repository if target_path is not None: self.install_file_path = target_path + os.sep + INSTALL_FILE_NAME self.run_ansible_playbook_module( install_file_path=self.install_file_path) Utils.print_success("Module: %s installed" % module_name) else: logger.debug( "[ResourcesManager] installation cancelled, deleting temp repo %s" % str(self.tmp_path)) shutil.rmtree(self.tmp_path)
def test_check_dna(self): # check with valid DNA file test_dna = { 'kalliope_supported_version': [0.4], 'author': 'Kalliope project team', 'type': 'neuron', 'name': 'neuron_test', 'tags': ['test'] } self.assertTrue( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA, non existing resource type test_dna = { 'kalliope_supported_version': [0.5], 'author': 'Kalliope project team', 'type': 'non-existing', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # valid DNA, existing resource type list_valid_dna = ["neuron", "stt", "tts", "trigger", "signal"] for valid_resource in list_valid_dna: test_dna = { 'kalliope_supported_version': [0.5], 'author': 'Kalliope project team', 'type': valid_resource, 'name': '%s_test' % valid_resource, 'tags': ['test'] } self.assertTrue( DnaLoader( file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA file, no name test_dna = { 'kalliope_supported_version': [0.4], 'author': 'Kalliope project team', 'type': 'neuron', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA file, no type test_dna = { 'kalliope_supported_version': [0.4], 'author': 'Kalliope project team', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA, wrong type test_dna = { 'kalliope_supported_version': [0.4], 'author': 'Kalliope project team', 'type': 'doesnotexist', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA, no kalliope_supported_version test_dna = { 'author': 'Kalliope project team', 'type': 'neuron', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA, kalliope_supported_version empty test_dna = { 'kalliope_supported_version': [], 'author': 'Kalliope project team', 'type': 'neuron', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA, kalliope_supported_version wrong format test_dna = { 'kalliope_supported_version': ['0.4.1'], 'author': 'Kalliope project team', 'type': 'neuron', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna))
def test_check_dna(self): # check with valid DNA file test_dna = { 'kalliope_supported_version': [0.4], 'author': 'Kalliope project team', 'type': 'neuron', 'name': 'neuron_test', 'tags': ['test'] } self.assertTrue( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA file, no name test_dna = { 'kalliope_supported_version': [0.4], 'author': 'Kalliope project team', 'type': 'neuron', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA file, no type test_dna = { 'kalliope_supported_version': [0.4], 'author': 'Kalliope project team', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA, wrong type test_dna = { 'kalliope_supported_version': [0.4], 'author': 'Kalliope project team', 'type': 'doesnotexist', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA, no kalliope_supported_version test_dna = { 'author': 'Kalliope project team', 'type': 'neuron', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA, kalliope_supported_version empty test_dna = { 'kalliope_supported_version': [], 'author': 'Kalliope project team', 'type': 'neuron', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna)) # invalid DNA, kalliope_supported_version wrong format test_dna = { 'kalliope_supported_version': ['0.4.1'], 'author': 'Kalliope project team', 'type': 'neuron', 'name': 'neuron_test', 'tags': ['test'] } self.assertFalse( DnaLoader(file_path=self.dna_test_file)._check_dna_file(test_dna))
def install(self, force=False): """ Module installation method. :arg force: True to skip the version compatibility :return Dna object if resource installed """ # first, we clone the repo self._clone_repo(path=self.tmp_path, git_url=self.git_url) # check the content of the cloned repo if not self.is_repo_ok(dna_file_path=self.dna_file_path, install_file_path=self.install_file_path): logger.debug("[ResourcesManager] Invalid resource repository") self.cleanup_after_failed_installation() raise ResourcesManagerException("Invalid resource repository") # Load the dna.yml file self.dna = DnaLoader(self.dna_file_path).get_dna() if self.dna is None: logger.debug( "[ResourcesManager] No DNA file found in the resource to install" ) self.cleanup_after_failed_installation() raise ResourcesManagerException( "No DNA file found in the resource to install") logger.debug("[ResourcesManager] DNA file content: " + str(self.dna)) if not self.is_settings_ok(resources=self.settings.resources, dna=self.dna): logger.debug("[ResourcesManager] Invalid settings") self.cleanup_after_failed_installation() raise ResourcesManagerException("Invalid settings") # the dna file is ok, check the supported version if not force: if not self._check_supported_version( current_version=self.settings.kalliope_version, supported_versions=self.dna.kalliope_supported_version): logger.debug("[ResourcesManager] Non supported version") self.cleanup_after_failed_installation() raise ResourcesManagerException("Non supported version") # Let's find the target folder depending the type module_type = self.dna.module_type.lower() target_folder = self._get_target_folder( resources=self.settings.resources, module_type=module_type) if target_folder is None: self.cleanup_after_failed_installation() raise ResourcesManagerException( "No resource folder set in settings") # let's move the tmp folder in the right folder and get a new path for the module module_name = self.dna.name.lower() target_path = self._rename_temp_folder(name=self.dna.name.lower(), target_folder=target_folder, tmp_path=self.tmp_path) # if the target_path exists, then run the install file within the new repository if target_path is None: raise ResourcesManagerException("Resource already present") else: self.install_file_path = target_path + os.sep + INSTALL_FILE_NAME if self.run_ansible_playbook_module( install_file_path=self.install_file_path): Utils.print_success("Module: %s installed" % module_name) return self.dna else: Utils.print_danger("Module: %s not installed" % module_name) return None