aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/xml/HandlerBuilderTest.java
blob: fdeea85c5a3b8285a67d0ef1ca7d84f47b24c1d5 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.xml;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.model.ConfigModelContext;
import com.yahoo.config.model.builder.xml.test.DomBuilderTest;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.container.ComponentsConfig;
import com.yahoo.container.jdisc.JdiscBindingsConfig;
import com.yahoo.container.usability.BindingsOverviewHandler;
import com.yahoo.vespa.model.container.ApplicationContainerCluster;
import com.yahoo.vespa.model.container.component.Component;
import com.yahoo.vespa.model.container.component.Handler;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import static com.yahoo.vespa.model.container.ContainerCluster.ROOT_HANDLER_BINDING;
import static com.yahoo.vespa.model.container.ContainerCluster.STATE_HANDLER_BINDING_1;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * Tests for container model building with custom handlers.
 *
 * @author gjoranv
 */
public class HandlerBuilderTest extends ContainerModelBuilderTestBase {

    @Test
    void handlers_are_included_in_components_config() {
        createClusterWithJDiscHandler();
        assertThat(componentsConfig().toString(), containsString(".id \"discHandler\""));
    }

    @Test
    void handler_bindings_are_included_in_discBindings_config() {
        createClusterWithJDiscHandler();
        String discBindingsConfig = root.getConfig(JdiscBindingsConfig.class, "default").toString();
        assertThat(discBindingsConfig, containsString(".serverBindings[0] \"http://*/binding0\""));
        assertThat(discBindingsConfig, containsString(".serverBindings[1] \"http://*/binding1\""));
    }

    @Test
    void nested_components_are_injected_to_handlers() {
        Element clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>",
                "  <handler id='myHandler'>",
                "    <component id='injected' />",
                "  </handler>",
                "</container>");

