def test_bridged_network(self):
        # Verify access to the containerized application from inside its own container
        self.is_kafka_music_app_healthy_for_service(
            "kafka-streams-examples-bridge", port=17070)

        # Verify outside access to the containerized application over the host network from a new container
        list_running_app_instances_url_host = "http://{host}:{port}/kafka-music/instances".format(
            host="localhost", port=27070)
        host_network_logs = utils.run_docker_command(
            image="confluentinc/cp-kafka-streams-examples",
            command=KAFKA_MUSIC_APP_HEALTH_CHECK.format(
                url=list_running_app_instances_url_host),
            host_config={'NetworkMode': 'host'})

        assert "PASS" in host_network_logs

        # Verify outside access to the containerized application over the bridge network from a new container
        list_running_app_instances_url_bridge = "http://{host}:{port}/kafka-music/instances".format(
            host="kafka-streams-examples-bridge", port=17070)
        bridge_network_logs = utils.run_docker_command(
            image="confluentinc/cp-kafka-streams-examples",
            command=KAFKA_MUSIC_APP_HEALTH_CHECK.format(
                url=list_running_app_instances_url_bridge),
            host_config={'NetworkMode': 'standalone-network-test_acme'})

        assert "PASS" in bridge_network_logs
 def test_cub_dub_runable(self):
     dub_cmd = "bash -c '/usr/local/bin/dub --help'"
     cub_cmd = "bash -c '/usr/local/bin/cub --help'"
     self.assertTrue(b"Docker Utility Belt" in utils.run_docker_command(
         image=self.image, command=dub_cmd))
     self.assertTrue(b"Confluent Platform Utility Belt." in utils.
                     run_docker_command(image=self.image, command=cub_cmd))
 def is_c3_healthy_for_service(cls, service):
     output = utils.run_docker_command(
         600,
         image=IMAGE_NAME,
         command=C3_CHECK.format(host=service, port=9021),
         host_config={'NetworkMode': 'config-test_default'}
     ).decode()
     assert "PASS" in output
    def test_bridged_network(self):
        # Verify access to the containerized application from inside its own container
        self.is_kafka_music_app_healthy_for_service("kafka-streams-examples-bridge", port=17070)

        # Verify outside access to the containerized application over the host network from a new container
        list_running_app_instances_url_host = "http://{host}:{port}/kafka-music/instances".format(host="localhost", port=27070)
        host_network_logs = utils.run_docker_command(
            image="confluentinc/cp-kafka-streams-examples",
            command=KAFKA_MUSIC_APP_HEALTH_CHECK.format(url=list_running_app_instances_url_host),
            host_config={'NetworkMode': 'host'})

        assert "PASS" in host_network_logs

        # Verify outside access to the containerized application over the bridge network from a new container
        list_running_app_instances_url_bridge = "http://{host}:{port}/kafka-music/instances".format(host="kafka-streams-examples-bridge", port=17070)
        bridge_network_logs = utils.run_docker_command(
            image="confluentinc/cp-kafka-streams-examples",
            command=KAFKA_MUSIC_APP_HEALTH_CHECK.format(url=list_running_app_instances_url_bridge),
            host_config={'NetworkMode': 'standalone-network-test_acme'})

        assert "PASS" in bridge_network_logs
