aboutsummaryrefslogtreecommitdiffstats
path: root/routing-generator/src/test/java/com/yahoo/vespa/hosted/routing/nginx/NginxTest.java
blob: 3e88fecd222f731062facdb279a74aeff4481159 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.routing.nginx;

import com.google.common.jimfs.Jimfs;
import com.yahoo.collections.Pair;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.zone.RoutingMethod;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.jdisc.test.MockMetric;
import com.yahoo.system.ProcessExecuter;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.hosted.routing.RoutingTable;
import com.yahoo.vespa.hosted.routing.TestUtil;
import com.yahoo.vespa.hosted.routing.mock.RoutingStatusMock;
import com.yahoo.yolean.Exceptions;
import com.yahoo.yolean.concurrent.Sleeper;
import org.junit.Test;

import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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

    private static final String diffCommand = "diff -U1 /opt/vespa/var/vespa-hosted/routing/nginxl4.conf /opt/vespa/var/vespa-hosted/routing/nginxl4.conf.tmp";

    @Test
    public void load_routing_table() {
        NginxTester tester = new NginxTester();
        tester.clock.setInstant(Instant.parse("2022-01-01T15:00:00Z"));

        // Load routing table
        RoutingTable table0 = TestUtil.readRoutingTable("lbservices-config");
        tester.load(table0)
              .assertVerifiedConfig(1)
              .assertLoadedConfig(true)
              .assertConfigContents("nginx.conf")
              .assertTemporaryConfigRemoved(true)
              .assertMetric(Nginx.CONFIG_RELOADS_METRIC, 1)
              .assertMetric(Nginx.OK_CONFIG_RELOADS_METRIC, 1)
              .assertMetric(Nginx.GENERATED_UPSTREAMS_METRIC, 5);

        // Loading the same table again does nothing
        tester.load(table0)
              .assertVerifiedConfig(1)
              .assertLoadedConfig(false)
              .assertConfigContents("nginx.conf")
              .assertTemporaryConfigRemoved(true)
              .assertMetric(Nginx.CONFIG_RELOADS_METRIC, 1)
              .assertMetric(Nginx.OK_CONFIG_RELOADS_METRIC, 1)
              .assertMetric(Nginx.GENERATED_UPSTREAMS_METRIC, 5);

        // A new table is loaded
        Map<RoutingTable.Endpoint, RoutingTable.Target> newEntries = new HashMap<>(table0.asMap());
        newEntries.put(new RoutingTable.Endpoint("endpoint1", RoutingMethod.sharedLayer4),
                       RoutingTable.Target.create(ApplicationId.from("t1", "a1", "i1"),
                                                  ClusterSpec.Id.from("default"),
                                                  ZoneId.from("prod", "us-north-1"),
                                                  List.of(new RoutingTable.Real("host42", 4443, 1, true))));
        RoutingTable table1 = new RoutingTable(newEntries, 43);

        // Verification of new table fails enough times to exhaust retries
        tester.processExecuter.withFailCount(10);
        try {
            tester.load(table1);
            fail("Expected exception");
        } catch (Exception ignored) {}
        tester.assertVerifiedConfig(5)
              .assertLoadedConfig(false)
              .assertConfigContents("nginx.conf")
              .assertTemporaryConfigRemoved(false)
              .assertMetric(Nginx.CONFIG_RELOADS_METRIC, 1)
              .assertMetric(Nginx.OK_CONFIG_RELOADS_METRIC, 1);

        // Verification succeeds, with few enough failures
        tester.processExecuter.withFailCount(3);
        tester.load(table1)
              .assertVerifiedConfig(3)
              .assertLoadedConfig(true)
              .assertConfigContents("nginx-updated.conf")
              .assertTemporaryConfigRemoved(true)
              .assertProducedDiff()
              .assertRotatedFiles("nginxl4.conf-2022-01-01-15:00:00.000")
              .assertMetric(Nginx.CONFIG_RELOADS_METRIC, 2)
              .assertMetric(Nginx.OK_CONFIG_RELOADS_METRIC, 2);

        // Some time passes and new tables are loaded. Old rotated files are removed
        tester.clock.advance(Duration.ofDays(3));
        tester.load(table0);
        tester.clock.advance(Duration.ofDays(4).plusSeconds(1));
        tester.load(table1)
              .assertProducedDiff()
              .assertRotatedFiles("nginxl4.conf-2022-01-04-15:00:00.000",
                                  "nginxl4.conf-2022-01-08-15:00:01.000");
        tester.clock.advance(Duration.ofDays(4));
        tester.load(table1) // Same table is loaded again, which is a no-op, but old rotated files are still removed
              .assertRotatedFiles("nginxl4.conf-2022-01-08-15:00:01.000");
    }

    private static class NginxTester {

        private final FileSystem fileSystem =  Jimfs.newFileSystem();
        private final ManualClock clock = new ManualClock();
        private final RoutingStatusMock routingStatus = new RoutingStatusMock();
        private final ProcessExecuterMock processExecuter = new ProcessExecuterMock();
        private final MockMetric metric = new MockMetric();
        private final Nginx nginx = new Nginx(fileSystem, processExecuter, Sleeper.NOOP, clock, routingStatus, metric, true);

        public NginxTester load(RoutingTable table) {
            processExecuter.clearHistory();
            nginx.load(table);
            return this;
        }

        public NginxTester assertMetric(String name, double expected) {
            assertEquals("Metric " + name + " has expected value", expected, metric.metrics().get(name).get(Map.of()), Double.MIN_VALUE);
            return this;
        }

        public NginxTester assertConfigContents(String expectedConfig) {
            String expected = Exceptions.uncheck(() -> Files.readString(TestUtil.testFile(expectedConfig)));
            String actual = Exceptions.uncheck(() -> Files.readString(NginxPath.config.in(fileSystem)));
            assertEquals(expected, actual);
            return this;
        }

        public NginxTester assertTemporaryConfigRemoved(boolean removed) {
            Path path = NginxPath.temporaryConfig.in(fileSystem);
            assertEquals(path + (removed ? " does not exist" : " exists"), removed, !Files.exists(path));
            return this;
        }

        public NginxTester assertRotatedFiles(String... expectedRotatedFiles) {
            List<String> rotatedFiles = Exceptions.uncheck(() -> Files.list(NginxPath.root.in(fileSystem))
                                                                      .map(path -> path.getFileName().toString())
                                                                      .filter(filename -> filename.contains(".conf-"))
                                                                      .toList());
            assertEquals(List.of(expectedRotatedFiles), rotatedFiles);
            return this;
        }

        public NginxTester assertVerifiedConfig(int times) {
            for (int i = 0; i < times; i++) {
                assertEquals("/usr/bin/sudo /opt/vespa/bin/vespa-verify-nginx", processExecuter.history().get(i));
            }
            return this;
        }


        public NginxTester assertProducedDiff() {
            assertTrue(processExecuter.history.contains(diffCommand));
            return this;
        }

        public NginxTester assertLoadedConfig(boolean loaded) {
            String reloadCommand = "/usr/bin/sudo /opt/vespa/bin/vespa-reload-nginx";
            if (loaded) {
                assertEquals(reloadCommand, processExecuter.history().get(processExecuter.history().size() - 1));
            } else {
                assertTrue("Config is not loaded",
                           processExecuter.history.stream().noneMatch(command -> command.equals(reloadCommand)));
            }
            return this;
        }

    }

    private static class ProcessExecuterMock extends ProcessExecuter {

        private final List<String> history = new ArrayList<>();

        private int wantedFailCount = 0;
        private int currentFailCount = 0;

        public List<String> history() {
            return Collections.unmodifiableList(history);
        }

        public ProcessExecuterMock clearHistory() {
            history.clear();
            return this;
        }

        public ProcessExecuterMock withFailCount(int count) {
            this.wantedFailCount = count;
            this.currentFailCount = 0;
            return this;
        }

        @Override
        public Pair<Integer, String> exec(String command) {
            history.add(command);
            int exitCode = 0;
            String out = "";
            if(command.equals(diffCommand))
                return new Pair<>(1, """
                        --- /opt/vespa/var/vespa-hosted/routing/nginxl4.conf	2023-10-09 05:28:09.815315000 +0000
                        +++ /opt/vespa/var/vespa-hosted/routing/nginxl4.conf.tmp	2023-10-09 05:28:27.223030000 +0000
                        @@ -45,7 +45,6 @@
                              server 123.example.com:4443;
                           -  server 456.example.com:4443;
                              server 789.example.com:4443;""");

            if (++currentFailCount <= wantedFailCount) {
                exitCode = 1;
                out = "failing to unit test";
            }
            return new Pair<>(exitCode, out);
        }

        @Override
        public Pair<Integer, String> exec(String[] command) {
            return exec(String.join(" ", command));
        }

    }

}