def test_creating_cro_allows_fails_whencrd_not_applied_first( cl, client, has_conf): has_conf.return_value = False resp_data = { 'kind': 'Status', 'apiVersion': 'v1', 'metadata': {}, 'status': 'Failure', 'message': 'crontabs.stable.example.com "my-new-cron-object" already exists', 'reason': 'AlreadyExists', 'details': { 'name': 'my-new-cron-object', 'group': 'stable.example.com', 'kind': 'crontabs' }, 'code': 409 } resp = MagicMock() resp.status = 404 resp.reason = "Not Found" resource = { "group": "stable.example.com", "version": "v1", "plural": "crontabs", "resource": { "apiVersion": "stable.example.com/v1", "kind": "CronTab", "metadata": { "name": "my-new-cron-object" }, "spec": { "cronSpec": "* * * * */5", "image": "my-awesome-cron-image" } } } v1 = MagicMock() client.CustomObjectsApi.return_value = v1 v1.create_namespaced_custom_object.side_effect = ApiException( http_resp=resp) with pytest.raises(ActivityFailed): create_custom_object(group="stable.example.com", version="v1", plural="crontabs", resource=resource) assert v1.create_namespaced_custom_object.call_count == 1 v1.create_namespaced_custom_object.assert_called_with( "stable.example.com", "v1", "default", "crontabs", resource, _preload_content=False)
def test_creating_cro_allows_conflicts(cl, client, has_conf): has_conf.return_value = False resp_data = { "kind": "Status", "apiVersion": "v1", "metadata": {}, "status": "Failure", "message": 'crontabs.stable.example.com "my-new-cron-object" already exists', "reason": "AlreadyExists", "details": { "name": "my-new-cron-object", "group": "stable.example.com", "kind": "crontabs", }, "code": 409, } resp = MagicMock() resp.status = 409 resp.reason = "Conflicts" resp.data = json.dumps(resp_data) resource = { "group": "stable.example.com", "version": "v1", "plural": "crontabs", "resource": { "apiVersion": "stable.example.com/v1", "kind": "CronTab", "metadata": { "name": "my-new-cron-object" }, "spec": { "cronSpec": "* * * * */5", "image": "my-awesome-cron-image" }, }, } v1 = MagicMock() client.CustomObjectsApi.return_value = v1 v1.create_namespaced_custom_object.side_effect = ApiException( http_resp=resp) o = create_custom_object(group="stable.example.com", version="v1", plural="crontabs", resource=resource) assert o == resp_data assert v1.create_namespaced_custom_object.call_count == 1 v1.create_namespaced_custom_object.assert_called_with( "stable.example.com", "v1", "default", "crontabs", resource, _preload_content=False, )
def test_creating_cro_from_file(cl, client, has_conf): has_conf.return_value = False resp_data = {"apiVersion": "stable.example.com/v1", "kind": "CronTab"} resp = MagicMock() resp.data = json.dumps(resp_data) with NamedTemporaryFile() as f: f.write( json.dumps({ "apiVersion": "stable.example.com/v1", "kind": "CronTab", "metadata": { "name": "my-new-cron-object" }, "spec": { "cronSpec": "* * * * */5", "image": "my-awesome-cron-image", }, }).encode("utf-8")) f.seek(0) resource = { "group": "stable.example.com", "version": "v1", "plural": "crontabs", "resource_as_yaml_file": f.name, } v1 = MagicMock() client.CustomObjectsApi.return_value = v1 v1.create_namespaced_custom_object.return_value = resp o = create_custom_object( group="stable.example.com", version="v1", plural="crontabs", resource=resource, ) assert o == resp_data assert v1.create_namespaced_custom_object.call_count == 1 v1.create_namespaced_custom_object.assert_called_with( "stable.example.com", "v1", "default", "crontabs", ANY, _preload_content=False, )
def test_creating_cro(cl, client, has_conf): has_conf.return_value = False resp_data = {"apiVersion": "stable.example.com/v1", "kind": "CronTab"} resp = MagicMock() resp.data = json.dumps(resp_data) resource = { "group": "stable.example.com", "version": "v1", "plural": "crontabs", "resource": { "apiVersion": "stable.example.com/v1", "kind": "CronTab", "metadata": { "name": "my-new-cron-object" }, "spec": { "cronSpec": "* * * * */5", "image": "my-awesome-cron-image" }, }, } v1 = MagicMock() client.CustomObjectsApi.return_value = v1 v1.create_namespaced_custom_object.return_value = resp o = create_custom_object(group="stable.example.com", version="v1", plural="crontabs", resource=resource) assert o == resp_data assert v1.create_namespaced_custom_object.call_count == 1 v1.create_namespaced_custom_object.assert_called_with( "stable.example.com", "v1", "default", "crontabs", resource, _preload_content=False, )