def test_on_can_run_on_failure(): def _capture_value(x): global on_failure_value on_failure_value = x + "Plus" E.failure("someError") | E.on | dict(failure=_capture_value) assert on_failure_value == "someErrorPlus"
def test_on_can_run_whatever_on_failure(): def _capture_value(_, error): global on_whatever_value on_whatever_value = error + "Plus" E.failure("someError") | E.on | dict(whatever=_capture_value) assert on_whatever_value == "someErrorPlus"
def _run_dsl(args): os: MockOsSystem = args["os_system"] project_id = "project1" cluster_name = 'prod-us-east1' kubectl_version = "v1.15.3" service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) os.mock_clusters_list([cluster_name]) os.mock_kubernetes_release(E.success(kubectl_version)) os.mock_download_install_kubectl(kubectl_version, E.success()) os.mock_create_kube_config(cluster_name, E.success()) os.mock_kubectl(cluster_name, 'get mappings', E.failure('No such resource')) os.mock_kubectl(cluster_name, 'get virtualservices', E.failure('No such resource')) with patch.object(sys, "exit") as mock_exit: _, error = run_in_cluster(conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ gateway_mapping( 'api', 'api.com', 'api.default.svc.cluster.local') ]) assert mock_exit.call_args[0][0] == 1 assert str(error.caught_error) == 'Unsupported gateway'
def os_run(command, log: Logger): log.debug("OS_RUN: {}".format(command)) process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True) output = [] for line in iter(process.stdout.readline, 'b'): if line == b'': break line = line.rstrip().decode("utf-8") log.debug(line) output.append(line) for line in iter(process.stderr.readline, 'b'): if line == b'': break line = line.rstrip().decode("utf-8") log.error("OS_RUN: {}".format(line)) process.communicate() if process.returncode != 0: return E.failure("ErrorCode: " + str(process.returncode)) return E.success('\n'.join(output))
def _run_dsl(args): os: MockOsSystem = args["os_system"] fs: MockFileSystem = args['file_system'] project_id = "project1" cluster_name = 'prod-us-east1' kubectl_version = "v1.15.3" service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) os.mock_clusters_list([cluster_name]) os.mock_kubernetes_release(E.success(kubectl_version)) os.mock_download_install_kubectl(kubectl_version, E.success()) os.mock_create_kube_config(cluster_name, E.success()) os.mock_kubectl(cluster_name, 'get mappings', E.failure('No such resource')) os.mock_kubectl(cluster_name, 'get virtualservices', E.success()) _, result = run_in_cluster(conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ gateway_mapping( 'api', 'api.com', 'api.default.svc.cluster.local') ]) yamls = find_write_template_calls(fs) output_yaml = yamls[0] assert output_yaml == { 'apiVersion': 'networking.istio.io/v1alpha3', 'kind': 'VirtualService', 'metadata': { 'name': 'api-vs' }, 'spec': { 'hosts': ['api.com'], 'gateways': ['cluster-gateway'], 'http': [{ 'route': [{ 'destination': { 'host': 'api.default.svc.cluster.local' } }] }] } }
def _run_dsl(args): fs: MockFileSystem = args['file_system'] os: MockOsSystem = args["os_system"] project_id = "project1" cluster_name = 'prod-us-east1' kubectl_version = "v1.15.3" service_account_b64 = b64encode( json.dumps({ 'key': 'someKey' }).encode('utf-8')) os.mock_clusters_list([cluster_name]) os.mock_kubernetes_release(E.success(kubectl_version)) os.mock_download_install_kubectl(kubectl_version, E.success()) os.mock_create_kube_config(cluster_name, E.success()) os.mock_kubectl(cluster_name, 'get mappings', E.success()) os.mock_kubectl(cluster_name, 'get virtualservices', E.failure('No such resource')) _, result = run_in_cluster( conn=gke_conn(cluster_name, project_id, service_account_b64), templates=[ gateway_mapping('api', '*', 'api.default.svc.cluster.local') ]) yamls = find_write_template_calls(fs) output_yaml = yamls[0] assert output_yaml == { 'apiVersion': 'getambassador.io/v1', 'kind': 'Mapping', 'metadata': { 'name': 'api-mapping' }, 'spec': { 'prefix': '/', 'service': 'api.default.svc.cluster.local' } }
def test_from_either_can_return_on_failure(): assert E.failure("someError") \ | E.from_either | dict( if_success=lambda _: "shouldSucceed", if_failure=lambda e: e + "Plus") == "someErrorPlus"
def test_can_catch_error_and_default_non_either_result_to_success(): assert E.failure("someError") | E.catch_error | ( lambda _: 1) == E.success(1)
def test_can_catch_error_and_return_success(): assert E.failure("someError") | E.catch_error | ( lambda x: E.success(1)) == E.success(1)
def test_can_catch_error_and_return_error(): assert E.failure("someError") | E.catch_error | ( lambda x: E.failure(x + "Plus")) == E.failure("someErrorPlus")
def test_can_map_error(): assert E.failure("someError") | E.map_error | ( lambda x: x + "Plus") == E.failure("someErrorPlus")
def test_can_chain_success_either_and_fail(): assert E.success(1) | E.then | ( lambda _: E.failure("someError")) == E.failure("someError")
def test_can_map_failure_either(): assert E.failure("someError") | E.map | ( lambda x: x + 1) == E.failure("someError")
def test_can_make_failure_either(): assert E.failure("someError") == ("failure", "someError")