diff --git a/.env.example b/.env.example index d76831f0..f9ead3be 100644 --- a/.env.example +++ b/.env.example @@ -34,5 +34,5 @@ QINIU_SANDBOX_KODO_PREFIX= QINIU_SANDBOX_HTTP_INJECTION_TOKEN=real_token QINIU_SANDBOX_OPENAI_API_KEY= -# Optional max retry count for sandbox create/connect (default: 5, 0 to disable). +# Optional max retry count for create/connect/snapshot (default: 5, 0 to disable). SANDBOX_RETRY_MAX=5 diff --git a/README.md b/README.md index d5291e97..bade7f54 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,10 @@ import qiniu ``` 更多参见SDK使用指南: https://developer.qiniu.com/kodo/sdk/python -``` + +### Sandbox + +Sandbox 的生命周期、持久快照、模板与请求注入用法见 [Sandbox 示例说明](examples/README-sandbox.md)。 ## 测试 diff --git a/examples/README-sandbox.md b/examples/README-sandbox.md new file mode 100644 index 00000000..d95eafe9 --- /dev/null +++ b/examples/README-sandbox.md @@ -0,0 +1,57 @@ +# Sandbox 示例 + +SDK 已对齐 [qiniu/api-specs](https://github.com/qiniu/api-specs/tree/6cb3b18a89584b0cd05f94c2d0a85aa7926c9f05/sandbox) 的公共 API。 +从项目根目录复制 `.env.example` 为 `.env`,填写 `QINIU_SANDBOX_API_KEY`,按需设置区域端点和模板,然后运行: + +```bash +python examples/sandbox_create.py +python examples/sandbox_lifecycle.py +python examples/sandbox_snapshots.py +python examples/sandbox_template_lifecycle.py +python examples/sandbox_runtime_configuration.py +python examples/sandbox_injection_rules.py +``` + +示例会创建资源并在结束时清理。保存的注入规则还需要 AK/SK;直接设置运行时 HTTP 注入可使用 API key。 + +## 暂停与恢复 + +`Sandbox.connect()`、实例 `connect()` 和 `SandboxClient.connect_sandbox()` 使用 Connect v2。省略 `timeout` 使用服务端默认的 300 秒;显式值必须至少为 1 秒。连接只延长 TTL,不会缩短已有 TTL;需要主动调整过期时间时使用 `set_timeout()`。 + +创建时可传入 `auto_pause=True`、`auto_pause_memory=False` 和 `auto_resume={'enabled': False}`(也可使用布尔值)。不传这些选项时保留服务端默认值;显式 `False` 会发送到服务端。`lifecycle` 的 `keep_memory` / `auto_resume` 也映射到对应选项。 + +`pause(memory=False)` 只保存文件系统,恢复会冷启动并丢失运行中的进程和内存状态,需要显式调用 `connect()` 或 `resume()`,不能靠流量自动恢复。`auto_pause_memory=False` 不能和启用的 `auto_resume` 同时使用。 + +## 持久快照 + +`sandbox.create_snapshot(idempotency_key, memory=None, name=None)` 或 `client.create_sandbox_snapshot(sandbox_id, idempotency_key, ...)` 返回原始字典,包含 `snapshotID` 和 `names`。可用 `Sandbox.create(snapshot['snapshotID'])` 创建沙箱,用 `client.delete_template(snapshot_id)` 删除快照模板。 + +每次新捕获必须使用新的幂等键(1–128 字节),包括同名快照的新版本。调用前应持久保存键与请求体;等待超时或网络中断后用原键、原沙箱 ID 和原请求体重试。取消客户端等待不会取消已接受的捕获,服务端会重放终态结果,包括失败。幂等记录在完成后保留 35 分钟;结果未知时先按来源或名称查询核对,不能直接换键或在记录过期后假定重试仍然安全。 + +快照自动重试网络错误(包括响应体传输中断)、408、502、504 和 `error_code=api_shutdown` 的 503,遵循有效的 ASCII 非负整数 `Retry-After`;无效或超出支持范围的值回退到指数退避,500 和其他 503 直接返回。自动重试从首次请求开始最多持续 35 分钟,包含请求耗时和等待;若下次重试将超出窗口,则抛出结果可能未知的 `SandboxError`,保留原始错误供排查。该限制不会中断正在进行的请求;重试窗口耗尽后应先查询快照核对结果。`SandboxError.error_code` 可读取语义错误码,完整响应保留在 `data` / `response`。`SANDBOX_RETRY_MAX` 控制重试次数(默认 5,0 禁用)。 + +`client.list_snapshots(sandbox_id=None, name=None, limit=None, next_token=None)` 返回 `{'items': [...], 'nextToken': ...}`,游标来自响应的 `X-Next-Token`。持续传入 `next_token` 直到返回值为空,避免遗漏快照。`name` 可带命名空间和标签,创建快照时的 `name` 只能是别名和可选标签,不能带 `/`。 + +## 模板磁盘与标签 + +`create_template(min_disk_mb=10240, min_free_disk_mb=1024)` 分别指定构建后的最小总磁盘和最小剩余空间(MiB),同时设置时满足两者要求。总磁盘省略或为 0 表示不设置最小值;剩余空间省略时使用团队默认值,为 0 表示不为剩余空间扩容。已有文件系统不会缩小,剩余空间增长是尽力而为,实际值可能略小于目标。 + +旧的 `disk_size_mb` / `diskSizeMB` 已从 v3 创建模板规范移除,SDK 会提示改用上述选项,避免将总大小误解为剩余空间。API 原始字段 `minDiskMb` / `minFreeDiskMb` 也可使用。 + +`client.list_template_tags(template_id)` 返回每个标签最近一次分配的 `tag`、`buildID` 和 `createdAt`。 + +## HTTP JSON 注入 + +HTTP 规则支持 `if_body`(兼容 `ifBody`)按顶层 JSON 字符串字段精确匹配,以及 `body` 写入秘密字符串字段。匹配条件只是请求筛选条件,沙箱进程也能构造匹配请求,应通过精确的 `base_url` 限制目标。仅支持未压缩、最大 1 MiB 的 `application/json` 请求;需要处理 body 时,非法或不支持的 body 会报错。目标字段已有非字符串值也会报错。`body` 的秘密值不会出现在 API 响应中。 + +## 测试 + +```bash +python -m pytest tests/cases/test_services/test_sandbox -q +``` + +单元测试使用记录请求的 Session 验证 HTTP 契约。集成测试在配置 API key 后访问实际服务并清理创建的资源;无凭据时跳过。仅运行本次新增契约的实际服务检查: + +```bash +python -m pytest tests/cases/test_services/test_sandbox/test_integration.py -k 'snapshot or template_disk or http_body' -q +``` diff --git a/examples/sandbox_injection_rules.py b/examples/sandbox_injection_rules.py index 9ddcd3d7..0fd2aa45 100644 --- a/examples/sandbox_injection_rules.py +++ b/examples/sandbox_injection_rules.py @@ -15,6 +15,8 @@ def main(): 'base_url': 'https://api.example.com/v1/*', 'if_headers': {'X-Qiniu-Sandbox': 'enabled'}, 'if_queries': {'tenant': 'demo'}, + 'if_body': {'model': 'demo'}, + 'body': {'api_key': 'replace-with-service-api-key'}, 'headers': {'X-From-Sandbox': 'qiniu-python-sdk'}, }, ) diff --git a/examples/sandbox_lifecycle.py b/examples/sandbox_lifecycle.py index 9df5335a..74dc2a73 100644 --- a/examples/sandbox_lifecycle.py +++ b/examples/sandbox_lifecycle.py @@ -18,7 +18,9 @@ def print_optional(label, fn): def main(): - sandbox = create_sandbox(timeout=300) + sandbox = create_sandbox( + timeout=300, auto_pause=True, auto_pause_memory=True, + auto_resume={'enabled': True}, network={'allowPublicTraffic': True}) try: print('created:', sandbox.sandbox_id) sandbox.set_timeout(600) @@ -31,10 +33,10 @@ def main(): print('paused:', sandbox.sandbox_id) sandbox.resume(timeout=300) print('resumed:', sandbox.sandbox_id) - try: - print('network:', sandbox.update_network({'allowInternet': True})) - except Exception as err: - print('update network skipped:', err) + # Disk-only pause loses running processes; explicitly connect to restart. + sandbox.pause(memory=False) + sandbox.connect() # v2: extend TTL to at least 300s, never shorten it. + print('cold-booted:', sandbox.sandbox_id) finally: cleanup_sandbox(sandbox) diff --git a/examples/sandbox_runtime_configuration.py b/examples/sandbox_runtime_configuration.py index 317b713d..e89fbea5 100644 --- a/examples/sandbox_runtime_configuration.py +++ b/examples/sandbox_runtime_configuration.py @@ -18,6 +18,8 @@ def main(): sandbox.update_injections([{ 'type': 'http', 'base_url': 'https://api.example.com/v1/*', + 'if_body': {'model': 'demo'}, + 'body': {'api_key': 'replace-with-service-api-key'}, 'headers': {'X-From-Sandbox': 'qiniu-python-sdk'}, }]) print('updated injections:', sandbox.get_injections()) diff --git a/examples/sandbox_snapshots.py b/examples/sandbox_snapshots.py new file mode 100644 index 00000000..e3333df6 --- /dev/null +++ b/examples/sandbox_snapshots.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +from __future__ import print_function + +import uuid + +from qiniu.services.sandbox import Sandbox +from sandbox_common import cleanup_sandbox, create_sandbox, run_example + + +def main(): + source = create_sandbox(timeout=300) + restored = None + snapshot_id = None + # Persist this key in your application before capture. Reuse it and the same + # body after a timeout; a new key means a new capture, even for the same name. + capture_key = str(uuid.uuid4()) + name = 'python-sdk-snapshot-{0}:v1'.format(capture_key) + try: + source.files.write('/home/user/snapshot-demo.txt', 'saved by python sdk') + print('capture key:', capture_key) + snapshot = source.create_snapshot( + capture_key, memory=False, name=name) + snapshot_id = snapshot['snapshotID'] + print('snapshot:', snapshot_id, snapshot['names']) + + # Replaying a completed capture returns the original snapshot. + replay = source.create_snapshot(capture_key, memory=False, name=name) + assert replay['snapshotID'] == snapshot_id + next_token = None + while True: + page = source.client.list_snapshots( + sandbox_id=source.sandbox_id, name=name, limit=1, + next_token=next_token) + print('snapshots:', page['items']) + next_token = page['nextToken'] + if not next_token: + break + print('tags:', source.client.list_template_tags(snapshot_id)) + + restored = Sandbox.create(snapshot_id, client=source.client, timeout=300) + restored.wait_for_ready() + print('restored file:', restored.files.read_text('/home/user/snapshot-demo.txt')) + finally: + cleanup_sandbox(restored) + cleanup_sandbox(source) + if snapshot_id: + source.client.delete_template(snapshot_id) + print('snapshot deleted:', snapshot_id) + else: + print('If capture outcome is unknown, investigate with list_snapshots ' + 'using name {0} and retry with key {1}; do not change the key.' + .format(name, capture_key)) + + +if __name__ == '__main__': + run_example(main) diff --git a/examples/sandbox_template_lifecycle.py b/examples/sandbox_template_lifecycle.py index 5866cc22..ae3dc175 100644 --- a/examples/sandbox_template_lifecycle.py +++ b/examples/sandbox_template_lifecycle.py @@ -56,7 +56,8 @@ def main(): ]) try: - created = client.create_template(name=target) + created = client.create_template( + name=target, min_disk_mb=10240, min_free_disk_mb=1024) template_id = pick( created, 'templateID', @@ -110,6 +111,7 @@ def main(): tags=['example'], ) print('tagged:', template_id) + print('tags:', client.list_template_tags(template_id)) client.delete_template_tags(name=name, tags=['example']) print('untagged:', template_id) except SandboxError as err: diff --git a/qiniu/services/sandbox/client.py b/qiniu/services/sandbox/client.py index e8a80055..9bc7c697 100644 --- a/qiniu/services/sandbox/client.py +++ b/qiniu/services/sandbox/client.py @@ -26,6 +26,7 @@ _UNSET = object() +_SNAPSHOT_RETRY_WINDOW = 35 * 60 try: @@ -40,6 +41,25 @@ def _to_dict(value): return value +def _retry_after_seconds(err): + response = getattr(err, 'response', None) + value = getattr(response, 'headers', {}).get('Retry-After') + if not isinstance(value, basestring): + return None + if isinstance(value, bytes_type): + try: + value = value.decode('ascii') + except UnicodeDecodeError: + return None + value = value.strip() + # Bound conversion and accept only the ASCII decimal HTTP field syntax. + if (not value or len(value) > 10 or + any(char < '0' or char > '9' for char in value)): + return None + seconds = int(value) + return seconds if seconds <= 2147483647 else None + + def _normalize_injection(injection): if hasattr(injection, 'to_dict'): injection = injection.to_dict() @@ -54,6 +74,8 @@ def _normalize_injection(injection): data['if_headers'] = data.pop('ifHeaders') if 'ifQueries' in data and 'if_queries' not in data: data['if_queries'] = data.pop('ifQueries') + if 'ifBody' in data and 'if_body' not in data: + data['if_body'] = data.pop('ifBody') if 'ruleId' in data and 'ruleID' not in data: data['ruleID'] = data.pop('ruleId') if 'rule_id' in data and 'ruleID' not in data: @@ -119,8 +141,13 @@ def _normalize_sandbox_create_options(template=None, **opts): ): if opts.get(key) is not None: body[key] = opts.get(key) - if opts.get('auto_pause') is not None: - body['autoPause'] = opts.get('auto_pause') + for snake, wire in ( + ('auto_pause', 'autoPause'), + ('auto_pause_memory', 'autoPauseMemory'), + ('auto_resume', 'autoResume')): + value = _single_alias_value(opts, snake, wire) + if value is not None: + body[wire] = value allow_internet_access = _single_alias_value( opts, 'allow_internet_access', 'allowInternetAccess') if allow_internet_access is not None: @@ -134,6 +161,23 @@ def _normalize_sandbox_create_options(template=None, **opts): on_timeout = lifecycle.get('on_timeout') or lifecycle.get('onTimeout') if on_timeout == 'pause': body['autoPause'] = True + for snake, camel, wire in ( + ('keep_memory', 'keepMemory', 'autoPauseMemory'), + ('auto_resume', 'autoResume', 'autoResume')): + value = _single_alias_value(lifecycle, snake, camel) + if value is not None: + if wire in body: + raise SandboxError('Conflicting lifecycle option: ' + wire) + body[wire] = value + if isinstance(body.get('autoResume'), bool): + body['autoResume'] = {'enabled': body['autoResume']} + if body.get('autoResume') is not None and not isinstance( + body['autoResume'], dict): + raise SandboxError('auto_resume must be a bool or config dict') + if (body.get('autoPauseMemory') is False and + (body.get('autoResume') or {}).get('enabled')): + raise SandboxError( + 'auto_resume cannot be enabled with auto_pause_memory=False') if opts.get('injections') is not None: body['injections'] = [_normalize_injection( item) for item in opts.get('injections')] @@ -146,7 +190,7 @@ def _single_alias_value(opts, *names): present = [name for name in names if opts.get(name) is not None] if len(present) > 1: raise SandboxError( - 'Conflicting sandbox create options: {0}'.format( + 'Conflicting options: {0}'.format( ', '.join(present))) if present: return opts.get(present[0]) @@ -192,12 +236,21 @@ def _normalize_list_options(opts): def _normalize_template_create_options(opts): body = dict(opts or {}) - disk_size_mb = _single_alias_value( - body, 'disk_size_mb', 'diskSizeMB') + legacy_disk = _single_alias_value(body, 'disk_size_mb', 'diskSizeMB') body.pop('disk_size_mb', None) body.pop('diskSizeMB', None) - if disk_size_mb is not None: - body['diskSizeMB'] = disk_size_mb + if legacy_disk is not None: + raise SandboxError( + 'disk_size_mb/diskSizeMB is no longer supported; use ' + 'min_disk_mb for total size or min_free_disk_mb for free space') + for snake, wire in ( + ('min_disk_mb', 'minDiskMb'), + ('min_free_disk_mb', 'minFreeDiskMb')): + value = _single_alias_value(body, snake, wire) + body.pop(snake, None) + body.pop(wire, None) + if value is not None: + body[wire] = value return body @@ -290,7 +343,7 @@ def _auth(self, auth_type=None): return None def _request(self, method, path, params=None, body=_UNSET, - auth_type=None, empty=False, extra_headers=None): + auth_type=None, empty=False, extra_headers=None, page=False): url = self.endpoint + path data = None if body is _UNSET else json_dumps(body) headers = self._headers(auth_type) @@ -337,7 +390,11 @@ def _request(self, method, path, params=None, body=_UNSET, raise SandboxError(message, response, response_data) if empty: return None - return parse_json_response(response) + data = parse_json_response(response) + if page: + return {'items': data, 'nextToken': response.headers.get( + 'X-Next-Token')} + return data def _is_retryable(self, err): if isinstance(err, SandboxError): @@ -349,14 +406,34 @@ def _is_retryable(self, err): err = getattr(err, 'cause', None) return isinstance(err, (requests.ConnectionError, requests.Timeout)) - def _retry_call(self, fn): + def _retry_call(self, fn, retryable=None, retry_after=False, + retry_budget=None): + retryable = retryable or self._is_retryable + deadline = (None if retry_budget is None else + _monotonic_time() + retry_budget) for attempt in range(self.max_retries + 1): try: return fn() except (SandboxError, requests.RequestException) as err: - if attempt < self.max_retries and self._is_retryable(err): - base = min(0.5 * (2 ** attempt), 10) - time.sleep(base + random.random() * base / 2) + if attempt < self.max_retries and retryable(err): + base = min(0.5 * (2 ** min(attempt, 5)), 10) + delay = base + random.random() * base / 2 + if retry_after: + value = _retry_after_seconds(err) + if value is not None: + delay = value + within_budget = (deadline is None or + _monotonic_time() + delay < deadline) + if within_budget: + time.sleep(delay) + if deadline is not None and ( + not within_budget or _monotonic_time() >= deadline): + raise SandboxError( + 'Snapshot automatic retry window exhausted; outcome ' + 'may be unknown. Inspect snapshots before retrying ' + 'or starting a new capture.', + response=getattr(err, 'response', None), + data=getattr(err, 'data', None), cause=err) continue raise @@ -418,12 +495,13 @@ def delete_sandbox(self, sandbox_id): killSandbox = delete_sandbox kill = delete_sandbox - def pause_sandbox(self, sandbox_id): + def pause_sandbox(self, sandbox_id, memory=None): + """Pause; memory=False saves disk only and resumes with a cold boot.""" _require_sandbox_id(sandbox_id) return self._request( 'POST', '/sandboxes/{0}/pause'.format(encode_path(sandbox_id)), - body={}, + body={} if memory is None else {'memory': memory}, empty=True, ) @@ -439,19 +517,71 @@ def resume_sandbox(self, sandbox_id, **opts): resumeSandbox = resume_sandbox - def connect_sandbox(self, sandbox_id, timeout=15): + def connect_sandbox(self, sandbox_id, timeout=None): + """Connect through v2, extending TTL (default 300s) without shortening it.""" _require_sandbox_id(sandbox_id) return self._retry_call( lambda: self._request( 'POST', - '/sandboxes/{0}/connect'.format(encode_path(sandbox_id)), - body={'timeout': timeout}, + '/v2/sandboxes/{0}/connect'.format(encode_path(sandbox_id)), + body={} if timeout is None else {'timeout': timeout}, ) ) connectSandbox = connect_sandbox + connect_sandbox_v2 = connect_sandbox + connectSandboxV2 = connect_sandbox connect = connect_sandbox + def _is_snapshot_retryable(self, err): + if isinstance(err, SandboxError) and err.status_code is not None: + return (err.status_code in (408, 502, 504) or + (err.status_code == 503 and err.error_code == 'api_shutdown')) + cause = err.cause if isinstance(err, SandboxError) else err + return (isinstance(cause, requests.exceptions.ChunkedEncodingError) or + self._is_retryable(err)) + + def create_sandbox_snapshot(self, sandbox_id, idempotency_key, + memory=None, name=None): + """Capture a persistent snapshot using a caller-owned idempotency key. + + Reuse the key and body after an unknown outcome; a new capture needs a + new key. Cancellation does not cancel capture. Records expire 35 minutes + after completion, so investigate an unknown outcome before changing keys. + Automatic retries stop within 35 minutes, including requests and sleeps. + """ + _require_sandbox_id(sandbox_id) + key_bytes = idempotency_key + if isinstance(idempotency_key, text_type): + key_bytes = idempotency_key.encode('utf-8') + if (not isinstance(idempotency_key, basestring) or + not 1 <= len(key_bytes) <= 128): + raise SandboxError('idempotency_key must contain 1 to 128 bytes') + body = {} + if memory is not None: + body['memory'] = memory + if name is not None: + body['name'] = name + return self._retry_call( + lambda: self._request( + 'POST', '/sandboxes/{0}/snapshots'.format(encode_path(sandbox_id)), + body=body, extra_headers={'Idempotency-Key': key_bytes}), + retryable=self._is_snapshot_retryable, retry_after=True, + retry_budget=_SNAPSHOT_RETRY_WINDOW) + + createSandboxSnapshot = create_sandbox_snapshot + create_snapshot = create_sandbox_snapshot + + def list_snapshots(self, sandbox_id=None, name=None, limit=None, + next_token=None): + """Return {'items': [...], 'nextToken': ...}; None marks the last page.""" + return self._request('GET', '/snapshots', params={ + 'sandboxID': sandbox_id, 'name': name, 'limit': limit, + 'nextToken': next_token, + }, page=True) + + listSnapshots = list_snapshots + def update_sandbox_timeout(self, sandbox_id, timeout=None, **opts): _require_sandbox_id(sandbox_id) if timeout is None: @@ -631,6 +761,12 @@ def get_sandbox_logs(self, sandbox_id, **opts): getLogs = get_sandbox_logs def create_template(self, **opts): + """Allocate a build with min_disk_mb and/or min_free_disk_mb in MiB. + + Omitted free space uses the team default; zero requests no minimum + growth. Total size defaults to no minimum. Existing disks never shrink. + Free space growth is best effort and can fall slightly below the target. + """ return self._request( 'POST', '/v3/templates', @@ -746,6 +882,15 @@ def delete_template_tags(self, **opts): deleteTemplateTags = delete_template_tags + def list_template_tags(self, template_id): + """List each tag's latest build assignment.""" + if not template_id: + raise SandboxError('template_id is required') + return self._request( + 'GET', '/templates/{0}/tags'.format(encode_path(template_id))) + + listTemplateTags = list_template_tags + def get_template_by_alias(self, alias): return self._request( 'GET', '/templates/aliases/{0}'.format(encode_path(alias))) diff --git a/qiniu/services/sandbox/errors.py b/qiniu/services/sandbox/errors.py index c2b85ce7..7437f7a8 100644 --- a/qiniu/services/sandbox/errors.py +++ b/qiniu/services/sandbox/errors.py @@ -8,6 +8,7 @@ def __init__(self, message, response=None, data=None, cause=None): self.data = data self.cause = cause self.status_code = getattr(response, 'status_code', None) + self.error_code = data.get('error_code') if isinstance(data, dict) else None class TemplateBuildError(SandboxError): diff --git a/qiniu/services/sandbox/sandbox.py b/qiniu/services/sandbox/sandbox.py index bc02a520..024848a7 100644 --- a/qiniu/services/sandbox/sandbox.py +++ b/qiniu/services/sandbox/sandbox.py @@ -27,7 +27,7 @@ class _ConnectDescriptor(object): def __get__(self, obj, cls): if obj is None: - def class_connect(sandbox_id, client=None, timeout=15, **opts): + def class_connect(sandbox_id, client=None, timeout=None, **opts): client = client or SandboxClient(**opts) info = client.connect_sandbox(sandbox_id, timeout=timeout) sandbox = cls( @@ -40,7 +40,7 @@ def class_connect(sandbox_id, client=None, timeout=15, **opts): return sandbox return class_connect - def instance_connect(timeout=15): + def instance_connect(timeout=None): info = obj.client.connect_sandbox( obj.sandbox_id, timeout=timeout) obj.update_info(info) @@ -237,12 +237,18 @@ def refresh(self, duration=None, **opts): opts['duration'] = duration return self.client.refresh_sandbox(self.sandbox_id, **opts) - def pause(self): - return self.client.pause_sandbox(self.sandbox_id) + def pause(self, memory=None): + return self.client.pause_sandbox(self.sandbox_id, memory=memory) beta_pause = pause betaPause = pause + def create_snapshot(self, idempotency_key, memory=None, name=None): + return self.client.create_sandbox_snapshot( + self.sandbox_id, idempotency_key, memory=memory, name=name) + + createSnapshot = create_snapshot + def resume(self, **opts): info = self.client.resume_sandbox(self.sandbox_id, **opts) self.update_info(info) diff --git a/tests/cases/test_services/test_sandbox/test_client.py b/tests/cases/test_services/test_sandbox/test_client.py index 9a670543..bcb5274d 100644 --- a/tests/cases/test_services/test_sandbox/test_client.py +++ b/tests/cases/test_services/test_sandbox/test_client.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import gc import json +import threading import pytest import requests @@ -12,6 +13,11 @@ except ImportError: from urlparse import parse_qs, urlparse +try: + from socketserver import StreamRequestHandler, TCPServer +except ImportError: + from SocketServer import StreamRequestHandler, TCPServer + from qiniu.services.sandbox import ( CommandExitError, DEFAULT_ENDPOINT, @@ -37,6 +43,15 @@ from qiniu.services.sandbox.util import encode_path, file_signature +def patch_retry_sleep(monkeypatch, sleep): + # Keep other threads (including Python 2 ThreadPool workers) on real time. + class RetryTime(object): + pass + + RetryTime.sleep = staticmethod(sleep) + monkeypatch.setattr(sandbox_client_module, 'time', RetryTime) + + class DummyResponse(object): def __init__(self, status_code=200, body=None): self.status_code = status_code @@ -123,21 +138,22 @@ def test_client_uses_default_endpoint_and_api_key_headers(): } -def test_create_template_maps_disk_size_mb_to_api_field(): +def test_create_template_maps_minimum_disk_sizes_to_api_fields(): session = RecordingSession([DummyResponse(202, { 'templateID': 'tpl123', 'buildID': 'build123', })]) client = SandboxClient(api_key='api-key', session=session) - client.create_template(name='disk-size-test', disk_size_mb=15360) + client.create_template(name='disk-size-test', min_disk_mb=15360, min_free_disk_mb=5120) req = session.requests[0] assert req.method == 'POST' assert req.url == DEFAULT_ENDPOINT + '/v3/templates' assert body_of(req) == { 'name': 'disk-size-test', - 'diskSizeMB': 15360, + 'minDiskMb': 15360, + 'minFreeDiskMb': 5120, } @@ -176,7 +192,7 @@ def test_create_sandbox_retries_retryable_status_and_reuses_idempotency_key( ErrorResponse(status_code), DummyResponse(201, {'sandboxID': 'sbx123'}), ]) - monkeypatch.setattr(sandbox_client_module.time, 'sleep', lambda _: None) + patch_retry_sleep(monkeypatch, lambda _: None) client = SandboxClient( api_key='api-key', session=session, max_retries=1) @@ -197,7 +213,7 @@ def test_create_sandbox_retries_transport_error_and_reuses_idempotency_key( error_type('[Errno 101] Network is unreachable'), DummyResponse(201, {'sandboxID': 'sbx123'}), ]) - monkeypatch.setattr(sandbox_client_module.time, 'sleep', lambda _: None) + patch_retry_sleep(monkeypatch, lambda _: None) client = SandboxClient( api_key='api-key', session=session, max_retries=1) @@ -647,7 +663,7 @@ def test_sandbox_instance_lifecycle_methods_call_control_plane(): assert session.requests[0].url.endswith('/sandboxes/sbx123') assert session.requests[1].url.endswith('/sandboxes/sbx123/timeout') assert body_of(session.requests[1]) == {'timeout': 30} - assert session.requests[2].url.endswith('/sandboxes/sbx123/connect') + assert session.requests[2].url.endswith('/v2/sandboxes/sbx123/connect') assert body_of(session.requests[2]) == {'timeout': 45} @@ -696,7 +712,7 @@ def test_sandbox_connect_forwards_envd_options_to_instance(): assert sandbox.envd_url() == 'http://envd.local' assert sandbox.envdAccessToken == 'envd-token' - assert body_of(session.requests[0]) == {'timeout': 15} + assert body_of(session.requests[0]) == {} def test_delete_template_requires_template_id(): @@ -2374,3 +2390,429 @@ def test_git_config_helpers_accept_legacy_repo_path_signatures(): assert commands.calls[5][1]['cwd'] == 'my.repo' assert commands.calls[6][0] == 'git config --local --get user.name' assert commands.calls[6][1]['cwd'] == '/repo' + + +@pytest.mark.parametrize('opts,expected', [ + ({}, {}), + ({'min_disk_mb': 0}, {'minDiskMb': 0}), + ({'min_free_disk_mb': 0}, {'minFreeDiskMb': 0}), + ({'minDiskMb': 10240, 'minFreeDiskMb': 2048}, + {'minDiskMb': 10240, 'minFreeDiskMb': 2048}), + ({'min_disk_mb': None, 'min_free_disk_mb': None}, {}), + ({'disk_size_mb': None, 'diskSizeMB': None}, {}), +]) +def test_template_disk_constraints_preserve_zero_and_omission(opts, expected): + session = RecordingSession() + SandboxClient(api_key='key', session=session).create_template(**opts) + assert body_of(session.requests[0]) == expected + + +@pytest.mark.parametrize('opts', [ + {'disk_size_mb': 10240}, {'diskSizeMB': 10240}, + {'min_disk_mb': 0, 'minDiskMb': 10240}, + {'min_free_disk_mb': 0, 'minFreeDiskMb': 10240}, +]) +def test_template_disk_constraints_reject_legacy_or_conflicting_options(opts): + client = SandboxClient(api_key='key', session=RecordingSession()) + with pytest.raises(SandboxError): + client.create_template(**opts) + assert client.session.requests == [] + + +@pytest.mark.parametrize('opts,expected', [ + ({}, {}), + ({'auto_pause_memory': False, 'auto_resume': False}, + {'autoPauseMemory': False, 'autoResume': {'enabled': False}}), + ({'autoPause': True, 'autoPauseMemory': True, + 'autoResume': {'enabled': True}}, + {'autoPause': True, 'autoPauseMemory': True, + 'autoResume': {'enabled': True}}), + ({'lifecycle': {'on_timeout': 'pause', 'keep_memory': False, + 'auto_resume': False}}, + {'autoPause': True, 'autoPauseMemory': False, + 'autoResume': {'enabled': False}}), + ({'lifecycle': {'onTimeout': 'pause', 'keepMemory': True, + 'autoResume': True}}, + {'autoPause': True, 'autoPauseMemory': True, + 'autoResume': {'enabled': True}}), +]) +def test_create_auto_pause_and_resume_fields(opts, expected): + session = RecordingSession() + SandboxClient(api_key='key', session=session).create_sandbox(**opts) + body = body_of(session.requests[0]) + assert {key: value for key, value in body.items() + if key.startswith('auto')} == expected + + +@pytest.mark.parametrize('opts', [ + {'auto_pause_memory': False, 'auto_resume': True}, + {'autoPauseMemory': False, 'autoResume': {'enabled': True}}, + {'auto_resume': True, 'autoResume': False}, + {'lifecycle': {'on_timeout': 'pause', 'keep_memory': False, + 'auto_resume': True}}, + {'auto_pause_memory': True, 'lifecycle': {'keep_memory': False}}, +]) +def test_create_rejects_conflicting_auto_resume_options(opts): + client = SandboxClient(api_key='key', session=RecordingSession()) + with pytest.raises(SandboxError): + client.create_sandbox(**opts) + assert client.session.requests == [] + + +@pytest.mark.parametrize('memory,expected', [ + (None, {}), (False, {'memory': False}), (True, {'memory': True}), +]) +def test_pause_supports_memory_choice(memory, expected): + session = RecordingSession([DummyResponse(204)]) + sandbox = Sandbox(sandbox_id='sb/id', client=SandboxClient( + api_key='key', session=session)) + assert sandbox.pause(memory=memory) is None + assert session.requests[0].method == 'POST' + assert session.requests[0].url == DEFAULT_ENDPOINT + '/sandboxes/sb%2Fid/pause' + assert body_of(session.requests[0]) == expected + + +@pytest.mark.parametrize('status', [200, 201]) +@pytest.mark.parametrize('timeout,expected', [(None, {}), (60, {'timeout': 60})]) +def test_connect_uses_v2_and_server_default(status, timeout, expected): + session = RecordingSession([DummyResponse(status, { + 'sandboxID': 'sb/id', 'envdAccessToken': 'new-token', + })]) + client = SandboxClient(api_key='key', session=session) + sandbox = Sandbox.connect('sb/id', client=client, timeout=timeout) + assert sandbox.envd_access_token == 'new-token' + assert session.requests[0].method == 'POST' + assert session.requests[0].url == DEFAULT_ENDPOINT + '/v2/sandboxes/sb%2Fid/connect' + assert body_of(session.requests[0]) == expected + + +@pytest.mark.parametrize('failure', [ + DummyResponse(408), DummyResponse(502), DummyResponse(504), + DummyResponse(503, {'error_code': 'api_shutdown'}), + requests.Timeout('unknown capture outcome'), + requests.ConnectionError('connection lost'), + requests.exceptions.ChunkedEncodingError('response interrupted'), +]) +def test_snapshot_retry_reuses_key_and_body(monkeypatch, failure): + sleeps = [] + patch_retry_sleep(monkeypatch, sleeps.append) + session = RecordingSession([failure, DummyResponse(201, { + 'snapshotID': 'snapshot-1', 'names': ['team/demo:v1'], + })]) + sandbox = Sandbox(client=SandboxClient(api_key='key', session=session), + sandbox_id='source/id') + result = sandbox.create_snapshot('capture-1', memory=False, name='demo:v1') + assert result['snapshotID'] == 'snapshot-1' + assert len(session.requests) == 2 + assert len(sleeps) == 1 + for req in session.requests: + assert req.method == 'POST' + assert req.url == DEFAULT_ENDPOINT + '/sandboxes/source%2Fid/snapshots' + assert req.headers['Idempotency-Key'] == b'capture-1' + assert body_of(req) == {'memory': False, 'name': 'demo:v1'} + + +@pytest.mark.parametrize('status,code', [ + (400, None), (404, None), (409, None), (500, None), + (503, None), (503, 'capture_failed'), +]) +def test_snapshot_does_not_retry_terminal_errors(monkeypatch, status, code): + patch_retry_sleep(monkeypatch, lambda _: None) + session = RecordingSession([DummyResponse(status, {'error_code': code})]) + client = SandboxClient(api_key='key', session=session) + with pytest.raises(SandboxError) as err: + client.create_sandbox_snapshot('sb', 'key') + assert err.value.error_code == code + assert len(session.requests) == 1 + + +@pytest.mark.parametrize('value,expected', [ + ('7', 7), ('0', 0), (' 7 ', 7), (b'7', 7), + ('bad', None), ('-1', None), ('', None), (None, None), + (u'\u00b2', None), (u'\u0667', None), (b'\xff', None), + ('9' * 5000, None), ('2147483648', None), +]) +def test_snapshot_honors_valid_retry_after(monkeypatch, value, expected): + sleeps = [] + patch_retry_sleep(monkeypatch, sleeps.append) + failed = DummyResponse(408) + failed.headers['Retry-After'] = value + client = SandboxClient(api_key='key', session=RecordingSession([ + failed, DummyResponse(201, {'snapshotID': 'snapshot-1', 'names': []})])) + client.create_sandbox_snapshot('sb', 'key') + assert len(sleeps) == 1 + if expected is not None: + assert sleeps == [expected] + else: + assert 0.5 <= sleeps[0] <= 0.75 + + +@pytest.mark.parametrize('retry_after', ['2100', '2161', '2147483647']) +def test_snapshot_rejects_retry_after_beyond_retention(monkeypatch, retry_after): + sleeps = [] + patch_retry_sleep(monkeypatch, sleeps.append) + monkeypatch.setattr(sandbox_client_module, '_monotonic_time', lambda: 0) + failed = DummyResponse(408, {'message': 'still capturing'}) + failed.headers['Retry-After'] = retry_after + session = RecordingSession([failed]) + client = SandboxClient(api_key='key', session=session) + with pytest.raises(SandboxError, match='outcome may be unknown') as err: + client.create_snapshot('sb', 'persisted-key') + assert len(session.requests) == 1 + assert sleeps == [] + assert err.value.response is failed + assert err.value.data == {'message': 'still capturing'} + assert err.value.cause.status_code == 408 + + +@pytest.mark.parametrize('durations,oversleep,expected_sleeps', [ + ([2100], 0, []), + ([2099.5], 0, []), + ([1000, 1099.5], 0, [0.5]), + ([0], 2100, [0.5]), +]) +def test_snapshot_retry_budget_includes_requests_and_sleeps( + monkeypatch, durations, oversleep, expected_sleeps): + clock = [0] + sleeps = [] + monkeypatch.setattr(sandbox_client_module, '_monotonic_time', lambda: clock[0]) + monkeypatch.setattr(sandbox_client_module.random, 'random', lambda: 0) + + def sleep(delay): + sleeps.append(delay) + clock[0] += delay + oversleep + + patch_retry_sleep(monkeypatch, sleep) + session = RecordingSession([requests.Timeout('unknown outcome') + for _ in durations]) + send = session.send + + def timed_send(request, **kwargs): + clock[0] += durations[len(session.requests)] + return send(request, **kwargs) + + monkeypatch.setattr(session, 'send', timed_send) + client = SandboxClient(api_key='key', session=session, max_retries=10000) + with pytest.raises(SandboxError, match='outcome may be unknown') as err: + client.create_snapshot('sb', 'persisted-key', memory=False) + assert isinstance(err.value.cause.cause, requests.Timeout) + assert len(session.requests) == len(durations) + assert sleeps == expected_sleeps + assert all(req.headers['Idempotency-Key'] == b'persisted-key' + for req in session.requests) + assert all(body_of(req) == {'memory': False} for req in session.requests) + + +def test_snapshot_retry_can_succeed_within_budget(monkeypatch): + clock = [0] + monkeypatch.setattr(sandbox_client_module, '_monotonic_time', lambda: clock[0]) + + def sleep(delay): + clock[0] += delay + + patch_retry_sleep(monkeypatch, sleep) + session = RecordingSession([requests.Timeout('unknown outcome'), + DummyResponse(201, {'snapshotID': 'original'})]) + send = session.send + + def timed_send(request, **kwargs): + clock[0] += 1000 + return send(request, **kwargs) + + monkeypatch.setattr(session, 'send', timed_send) + client = SandboxClient(api_key='key', session=session) + assert client.create_snapshot('sb', 'persisted-key')['snapshotID'] == 'original' + assert 2000 < clock[0] < 2100 + assert len(session.requests) == 2 + assert session.requests[0].body == session.requests[1].body + assert session.requests[0].headers['Idempotency-Key'] == session.requests[1].headers['Idempotency-Key'] + + +def test_snapshot_large_retry_count_with_zero_retry_after(monkeypatch): + patch_retry_sleep(monkeypatch, lambda _: None) + monkeypatch.setattr(sandbox_client_module, '_monotonic_time', lambda: 0) + failed = DummyResponse(408) + failed.headers['Retry-After'] = '0' + session = RecordingSession([failed] * 1100 + [ + DummyResponse(201, {'snapshotID': 'original'})]) + client = SandboxClient(api_key='key', session=session, max_retries=1100) + assert client.create_snapshot('sb', 'persisted-key')['snapshotID'] == 'original' + assert len(session.requests) == 1101 + + +def test_snapshot_unknown_outcome_can_be_replayed_after_retry_exhaustion(): + session = RecordingSession([ + requests.Timeout('still processing'), + DummyResponse(201, {'snapshotID': 'original', 'names': []}), + ]) + client = SandboxClient(api_key='key', session=session, max_retries=0) + with pytest.raises(SandboxError): + client.create_snapshot('sb', 'persisted-key', memory=False) + assert client.create_snapshot('sb', 'persisted-key', memory=False) == { + 'snapshotID': 'original', 'names': [], + } + assert session.requests[0].body == session.requests[1].body + assert all(req.headers['Idempotency-Key'] == b'persisted-key' + for req in session.requests) + + +@pytest.mark.parametrize('key', [None, '', 'x' * 129, 123]) +def test_snapshot_requires_caller_owned_idempotency_key(key): + client = SandboxClient(api_key='key', session=RecordingSession()) + with pytest.raises(SandboxError, match='idempotency_key'): + client.create_snapshot('sb', key) + assert client.session.requests == [] + + +def test_snapshot_default_body_and_pagination(): + first = DummyResponse(200, [{'snapshotID': 'snap-1', 'names': ['team/demo']}]) + first.headers['X-Next-Token'] = 'cursor&next' + session = RecordingSession([DummyResponse(201, {'snapshotID': 'snap-1'}), + first, DummyResponse(200, [])]) + client = SandboxClient(api_key='key', session=session) + client.createSandboxSnapshot('source', 'capture') + assert body_of(session.requests[0]) == {} + page = client.list_snapshots(sandbox_id='source/id', name='team/demo:v1', limit=1) + assert page['items'][0]['snapshotID'] == 'snap-1' + assert page['nextToken'] == 'cursor&next' + last = client.listSnapshots(next_token=page['nextToken']) + assert last == {'items': [], 'nextToken': None} + assert session.requests[1].method == 'GET' + assert parse_qs(urlparse(session.requests[1].url).query) == { + 'sandboxID': ['source/id'], 'name': ['team/demo:v1'], 'limit': ['1'], + } + assert parse_qs(urlparse(session.requests[2].url).query) == { + 'nextToken': ['cursor&next'], + } + + +def test_list_template_tags_returns_assignments_and_encodes_path(): + tags = [{'tag': 'v1', 'buildID': 'build-1', 'createdAt': '2026-11-12T00:00:00Z'}] + session = RecordingSession([DummyResponse(200, tags)]) + client = SandboxClient(api_key='key', session=session) + assert client.listTemplateTags('template/id') == tags + assert session.requests[0].method == 'GET' + assert session.requests[0].url == DEFAULT_ENDPOINT + '/templates/template%2Fid/tags' + + +@pytest.mark.parametrize('operation', ['create', 'replace', 'rule', 'update_rule']) +def test_http_body_injections_are_serialized_for_all_write_apis(operation): + session = RecordingSession() + client = SandboxClient(api_key='key', access_key='ak', secret_key='sk', + session=session) + injection = {'type': 'http', 'baseUrl': 'https://api.example.com/v1/*', + 'ifBody': {'model': 'demo'}, 'body': {'api_key': 'secret'}} + expected = {'type': 'http', 'base_url': 'https://api.example.com/v1/*', + 'if_body': {'model': 'demo'}, 'body': {'api_key': 'secret'}} + if operation == 'create': + client.create_sandbox(injections=[injection]) + elif operation == 'replace': + client.update_sandbox_injections('sb', [injection]) + elif operation == 'rule': + client.create_injection_rule(name='demo', injection=injection) + else: + client.update_injection_rule('rule', injection=injection) + body = body_of(session.requests[0]) + assert body.get('injection', (body.get('injections') or [None])[0]) == expected + assert injection['ifBody'] == {'model': 'demo'} + + +@pytest.mark.parametrize('key', ['x' * 128, b'capture-bytes']) +def test_snapshot_accepts_boundary_and_bytes_idempotency_keys(key): + session = RecordingSession() + client = SandboxClient(api_key='key', session=session) + client.create_snapshot('sb', key) + expected_key = key if isinstance(key, bytes) else key.encode('utf-8') + assert session.requests[0].headers['Idempotency-Key'] == expected_key + + +@pytest.fixture +def snapshot_http_server(): + received = [] + responses = [] + + class Handler(StreamRequestHandler): + def handle(self): + # Parse CRLF-delimited bytes: legacy HTTPServer treats UTF-8 NEL + # bytes as a Unicode line break after decoding header values. + path = self.rfile.readline().split(b' ')[1] + headers = {} + while True: + line = self.rfile.readline() + if line in (b'\r\n', b''): + break + name, value = line.rstrip(b'\r\n').split(b':', 1) + headers[name.lower()] = value.strip() + body = self.rfile.read(int(headers[b'content-length'])) + received.append((path, headers[b'idempotency-key'], body)) + self.wfile.write(responses.pop(0)) + + server = TCPServer(('127.0.0.1', 0), Handler) + thread = threading.Thread( + target=lambda: server.serve_forever(poll_interval=0.01)) + thread.daemon = True + thread.start() + session = requests.Session() + session.trust_env = False + try: + yield ('http://127.0.0.1:{0}'.format(server.server_address[1]), + session, received, responses) + finally: + session.close() + server.shutdown() + thread.join(timeout=2) + server.server_close() + + +def snapshot_http_response(status): + body = b'{"snapshotID":"snapshot-1","names":[]}' + return (b'HTTP/1.1 ' + status + b'\r\nContent-Type: application/json\r\n' + b'Content-Length: ' + str(len(body)).encode('ascii') + + b'\r\nConnection: close\r\n\r\n' + body) + + +@pytest.mark.parametrize('key', [u'快照-1', u'caf\u00e9', b'capture-bytes']) +def test_snapshot_sends_utf8_idempotency_key_over_http( + monkeypatch, key, snapshot_http_server): + endpoint, session, received, responses = snapshot_http_server + responses.extend([snapshot_http_response(b'408 Request Timeout'), + snapshot_http_response(b'201 Created')]) + patch_retry_sleep(monkeypatch, lambda _: None) + client = SandboxClient(endpoint=endpoint, api_key='key', session=session, + timeout=2, max_retries=1) + result = client.create_snapshot('sb', key, memory=False) + assert result['snapshotID'] == 'snapshot-1' + expected_key = key if isinstance(key, bytes) else key.encode('utf-8') + assert len(received) == 2 + for path, header, body in received: + assert path == b'/sandboxes/sb/snapshots' + assert header == expected_key + assert json.loads(body.decode('utf-8')) == {'memory': False} + + +def test_snapshot_retries_interrupted_http_response(monkeypatch, snapshot_http_server): + endpoint, session, received, responses = snapshot_http_server + responses.extend([ + b'HTTP/1.1 201 Created\r\nTransfer-Encoding: chunked\r\n' + b'Connection: close\r\n\r\n20\r\n{"snapshotID":', + snapshot_http_response(b'201 Created'), + ]) + patch_retry_sleep(monkeypatch, lambda _: None) + client = SandboxClient(endpoint=endpoint, api_key='key', session=session, + timeout=2, max_retries=1) + assert client.create_snapshot('sb', 'capture', memory=False)['snapshotID'] == 'snapshot-1' + assert len(received) == 2 + assert received[0] == received[1] + assert received[0][1] == b'capture' + assert json.loads(received[0][2].decode('utf-8')) == {'memory': False} + + +@pytest.mark.parametrize('method,args', [ + ('create_sandbox_snapshot', ('', 'key')), + ('list_template_tags', ('',)), +]) +def test_new_control_methods_require_identifiers(method, args): + client = SandboxClient(api_key='key', session=RecordingSession()) + with pytest.raises(SandboxError): + getattr(client, method)(*args) + assert client.session.requests == [] diff --git a/tests/cases/test_services/test_sandbox/test_example_config.py b/tests/cases/test_services/test_sandbox/test_example_config.py index 0ea43a20..2d3c0f13 100644 --- a/tests/cases/test_services/test_sandbox/test_example_config.py +++ b/tests/cases/test_services/test_sandbox/test_example_config.py @@ -58,6 +58,7 @@ def test_examples_cover_primary_sandbox_surfaces(): 'sandbox_resources.py', 'sandbox_runtime_configuration.py', 'sandbox_runtime.py', + 'sandbox_snapshots.py', 'sandbox_template_lifecycle.py', 'sandbox_templates.py', ] @@ -108,6 +109,9 @@ def test_examples_cover_primary_sandbox_surfaces(): 'update_template', 'assign_template_tags', 'delete_template_tags', + 'list_template_tags', + 'min_disk_mb=10240', + 'min_free_disk_mb=1024', 'get_template_build_status', 'get_template_build_logs', 'start_template_build', @@ -126,7 +130,11 @@ def test_examples_cover_primary_sandbox_surfaces(): 'refresh', 'pause', 'resume', - 'update_network', + "network={'allowPublicTraffic': True}", + ], + 'sandbox_snapshots.py': [ + 'create_snapshot', 'list_snapshots', 'next_token=next_token', + 'list_template_tags', 'delete_template', ], 'sandbox_resources.py': [ 'GitRepositoryResource', diff --git a/tests/cases/test_services/test_sandbox/test_integration.py b/tests/cases/test_services/test_sandbox/test_integration.py index 739905c4..2bc6db08 100644 --- a/tests/cases/test_services/test_sandbox/test_integration.py +++ b/tests/cases/test_services/test_sandbox/test_integration.py @@ -298,3 +298,115 @@ def test_list_and_connect_existing_sandbox(): connected = Sandbox.connect(page[0].sandbox_id, client=client, timeout=60) assert connected.sandbox_id == page[0].sandbox_id + + +@pytest.mark.parametrize('memory', [False, True]) +def test_snapshot_lifecycle_and_connect_v2(memory): + import uuid + + client = integration_client() + source = None + restored = None + snapshot_id = None + key = str(uuid.uuid4()) + name = 'python-sdk-it-{0}:v1'.format(key) + try: + source = Sandbox.create( + os.getenv('QINIU_SANDBOX_TEMPLATE', 'base'), client=client, + timeout=300, auto_pause=True, auto_pause_memory=memory, + auto_resume={'enabled': False}) + source.files.write('/home/user/sdk-snapshot.txt', key) + snapshot = source.create_snapshot(key, memory=memory, name=name) + snapshot_id = snapshot['snapshotID'] + assert snapshot['names'] + replay = source.create_snapshot(key, memory=memory, name=name) + assert replay['snapshotID'] == snapshot_id + + page = client.list_snapshots( + sandbox_id=source.sandbox_id, name=name, limit=1) + found = [] + while True: + found.extend(item['snapshotID'] for item in page['items']) + if not page['nextToken']: + break + page = client.list_snapshots( + sandbox_id=source.sandbox_id, name=name, limit=1, + next_token=page['nextToken']) + assert snapshot_id in found + tags = client.list_template_tags(snapshot_id) + assert any(tag['tag'] == 'v1' for tag in tags) + assert all(tag['buildID'] and tag['createdAt'] for tag in tags) + + restored = Sandbox.create(snapshot_id, client=client, timeout=300) + restored.wait_for_ready() + assert restored.files.read_text('/home/user/sdk-snapshot.txt') == key + boot_id_command = 'cat /proc/sys/kernel/random/boot_id' + before_boot = source.commands.run(boot_id_command).stdout.strip() + assert before_boot + source.pause(memory=memory) + assert source.get_info()['state'] == 'paused' + source.connect() + source.wait_for_ready() + assert source.files.read_text('/home/user/sdk-snapshot.txt') == key + after_boot = source.commands.run(boot_id_command).stdout.strip() + assert after_boot + assert (before_boot == after_boot) is memory + finally: + try: + if restored is not None: + restored.kill() + finally: + try: + if source is not None: + source.kill() + finally: + if snapshot_id: + client.delete_template(snapshot_id) + + +def test_template_disk_constraints(): + import uuid + + client = integration_client() + template_id = None + try: + created = client.create_template( + name='python-sdk-disk-{0}'.format(uuid.uuid4()), + min_disk_mb=10240, min_free_disk_mb=0) + template_id = created['templateID'] + assert created['buildID'] + client.start_template_build( + template_id, created['buildID'], + fromTemplate=os.getenv('QINIU_SANDBOX_TEMPLATE', 'base')) + final = client.wait_for_build( + template_id, created['buildID'], interval=3, timeout=300) + assert final['status'] == 'ready' + finally: + if template_id: + client.delete_template(template_id) + + +def test_http_body_injection_configuration(): + sandbox = None + injection = { + 'type': 'http', 'base_url': 'https://api.example.com/v1/*', + 'if_body': {'model': 'sdk-test'}, 'body': {'api_key': 'sdk-test-secret'}, + } + try: + sandbox = Sandbox.create( + os.getenv('QINIU_SANDBOX_TEMPLATE', 'base'), + client=integration_client(), timeout=300, injections=[injection]) + rules = sandbox.get_injections()['injections'] + assert len(rules) == 1 + assert rules[0]['if_body'] == {'model': 'sdk-test'} + assert rules[0].get('body') is None + injection['if_body'] = {'model': 'updated-sdk-test'} + sandbox.update_injections([injection]) + rules = sandbox.get_injections()['injections'] + assert rules[0]['if_body'] == {'model': 'updated-sdk-test'} + assert rules[0].get('body') is None + sandbox.update_injections([]) + assert sandbox.get_injections()['injections'] == [] + finally: + if sandbox is not None: + sandbox.kill()