-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fix/add_storage_url_tests' into 'main'
fix: ignore local storage urls when generating partial mirror See merge request espressif/idf-component-manager!448
- Loading branch information
Showing
6 changed files
with
215 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import json | ||
import os | ||
|
||
from jsonref import requests | ||
from pytest import fixture, raises, warns | ||
|
||
from idf_component_tools.config import Config | ||
from idf_component_tools.constants import ( | ||
DEFAULT_NAMESPACE, | ||
IDF_COMPONENT_STAGING_REGISTRY_URL, | ||
) | ||
from idf_component_tools.messages import UserDeprecationWarning | ||
from idf_component_tools.registry.client_errors import APIClientError | ||
from idf_component_tools.registry.service_details import ( | ||
NoSuchProfile, | ||
get_api_client, | ||
get_profile, | ||
get_storage_client, | ||
) | ||
|
||
|
||
@fixture() | ||
def service_config(tmp_path): | ||
return Config().profiles.get('default', {}) | ||
|
||
|
||
@fixture() | ||
def config_path(tmp_path): | ||
config_path = os.path.join(str(tmp_path), 'idf_component_manager.yml') | ||
with open(config_path, 'w+') as f: | ||
f.write( | ||
json.dumps({ | ||
'profiles': { | ||
'default': { | ||
'registry_url': 'https://default.com/', | ||
'default_namespace': 'default_ns', | ||
'api_token': None, | ||
}, | ||
'test': { | ||
'registry_url': 'https://example.com/', | ||
'default_namespace': 'test', | ||
'api_token': 'token', | ||
}, | ||
'test2': { | ||
'registry_url': 'https://example2.com/', | ||
'default_namespace': 'test2', | ||
'api_token': 'token', | ||
}, | ||
'test3': { | ||
'registry_url': 'https://example3.com/', | ||
'default_namespace': 'test3', | ||
'api_token': 'token', | ||
}, | ||
'emptyprofile': None, | ||
} | ||
}) | ||
) | ||
return config_path | ||
|
||
|
||
class TestGetProfile: | ||
def test_get_profile_success(self, config_path): | ||
profile = get_profile('test', config_path) | ||
assert profile.registry_url == 'https://example.com/' | ||
assert profile.default_namespace == 'test' | ||
assert profile.api_token == 'token' | ||
|
||
def test_get_service_profile_env_dep(self, config_path, monkeypatch): | ||
monkeypatch.setenv('IDF_COMPONENT_SERVICE_PROFILE', 'test') | ||
with warns(UserDeprecationWarning): | ||
assert get_profile(None, config_path=config_path).default_namespace == 'test' | ||
|
||
def test_get_registry_profile_env_dep(self, config_path, monkeypatch): | ||
monkeypatch.setenv('IDF_COMPONENT_REGISTRY_PROFILE', 'test2') | ||
with warns(UserDeprecationWarning): | ||
assert get_profile(None, config_path=config_path).default_namespace == 'test2' | ||
|
||
def test_get_profile_env(self, config_path, monkeypatch): | ||
monkeypatch.setenv('IDF_COMPONENT_PROFILE', 'test3') | ||
assert get_profile(None, config_path=config_path).default_namespace == 'test3' | ||
|
||
def test_get_profile_not_exist(self, config_path): | ||
assert get_profile('not_exists', config_path) is None | ||
|
||
def test_get_profile_with_default_name(self, config_path): | ||
profile = get_profile('default', config_path) | ||
assert profile.registry_url == 'https://default.com/' | ||
assert profile.default_namespace == 'default_ns' | ||
assert profile.api_token is None | ||
|
||
|
||
class TestApiClient: | ||
def test_get_namespace_with_namespace(self): | ||
assert get_api_client(namespace='example').default_namespace == 'example' | ||
|
||
def test_get_namespace_from_profile(self, config_path): | ||
api_client = get_api_client(profile_name='test', config_path=config_path) | ||
assert api_client.default_namespace == 'test' | ||
|
||
def test_get_token_env(self, monkeypatch): | ||
monkeypatch.setenv('IDF_COMPONENT_API_TOKEN', 'some_token') | ||
|
||
assert get_api_client().api_token == 'some_token' | ||
|
||
def test_empty_env_profile(self, monkeypatch): | ||
monkeypatch.setenv('IDF_COMPONENT_PROFILE', '') | ||
with raises( | ||
NoSuchProfile, | ||
match='Profile "not_exists" not found in the idf_component_manager.yml config file', | ||
): | ||
get_api_client(profile_name='not_exists') | ||
|
||
def test_get_token_profile(self, config_path, monkeypatch): | ||
api_client = get_api_client(profile_name='test', config_path=config_path) | ||
assert api_client.api_token == 'token' | ||
|
||
def test_service_details_without_token(self, tmp_path): | ||
client = get_api_client(config_path=str(tmp_path), namespace='test') | ||
|
||
with raises(APIClientError, match='API token is required'): | ||
client.upload_version('file', 'component', '1.0.0') | ||
|
||
def test_service_details_with_empty_profile(self, config_path): | ||
client = get_api_client(config_path=config_path, profile_name='emptyprofile') | ||
with raises(APIClientError, match='API token is required'): | ||
client.upload_version('file', 'component', '1.0.0') | ||
|
||
|
||
class TestMultiStorageClient: | ||
def test_get_namespace_default(self): | ||
assert get_storage_client().default_namespace == DEFAULT_NAMESPACE | ||
|
||
def test_service_details_success(self, config_path): | ||
client = get_storage_client(profile_name='test', namespace='test', config_path=config_path) | ||
assert client.registry_url == 'https://example.com/' | ||
assert client.default_namespace == 'test' | ||
|
||
def test_service_details_namespace_not_exist(self, tmp_path): | ||
with raises(NoSuchProfile): | ||
get_storage_client(config_path=str(tmp_path), profile_name='not_exists') | ||
|
||
def test_service_details_without_profile(self, tmp_path): | ||
with raises(NoSuchProfile, match='Profile "test" not found*'): | ||
get_storage_client(config_path=str(tmp_path), profile_name='test', namespace='test') | ||
|
||
def test_get_component_registry_url_with_profile(self, monkeypatch, config_path): | ||
monkeypatch.setenv('IDF_COMPONENT_PROFILE', 'test') | ||
|
||
client = get_storage_client(config_path=config_path) | ||
|
||
assert client.registry_url == 'https://example.com/' | ||
assert client.storage_urls == [] | ||
|
||
def test_registry_storage_url(self): | ||
client = get_storage_client(registry_url=IDF_COMPONENT_STAGING_REGISTRY_URL) | ||
|
||
assert ( | ||
client.registry_storage_client.storage_url | ||
== requests.get(IDF_COMPONENT_STAGING_REGISTRY_URL + '/api').json()[ | ||
'components_base_url' | ||
] | ||
) | ||
|
||
def test_storage_clients_precedence(self): | ||
client = get_storage_client( | ||
registry_url=IDF_COMPONENT_STAGING_REGISTRY_URL, | ||
storage_urls=['https://something.else'], | ||
local_storage_urls=['file://local1', 'file://local2'], | ||
) | ||
|
||
assert client.storage_clients[0].storage_url == 'file://local1' | ||
assert client.storage_clients[1].storage_url == 'file://local2' | ||
assert client.storage_clients[2].storage_url == 'https://something.else' | ||
assert ( | ||
client.storage_clients[3].storage_url | ||
== requests.get(IDF_COMPONENT_STAGING_REGISTRY_URL + '/api').json()[ | ||
'components_base_url' | ||
] | ||
) |
Oops, something went wrong.