summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/xml/AccessLogTest.java
blob: 56f09eefe824275b5065155023c85dd8007d20e4 (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
// Copyright 2017 Yahoo Holdings. 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.component.ComponentId;
import com.yahoo.config.model.builder.xml.test.DomBuilderTest;
import com.yahoo.container.core.AccessLogConfig;
import com.yahoo.container.logging.ConnectionLogConfig;
import com.yahoo.container.logging.FileConnectionLog;
import com.yahoo.container.logging.JSONAccessLog;
import com.yahoo.container.logging.VespaAccessLog;
import com.yahoo.jdisc.http.server.jetty.VoidConnectionLog;
import com.yahoo.vespa.model.container.ApplicationContainerCluster;
import com.yahoo.vespa.model.container.component.Component;
import org.junit.Test;
import org.w3c.dom.Element;

import static com.yahoo.text.StringUtilities.quote;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertEquals;

/**
 * @author gjoranv
 */
public class AccessLogTest extends ContainerModelBuilderTestBase {

    @Test
    public void default_access_log_is_added_by_default() {
        Element cluster1Elem = DomBuilderTest.parse(
                "<container id='cluster1' version='1.0'>",
                "  <nodes>",
                "    <node hostalias='mockhost' baseport='1234' />",
                "  </nodes>",
                "</container>" );

        createModel(root, cluster1Elem);

        assertNotNull(getJsonAccessLog("cluster1"));
        assertNull(getVespaAccessLog("cluster1"));
    }

    @Test
    public void default_search_access_log_can_be_disabled() {
        final String jdiscClusterId = "jdisc-cluster";

        Element clusterElem = DomBuilderTest.parse(
                "<container id=" + quote(jdiscClusterId) + " version='1.0'>" +
                        "  <search />" +
                        "  <accesslog type='disabled' />" +
                        "</container>" );

        createModel(root, clusterElem);
        assertNull(getVespaAccessLog(jdiscClusterId));
        assertNull(getJsonAccessLog(jdiscClusterId));
    }

    private Component<?, ?> getVespaAccessLog(String clusterName) {
        ApplicationContainerCluster cluster = (ApplicationContainerCluster) root.getChildren().get(clusterName);
        return cluster.getComponentsMap().get(ComponentId.fromString((VespaAccessLog.class.getName())));
    }
    private Component<?, ?> getJsonAccessLog(String clusterName) {
        ApplicationContainerCluster cluster = (ApplicationContainerCluster) root.getChildren().get(clusterName);
        return cluster.getComponentsMap().get(ComponentId.fromString((JSONAccessLog.class.getName())));
    }

    @Test
    public void access_log_can_be_configured() {
        Element clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>",
                "  <accesslog type='vespa' ",
                "             fileNamePattern='pattern' rotationInterval='interval' />",
                "  <accesslog type='json' ",
                "             fileNamePattern='pattern' rotationInterval='interval' />",
                nodesXml,
                "</container>" );

        createModel(root, clusterElem);
        assertNotNull(getJsonAccessLog("default"));
        assertNotNull(getVespaAccessLog("default"));

        { // vespa
            Component<?, ?> accessLogComponent = getContainerComponent("default", VespaAccessLog.class.getName());
            assertNotNull(accessLogComponent);
            assertEquals(VespaAccessLog.class.getName(), accessLogComponent.getClassId().getName(), VespaAccessLog.class.getName());
            AccessLogConfig config = root.getConfig(AccessLogConfig.class, "default/component/com.yahoo.container.logging.VespaAccessLog");
            AccessLogConfig.FileHandler fileHandlerConfig = config.fileHandler();
            assertEquals("pattern", fileHandlerConfig.pattern());
            assertEquals("interval", fileHandlerConfig.rotation());
            assertEquals(10000, fileHandlerConfig.queueSize());
        }

        { // json
            Component<?, ?> accessLogComponent = getContainerComponent("default", JSONAccessLog.class.getName());
            assertNotNull(accessLogComponent);
            assertEquals(JSONAccessLog.class.getName(), accessLogComponent.getClassId().getName(), JSONAccessLog.class.getName());
            AccessLogConfig config = root.getConfig(AccessLogConfig.class, "default/component/com.yahoo.container.logging.JSONAccessLog");
            AccessLogConfig.FileHandler fileHandlerConfig = config.fileHandler();
            assertEquals("pattern", fileHandlerConfig.pattern());
            assertEquals("interval", fileHandlerConfig.rotation());
            assertEquals(10000, fileHandlerConfig.queueSize());
        }
    }

    @Test
    public void connection_log_configured_when_access_log_not_disabled() {
        Element clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>",
                "  <accesslog type='vespa' ",
                "             fileNamePattern='pattern' rotationInterval='interval' />",
                "  <accesslog type='json' ",
                "             fileNamePattern='pattern' rotationInterval='interval' />",
                nodesXml,
                "</container>" );
        createModel(root, clusterElem);
        Component<?, ?> connectionLogComponent = getContainerComponent("default", FileConnectionLog.class.getName());
        assertNotNull(connectionLogComponent);
        ConnectionLogConfig config = root.getConfig(ConnectionLogConfig.class, "default/component/com.yahoo.container.logging.FileConnectionLog");
        assertEquals("default", config.cluster());
        assertEquals(10000, config.queueSize());
    }

    @Test
    public void connection_log_disabled_when_access_log_disabled() {
        Element clusterElem = DomBuilderTest.parse(
                "<container id='default' version='1.0'>",
                "  <accesslog type='disabled' />",
                nodesXml,
                "</container>" );
        createModel(root, clusterElem);
        Component<?, ?> voidConnectionLogComponent = getContainerComponent("default", VoidConnectionLog.class.getName());
        assertNotNull(voidConnectionLogComponent);
        Component<?, ?> fileConnectionLogComponent = getContainerComponent("default", FileConnectionLog.class.getName());
        assertNull(fileConnectionLogComponent);
    }
}