def test_service_creator_autocli(sonic_fs, manifest, mock_cli_gen, mock_config_mgmt, service_creator): test_yang = 'TEST YANG' test_yang_module = 'sonic-test' manifest['cli']['auto-generate-show'] = True manifest['cli']['auto-generate-config'] = True entry = PackageEntry('test', 'azure/sonic-test') package = Package(entry, Metadata(manifest, yang_module_str=test_yang)) mock_config_mgmt.get_module_name = Mock(return_value=test_yang_module) service_creator.create(package) mock_cli_gen.generate_cli_plugin.assert_has_calls( [ call('show', test_yang_module), call('config', test_yang_module), ], any_order=True ) service_creator.remove(package) mock_cli_gen.remove_cli_plugin.assert_has_calls( [ call('show', test_yang_module), call('config', test_yang_module), ], any_order=True )
def test_service_creator_initial_config(sonic_fs, manifest, mock_feature_registry, mock_sonic_db): mock_table = Mock() mock_table.get = Mock(return_value=(True, (('field_2', 'original_value_2'), ))) mock_sonic_db.initial_table = Mock(return_value=mock_table) mock_sonic_db.persistent_table = Mock(return_value=mock_table) mock_sonic_db.running_table = Mock(return_value=mock_table) creator = ServiceCreator(mock_feature_registry, mock_sonic_db) entry = PackageEntry('test', 'azure/sonic-test') package = Package(entry, Metadata(manifest)) creator.create(package) assert not sonic_fs.exists(os.path.join(DEBUG_DUMP_SCRIPT_LOCATION, 'test')) manifest['package']['init-cfg'] = { 'TABLE_A': { 'key_a': { 'field_1': 'value_1', 'field_2': 'value_2' }, }, } package = Package(entry, Metadata(manifest)) creator.create(package) mock_table.set.assert_called_with('key_a', [('field_1', 'value_1'), ('field_2', 'original_value_2')]) creator.remove(package) mock_table._del.assert_called_with('key_a')
def get_package(self) -> Package: """ Returns SONiC Package based on manifest. Returns: SONiC Package """ metadata = self.get_metadata() manifest = metadata.manifest name = manifest['package']['name'] description = manifest['package']['description'] # Will be resolved in install() method. # When installing from tarball we don't know yet # the repository for this package. repository = None if self.database.has_package(name): # inherit package database info package_entry = self.database.get_package(name) else: package_entry = PackageEntry(name, repository, description=description) return Package(package_entry, metadata)
def test_service_creator_with_timer_unit(sonic_fs, manifest, service_creator): entry = PackageEntry('test', 'azure/sonic-test') package = Package(entry, Metadata(manifest)) service_creator.create(package) assert not sonic_fs.exists(os.path.join(SYSTEMD_LOCATION, 'test.timer')) manifest['service']['delayed'] = True package = Package(entry, Metadata(manifest)) service_creator.create(package) assert sonic_fs.exists(os.path.join(SYSTEMD_LOCATION, 'test.timer'))
def test_service_creator_with_debug_dump(sonic_fs, manifest, service_creator): entry = PackageEntry('test', 'azure/sonic-test') package = Package(entry, Metadata(manifest)) service_creator.create(package) assert not sonic_fs.exists(os.path.join(DEBUG_DUMP_SCRIPT_LOCATION, 'test')) manifest['package']['debug-dump'] = '/some/command' package = Package(entry, Metadata(manifest)) service_creator.create(package) assert sonic_fs.exists(os.path.join(DEBUG_DUMP_SCRIPT_LOCATION, 'test'))
def test_service_creator_yang(sonic_fs, manifest, mock_sonic_db, mock_config_mgmt, service_creator): test_yang = 'TEST YANG' test_yang_module = 'sonic-test' mock_connector = Mock() mock_sonic_db.get_connectors = Mock(return_value=[mock_connector]) mock_connector.get_table = Mock( return_value={'key_a': { 'field_1': 'value_1' }}) mock_connector.get_config = Mock( return_value={ 'TABLE_A': mock_connector.get_table(''), 'TABLE_B': mock_connector.get_table(''), 'TABLE_C': mock_connector.get_table(''), }) entry = PackageEntry('test', 'azure/sonic-test') package = Package(entry, Metadata(manifest, yang_module_str=test_yang)) service_creator.create(package) mock_config_mgmt.add_module.assert_called_with(test_yang) mock_config_mgmt.get_module_name = Mock(return_value=test_yang_module) manifest['package']['init-cfg'] = { 'TABLE_A': { 'key_a': { 'field_1': 'new_value_1', 'field_2': 'value_2' }, }, } package = Package(entry, Metadata(manifest, yang_module_str=test_yang)) service_creator.create(package) mock_config_mgmt.add_module.assert_called_with(test_yang) mock_connector.mod_entry.assert_called_once_with('TABLE_A', 'key_a', { 'field_1': 'value_1', 'field_2': 'value_2' }) mock_config_mgmt.sy.confDbYangMap = { 'TABLE_A': { 'module': test_yang_module } } service_creator.remove(package) mock_connector.set_entry.assert_called_with('TABLE_A', 'key_a', None) mock_config_mgmt.remove_module.assert_called_with(test_yang_module)
def test_service_creator(sonic_fs, manifest, mock_feature_registry, mock_sonic_db): creator = ServiceCreator(mock_feature_registry, mock_sonic_db) entry = PackageEntry('test', 'azure/sonic-test') package = Package(entry, Metadata(manifest)) creator.create(package) assert sonic_fs.exists(os.path.join(ETC_SONIC_PATH, 'swss_dependent')) assert sonic_fs.exists(os.path.join(DOCKER_CTL_SCRIPT_LOCATION, 'test.sh')) assert sonic_fs.exists( os.path.join(SERVICE_MGMT_SCRIPT_LOCATION, 'test.sh')) assert sonic_fs.exists(os.path.join(SYSTEMD_LOCATION, 'test.service'))
def add_package(content, metadata_resolver, repository, reference, **kwargs): metadata = metadata_resolver.from_registry(repository, reference) name = metadata.manifest['package']['name'] version = metadata.manifest['package']['version'] installed = kwargs.get('installed', False) built_in = kwargs.get('built-in', False) if installed and not built_in and 'image_id' not in kwargs: kwargs['image_id'] = f'{repository}:{reference}' if installed and 'version' not in kwargs: kwargs['version'] = version content[name] = PackageEntry(name, repository, **kwargs)
def test_service_creator(sonic_fs, manifest, service_creator, package_manager): entry = PackageEntry('test', 'azure/sonic-test') package = Package(entry, Metadata(manifest)) installed_packages = package_manager._get_installed_packages_and(package) service_creator.create(package) service_creator.generate_shutdown_sequence_files(installed_packages) assert sonic_fs.exists(os.path.join(ETC_SONIC_PATH, 'swss_dependent')) assert sonic_fs.exists(os.path.join(DOCKER_CTL_SCRIPT_LOCATION, 'test.sh')) assert sonic_fs.exists(os.path.join(SERVICE_MGMT_SCRIPT_LOCATION, 'test.sh')) assert sonic_fs.exists(os.path.join(SYSTEMD_LOCATION, 'test.service')) def read_file(name): with open(os.path.join(ETC_SONIC_PATH, name)) as file: return file.read() assert read_file('warm-reboot_order') == 'swss teamd test syncd' assert read_file('fast-reboot_order') == 'teamd test swss syncd' assert read_file('test_reconcile') == 'test-process test-process-3'
def test_database_update_package_non_existing(fake_db): test_package = PackageEntry('abc', 'abc') with pytest.raises(PackageNotFoundError): fake_db.update_package(test_package)