aboutsummaryrefslogtreecommitdiffstats
path: root/logserver/src/test/java/com/yahoo/logserver/handlers/archive/FilesArchivedTestCase.java
blob: babe4b1479d34d00ef63f101613f255f10f5674b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.logserver.handlers.archive;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

/**
 * @author Arne Juul
 */
public class FilesArchivedTestCase {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    File tmpDir;

    private void makeLogfile(String name, long hours) throws IOException {
        File f = new File(tmpDir, name);
        f.getParentFile().mkdirs();
        new FileWriter(f).write("foo bar baz\n");
        long now = System.currentTimeMillis();
        f.setLastModified(now - (hours * 3600 * 1000));
    }

    void checkExist(String name) {
        assertTrue(new File(tmpDir, name).isFile());
    }
    void checkNoExist(String name) {
        assertFalse(new File(tmpDir, name).isFile());
    }

    @Test
    public void testMaintenance() throws java.io.IOException {
        tmpDir = temporaryFolder.newFolder();

        makeLogfile("foo/bar", 35*24); // non-matching file

        makeLogfile("2018/11/20/13-0", 35*24);
        makeLogfile("2018/11/21/13-0", 34*24);
        makeLogfile("2018/12/28/13-0", 3*24);
        makeLogfile("2018/12/29/13-0", 2*24);
        makeLogfile("2018/12/30/13-0", 1*24);
        makeLogfile("2018/12/31/14-0", 3);
        makeLogfile("2018/12/31/16-0", 1);
        makeLogfile("2018/12/31/17-0", 0);
        dumpFiles("before archive maintenance");
        FilesArchived a = new FilesArchived(tmpDir, "zstd");
        dumpFiles("also before archive maintenance");

        checkExist("foo/bar");
        checkExist("2018/11/20/13-0");
        checkExist("2018/11/21/13-0");
        checkExist("2018/12/28/13-0");
        checkExist("2018/12/29/13-0");
        checkExist("2018/12/30/13-0");
        checkExist("2018/12/31/14-0");
        checkExist("2018/12/31/16-0");
        checkExist("2018/12/31/17-0");
        checkNoExist("2018/11/20/13-0.zst");
        checkNoExist("2018/11/21/13-0.zst");
        checkNoExist("2018/12/28/13-0.zst");
        checkNoExist("2018/12/29/13-0.zst");
        checkNoExist("2018/12/30/13-0.zst");
        checkNoExist("2018/12/31/14-0.zst");
        checkNoExist("2018/12/31/16-0.zst");
        checkNoExist("2018/12/31/17-0.zst");

        a.maintenance();

        dumpFiles("after archive maintenance");
        checkExist("foo/bar");
        checkExist("2018/12/31/17-0");
        checkExist("2018/12/31/16-0");
        checkExist("2018/12/31/14-0.zst");
        checkExist("2018/12/28/13-0.zst");
        checkExist("2018/12/29/13-0.zst");
        checkExist("2018/12/30/13-0.zst");

        checkNoExist("2018/12/31/17-0.zst");
        checkNoExist("2018/12/31/16-0.zst");
        checkNoExist("2018/12/31/14-0");
        checkNoExist("2018/12/28/13-0");
        checkNoExist("2018/12/29/13-0");
        checkNoExist("2018/12/30/13-0");

        checkNoExist("2018/11/20/13-0");
        checkNoExist("2018/11/20/13-0.zst");
        checkNoExist("2018/11/21/13-0");
        checkNoExist("2018/11/21/13-0.zst");

        makeLogfile("2018/12/31/16-0", 3);
        makeLogfile("2018/12/31/17-0", 3);
        makeLogfile("2018/12/31/17-1", 1);
        makeLogfile("2018/12/31/17-2", 0);

        dumpFiles("before second archive maintenance");
        a.maintenance();
        dumpFiles("after second archive maintenance");

        checkExist("2018/12/31/17-2");
        checkExist("2018/12/31/17-1");
        checkExist("2018/12/31/16-0.zst");
        checkExist("2018/12/31/17-0.zst");

        checkNoExist("2018/12/31/16-0");
        checkNoExist("2018/12/31/17-0");
        checkExist("foo/bar");
    }

    private void dumpFiles(String header) {
        System.out.println(">>> " + header + " >>> :");
        List<String> seen = scanDir(tmpDir);
        seen.sort(null);
        for (String s : seen) {
            System.err.println("   " + s);
        }
        System.out.println("<<< " + header + " <<<");
    }

    private static List<String> scanDir(File top) {
        List<String> retval = new ArrayList<>();
        String[] names = top.list();
        if (names != null) {
            for (String name : names) {
                File sub = new File(top, name);
                if (sub.isFile()) {
                    retval.add(sub.toString());
                } else if (sub.isDirectory()) {
                    for (String subFile : scanDir(sub)) {
                        retval.add(subFile);
                    }
                }
            }
        }
        return retval;
    }

}