Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/sandbox_default_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

from sandbox_common import run_example, sandbox_client


def main():
client = sandbox_client()
templates = client.list_default_templates()

print('default templates:', len(templates))
for template in templates:
print(
' - {0} (names: {1}, build status: {2})'.format(
template.get('templateID'),
template.get('names') or [],
template.get('buildStatus'),
)
)


if __name__ == '__main__':
run_example(main)
11 changes: 11 additions & 0 deletions examples/sandbox_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def main():
.set_env('PYTHONUNBUFFERED', '1')
)

default_templates = client.list_default_templates()
print('default templates:', len(default_templates))
for default_template in default_templates:
print(
' - {0} (names: {1}, build status: {2})'.format(
default_template.get('templateID'),
default_template.get('names') or [],
default_template.get('buildStatus'),
)
)

try:
created = client.create_template(
name='qiniu-python-sdk-example-{0}'.format(
Expand Down
47 changes: 47 additions & 0 deletions tests/cases/test_services/test_sandbox/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,53 @@ def test_delete_template_requires_template_id():
assert 'template_id is required' in str(err.value)


def test_list_default_templates_uses_public_endpoint():
templates = [
{
'templateID': 'tmpl-default-1',
'names': ['qiniu/code-interpreter'],
'buildStatus': 'ready',
},
{
'templateID': 'tmpl-default-2',
'names': ['qiniu/python'],
'buildStatus': 'ready',
},
]
session = RecordingSession([DummyResponse(200, templates)])
client = SandboxClient(api_key='api-key', session=session)

result = client.list_default_templates()

assert result == templates
request = session.requests[0]
assert request.method == 'GET'
assert request.url == DEFAULT_ENDPOINT + '/default-templates'


def test_list_default_templates_raises_sandbox_error_on_api_error():
session = RecordingSession([
DummyResponse(401, {'message': 'unauthorized'}),
])
client = SandboxClient(api_key='api-key', session=session)

with pytest.raises(SandboxError) as err:
client.list_default_templates()

assert 'status 401: unauthorized' in str(err.value)
assert err.value.response.status_code == 401
assert err.value.data == {'message': 'unauthorized'}


def test_list_default_templates_camel_case_alias():
templates = [{'templateID': 'tmpl-default'}]
session = RecordingSession([DummyResponse(200, templates)])
client = SandboxClient(api_key='api-key', session=session)

assert client.listDefaultTemplates() == templates
assert session.requests[0].url == DEFAULT_ENDPOINT + '/default-templates'


def test_sandbox_envd_and_file_urls_are_signed_when_token_is_available():
sandbox = Sandbox(info={
'sandboxID': 'sbx123',
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/test_services/test_sandbox/test_example_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_examples_cover_primary_sandbox_surfaces():
'sandbox_commands.py',
'sandbox_connect.py',
'sandbox_create.py',
'sandbox_default_templates.py',
'sandbox_filesystem.py',
'sandbox_git.py',
'sandbox_injection_rules.py',
Expand All @@ -72,6 +73,13 @@ def test_examples_cover_primary_sandbox_surfaces():
'commands.kill',
'commands.connect',
],
'sandbox_default_templates.py': [
'run_example(main)',
'list_default_templates',
"template.get('templateID')",
"template.get('names')",
"template.get('buildStatus')",
],
'sandbox_filesystem.py': [
'files.make_dir',
'files.write(',
Expand Down Expand Up @@ -135,6 +143,7 @@ def test_examples_cover_primary_sandbox_surfaces():
'template=[sandbox.template_id]',
],
'sandbox_templates.py': [
'list_default_templates',
'get_template',
"details.get('names')",
"details.get('isOwner')",
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/test_services/test_sandbox/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ def remote_git_credentials():
return repo_url, username, password


def test_list_default_templates():
templates = integration_client().list_default_templates()

assert isinstance(templates, list)
assert templates
for template in templates:
assert isinstance(template, dict)
assert template.get('templateID')


def test_create_run_filesystem_and_kill_sandbox():
sandbox = None
client = integration_client()
Expand Down
Loading