def test_error_changing_config(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': 'changed'},
            }
        }

        subprocess.Popen.side_effect = self.popen_invalid

        with pytest.raises(kafka_topic_enforcer.TopicOperationError):
            kafka_topic_enforcer.check_and_change_topics(existing, new)

        subprocess.Popen.assert_called_once_with(
            ['kafka/home/bin/kafka-topics.sh', '--zookeeper',
             'localhost:2181', '--alter', '--topic', 'test', '--config',
             'retention.ms=8640000', '--config', 'random.config=changed'],
            stdout=subprocess.PIPE)
    def test_no_matching_configs(self, subprocess):
        existing = {
            'test': {
                'PartitionCount': '7',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs':
                {'retention.ms': '8640000',
                 'random.config': '123'},
            }
        }
        new = {
            'not_matching_test': {
                'PartitionCount': '1',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs':
                {'retention.ms': '8640000',
                 'random.config': '123'},
            }
        }

        subprocess.Popen.side_effect = self.popen_valid

        kafka_topic_enforcer.check_and_change_topics(existing, new)

        assert not subprocess.Popen.called
    def test_changing_configs_and_partition_and_deleteing(self, subprocess):
        existing = {
            'test': {
                'PartitionCount': '7',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs':
                {'retention.ms': '8640000',
                 'random.config': '123'},
            }
        }
        new = {
            'test': {
                'PartitionCount': '100',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs': {'retention.ms': '1'},
            }
        }

        subprocess.Popen.side_effect = self.popen_valid
        kafka_topic_enforcer.check_and_change_topics(existing, new)

        assert subprocess.Popen.call_count == 2
        subprocess.Popen.assert_any_call(
            ['kafka/home/bin/kafka-topics.sh', '--zookeeper',
             'localhost:2181', '--alter', '--topic', 'test', '--partitions',
             '100'],
            stdout=subprocess.PIPE)
        subprocess.Popen.assert_any_call(
            ['kafka/home/bin/kafka-topics.sh', '--zookeeper',
             'localhost:2181', '--alter', '--topic', 'test', '--config',
             'retention.ms=1'],
            stdout=subprocess.PIPE)
    def test_fail_decreasing_partitions(self, subprocess):
        existing = {
            'test': {
                'PartitionCount': '7',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs':
                {'retention.ms': '8640000',
                 'random.config': '123'},
            }
        }
        new = {
            'test': {
                'PartitionCount': '1',
                'ReplicationFactor': '4',
                'Topic': 'test',
                'Configs':
                {'retention.ms': '8640000',
                 'random.config': '123'},
            }
        }

        subprocess.Popen.side_effect = self.popen_valid

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

        assert not subprocess.Popen.called
    def test_no_change(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_change_topics(existing, new)

        assert not subprocess.Popen.called