Cosmos Nicolaou | f2c891f | 2014-12-08 23:04:07 -0800 | [diff] [blame] | 1 | // +build -darwin |
| 2 | |
Robin Thellend | 4523df6 | 2014-12-08 09:55:46 -0800 | [diff] [blame] | 3 | package main |
| 4 | |
Cosmos Nicolaou | f2c891f | 2014-12-08 23:04:07 -0800 | [diff] [blame] | 5 | // These tests need go 1.4 to run successfully on a mac. |
| 6 | |
Robin Thellend | 4523df6 | 2014-12-08 09:55:46 -0800 | [diff] [blame] | 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "io/ioutil" |
| 11 | "os" |
| 12 | "os/user" |
| 13 | "path/filepath" |
| 14 | "sort" |
| 15 | "strings" |
| 16 | "testing" |
| 17 | "time" |
| 18 | ) |
| 19 | |
| 20 | func setup(t *testing.T, workdir, username string) (tmpdir string) { |
| 21 | var err error |
Robin Thellend | cf140c0 | 2014-12-08 14:56:24 -0800 | [diff] [blame] | 22 | tmpdir, err = ioutil.TempDir(workdir, "gclogs-test-setup-") |
Robin Thellend | 4523df6 | 2014-12-08 09:55:46 -0800 | [diff] [blame] | 23 | if err != nil { |
| 24 | t.Fatalf("ioutil.TempDir failed: %v", err) |
| 25 | } |
| 26 | logfiles := []struct { |
| 27 | name string |
| 28 | link string |
| 29 | age time.Duration |
| 30 | }{ |
| 31 | {"prog1.host.%USER%.log.veyron.INFO.20141204-131502.12345", "", 4 * time.Hour}, |
| 32 | {"prog1.host.%USER%.log.veyron.INFO.20141204-145040.23456", "prog1.INFO", 1 * time.Hour}, |
| 33 | {"prog1.host.%USER%.log.veyron.ERROR.20141204-145040.23456", "prog1.ERROR", 1 * time.Hour}, |
| 34 | {"prog2.host.%USER%.log.veyron.INFO.20141204-135040.23456", "prog2.INFO", 4 * time.Hour}, |
| 35 | {"prog3.host.otheruser.log.veyron.INFO.20141204-135040.23456", "prog3.INFO", 1 * time.Hour}, |
| 36 | {"foo.txt", "", 1 * time.Hour}, |
| 37 | {"bar.txt", "", 1 * time.Hour}, |
| 38 | } |
| 39 | for _, l := range logfiles { |
| 40 | l.name = strings.Replace(l.name, "%USER%", username, -1) |
| 41 | filename := filepath.Join(tmpdir, l.name) |
| 42 | if err := ioutil.WriteFile(filename, []byte{}, 0644); err != nil { |
| 43 | t.Fatalf("ioutil.WriteFile failed: %v", err) |
| 44 | } |
| 45 | mtime := time.Now().Add(-l.age) |
| 46 | if err := os.Chtimes(filename, mtime, mtime); err != nil { |
| 47 | t.Fatalf("os.Chtimes failed: %v", err) |
| 48 | } |
| 49 | if l.link != "" { |
| 50 | if err := os.Symlink(l.name, filepath.Join(tmpdir, l.link)); err != nil { |
| 51 | t.Fatalf("os.Symlink failed: %v", err) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | if err := os.Mkdir(filepath.Join(tmpdir, "subdir"), 0700); err != nil { |
| 56 | t.Fatalf("os.Mkdir failed: %v", err) |
| 57 | } |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | func TestGCLogs(t *testing.T) { |
| 62 | workdir, err := ioutil.TempDir("", "parse-file-info-") |
| 63 | if err != nil { |
| 64 | t.Fatalf("ioutil.TempDir failed: %v", err) |
| 65 | } |
| 66 | defer os.RemoveAll(workdir) |
| 67 | |
| 68 | u, err := user.Current() |
| 69 | if err != nil { |
| 70 | t.Fatalf("user.Current failed: %v", err) |
| 71 | } |
| 72 | |
| 73 | cmd := cmdGCLogs |
| 74 | var stdout, stderr bytes.Buffer |
| 75 | cmd.Init(nil, &stdout, &stderr) |
| 76 | |
| 77 | testcases := []struct { |
| 78 | cutoff time.Duration |
| 79 | verbose bool |
| 80 | dryrun bool |
| 81 | expected []string |
| 82 | }{ |
| 83 | { |
| 84 | cutoff: 6 * time.Hour, |
| 85 | verbose: false, |
| 86 | dryrun: false, |
| 87 | expected: []string{ |
| 88 | `Processing: "%TESTDIR%"`, |
| 89 | `Number of files deleted: 0`, |
| 90 | }, |
| 91 | }, |
| 92 | { |
| 93 | cutoff: 2 * time.Hour, |
| 94 | verbose: false, |
| 95 | dryrun: true, |
| 96 | expected: []string{ |
| 97 | `Processing: "%TESTDIR%"`, |
| 98 | `Would delete "%TESTDIR%/prog1.host.%USER%.log.veyron.INFO.20141204-131502.12345"`, |
| 99 | `Would delete "%TESTDIR%/prog2.host.%USER%.log.veyron.INFO.20141204-135040.23456"`, |
| 100 | `Number of files deleted: 0`, |
| 101 | }, |
| 102 | }, |
| 103 | { |
| 104 | cutoff: 2 * time.Hour, |
| 105 | verbose: false, |
| 106 | dryrun: false, |
| 107 | expected: []string{ |
| 108 | `Processing: "%TESTDIR%"`, |
| 109 | `Number of files deleted: 3`, |
| 110 | }, |
| 111 | }, |
| 112 | { |
| 113 | cutoff: 2 * time.Hour, |
| 114 | verbose: true, |
| 115 | dryrun: false, |
| 116 | expected: []string{ |
| 117 | `Processing: "%TESTDIR%"`, |
| 118 | `Deleting "%TESTDIR%/prog1.host.%USER%.log.veyron.INFO.20141204-131502.12345"`, |
| 119 | `Deleting "%TESTDIR%/prog2.host.%USER%.log.veyron.INFO.20141204-135040.23456"`, |
| 120 | `Deleting symlink "%TESTDIR%/prog2.INFO"`, |
| 121 | `Not a log file: "%TESTDIR%/bar.txt"`, |
| 122 | `Not a log file: "%TESTDIR%/foo.txt"`, |
| 123 | `Skipped directory: "%TESTDIR%/subdir"`, |
| 124 | `Skipped log file created by other user: "%TESTDIR%/prog3.INFO"`, |
| 125 | `Skipped log file created by other user: "%TESTDIR%/prog3.host.otheruser.log.veyron.INFO.20141204-135040.23456"`, |
| 126 | `Number of files deleted: 3`, |
| 127 | }, |
| 128 | }, |
| 129 | { |
| 130 | cutoff: time.Minute, |
| 131 | verbose: true, |
| 132 | dryrun: false, |
| 133 | expected: []string{ |
| 134 | `Processing: "%TESTDIR%"`, |
| 135 | `Deleting "%TESTDIR%/prog1.host.%USER%.log.veyron.ERROR.20141204-145040.23456"`, |
| 136 | `Deleting "%TESTDIR%/prog1.host.%USER%.log.veyron.INFO.20141204-131502.12345"`, |
| 137 | `Deleting "%TESTDIR%/prog1.host.%USER%.log.veyron.INFO.20141204-145040.23456"`, |
| 138 | `Deleting "%TESTDIR%/prog2.host.%USER%.log.veyron.INFO.20141204-135040.23456"`, |
| 139 | `Deleting symlink "%TESTDIR%/prog1.ERROR"`, |
| 140 | `Deleting symlink "%TESTDIR%/prog1.INFO"`, |
| 141 | `Deleting symlink "%TESTDIR%/prog2.INFO"`, |
| 142 | `Not a log file: "%TESTDIR%/bar.txt"`, |
| 143 | `Not a log file: "%TESTDIR%/foo.txt"`, |
| 144 | `Skipped directory: "%TESTDIR%/subdir"`, |
| 145 | `Skipped log file created by other user: "%TESTDIR%/prog3.INFO"`, |
| 146 | `Skipped log file created by other user: "%TESTDIR%/prog3.host.otheruser.log.veyron.INFO.20141204-135040.23456"`, |
| 147 | `Number of files deleted: 7`, |
| 148 | }, |
| 149 | }, |
| 150 | { |
| 151 | cutoff: time.Minute, |
| 152 | verbose: false, |
| 153 | dryrun: false, |
| 154 | expected: []string{ |
| 155 | `Processing: "%TESTDIR%"`, |
| 156 | `Number of files deleted: 7`, |
| 157 | }, |
| 158 | }, |
| 159 | } |
| 160 | for _, tc := range testcases { |
| 161 | testdir := setup(t, workdir, u.Username) |
| 162 | cutoff := fmt.Sprintf("--cutoff=%s", tc.cutoff) |
| 163 | verbose := fmt.Sprintf("--verbose=%v", tc.verbose) |
| 164 | dryrun := fmt.Sprintf("--n=%v", tc.dryrun) |
| 165 | if err := cmd.Execute([]string{cutoff, verbose, dryrun, testdir}); err != nil { |
| 166 | t.Fatalf("%v: %v", stderr.String(), err) |
| 167 | } |
| 168 | gotsl := strings.Split(stdout.String(), "\n") |
| 169 | if len(gotsl) >= 2 { |
| 170 | sort.Strings(gotsl[1 : len(gotsl)-2]) |
| 171 | } |
| 172 | got := strings.Join(gotsl, "\n") |
| 173 | expected := strings.Join(tc.expected, "\n") + "\n" |
| 174 | expected = strings.Replace(expected, "%TESTDIR%", testdir, -1) |
| 175 | expected = strings.Replace(expected, "%USER%", u.Username, -1) |
| 176 | if got != expected { |
| 177 | t.Errorf("Unexpected result for (%v, %v): got %q, expected %q", tc.cutoff, tc.verbose, got, expected) |
| 178 | } |
| 179 | stdout.Reset() |
| 180 | } |
| 181 | } |