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
6 changes: 3 additions & 3 deletions cortexapps_cli/commands/deploys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import defaultdict
from datetime import datetime
from datetime import datetime, timezone
from enum import Enum
import json
from rich import print_json
Expand Down Expand Up @@ -168,7 +168,7 @@ def add(

if customData:
data["customData"] = dict(customData)
data["timestamp"] = data["timestamp"].strftime('%Y-%m-%dT%H:%M:%SZ')
data["timestamp"] = data["timestamp"].astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')

r = client.post("api/v1/catalog/" + tag + "/deploys", data=data)
print_json(data=r)
Expand Down Expand Up @@ -236,7 +236,7 @@ def update_by_uuid(
data["deployer"]["email"] = email
if name:
data["deployer"]["name"] = name
data["timestamp"] = data["timestamp"].strftime('%Y-%m-%dT%H:%M:%SZ')
data["timestamp"] = data["timestamp"].astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')

r = client.put("api/v1/catalog/" + tag + "/deploys/" + uuid, data=data)
print_json(data=r)
47 changes: 47 additions & 0 deletions tests/test_deploys_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import time

from tests.helpers.utils import *

# Fixed-offset zone (UTC-5, no DST) so the local->UTC conversion is deterministic
# regardless of the machine/CI timezone.
_TZ = "Etc/GMT+5"


def _with_fixed_tz(fn):
old_tz = os.environ.get("TZ")
os.environ["TZ"] = _TZ
time.tzset()
try:
fn()
finally:
if old_tz is None:
os.environ.pop("TZ", None)
else:
os.environ["TZ"] = old_tz
time.tzset()


def _posted_timestamp(cli_args):
responses.add(
responses.POST,
os.getenv("CORTEX_BASE_URL") + "/api/v1/catalog/cli-test-service/deploys",
json={},
status=200,
)
cli(cli_args)
return json.loads(responses.calls[0].request.body)["timestamp"]


@responses.activate
def test_add_converts_local_timestamp_to_utc():
"""An explicit local --timestamp is converted to UTC before being sent with a Z suffix"""
def check():
ts = _posted_timestamp([
"deploys", "add", "-t", "cli-test-service",
"--title", "my title", "--type", "DEPLOY",
"--timestamp", "2020-01-15T10:30:00",
])
# 10:30 in UTC-5 is 15:30 UTC
assert ts == "2020-01-15T15:30:00Z"

_with_fixed_tz(check)