Beispiel #1
0
def test_k8s_watch_bomb_on_deletion():
    def apply_name(mock, name):
        p = PropertyMock(return_value=name)
        type(mock).name = p

    md_mock1 = Mock()
    apply_name(md_mock1, 'DOES_NOT_MATCH')
    event_object1 = Mock(metadata=md_mock1)

    md_mock2 = Mock()
    apply_name(md_mock2, 'name')
    event_object2 = Mock(metadata=md_mock2)
    events = [{
        'object': event_object1,
        'type': 'DELETED'
    }, {
        'object': event_object2,
        'type': 'DELETED'
    }]

    mock_client = Mock(spec=yapconf.kubernetes_client.CoreV1Api)
    mock_client.list_namespaced_config_map = Mock(return_value=[])
    handler = Mock()
    source = get_source('label', 'kubernetes', client=mock_client, name='name')
    with patch('yapconf.sources.watch.Watch') as WatchMock:
        mock_watch = Mock()
        WatchMock.return_value = mock_watch

        mock_watch.stream = Mock(return_value=events)
        with pytest.raises(YapconfSourceError):
            source._watch(handler, {})
Beispiel #2
0
def test_k8s_watch():
    def apply_name(mock, name):
        p = PropertyMock(return_value=name)
        type(mock).name = p

    md_mock1 = Mock()
    apply_name(md_mock1, 'name')
    event_object1 = Mock(metadata=md_mock1)

    events = [
        {
            'object': event_object1,
            'type': 'MODIFIED'
        },
    ]

    mock_client = Mock(spec=yapconf.kubernetes_client.CoreV1Api)
    handler = Mock()
    source = get_source('label', 'kubernetes', client=mock_client, name='name')
    source.get_data = Mock(return_value="NEW DATA")
    with patch('yapconf.sources.watch.Watch') as WatchMock:
        mock_watch = Mock()
        WatchMock.return_value = mock_watch

        mock_watch.stream = Mock(return_value=events)
        source._watch(handler, {})

    handler.handle_config_change.assert_called_with("NEW DATA")
Beispiel #3
0
    def _extract_string_source(self, label, value, file_type):
        if value in self._sources:
            return self._sources[value]
        elif value == "ENVIRONMENT":
            return get_source(value, "environment")
        elif value == "CLI":
            return get_source(value, "cli", spec=self)
        elif file_type in ["json", "yaml"]:
            return get_source(value, file_type, filename=value, encoding=self._encoding)

        raise YapconfLoadError(
            "Invalid override given: %s. A string type was detected "
            "but no valid source could be generated. This should be "
            "a string which points to a label from the sources you added, "
            'the string "ENVIRONMENT" or have a file_type of "json" or "yaml"'
            "got a (value, file_type) of: (%s %s)" % (label, value, file_type)
        )
Beispiel #4
0
def test_etcd_watch():

    mock_client = Mock(
        spec=yapconf.etcd_client.Client,
        read=Mock(side_effect=[EtcdWatchTimedOut, "result", ValueError]))
    handler = Mock()
    source = get_source('label', 'etcd', client=mock_client)
    source.get_data = Mock(return_value='NEW_DATA')
    with pytest.raises(ValueError):
        source._watch(handler, {})

    handler.handle_config_change('NEW_DATA')
Beispiel #5
0
    def _extract_string_source(self, label, value, file_type):
        if value in self._sources:
            return self._sources[value]
        elif value == 'ENVIRONMENT':
            return get_source(value, 'environment')
        elif file_type in ['json', 'yaml']:
            return get_source(
                value,
                file_type,
                filename=value,
                encoding=self._encoding
            )

        raise YapconfLoadError(
            'Invalid override given: %s. A string type was detected '
            'but no valid source could be generated. This should be '
            'a string which points to a label from the sources you added, '
            'the string "ENVIRONMENT" or have a file_type of "json" or "yaml"'
            'got a (value, file_type) of: (%s %s)' %
            (label, value, file_type)
        )
Beispiel #6
0
    def _extract_source(self, index, override):
        label, unpacked_value, file_type = self._explode_override(override)

        if isinstance(unpacked_value, six.string_types):
            return self._extract_string_source(label, unpacked_value, file_type)

        elif isinstance(unpacked_value, dict):
            label = label or "dict-%d" % index
            return get_source(label, "dict", data=unpacked_value)

        raise YapconfLoadError(
            "Invalid override given: %s overrides must be one of the "
            "following: a dictionary, a filename, a label for a source, or "
            '"ENVIRONMENT". Got: %s' % (label, unpacked_value)
        )
Beispiel #7
0
    def add_source(self, label, source_type, **kwargs):
        """Add a source to the spec.

        Sources should have a unique label. This will help tracing where your
        configurations are coming from if you turn up the log-level.

        The keyword arguments are significant. Different sources require
        different keyword arguments. Required keys for each source_type are
        listed below, for a detailed list of all possible arguments, see the
        individual source's documentation.

        source_type: dict
            required keyword arguments:
                - data - A dictionary

        source_type: environment
            No required keyword arguments.

        source_type: etcd
            required keyword arguments:
                - client - A client from the python-etcd package.

        source_type: json
            required keyword arguments:
                - filename - A JSON file.
                - data - A string representation of JSON

        source_type: kubernetes
            required keyword arguments:
                - client - A client from the kubernetes package
                - name - The name of the ConfigMap to load

        source_type: yaml
            required keyword arguments:
                - filename - A YAML file.

        Args:
            label (str): A label for the source.
            source_type (str): A source type, available source types depend
            on the packages installed. See ``yapconf.ALL_SUPPORTED_SOURCES``
            for a complete list.

        """
        self._sources[label] = get_source(label, source_type, **kwargs)
Beispiel #8
0
def test_get_source(source_type, kwargs, klazz):
    source = get_source('label', source_type, **kwargs)
    assert isinstance(source, klazz)
Beispiel #9
0
def test_get_source_error(source_type, sources, kwargs):
    yapconf.SUPPORTED_SOURCES = sources
    with pytest.raises(YapconfSourceError):
        get_source('label', source_type, **kwargs)
Beispiel #10
0
def test_environment():
    source = get_source('label', 'environment')
    assert isinstance(source.get_data(), dict)