def print_graph(provider): """ Print a graph of all services for provider. """ if provider == "aws": client = butter.Client("aws", {}) elif provider == "gce": client = butter.Client( "gce", { "user_id": os.environ['BUTTER_GCE_USER_ID'], "key": os.environ['BUTTER_GCE_CREDENTIALS_PATH'], "project": os.environ['BUTTER_GCE_PROJECT_NAME'] }) else: raise NotImplementedError("Provider %s not supported" % provider) print(client.graph())
def run_network_test(provider, credentials): """ Test network management on the given provider. """ # Get the client for this test client = butter.Client(provider, credentials) # Get somewhat unique network names network1_name = generate_unique_name("network1") network2_name = generate_unique_name("network2") # Create two private cloud networks network1 = client.network.create(name=network1_name, blueprint=NETWORK_BLUEPRINT) network2 = client.network.create(name=network2_name) assert isinstance(network1, Network) assert isinstance(network2, Network) assert network2 != network1 # pylint: disable=comparison-with-itself assert network1 == network1 # pylint: disable=comparison-with-itself assert network2 == network2 # Make sure we can discover them again assert client.network.get(network1_name) == network1 assert client.network.get(network2_name) == network2 # Destroy them and make sure they no longer exist client.network.destroy(network1) client.network.destroy(network2) assert not client.network.get(network1_name) assert not client.network.get(network2_name)
def run_instance_fitter_test(provider, credentials): """ Test that we get the proper instance sizes for the given provider. """ # Get the client for this test client = butter.Client(provider, credentials) # If no memory, cpu, or storage is passed in, find the cheapest. if provider == "aws": assert get_fitting_instance(client.service, SMALL_INSTANCE_BLUEPRINT) == "t2.small" assert get_fitting_instance(client.service, LARGE_INSTANCE_BLUEPRINT) == "m4.xlarge" if provider == "gce": assert get_fitting_instance(client.service, SMALL_INSTANCE_BLUEPRINT) == "n1-highcpu-4" assert get_fitting_instance(client.service, LARGE_INSTANCE_BLUEPRINT) == "n1-highmem-4"
def run_blueprint_tester_test(provider, credentials): """ Test blueprint test framework with the given provider. """ # Get the client for this test client = butter.Client(provider, credentials) # First make sure we have no state if we teardown do_teardown(client, BLUEPRINT_DIR) state = get_state(BLUEPRINT_DIR) assert not state # Then, make sure the full run works run_all(client, BLUEPRINT_DIR) # Now make sure the state got cleared state = get_state(BLUEPRINT_DIR) assert not state # Now do just the setup and teardown do_setup(client, BLUEPRINT_DIR) state = get_state(BLUEPRINT_DIR) assert state assert client.network.get(state["network_name"]) assert client.service.get(client.network.get(state["network_name"]), state["service_name"]) assert state["setup_info"] do_teardown(client, BLUEPRINT_DIR) state = get_state(BLUEPRINT_DIR) assert not state # Now do setup, multiple verifies and teardown do_setup(client, BLUEPRINT_DIR) state = get_state(BLUEPRINT_DIR) assert state assert client.network.get(state["network_name"]) assert client.service.get(client.network.get(state["network_name"]), state["service_name"]) assert state["setup_info"] do_verify(client, BLUEPRINT_DIR) do_verify(client, BLUEPRINT_DIR) do_teardown(client, BLUEPRINT_DIR) state = get_state(BLUEPRINT_DIR) assert not state
def run_instances_test(provider, credentials): """ Test that the instance management works against the given provider. """ # Get the client for this test client = butter.Client(provider, credentials) # Get a somewhat unique network name network_name = generate_unique_name("unittest") # Provision all the resources test_network = client.network.create(network_name, blueprint=NETWORK_BLUEPRINT) if provider in ["aws", "mock-aws"]: lb_service = client.service.create(test_network, "web-lb", AWS_SERVICE_BLUEPRINT, {}) web_service = client.service.create(test_network, "web", AWS_SERVICE_BLUEPRINT, {}, count=6) else: assert provider == "gce" lb_service = client.service.create(test_network, "web-lb", GCE_SERVICE_BLUEPRINT, {}) web_service = client.service.create(test_network, "web", GCE_SERVICE_BLUEPRINT, {}, count=6) def validate_service(network, service, count): discovered_service = client.service.get(network, service.name) assert discovered_service.network == network assert discovered_service.name == service.name assert discovered_service == service assert isinstance(discovered_service, Service) assert isinstance(service, Service) instances = [] for subnetwork in discovered_service.subnetworks: assert subnetwork.instances instances.extend(subnetwork.instances) assert len(instances) == count assert instances == client.service.get_instances(service) validate_service(test_network, lb_service, 3) validate_service(test_network, web_service, 6) if provider in ["aws", "mock-aws"]: # Networking ec2 = boto3.client("ec2") dc_id = client.network.get(network_name).network_id subnets = ec2.describe_subnets(Filters=[{ 'Name': 'vpc-id', 'Values': [dc_id] }]) route_tables = ec2.describe_route_tables(Filters=[{ 'Name': 'vpc-id', 'Values': [dc_id] }]) assert len(route_tables["RouteTables"]) == 7 assert len(subnets["Subnets"]) == 6 # AutoScalingGroup autoscaling = boto3.client("autoscaling") web_asgs = autoscaling.describe_auto_scaling_groups( AutoScalingGroupNames=["%s.web" % network_name]) assert len(web_asgs["AutoScalingGroups"]) == 1 web_asg = web_asgs["AutoScalingGroups"][0] assert web_asg["AutoScalingGroupName"] == "%s.web" % network_name assert web_asg["LaunchConfigurationName"] == "%s.web" % network_name web_lb_asgs = autoscaling.describe_auto_scaling_groups( AutoScalingGroupNames=["%s.web-lb" % network_name]) assert len(web_lb_asgs["AutoScalingGroups"]) == 1 web_lb_asg = web_lb_asgs["AutoScalingGroups"][0] assert web_lb_asg["AutoScalingGroupName"] == "%s.web-lb" % network_name assert web_lb_asg[ "LaunchConfigurationName"] == "%s.web-lb" % network_name # Make sure subnets don't overlap web_asg = web_asgs["AutoScalingGroups"][0] web_asg_subnet_ids = web_asg["VPCZoneIdentifier"].split(",") assert len(web_asg_subnet_ids) == 3 web_lb_asg = web_lb_asgs["AutoScalingGroups"][0] web_lb_asg_subnet_ids = web_lb_asg["VPCZoneIdentifier"].split(",") assert len(web_lb_asg_subnet_ids) == 3 web_asg_subnets = ec2.describe_subnets(SubnetIds=web_asg_subnet_ids) assert len(web_asg_subnets["Subnets"]) == 3 web_lb_asg_subnets = ec2.describe_subnets( SubnetIds=web_lb_asg_subnet_ids) assert len(web_lb_asg_subnets["Subnets"]) == 3 for web_asg_subnet in web_asg_subnets["Subnets"]: web_asg_cidr = ipaddress.ip_network( str(web_asg_subnet["CidrBlock"])) for web_lb_asg_subnet in web_lb_asg_subnets["Subnets"]: web_lb_asg_cidr = ipaddress.ip_network( str(web_lb_asg_subnet["CidrBlock"])) assert not web_asg_cidr.overlaps(web_lb_asg_cidr) # Make sure they got allocated in the same VPC web_asg_vpc_id = None for web_asg_subnet in web_asg_subnets["Subnets"]: if not web_asg_vpc_id: web_asg_vpc_id = web_asg_subnet["VpcId"] assert web_asg_subnet["VpcId"] == web_asg_vpc_id web_lb_asg_vpc_id = None for web_lb_asg_subnet in web_lb_asg_subnets["Subnets"]: if not web_lb_asg_vpc_id: web_lb_asg_vpc_id = web_lb_asg_subnet["VpcId"] assert web_lb_asg_subnet["VpcId"] == web_lb_asg_vpc_id assert web_asg_vpc_id == web_lb_asg_vpc_id # Make sure they are gone when I destroy them client.service.destroy(lb_service) if provider in ["aws", "mock-aws"]: # Networking ec2 = boto3.client("ec2") subnets = ec2.describe_subnets(Filters=[{ 'Name': 'vpc-id', 'Values': [dc_id] }]) route_tables = ec2.describe_route_tables(Filters=[{ 'Name': 'vpc-id', 'Values': [dc_id] }]) assert len(route_tables["RouteTables"]) == 4 assert len(subnets["Subnets"]) == 3 # AutoScalingGroup autoscaling = boto3.client("autoscaling") asgs = autoscaling.describe_auto_scaling_groups( AutoScalingGroupNames=["%s.web" % network_name]) assert len(asgs["AutoScalingGroups"]) == 1 asg = asgs["AutoScalingGroups"][0] assert asg["AutoScalingGroupName"] == "%s.web" % network_name assert asg["LaunchConfigurationName"] == "%s.web" % network_name # Now destroy the rest client.service.destroy(web_service) if provider in ["aws", "mock-aws"]: # AutoScalingGroups autoscaling = boto3.client("autoscaling") asgs = autoscaling.describe_auto_scaling_groups( AutoScalingGroupNames=["%s.web" % network_name]) assert not asgs["AutoScalingGroups"] asgs = autoscaling.describe_auto_scaling_groups( AutoScalingGroupNames=["%s.web-lb" % network_name]) assert not asgs["AutoScalingGroups"] # Clean up the VPC client.network.destroy(test_network)
def run(ctx): """ Run setup, verify, and teardown in sequence. """ client = butter.Client(ctx.obj['PROVIDER'], ctx.obj['CREDENTIALS']) run_all(client, ctx.obj['BLUEPRINT_DIR'])
def teardown(ctx): """ Clean up and destroy all resources. """ client = butter.Client(ctx.obj['PROVIDER'], ctx.obj['CREDENTIALS']) do_teardown(client, ctx.obj['BLUEPRINT_DIR'])
def verify(ctx): """ Verify the service is behaving as expected. """ client = butter.Client(ctx.obj['PROVIDER'], ctx.obj['CREDENTIALS']) do_verify(client, ctx.obj['BLUEPRINT_DIR'])
def setup(ctx): """ Deploy the service and dependencies. """ client = butter.Client(ctx.obj['PROVIDER'], ctx.obj['CREDENTIALS']) do_setup(client, ctx.obj['BLUEPRINT_DIR'])