aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/node/IPTest.java
blob: 1f402876560fe9f34c7cfb30a7ee678933429b50 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.node;

import com.yahoo.config.provision.CloudName;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.NodeFlavors;
import com.yahoo.config.provision.NodeType;
import com.yahoo.vespa.hosted.provision.LockedNodeList;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.provisioning.FlavorConfigBuilder;
import com.yahoo.vespa.hosted.provision.testutils.MockNameResolver;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author mpolden
 */
public class IPTest {

    private static final NodeFlavors nodeFlavors = FlavorConfigBuilder.createDummies("default");
    private static final LockedNodeList emptyList = new LockedNodeList(List.of(), () -> {});
    private final MockNameResolver resolver = new MockNameResolver().explicitReverseRecords();

    private IP.Allocation.Context contextOf(boolean exclave) {
        return IP.Allocation.Context.from(CloudName.AWS, exclave, resolver);
    }

    @Test
    public void test_find_allocation_ipv6_only() {
        IP.Pool pool = createNode(List.of(
                "::1",
                "::2",
                "::3"
        )).ipConfig().pool();

        resolver.addRecord("host1", "::2");
        resolver.addRecord("host2", "::3");
        resolver.addRecord("host3", "::1");
        resolver.addReverseRecord("::3", "host2");
        resolver.addReverseRecord("::1", "host3");
        resolver.addReverseRecord("::2", "host1");

        var context = contextOf(false);
        Optional<IP.Allocation> allocation = pool.findAllocation(context, emptyList);
        assertEquals(Optional.of("::1"), allocation.get().ipv6Address());
        assertFalse(allocation.get().ipv4Address().isPresent());
        assertEquals("host3", allocation.get().hostname());

        // Allocation fails if DNS record is missing
        resolver.removeRecord("host3");
        try {
            pool.findAllocation(context, emptyList);
            fail("Expected exception");
        } catch (Exception e) {
            assertEquals("java.net.UnknownHostException: Could not resolve: host3", e.getMessage());
        }
    }

    @Test
    public void test_find_allocation_ipv4_only() {
        var pool = testPool(false);
        var allocation = pool.findAllocation(contextOf(false), emptyList);
        assertFalse("Found allocation", allocation.isEmpty());
        assertEquals(Optional.of("127.0.0.1"), allocation.get().ipv4Address());
        assertTrue("No IPv6 address", allocation.get().ipv6Address().isEmpty());
    }

    @Test
    public void test_find_allocation_dual_stack() {
        IP.Pool pool = testPool(true);
        Optional<IP.Allocation> allocation = pool.findAllocation(contextOf(false), emptyList);
        assertEquals(Optional.of("::1"), allocation.get().ipv6Address());
        assertEquals("127.0.0.2", allocation.get().ipv4Address().get());
        assertEquals("host3", allocation.get().hostname());
    }

    @Test
    public void test_find_allocation_multiple_ipv4_addresses() {
        IP.Pool pool = testPool(true);
        resolver.addRecord("host3", "127.0.0.127");
        try {
            pool.findAllocation(contextOf(false), emptyList);
            fail("Expected exception");
        } catch (IllegalArgumentException e) {
            assertEquals("Hostname host3 resolved to more than 1 IPv4 address: [127.0.0.2, 127.0.0.127]",
                         e.getMessage());
        }
    }

    @Test
    public void test_find_allocation_invalid_ipv4_reverse_record() {
        var pool = testPool(true);
        resolver.removeRecord("127.0.0.2")
                .addReverseRecord("127.0.0.2", "host5");
        try {
            pool.findAllocation(contextOf(false), emptyList);
            fail("Expected exception");
        } catch (IllegalArgumentException e) {
            assertEquals("Hostnames resolved from each IP address do not point to the same hostname " +
                         "[::1 -> host3, 127.0.0.2 -> host5]", e.getMessage());
        }
    }

    @Test
    public void test_enclave() {
        // In Enclave, the hosts and their nodes have only public IPv6 addresses,
        // and DNS has AAAA records without PTR records.

        resolver.addRecord("host1", "2600:1f10:::1")
                .addRecord("node1", "2600:1f10:::2")
                .addRecord("node2", "2600:1f10:::3");

        IP.Config config = IP.Config.of(List.of("2600:1f10:::1"),
                                        List.of("2600:1f10:::2", "2600:1f10:::3"),
                                        List.of(HostName.of("node1"), HostName.of("node2")));
        IP.Pool pool = config.pool();
        Optional<IP.Allocation> allocation = pool.findAllocation(contextOf(true), emptyList);
    }

    private IP.Pool testPool(boolean dualStack) {
        var addresses = new ArrayList<String>();
        addresses.add("127.0.0.1");
        addresses.add("127.0.0.2");
        addresses.add("127.0.0.3");
        if (dualStack) {
            addresses.add("::1");
            addresses.add("::2");
            addresses.add("::3");
        }

        Node node = createNode(addresses);

        // IPv4 addresses
        resolver.addRecord("host1", "127.0.0.3")
                .addRecord("host2", "127.0.0.1")
                .addRecord("host3", "127.0.0.2")
                .addReverseRecord("127.0.0.1", "host2")
                .addReverseRecord("127.0.0.2", "host3")
                .addReverseRecord("127.0.0.3", "host1");

        // IPv6 addresses
        if (dualStack) {
            resolver.addRecord("host1", "::2")
                    .addRecord("host2", "::3")
                    .addRecord("host3", "::1")
                    .addReverseRecord("::3", "host2")
                    .addReverseRecord("::1", "host3")
                    .addReverseRecord("::2", "host1");
        }

        IP.Pool pool = node.ipConfig().pool();
        assertNotEquals(dualStack, pool.ipAddresses().stack() == IP.IpAddresses.Stack.ipv4);
        return pool;
    }

    private static Node createNode(List<String> ipAddresses) {
        return Node.create("id1", IP.Config.of(List.of("127.0.0.1"), ipAddresses),
                           "host1", nodeFlavors.getFlavorOrThrow("default"), NodeType.host).build();
    }

}