def test_run_docker_command(official_image):
    cmd = "java -version"
    expected = b'OpenJDK Runtime Environment (Zulu 8.'
    output = utils.run_docker_command(image=OFFICIAL_IMAGE, command=cmd)
    assert expected in output
 def test_java_install(self):
     cmd = "java -version"
     expected = 'OpenJDK Runtime Environment (Zulu 8.'
     output = utils.run_docker_command(image=self.image, command=cmd)
     self.assertTrue(expected in output)
    def test_bridged_network(self):
        # Test from within the container
        self.is_c3_healthy_for_service("control-center-bridge", "standalone-network-test_zk")

        INTERCEPTOR_CLIENTS_CMD = """bash -xc '\
            export TOPIC="{topic}" \
            export MESSAGES="{messages}" \
            export CHECK_MESSAGES="{check_messages}"
            cub kafka-ready 1 40 -z "$ZOOKEEPER_CONNECT" \
            && control-center-run-class kafka.admin.TopicCommand --create --topic "$TOPIC" --partitions 1 --replication-factor 1 --zookeeper "$ZOOKEEPER_CONNECT" --if-not-exists \
            && seq "$MESSAGES" | control-center-run-class kafka.tools.ConsoleProducer --broker-list "$BOOTSTRAP_SERVERS" --topic "$TOPIC" --producer-property interceptor.classes=io.confluent.monitoring.clients.interceptor.MonitoringProducerInterceptor \
            && echo PRODUCED "$MESSAGES" messages. \
            && echo "interceptor.classes=io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor" > /tmp/consumer.config \
            && control-center-run-class kafka.tools.ConsoleConsumer --bootstrap-server "$BOOTSTRAP_SERVERS" --topic "$TOPIC" --from-beginning --max-messages "$CHECK_MESSAGES" --consumer.config /tmp/consumer.config'
            """

        MESSAGES = 10000
        TOPIC = 'test-topic'
        # Run producer and consumer with interceptor to generate monitoring data
        out = utils.run_docker_command(
            image=IMAGE_NAME,
            command=INTERCEPTOR_CLIENTS_CMD.format(topic=TOPIC, messages=MESSAGES, check_messages=MESSAGES),
            host_config={'NetworkMode': 'standalone-network-test_zk'},
            environment={'ZOOKEEPER_CONNECT': 'zookeeper-bridge:2181', 'BOOTSTRAP_SERVERS': 'kafka-bridge:19092'}
        )
        self.assertTrue("PRODUCED %s messages" % MESSAGES in out)

        # Check that data was processed
        # Calculate last hour and next hour in case we cross the border
        now_unix = int(time.time())
        prev_hr_start_unix = now_unix - now_unix % 3600
        next_hr_start_unix = prev_hr_start_unix + 2 * 3600

        # Fetch cluster id
        FETCH_CLUSTERS_CMD = """curl -s -H 'Content-Type: application/json' 'http://{host}:{port}/2.0/clusters/kafka/display/stream-monitoring'"""
        fetch_cluster_cmd_args = {
            'image': IMAGE_NAME,
            'command': FETCH_CLUSTERS_CMD.format(host="control-center-bridge", port=9021),
            'host_config': {'NetworkMode': 'standalone-network-test_zk'},
        }

        attempts = 0
        while attempts <= 60:
            attempts += 1
            out = json.loads(utils.run_docker_command(**fetch_cluster_cmd_args))
            self.assertTrue('defaultClusterId' in out)
            if out['defaultClusterId'] is None:
                time.sleep(5)
                continue
            else:
                self.assertTrue(len(out['clusters']) == 1, "Expected a single cluster in %s" % out)
                cluster_id = out['defaultClusterId']
                break

        # Fetch monitoring data
        FETCH_MONITORING_DATA_CMD = """curl -s -H 'Content-Type: application/json' 'http://{host}:{port}/2.0/monitoring/{cluster_id}/consumer_groups?startTimeMs={start}&stopTimeMs={stop}&rollup=ONE_HOUR'"""
        fetch_cmd_args = {
            'image': IMAGE_NAME,
            'command': FETCH_MONITORING_DATA_CMD.format(host="control-center-bridge", port=9021, start=prev_hr_start_unix * 1000, stop=next_hr_start_unix * 1000, cluster_id=cluster_id),
            'host_config': {'NetworkMode': 'standalone-network-test_zk'},
        }

        attempts = 0
        while attempts <= 60:
            attempts += 1
            out = json.loads(utils.run_docker_command(**fetch_cmd_args))
            self.assertTrue('sources' in out, 'Unexpected return data %s' % out)
            if len(out['sources']) == 0:
                time.sleep(5)
                continue
            else:
                self.assertEquals(1, len(out['sources']))
                self.assertEquals(TOPIC, out['sources'][0]['topic'])
                break