def test_catchall_versions_are_ignored_for_nonpreferred_resources(): resource = Resource( group='group1', version='version1', preferred=False, plural='plural1', singular='singular1', kind='kind1', shortcuts=['shortcut1', 'shortcut2'], categories=['category1', 'category2'], ) selector = Selector(EVERYTHING) matches = selector.check(resource) assert not matches
def test_all_selectors(operator_registry_cls): registry = operator_registry_cls() @kopf.index('resource0', registry=registry) @kopf.on.event('resource1', registry=registry) @kopf.on.event('resource2', registry=registry) @kopf.on.create('resource1', registry=registry) @kopf.on.update('resource2', registry=registry) @kopf.on.delete('resource3', registry=registry) @kopf.on.resume('resource4', registry=registry) @kopf.on.timer('resource5', registry=registry) @kopf.on.daemon('resource6', registry=registry) def fn(**_): ... assert registry._indexing.get_all_selectors() == {Selector('resource0')} assert registry._watching.get_all_selectors() == { Selector('resource1'), Selector('resource2') } assert registry._spawning.get_all_selectors() == { Selector('resource5'), Selector('resource6') } assert registry._changing.get_all_selectors() == { Selector('resource1'), Selector('resource2'), Selector('resource3'), Selector('resource4') }
def test_kwarg_any_name(): selector = Selector(any_name='name1') assert selector.group is None assert selector.version is None assert selector.kind is None assert selector.plural is None assert selector.singular is None assert selector.shortcut is None assert selector.category is None assert selector.any_name == 'name1'
def test_three_args(group, version, name): selector = Selector(group, version, name) assert selector.group == group assert selector.version == version assert selector.kind is None assert selector.plural is None assert selector.singular is None assert selector.shortcut is None assert selector.category is None assert selector.any_name == name
def test_two_args_and_corev1(name): selector = Selector('v1', name) assert selector.group == '' assert selector.version == 'v1' assert selector.kind is None assert selector.plural is None assert selector.singular is None assert selector.shortcut is None assert selector.category is None assert selector.any_name == name
def test_kwarg_version(): selector = Selector(version='version1', any_name='whatever') assert selector.group is None assert selector.version == 'version1' assert selector.kind is None assert selector.plural is None assert selector.singular is None assert selector.shortcut is None assert selector.category is None assert selector.any_name == 'whatever'
def test_one_arg_with_only_name(name): selector = Selector(name) assert selector.group is None assert selector.version is None assert selector.kind is None assert selector.plural is None assert selector.singular is None assert selector.shortcut is None assert selector.category is None assert selector.any_name == name
def test_two_args_with_group_and_conventional_version(version, group, name): selector = Selector(f'{group}/{version}', name) assert selector.group == group assert selector.version == version assert selector.kind is None assert selector.plural is None assert selector.singular is None assert selector.shortcut is None assert selector.category is None assert selector.any_name == name
def test_one_arg_with_group_and_conventional_version(version, group): selector = Selector(f'name1.{version}.{group}') assert selector.group == group assert selector.version == version assert selector.kind is None assert selector.plural is None assert selector.singular is None assert selector.shortcut is None assert selector.category is None assert selector.any_name == 'name1'
def test_two_args_with_only_group(group, name): selector = Selector(f'{group}', name) assert selector.group == group assert selector.version is None assert selector.kind is None assert selector.plural is None assert selector.singular is None assert selector.shortcut is None assert selector.category is None assert selector.any_name == name
def test_one_arg_with_group(group): selector = Selector(f'name1.{group}') assert selector.group == group assert selector.version is None assert selector.kind is None assert selector.plural is None assert selector.singular is None assert selector.shortcut is None assert selector.category is None assert selector.any_name == 'name1'
def test_when_matches_everything(resource, group, version): selector = Selector(EVERYTHING, group=group, version=version) matches = selector.check(resource) assert matches
def test_precedence_of_corev1_same_as_others_when_nonspecific(resource, v1_resource, kwarg, kwval): selector = Selector(**{kwarg: kwval}) selected = selector.select([resource, v1_resource]) assert not selector.is_specific # prerequisite assert selected == {resource, v1_resource}
def test_selection_of_nonspecific_resources(resource, kwarg, kwval): selector = Selector(**{kwarg: kwval}) selected = selector.select([resource]) assert not selector.is_specific # prerequisite assert selected == {resource}
def test_events_are_excluded_from_everything(resource_kwargs, selector_args): resource = Resource(**resource_kwargs, plural='events') selector = Selector(*selector_args) matches = selector.check(resource) assert not matches
def test_repr_with_names(kw): selector = Selector(**{kw: 'name1'}) selector_repr = repr(selector) assert selector_repr == f"Selector({kw}='name1')"
def test_empty_names_are_prohibited_with_kwargs(kwarg): with pytest.raises(TypeError) as err: Selector(**{kwarg: ''}) assert "Names must not be empty strings" in str(err.value)
def test_repr_with_group(group): selector = Selector(group=group, any_name='name1') selector_repr = repr(selector) assert selector_repr == f"Selector(group='{group}', any_name='name1')"
def test_no_args(): with pytest.raises(TypeError) as err: Selector() assert "Unspecific resource with no names" in str(err.value)
def test_empty_names_are_prohibited_with_positionals(): with pytest.raises(TypeError) as err: Selector('') assert "Names must not be empty strings" in str(err.value)
def test_when_matches_names(resource, kwarg, kwval, group, version): selector = Selector(group=group, version=version, **{kwarg: kwval}) matches = selector.check(resource) assert matches
def test_is_not_specific_with_categories(kw): selector = Selector(EVERYTHING) assert not selector.is_specific
def test_when_groupversion_mismatch_but_names_do_match(resource, kwarg, kwval, group, version): selector = Selector(group=group, version=version, **{kwarg: kwval}) matches = selector.check(resource) assert not matches
def test_conflicting_name_with_positional_name(kwarg): with pytest.raises(TypeError) as err: Selector('name1', **{kwarg: 'name2'}) assert "Ambiguous resource specification with names" in str(err.value)
def test_is_specific_with_names(kw): selector = Selector(**{kw: 'name1'}) assert selector.is_specific
def test_no_name_but_version(): with pytest.raises(TypeError) as err: Selector(version='version1') assert "Unspecific resource with no names" in str(err.value)
def test_is_not_specific_with_categories(kw): selector = Selector(**{kw: 'name1'}) assert not selector.is_specific
def test_events_are_matched_when_explicitly_named(selector_args): resource = Resource('', 'v1', 'events') selector = Selector(*selector_args) matches = selector.check(resource) assert matches
def selector(resource): """ The selector used in the tests. Usually mocked, so it does not matter. """ return Selector(group=resource.group, version=resource.version, plural=resource.plural)
def test_conflicting_names_with_only_kwargs(kwargs): with pytest.raises(TypeError) as err: Selector(**kwargs) assert "Ambiguous resource specification with names" in str(err.value)