def test_storing_to_status_storage_overwrites_old_content(cls): storage = cls(field='status.my-operator') patch = Patch() body = Body({}) storage.store(body=body, patch=patch, key=HandlerId('id1'), record=CONTENT_DATA_1) storage.store(body=body, patch=patch, key=HandlerId('id1'), record=CONTENT_DATA_2) assert patch assert patch['status']['my-operator']['id1'] == CONTENT_DATA_2
def test_activity_error_exception(): outcome = HandlerOutcome(final=True) outcomes: Mapping[HandlerId, HandlerOutcome] outcomes = {HandlerId('id'): outcome} error = ActivityError("message", outcomes=outcomes) assert str(error) == "message" assert error.outcomes == outcomes
def test_storing_to_status_storage_appends_keys(cls): storage = cls(field='status.my-operator') patch = Patch() body = Body({}) storage.store(body=body, patch=patch, key=HandlerId('id1'), record=CONTENT_DATA_1) storage.store(body=body, patch=patch, key=HandlerId('id2'), record=CONTENT_DATA_1) assert patch assert patch['status']['my-operator']['id1'] == CONTENT_DATA_1 assert patch['status']['my-operator']['id2'] == CONTENT_DATA_1
def test_storing_to_annotations_storage_overwrites_old_content(cls): storage = cls(prefix='my-operator.example.com', verbose=True) patch = Patch() body = Body({}) storage.store(body=body, patch=patch, key=HandlerId('id1'), record=CONTENT_DATA_1) storage.store(body=body, patch=patch, key=HandlerId('id1'), record=CONTENT_DATA_2) assert patch assert patch['metadata']['annotations'][ 'my-operator.example.com/id1'] == CONTENT_JSON_2
def parent_handler(selector): def parent_fn(**_): pass return ResourceChangingHandler( fn=parent_fn, id=HandlerId('parent_fn'), param=None, errors=None, retries=None, timeout=None, backoff=None, selector=selector, labels=None, annotations=None, when=None, field=None, value=None, old=None, new=None, field_needs_change=None, initial=None, deleted=None, requires_finalizer=None, reason=None, )
def test_purging_of_status_storage_nullifies_content(cls): storage = cls(field='status.my-operator') patch = Patch() body = Body({'status': {'my-operator': {'id1': CONTENT_DATA_1}}}) storage.purge(body=body, patch=patch, key=HandlerId('id1')) assert patch assert patch['status']['my-operator']['id1'] is None
def test_keys_normalized_on_storing(cls, prefix, provided_key, expected_key): storage = cls(prefix=prefix) patch = Patch() body = Body({'metadata': {'annotations': {expected_key: 'null'}}}) storage.store(body=body, patch=patch, key=HandlerId(provided_key), record=CONTENT_DATA) assert expected_key in patch.metadata.annotations
def fetch( self, *, key: handlers.HandlerId, body: bodies.Body, ) -> Optional[ProgressRecord]: safe_key = key.replace('/', '.') full_key = f'{self.prefix}/{safe_key}' if self.prefix else safe_key value = body.metadata.annotations.get(full_key, None) content = json.loads(value) if value is not None else None return cast(Optional[ProgressRecord], content)
def purge( self, *, key: handlers.HandlerId, body: bodies.Body, patch: patches.Patch, ) -> None: safe_key = key.replace('/', '.') full_key = f'{self.prefix}/{safe_key}' if self.prefix else safe_key if full_key in body.metadata.annotations or full_key in patch.meta.annotations: patch.meta.annotations[full_key] = None
def test_fetching_from_annotations_storage(cls): storage = cls(prefix='my-operator.example.com', verbose=True) body = Body({ 'metadata': { 'annotations': { 'my-operator.example.com/id1': CONTENT_JSON_1, } } }) content = storage.fetch(body=body, key=HandlerId('id1')) assert content == CONTENT_DATA_1
def store( self, *, key: handlers.HandlerId, record: ProgressRecord, body: bodies.Body, patch: patches.Patch, ) -> None: safe_key = key.replace('/', '.') full_key = f'{self.prefix}/{safe_key}' if self.prefix else safe_key clean_data = {key: val for key, val in record.items() if self.verbose or val is not None} patch.meta.annotations[full_key] = json.dumps(clean_data)
def parent_handler(): def parent_fn(**_): pass return ResourceChangingHandler( fn=parent_fn, id=HandlerId('parent_fn'), errors=None, retries=None, timeout=None, backoff=None, cooldown=None, labels=None, annotations=None, when=None, initial=None, deleted=None, requires_finalizer=None, reason=None, field=None, )
def test_fetching_from_status_storage(cls): storage = cls(field='status.my-operator') body = Body({ 'status': { 'my-operator': { 'id1': CONTENT_DATA_1, 'id2': CONTENT_DATA_2 } } }) content = storage.fetch(body=body, key=HandlerId('id1')) assert content == CONTENT_DATA_1
def test_purging_of_annotations_storage_nullifies_content(cls): storage = cls(prefix='my-operator.example.com', verbose=True) patch = Patch() body = Body({ 'metadata': { 'annotations': { 'my-operator.example.com/id1': CONTENT_JSON_1, } } }) storage.purge(body=body, patch=patch, key=HandlerId('id1')) assert patch assert patch['metadata']['annotations'][ 'my-operator.example.com/id1'] is None
def test_storing_to_annotations_storage_cleans_content(cls): storage = cls(prefix='my-operator.example.com') # no verbose= patch = Patch() body = Body({}) content = ProgressRecord( started=None, stopped=None, delayed=None, retries=None, success=None, failure=None, message=None, ) storage.store(body=body, patch=patch, key=HandlerId('id1'), record=content) assert patch assert patch['metadata']['annotations'][ 'my-operator.example.com/id1'] == json.dumps({})
def test_keys_hashed_on_fetching(cls, prefix, provided_key, expected_key): storage = cls(prefix=prefix) body = Body({'metadata': {'annotations': {expected_key: CONTENT_JSON}}}) record = storage.fetch(body=body, key=HandlerId(provided_key)) assert record is not None assert record == CONTENT_DATA
def test_keys_normalized_on_purging(cls, prefix, provided_key, expected_key): storage = cls(prefix=prefix) patch = Patch() body = Body({'metadata': {'annotations': {expected_key: 'null'}}}) storage.purge(body=body, patch=patch, key=HandlerId(provided_key)) assert set(patch.metadata.annotations) == {expected_key}
def test_purging_already_empty_body_does_nothing(cls: Type[ProgressStorage]): storage = cls() patch = Patch() body = Body({}) storage.purge(body=body, patch=patch, key=HandlerId('id1')) assert not patch
def test_fetching_from_empty_body_returns_none(cls: Type[ProgressStorage]): storage = cls() body = Body({}) data = storage.fetch(body=body, key=HandlerId('id1')) assert data is None