aboutsummaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/test/java/com/yahoo/config/provision/HostFilterTest.java
blob: e8ad99ff0d34b133a4bbe692f8125af738980fc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;

import com.yahoo.component.Vtag;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author bratseth
 */
public class HostFilterTest {

    @Test
    void testSingleConditionFilter() {
        HostFilter all = HostFilter.all();
        HostFilter hostname = HostFilter.hostname("host1");
        HostFilter type = HostFilter.clusterType(ClusterSpec.Type.content);
        HostFilter id = HostFilter.clusterId(ClusterSpec.Id.from("type1"));

        assertTrue(      all.matches("anyhost", "flavor", membership("container/anytype/0/0")));
        assertFalse(hostname.matches("anyhost", "flavor", membership("container/anytype/0/0")));
        assertFalse(type.matches("anyhost", "flavor", membership("container/anytype/0/0")));
        assertFalse(id.matches("anyhost", "flavor", membership("container/anytype/0/0")));

        assertTrue(      all.matches("anyhost", "flavor", membership("content/anytype/0/0/stateful")));
        assertFalse(hostname.matches("anyhost", "flavor", membership("content/anytype/0/0/stateful")));
        assertTrue(     type.matches("anyhost", "flavor", membership("content/anytype/0/0/stateful")));
        assertFalse(      id.matches("anyhost", "flavor", membership("content/anytype/0/0/stateful")));

        assertTrue(      all.matches("host1", "flavor", membership("content/anytype/0/0/stateful")));
        assertTrue(hostname.matches("host1", "flavor", membership("content/anytype/0/0/stateful")));
        assertTrue(     type.matches("host1", "flavor", membership("content/anytype/0/0/stateful")));
        assertFalse(      id.matches("host1", "flavor", membership("content/anytype/0/0/stateful")));

        assertTrue(      all.matches("host1", "flavor", membership("content/type1/0/0/stateful")));
        assertTrue(hostname.matches("host1", "flavor", membership("content/type1/0/0/stateful")));
        assertTrue(     type.matches("host1", "flavor", membership("content/type1/0/0/stateful")));
        assertTrue(       id.matches("host1", "flavor", membership("content/type1/0/0/stateful")));
    }

    @Test
    void testMultiConditionFilter() {
        HostFilter typeAndId = HostFilter.from(Collections.emptyList(),
                               Collections.emptyList(),
                               Collections.singletonList(ClusterSpec.Type.content),
                               Collections.singletonList(ClusterSpec.Id.from("type1")));

        assertFalse(typeAndId.matches("anyhost", "flavor", membership("content/anyType/0/0/stateful")));
        assertFalse(typeAndId.matches("anyhost", "flavor", membership("container/type1/0/0")));
        assertTrue(typeAndId.matches("anyhost", "flavor", membership("content/type1/0/0/stateful")));
    }

    @Test
    void testMultiConditionFilterFromStrings() {
        HostFilter typeAndId = HostFilter.from("host1  host2, host3,host4", "  , ,flavor", null, "type1  ");

        assertFalse(typeAndId.matches("anotherhost", "flavor", membership("content/type1/0/0/stateful")));
        assertTrue(typeAndId.matches("host1", "flavor", membership("content/type1/0/0/stateful")));
        assertTrue(typeAndId.matches("host2", "flavor", membership("content/type1/0/0/stateful")));
        assertTrue(typeAndId.matches("host3", "flavor", membership("content/type1/0/0/stateful")));
        assertTrue(typeAndId.matches("host4", "flavor", membership("content/type1/0/0/stateful")));
        assertFalse(typeAndId.matches("host1", "flavor", membership("content/type2/0/0/stateful")));
        assertFalse(typeAndId.matches("host4", "differentflavor", membership("content/type1/0/0/stateful")));
    }

    private Optional<ClusterMembership> membership(String membershipString) {
        return Optional.of(ClusterMembership.from(membershipString, Vtag.currentVersion, Optional.empty()));
    }

}