def test_access_and_secret_key_should_be_set_to_get_default_config(mocker, cfg_params): mocker.patch('ai.backend.client.config._config', None) # Neither SORNA_ACCESS_KEY nor SORNA_SECRET_KEY exists. mocker.patch('os.environ', {}) with pytest.raises(KeyError) as e: get_config() err_key = str(e.value) assert 'ACCESS_KEY' in err_key or 'SECRET_KEY' in err_key # SORNA_ACCESS_KEY exists, but not SORNA_SECRET_KEY mocker.patch('os.environ', {'BACKEND_ACCESS_KEY': cfg_params['access_key']}) with pytest.raises(KeyError) as e: get_config() assert 'SECRET_KEY' in str(e.value) # SORNA_SECRET_KEY exists, but not SORNA_ACCESS_KEY mocker.patch('os.environ', {'BACKEND_SECRET_KEY': cfg_params['secret_key']}) with pytest.raises(KeyError) as e: get_config() assert 'ACCESS_KEY' in str(e.value) # Both keys exist. No exception should be raised. mocker.patch('os.environ', { 'BACKEND_ACCESS_KEY': cfg_params['access_key'], 'BACKEND_SECRET_KEY': cfg_params['secret_key'] }) get_config()
def test_access_and_secret_key_should_be_set_to_get_default_config( mocker, cfg_params): mocker.patch('ai.backend.client.config._config', None) # Neither SORNA_ACCESS_KEY nor SORNA_SECRET_KEY exists. mocker.patch('os.environ', {}) with pytest.raises(KeyError) as e: get_config() err_key = str(e.value) assert 'ACCESS_KEY' in err_key or 'SECRET_KEY' in err_key # SORNA_ACCESS_KEY exists, but not SORNA_SECRET_KEY mocker.patch('os.environ', {'BACKEND_ACCESS_KEY': cfg_params['access_key']}) with pytest.raises(KeyError) as e: get_config() assert 'SECRET_KEY' in str(e.value) # SORNA_SECRET_KEY exists, but not SORNA_ACCESS_KEY mocker.patch('os.environ', {'BACKEND_SECRET_KEY': cfg_params['secret_key']}) with pytest.raises(KeyError) as e: get_config() assert 'ACCESS_KEY' in str(e.value) # Both keys exist. No exception should be raised. mocker.patch( 'os.environ', { 'BACKEND_ACCESS_KEY': cfg_params['access_key'], 'BACKEND_SECRET_KEY': cfg_params['secret_key'] }) get_config()
def test_set_and_get_config(mocker, cfg_params): # Mocking the global variable ``_config``. # The value of a global variable will affect other test cases. mocker.patch('ai.backend.client.config._config', None) cfg = APIConfig(**cfg_params) set_config(cfg) assert get_config() == cfg
def test_set_and_get_config(mocker, cfg_params): # Mocking the global variable ``_config``. # The value of a global variable will affect other test cases. mocker.patch('ai.backend.client.config._config', None) cfg = APIConfig(**cfg_params) set_config(cfg) assert get_config() == cfg
async def test_manipulate_resource_policy(self): access_key = get_config().access_key rpname = 'testrp-' + uuid.uuid4().hex with Session() as sess: try: rp = sess.ResourcePolicy(access_key) assert rp.info(rpname) is None rps = sess.ResourcePolicy.list() original_count = len(rps) # Create resource policy sess.ResourcePolicy.create(name=rpname, default_for_unspecified='LIMITED', total_resource_slots='{}', max_concurrent_sessions=1, max_containers_per_session=1, max_vfolder_count=1, max_vfolder_size=1, idle_timeout=1, allowed_vfolder_hosts=['local']) rps = sess.ResourcePolicy.list() assert len(rps) == original_count + 1 info = rp.info(rpname) assert info['name'] == rpname assert info['total_resource_slots'] == '{}' assert info['max_concurrent_sessions'] == 1 assert info['max_vfolder_count'] == 1 assert info['max_vfolder_size'] == 1 assert info['idle_timeout'] == 1 # Update resource policy sess.ResourcePolicy.update( name=rpname, default_for_unspecified='LIMITED', total_resource_slots='{"cpu": "count"}', max_concurrent_sessions=2, max_containers_per_session=2, max_vfolder_count=2, max_vfolder_size=2, idle_timeout=2, allowed_vfolder_hosts=['local']) rps = sess.ResourcePolicy.list() assert len(rps) == original_count + 1 info = rp.info(rpname) assert info['name'] == rpname assert info['total_resource_slots'] == '{"cpu": "count"}' assert info['max_concurrent_sessions'] == 2 assert info['max_vfolder_count'] == 2 assert info['max_vfolder_size'] == 2 assert info['idle_timeout'] == 2 # Delete ResourcePolicy sess.ResourcePolicy.delete(rpname) rps = sess.ResourcePolicy.list() assert len(rps) == original_count except Exception: sess.ResourcePolicy.delete(rpname) raise
def test_config(capsys, mocker): mocker.patch.object(sys, 'argv', ['backend.ai', 'config']) config = get_config() main() out, _ = capsys.readouterr() assert str(config.endpoint) in out assert config.version in out assert config.access_key in out assert config.secret_key[:6] in out assert config.hash_type in out
def test_build_correct_url(mock_request_params): config = get_config() canonical_url = str(config.endpoint).rstrip('/') + '/function?app=999' mock_request_params['path'] = '/function' rqst = Request(**mock_request_params) assert str(rqst._build_url()) == canonical_url mock_request_params['path'] = 'function' rqst = Request(**mock_request_params) assert str(rqst._build_url()) == canonical_url
async def test_manipulate_resource_policy(self): access_key = get_config().access_key rpname = 'testrp-' + uuid.uuid4().hex with Session() as sess: try: rp = sess.ResourcePolicy(access_key) assert rp.info(rpname) is None rps = sess.ResourcePolicy.list() original_count = len(rps) # Create resource policy sess.ResourcePolicy.create( name=rpname, default_for_unspecified='LIMITED', total_resource_slots='{}', max_concurrent_sessions=1, max_containers_per_session=1, max_vfolder_count=1, max_vfolder_size=1, idle_timeout=1, allowed_vfolder_hosts=['local'] ) rps = sess.ResourcePolicy.list() assert len(rps) == original_count + 1 info = rp.info(rpname) assert info['name'] == rpname assert info['total_resource_slots'] == '{}' assert info['max_concurrent_sessions'] == 1 assert info['max_vfolder_count'] == 1 assert info['max_vfolder_size'] == 1 assert info['idle_timeout'] == 1 # Update resource policy sess.ResourcePolicy.update( name=rpname, default_for_unspecified='LIMITED', total_resource_slots='{"cpu": "count"}', max_concurrent_sessions=2, max_containers_per_session=2, max_vfolder_count=2, max_vfolder_size=2, idle_timeout=2, allowed_vfolder_hosts=['local'] ) rps = sess.ResourcePolicy.list() assert len(rps) == original_count + 1 info = rp.info(rpname) assert info['name'] == rpname assert info['total_resource_slots'] == '{"cpu": "count"}' assert info['max_concurrent_sessions'] == 2 assert info['max_vfolder_count'] == 2 assert info['max_vfolder_size'] == 2 assert info['idle_timeout'] == 2 # Delete ResourcePolicy sess.ResourcePolicy.delete(rpname) rps = sess.ResourcePolicy.list() assert len(rps) == original_count except Exception: sess.ResourcePolicy.delete(rpname) raise
def test_get_config_return_default_config_when_config_is_none(mocker, cfg_params): mocker.patch('ai.backend.client.config._config', None) mocker.patch('os.environ', { 'BACKEND_ACCESS_KEY': cfg_params['access_key'], 'BACKEND_SECRET_KEY': cfg_params['secret_key'] }) cfg = get_config() assert str(cfg.endpoint) == APIConfig.DEFAULTS['endpoint'] assert cfg.version == APIConfig.DEFAULTS['version'] assert cfg.access_key == cfg_params['access_key'] assert cfg.secret_key == cfg_params['secret_key']
def test_get_config_return_default_config_when_config_is_none( mocker, cfg_params): mocker.patch('ai.backend.client.config._config', None) mocker.patch( 'os.environ', { 'SORNA_ACCESS_KEY': cfg_params['access_key'], 'SORNA_SECRET_KEY': cfg_params['secret_key'] }) cfg = get_config() for k, v in APIConfig.DEFAULTS.items(): assert getattr(cfg, k) == v
def test_get_config_return_default_config_when_config_is_none( mocker, cfg_params): mocker.patch('ai.backend.client.config._config', None) mocker.patch( 'os.environ', { 'BACKEND_ACCESS_KEY': cfg_params['access_key'], 'BACKEND_SECRET_KEY': cfg_params['secret_key'] }) cfg = get_config() assert str(cfg.endpoint) == APIConfig.DEFAULTS['endpoint'] assert cfg.version == APIConfig.DEFAULTS['version'] assert cfg.access_key == cfg_params['access_key'] assert cfg.secret_key == cfg_params['secret_key']
def test_config(runner, monkeypatch, example_keypair, unused_tcp_port_factory): api_port = unused_tcp_port_factory() api_url = 'http://127.0.0.1:{}'.format(api_port) set_config(None) monkeypatch.setenv('BACKEND_ACCESS_KEY', example_keypair[0]) monkeypatch.setenv('BACKEND_SECRET_KEY', example_keypair[1]) monkeypatch.setenv('BACKEND_ENDPOINT', api_url) config = get_config() result = runner.invoke(main, ['config']) assert result.exit_code == 0 assert str(config.endpoint) in result.output assert config.version in result.output assert config.access_key in result.output assert config.secret_key[:6] in result.output assert config.hash_type in result.output
async def test_keypair_info(self): current_config = get_config() with Session() as sess: result = sess.KeyPair(current_config.access_key).info() assert result['access_key'] == current_config.access_key assert result['secret_key'] == current_config.secret_key
async def test_keypair_info(self): current_config = get_config() with Session() as sess: result = sess.KeyPair(current_config.access_key).info() assert result['access_key'] == current_config.access_key assert result['secret_key'] == current_config.secret_key