summaryrefslogtreecommitdiffstats
path: root/node-maintainer/src/test/java/com/yahoo/vespa/hosted/node/maintainer/CoreCollectorTest.java
blob: b32f6d076a7599b68bb3128bdf4c35bd2c5a83ce (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.maintainer;

import com.yahoo.collections.Pair;
import static com.yahoo.vespa.defaults.Defaults.getDefaults;
import com.yahoo.system.ProcessExecuter;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author freva
 */
public class CoreCollectorTest {
    private final ProcessExecuter processExecuter = mock(ProcessExecuter.class);
    private final CoreCollector coreCollector = new CoreCollector(processExecuter);

    private final Path INSTALL_STATE_PATH = Paths.get("/path/to/install.state");
    private final Path TEST_CORE_PATH = Paths.get("/tmp/core.1234");
    private final Path TEST_BIN_PATH = Paths.get("/usr/bin/program");
    private final List<String> GDB_BACKTRACE = Arrays.asList("[New Thread 2703]",
            "Core was generated by `/usr/bin/program\'.", "Program terminated with signal 11, Segmentation fault.",
            "#0  0x00000000004004d8 in main (argv=0x1) at main.c:4", "4\t    printf(argv[3]);",
            "#0  0x00000000004004d8 in main (argv=0x1) at main.c:4");

    private final List<String> INSTALL_STATE = Arrays.asList("package: some_package-0.0.2",
            "variable 'value'",
            "ca_file /path/to/ca.pem");

    private final List<String> RPM_PACKAGES = Arrays.asList("some_package-0.0.2",
            "another_package-1.0.6",
            "last_pkg-3.10_102");

    @Rule
    public TemporaryFolder folder= new TemporaryFolder();

    private void mockExec(String[] cmd, String output) throws IOException {
        mockExec(cmd, output, "");
    }

    private void mockExec(String[] cmd, String output, String error) throws IOException {
        when(processExecuter.exec(cmd)).thenReturn(new Pair<>(error.isEmpty() ? 0 : 1, output + error));
    }

    static final String GDB_PATH = getDefaults().underVespaHome("bin64/gdb");

    @Test
    public void extractsBinaryPathTest() throws IOException {
        final String[] cmd = {"file", TEST_CORE_PATH.toString()};

        mockExec(cmd,
                "/tmp/core.1234: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from " +
                        "'/usr/bin/program'");
        assertEquals(TEST_BIN_PATH, coreCollector.readBinPath(TEST_CORE_PATH));

        mockExec(cmd,
                "/tmp/core.1234: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from " +
                        "'/usr/bin/program --foo --bar baz'");
        assertEquals(TEST_BIN_PATH, coreCollector.readBinPath(TEST_CORE_PATH));

        mockExec(cmd,
                "/tmp/core.1234: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from " +
                        "'/usr/bin//program'");
        assertEquals(TEST_BIN_PATH, coreCollector.readBinPath(TEST_CORE_PATH));

        mockExec(cmd,
                "/tmp/core.1234: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, " +
                        "from 'program', real uid: 0, effective uid: 0, real gid: 0, effective gid: 0, " +
                        "execfn: '/usr/bin/program', platform: 'x86_64");
        assertEquals(TEST_BIN_PATH, coreCollector.readBinPath(TEST_CORE_PATH));


        Path fallbackResponse = Paths.get("/response/from/fallback");
        mockExec(new String[]{"/bin/sh", "-c", GDB_PATH + " -n -batch -core /tmp/core.1234 | grep '^Core was generated by'"},
                "Core was generated by `/response/from/fallback'.");
        mockExec(cmd,
                "/tmp/core.1234: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style");
        assertEquals(fallbackResponse, coreCollector.readBinPath(TEST_CORE_PATH));

        mockExec(cmd, "", "Error code 1234");
        assertEquals(fallbackResponse, coreCollector.readBinPath(TEST_CORE_PATH));
    }

    @Test
    public void extractsBinaryPathUsingGdbTest() throws IOException {
        final String[] cmd = new String[]{"/bin/sh", "-c",
                GDB_PATH + " -n -batch -core /tmp/core.1234 | grep '^Core was generated by'"};

        mockExec(cmd, "Core was generated by `/usr/bin/program-from-gdb --identity foo/search/cluster.content_'.");
        assertEquals(Paths.get("/usr/bin/program-from-gdb"), coreCollector.readBinPathFallback(TEST_CORE_PATH));

        mockExec(cmd, "", "Error 123");
        try {
            coreCollector.readBinPathFallback(TEST_CORE_PATH);
            fail("Expected not to be able to get bin path");
        } catch (RuntimeException e) {
            assertEquals("Failed to extract binary path from GDB, result: (1,Error 123), command: " +
                    "[/bin/sh, -c, /opt/vespa/bin64/gdb -n -batch -core /tmp/core.1234 | grep '^Core was generated by']", e.getMessage());
        }
    }

    @Test
    public void extractsBacktraceUsingGdb() throws IOException {
        mockExec(new String[]{GDB_PATH, "-n", "-ex", "bt", "-batch", "/usr/bin/program", "/tmp/core.1234"},
                String.join("\n", GDB_BACKTRACE));
        assertEquals(GDB_BACKTRACE, coreCollector.readBacktrace(TEST_CORE_PATH, TEST_BIN_PATH, false));

        mockExec(new String[]{GDB_PATH, "-n", "-ex", "bt", "-batch", "/usr/bin/program", "/tmp/core.1234"},
                "", "Failure");
        try {
            coreCollector.readBacktrace(TEST_CORE_PATH, TEST_BIN_PATH, false);
            fail("Expected not to be able to read backtrace");
        } catch (RuntimeException e) {
            assertEquals("Failed to read backtrace (1,Failure), Command: " +
                    "[/opt/vespa/bin64/gdb, -n, -ex, bt, -batch, /usr/bin/program, /tmp/core.1234]", e.getMessage());
        }
    }

    @Test
    public void extractsBacktraceFromAllThreadsUsingGdb() throws IOException {
        mockExec(new String[]{GDB_PATH, "-n", "-ex", "thread apply all bt", "-batch",
                        "/usr/bin/program", "/tmp/core.1234"},
                String.join("\n", GDB_BACKTRACE));
        assertEquals(GDB_BACKTRACE, coreCollector.readBacktrace(TEST_CORE_PATH, TEST_BIN_PATH, true));
    }

    @Test
    public void collectsDataTest() throws IOException {
        mockExec(new String[]{"file", TEST_CORE_PATH.toString()},
                "/tmp/core.1234: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from " +
                        "'/usr/bin/program'");
        mockExec(new String[]{GDB_PATH, "-n", "-ex", "bt", "-batch", "/usr/bin/program", "/tmp/core.1234"},
                String.join("\n", GDB_BACKTRACE));
        mockExec(new String[]{GDB_PATH, "-n", "-ex", "thread apply all bt", "-batch",
                        "/usr/bin/program", "/tmp/core.1234"},
                String.join("\n", GDB_BACKTRACE));
        mockExec(new String[]{"cat", INSTALL_STATE_PATH.toString()}, String.join("\n", INSTALL_STATE));
        mockExec(new String[]{"rpm", "-qa"}, String.join("\n", RPM_PACKAGES));

        Map<String, Object> expectedData = new HashMap<>();
        expectedData.put("bin_path", TEST_BIN_PATH.toString());
        expectedData.put("backtrace", new ArrayList<>(GDB_BACKTRACE));
        expectedData.put("backtrace_all_threads", new ArrayList<>(GDB_BACKTRACE));
        expectedData.put("install_state", new ArrayList<>(INSTALL_STATE));
        expectedData.put("rpm_packages", new ArrayList<>(RPM_PACKAGES));
        assertEquals(expectedData, coreCollector.collect(TEST_CORE_PATH, Optional.of(INSTALL_STATE_PATH)));
    }

    @Test
    public void collectsPartialIfUnableToDetermineDumpingProgramTest() throws IOException {
        // We fail to get backtrace and RPM packages, but install state works, make sure it is returned
        mockExec(new String[]{"cat", INSTALL_STATE_PATH.toString()}, String.join("\n", INSTALL_STATE));

        Map<String, Object> expectedData = new HashMap<>();
        expectedData.put("install_state", new ArrayList<>(INSTALL_STATE));
        assertEquals(expectedData, coreCollector.collect(TEST_CORE_PATH, Optional.of(INSTALL_STATE_PATH)));
    }

    @Test
    public void collectsPartialIfBacktraceFailsTest() throws IOException {
        mockExec(new String[]{"file", TEST_CORE_PATH.toString()},
                "/tmp/core.1234: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from " +
                        "'/usr/bin/program'");
        mockExec(new String[]{GDB_PATH + " -n -ex bt -batch /usr/bin/program /tmp/core.1234"},
                "", "Failure");

        Map<String, Object> expectedData = new HashMap<>();
        expectedData.put("bin_path", TEST_BIN_PATH.toString());
        assertEquals(expectedData, coreCollector.collect(TEST_CORE_PATH, Optional.empty()));
    }

    @Test
    public void parseTotalMemoryTestTest() {
        String memInfo = "MemTotal:   100000000 kB\nMemUsed:   1000000 kB\n";
        assertEquals(100000000, coreCollector.parseTotalMemorySize(memInfo));

        String badMemInfo = "This string has no memTotal value";
        try {
            coreCollector.parseTotalMemorySize(badMemInfo);
            fail("Expected to fail on parsing");
        } catch (RuntimeException e) {
            assertEquals("Could not parse meminfo: " + badMemInfo, e.getMessage());
        }
    }

    @Test
    public void testDeleteUncompressedFiles() throws IOException {
        final String documentId = "UIDD-ABCD-EFGH";
        final String coreDumpFilename = "core.dump";

        Path coredumpPath = folder.newFolder("crash").toPath()
                .resolve(CoredumpHandler.PROCESSING_DIRECTORY_NAME)
                .resolve(documentId);
        coredumpPath.toFile().mkdirs();
        coredumpPath.resolve(coreDumpFilename).toFile().createNewFile();

        Set<Path> expectedContentsOfCoredump = new HashSet<>(Arrays.asList(
                coredumpPath.resolve(CoredumpHandler.METADATA_FILE_NAME),
                coredumpPath.resolve(coreDumpFilename + ".lz4")));
        expectedContentsOfCoredump.forEach(path -> {
            try {
                path.toFile().createNewFile();
            } catch (IOException ignored) { ignored.printStackTrace();}
        });
        coreCollector.deleteDecompressedCoredump(coredumpPath.resolve(coreDumpFilename));

        assertEquals(expectedContentsOfCoredump, Files.list(coredumpPath).collect(Collectors.toSet()));
    }

    @Test
    public void testDeleteUncompressedFilesWithoutLz4() throws IOException {
        final String documentId = "UIDD-ABCD-EFGH";
        final String coreDumpFilename = "core.dump";

        Path coredumpPath = folder.newFolder("crash").toPath()
                .resolve(CoredumpHandler.PROCESSING_DIRECTORY_NAME)
                .resolve(documentId);
        coredumpPath.toFile().mkdirs();

        Set<Path> expectedContentsOfCoredump = new HashSet<>(Arrays.asList(
                coredumpPath.resolve(CoredumpHandler.METADATA_FILE_NAME),
                coredumpPath.resolve(coreDumpFilename)));
        expectedContentsOfCoredump.forEach(path -> {
            try {
                path.toFile().createNewFile();
            } catch (IOException ignored) { ignored.printStackTrace();}
        });
        coreCollector.deleteDecompressedCoredump(coredumpPath.resolve(coreDumpFilename));

        assertEquals(expectedContentsOfCoredump, Files.list(coredumpPath).collect(Collectors.toSet()));
    }
}