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

package com.yahoo.logserver.handlers.archive;

import com.yahoo.io.IOUtils;

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();

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

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

    @Test
    public void testMaintenance() throws java.io.IOException {
        File tmpDir = temporaryFolder.newFolder();
        try {
            makeLogfile(tmpDir, "foo/bar", 35*24); // non-matching file

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

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

            checkNoExist(tmpDir, "2018/11/20/13-0");
            checkNoExist(tmpDir, "2018/11/20/13-0.gz");
            checkNoExist(tmpDir, "2018/11/21/13-0");
            checkNoExist(tmpDir, "2018/11/21/13-0.gz");

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

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

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

            checkNoExist(tmpDir, "2018/12/31/16-0");
            checkNoExist(tmpDir, "2018/12/31/17-0");
            checkExist(tmpDir, "foo/bar");
        } finally {
            IOUtils.recursiveDeleteDir(tmpDir);
        }
    }

    private void dumpFiles(File dir, String header) {
        System.out.println(">>> " + header + " >>> :");
        List<String> seen = scanDir(dir);
        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;
    }

}