def test_no_absent_topics(self, subprocess):
        existing = {
            'test': {
                'PartitionCount': '7',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs':
                {'retention.ms': '8640000',
                 'random.config': '123'},
            }
        }
        new = {
            'test': {
                'PartitionCount': '7',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs':
                {'retention.ms': '8640000',
                 'random.config': '123'},
            }
        }

        kafka_topic_enforcer.check_and_create_absent_topics(existing, new)

        assert not subprocess.Popen.called
    def test_error_response(self, subprocess):
        existing = {
            'test2': {
                'PartitionCount': '2',
                'ReplicationFactor': '1',
                'Topic': 'test2',
                'Configs': {'retention.ms': '8640000',
                            'some.config': '123'},
                'partitions': [
                    {'topic': 'test2',
                     'partition': '0',
                     'replicas': ['2']},
                    {'topic': 'test2',
                     'partition': '1',
                     'replicas': ['3']}
                ]
            }
        }
        new = {
            'test': {
                'PartitionCount': '7',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs':
                {'retention.ms': '8640000',
                 'random.config': '123'},
            }
        }

        subprocess.Popen.return_value.communicate.return_value.__getitem__ \
            .return_value = (
                'Exception ERROR \n problem problem problem problem')

        with pytest.raises(Exception):
            kafka_topic_enforcer.check_and_create_absent_topics(existing, new)

        subprocess.Popen.assert_called_with(
            ['kafka/home/bin/kafka-topics.sh', '--zookeeper',
             'localhost:2181', '--create', '--topic', 'test', '--partitions',
             '7', '--replication-factor', '4', '--config',
             'retention.ms=8640000', '--config', 'random.config=123'],
            stdout=subprocess.PIPE)
    def test_normal_no_existing_topic(self, subprocess):
        existing = {}
        new = {
            'test': {
                'PartitionCount': '7',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs':
                {'retention.ms': '8640000',
                 'random.config': '123'},
            }
        }

        subprocess.Popen.return_value.communicate.return_value.__getitem__ \
            .return_value = 'Created topic "test"'

        kafka_topic_enforcer.check_and_create_absent_topics(existing, new)

        subprocess.Popen.assert_called_once_with(
            ['kafka/home/bin/kafka-topics.sh', '--zookeeper',
             'localhost:2181', '--create', '--topic', 'test', '--partitions',
             '7', '--replication-factor', '4', '--config',
             'retention.ms=8640000', '--config', 'random.config=123'],
            stdout=subprocess.PIPE)