def verify(self, network, service, setup_info):
        """
        Given the network name and the service name of the service under test,
        verify that it's behaving as expected.
        """
        def check_vault_setup():
            public_ips = [
                i.public_ip for s in service.subnetworks for i in s.instances
            ]
            assert public_ips, "No services are running..."
            for public_ip in public_ips:
                shares = 1
                threshold = 1
                client = hvac.Client(url='http://%s:8200' % public_ip)
                result = client.initialize(shares, threshold)
                root_token = result['root_token']
                keys = result['keys']
                client.unseal_multi(keys)
                logged_in_client = hvac.Client(url='http://%s:8200' %
                                               public_ip,
                                               token=root_token)
                logged_in_client.write('secret/foo', baz='bar', lease='1h')
                my_secret = logged_in_client.read('secret/foo')
                logged_in_client.delete('secret/foo')
                assert "baz" in my_secret[
                    "data"], "Baz not in my_secret: %s" % my_secret
                assert my_secret["data"][
                    "baz"] == "bar", "Baz not 'bar': %s" % my_secret

        call_with_retries(check_vault_setup, RETRY_COUNT, RETRY_DELAY)
예제 #2
0
    def verify(self, network, service, setup_info):
        """
        Given the network name and the service name of the service under test,
        verify that it's behaving as expected.
        """
        def check_responsive():
            public_ips = [
                i.public_ip for s in service.subnetworks for i in s.instances
            ]
            assert public_ips
            for public_ip in public_ips:
                response = requests.get("http://%s" % public_ip)
                expected_content = "Hello World"
                assert response.content, "No content in response"
                assert expected_content in str(
                    response.content), ("Unexpected content in response: %s" %
                                        response.content)

        call_with_retries(check_responsive, RETRY_COUNT, RETRY_DELAY)
    def verify(self, network, service, setup_info):
        """
        Given the network name and the service name of the service under test,
        verify that it's behaving as expected.
        """
        def check_consul_setup():
            public_ips = [
                i.public_ip for s in service.subnetworks for i in s.instances
            ]
            assert public_ips, "No services are running..."
            for public_ip in public_ips:
                consul_client = consul.Consul(public_ip)
                assert consul_client.kv.put(
                    'testkey', 'testvalue'), "Failed to put test key!"
                testvalue = consul_client.kv.get('testkey')
                assert testvalue[1]["Key"] == "testkey"
                consul_client.kv.delete('testkey')

        call_with_retries(check_consul_setup, RETRY_COUNT, RETRY_DELAY)
예제 #4
0
    def verify(self, network, service, setup_info):
        """
        Given the network name and the service name of the service under test,
        verify that it's behaving as expected.
        """
        def check_prometheus():
            instances = self.client.service.get_instances(service)
            assert instances, "No instances found!"
            for instance in instances:
                endpoint = 'http://%s:9090/api/v1/query' % instance.public_ip
                params = {"query": "prometheus_build_info"}
                response = requests.get(endpoint, params=params)
                api_response = response.json()
                assert api_response["status"] == "success"
                monitoring_self = False
                for result in api_response["data"]["result"]:
                    if result["metric"]["instance"] == "localhost:9090":
                        monitoring_self = True
                assert monitoring_self, "Found no metrics for Prometheus"

        call_with_retries(check_prometheus, RETRY_COUNT, RETRY_DELAY)
예제 #5
0
    def verify(self, network, service, setup_info):
        """
        Given the network name and the service name of the service under test,
        verify that it's behaving as expected.
        """
        def check_consul_setup():
            instances = self.client.service.get_instances(service)
            assert instances, "No instances found!"
            postgres_ip = instances[0].public_ip
            conn = psycopg2.connect("host=%s dbname=test user=test" %
                                    postgres_ip)
            cur = conn.cursor()
            cur.execute(
                "CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);"
            )
            cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
                        (100, "abcdef"))
            cur.execute("SELECT * FROM test;")
            value = cur.fetchone()
            assert value == (1, 100, 'abcdef'), "Saved value does not match!"

        call_with_retries(check_consul_setup, RETRY_COUNT, RETRY_DELAY)
예제 #6
0
def run_ssh_test(profile=None, provider=None, credentials=None):
    """
    Test that the instance management works against the given provider.
    """

    # Get the client for this test
    client = cloudless.Client(profile, provider, credentials)

    # Get a somewhat unique network name
    network_name = generate_unique_name("unittest")

    # Get a somewhat unique service name
    service_name = generate_unique_name("unittest")

    # Get a keypair to use
    key_pair = generate_ssh_keypair()

    # Provision all the resources
    test_network = client.network.create(network_name,
                                         blueprint=NETWORK_BLUEPRINT)
    if client.provider in ["aws", "mock-aws"]:
        test_service = client.service.create(
            test_network,
            service_name,
            AWS_SERVICE_BLUEPRINT,
            template_vars={
                "cloudless_image_build_ssh_key": key_pair.public_key,
                "cloudless_image_build_ssh_username": "******"
            },
            count=1)
    else:
        assert client.provider == "gce"
        test_service = client.service.create(
            test_network,
            service_name,
            GCE_SERVICE_BLUEPRINT,
            template_vars={
                "cloudless_image_build_ssh_key": key_pair.public_key,
                "cloudless_image_build_ssh_username": "******"
            },
            count=1)

    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:
            instances.extend(subnetwork.instances)
        assert len(instances) == count
        assert instances == client.service.get_instances(service)

    # Check that our service is provisioned properly
    validate_service(test_network, test_service, 1)

    # Add a path for SSH
    internet = CidrBlock("0.0.0.0/0")
    client.paths.add(internet, test_service, 22)

    if client.provider != "mock-aws":
        # Test that we can connect with the given key
        def attempt_connection():
            ssh = paramiko.SSHClient()
            ssh_key = paramiko.RSAKey(file_obj=StringIO(key_pair.private_key))
            public_ip = [
                i.public_ip for i in client.service.get_instances(test_service)
            ][0]
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(hostname=public_ip, username="******", pkey=ssh_key)
            return ssh

        call_with_retries(attempt_connection, int(10), float(1.0))
        ssh = attempt_connection()
        _, ssh_stdout, ssh_stderr = ssh.exec_command("whoami")
        assert ssh_stdout.read().decode().strip() == "cloudless"
        assert ssh_stderr.read().decode().strip() == ""

    # Make sure they are gone when I destroy them
    client.service.destroy(test_service)

    # Clean up the VPC
    client.network.destroy(test_network)