        createModel(root, clusterElem);
        Component<?, ?> handler = getComponent("default", "myHandler");
        assertThat(handler.getInjectedComponentIds(), hasItem("injected@myHandler"));
    }

    @Test
    void warn_on_bindings_shared_by_multiple_handlers() {
        class TestDeployLogger implements DeployLogger {
            List<String> logs = new ArrayList<>();
            @Override public void log(Level level, String message) { logs.add(message); }
        }
        var clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>",
                "  <handler id='myHandler1'>",
                "    <binding>http://*/myhandler</binding>",
                "    <binding>https://*/myhandler</binding>",
                "  </handler>",
                "  <handler id='myHandler2'>",
                "    <binding>http://*/myhandler</binding>",
                "    <binding>https://*/myhandler</binding>",
                "  </handler>",
                "</container>");
        var logger = new TestDeployLogger();
        createModel(root, logger, clusterElem);
        assertEquals(
                List.of("Binding 'http://*/myhandler' was already in use by handler 'myHandler1', but will now be taken over by handler: myHandler2",
                        "Binding 'https://*/myhandler' was already in use by handler 'myHandler1', but will now be taken over by handler: myHandler2"),
                logger.logs);
    }

    @Test
    void default_root_handler_binding_can_be_stolen_by_user_configured_handler() {
        Element clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>" +
                        "  <handler id='userRootHandler'>" +
                        "    <binding>" + ROOT_HANDLER_BINDING.patternString() + "</binding>" +
                        "  </handler>" +
                        "</container>");
        createModel(root, clusterElem);

        // The handler is still set up.
        ComponentsConfig.Components userRootHandler = getComponentInConfig(componentsConfig(), BindingsOverviewHandler.class.getName());
        assertNotNull(userRootHandler);

        // .. but it has no bindings
        var discBindingsConfig = root.getConfig(JdiscBindingsConfig.class, "default");
        assertNull(discBindingsConfig.handlers(BindingsOverviewHandler.class.getName()));
    }

    @Test
    void reserved_binding_cannot_be_stolen_by_user_configured_handler() {
        Element clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>" +
                        "  <handler id='userHandler'>" +
                        "    <binding>" + STATE_HANDLER_BINDING_1.patternString() + "</binding>" +
                        "  </handler>" +
                        "</container>");
        try {
            createModel(root, clusterElem);
            fail("Expected exception when stealing a reserved binding.");
        } catch (IllegalArgumentException e) {
            assertThat(e.getMessage(), is("Binding 'http://*/state/v1' is a reserved Vespa binding " +
                    "and cannot be used by handler: userHandler"));
        }
    }

    @Test
    void custom_handler_gets_default_threadpool() {
        createClusterWithJDiscHandler();
        ApplicationContainerCluster cluster = (ApplicationContainerCluster) root.getChildren().get("default");
        Handler handler = cluster.getHandlers().stream()
                .filter(h -> h.getComponentId().toString().equals("discHandler"))
                .findAny().orElseThrow();

        assertTrue(handler.getInjectedComponentIds().contains("threadpool@default-handler-common"));
    }

    @Test
    void restricts_default_bindings_in_hosted_vespa() {
        DeployState deployState = new DeployState.Builder()
                .properties(new TestProperties().setHostedVespa(true))
                .build();
        verifyDefaultBindings(deployState, "http://*:4443");
    }

    @Test
    void restricts_custom_bindings_in_hosted_vespa() {
        DeployState deployState = new DeployState.Builder()
                .properties(new TestProperties().setHostedVespa(true))
                .build();
        verifyCustomSearchBindings(deployState, "http://*:4443");
    }

    @Test
    void does_not_restrict_default_bindings_in_self_hosted() {
        DeployState deployState = new DeployState.Builder()
                .properties(new TestProperties().setHostedVespa(false))
                .build();
        verifyDefaultBindings(deployState, "http://*");
    }

    @Test
    void does_not_restrict_custom_bindings_in_self_hosted() {
        DeployState deployState = new DeployState.Builder()
                .properties(new TestProperties().setHostedVespa(false))
                .build();
        verifyCustomSearchBindings(deployState, "http://*");
    }

    private void verifyDefaultBindings(DeployState deployState, String bindingPrefix) {
        verifyDefaultBindings(deployState, bindingPrefix, ConfigModelContext.ApplicationType.DEFAULT);
    }
    private void verifyDefaultBindings(DeployState deployState, String bindingPrefix, ConfigModelContext.ApplicationType applicationType) {
        Element clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>",
                "  <search/>",
                "  <document-api/>",
                "  <handler id='FooHandler'>",
                "     <binding>http://*/foo</binding>",
                "  </handler>",
                nodesXml,
                "</container>");

        createModel(root, deployState, null, clusterElem);
        JdiscBindingsConfig bindingsConfig = root.getConfig(JdiscBindingsConfig.class, "default");

        // Verify /search /feed /document and custom handler are bound correctly
        Map<String, JdiscBindingsConfig.Handlers> handlers = bindingsConfig.handlers();
        Map<String, List<String>> expectedHandlerMappings = Map.of(
                "com.yahoo.search.handler.SearchHandler", List.of("/search/*"),
                "com.yahoo.document.restapi.resource.DocumentV1ApiHandler", List.of("/document/v1/*", "/document/v1/*/"),
                "com.yahoo.vespa.http.server.FeedHandler", List.of("/reserved-for-internal-use/feedapi", "/reserved-for-internal-use/feedapi/"),
                "FooHandler", List.of("/foo"));
        expectedHandlerMappings.forEach((handler, bindings) -> validateHandler(handlers.get(handler), bindingPrefix, bindings));

        // All other handlers should be bound to default (http://*/...)
        handlers.entrySet().stream()
                .filter(e -> ! expectedHandlerMappings.containsKey(e.getKey()))
                .forEach(e -> assertTrue(e.getValue().serverBindings().stream().allMatch(s -> s.startsWith("http://*/"))));
    }

    private void verifyCustomSearchBindings(DeployState deployState, String bindingPrefix) {
        Element clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>",
                "  <search>",
                "     <binding>http://*/search-binding/</binding>",
                "  </search>",
                "  <document-api>",
                "     <binding>http://*/docapi-binding/</binding>",
                "  </document-api>",
                nodesXml,
                "</container>");

        createModel(root, deployState, null, clusterElem);
        JdiscBindingsConfig bindingsConfig = root.getConfig(JdiscBindingsConfig.class, "default");

        // Verify search feed and document handler are bound correctly
        Map<String, JdiscBindingsConfig.Handlers> handlers = bindingsConfig.handlers();
        Map<String, List<String>> expectedHandlerMappings = Map.of(
                "com.yahoo.search.handler.SearchHandler", List.of("/search-binding/"),
                "com.yahoo.document.restapi.resource.DocumentV1ApiHandler", List.of("/docapi-binding/document/v1/*", "/docapi-binding/document/v1/*/"),
                "com.yahoo.vespa.http.server.FeedHandler", List.of("/docapi-binding/reserved-for-internal-use/feedapi", "/docapi-binding/reserved-for-internal-use/feedapi/"));
        expectedHandlerMappings.forEach((handler, bindings) -> validateHandler(handlers.get(handler), bindingPrefix, bindings));

        // All other handlers should be bound to default (http://*/...)
        handlers.entrySet().stream()
                .filter(e -> ! expectedHandlerMappings.containsKey(e.getKey()))
                .forEach(e -> assertTrue(e.getValue().serverBindings().stream().allMatch(s -> s.startsWith("http://*/"))));

    }

    private void validateHandler(JdiscBindingsConfig.Handlers handler, String bindingPrefix, List<String> expectedBindings) {
        assertNotNull(handler);
        assertEquals(expectedBindings.size(), handler.serverBindings().size());
        assertThat(handler.serverBindings(), containsInAnyOrder(expectedBindings.stream().map(s->bindingPrefix+s).toArray()));
    }

    private void createClusterWithJDiscHandler() {
        Element clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>",
                "  <handler id='discHandler'>",
                "    <binding>http://*/binding0</binding>",
                "    <binding>http://*/binding1</binding>",
                "  </handler>",
                "</container>");

        createModel(root, clusterElem);
    }

}