Robin Thellend | 4523df6 | 2014-12-08 09:55:46 -0800 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "io/ioutil" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func TestParseFileNameNoError(t *testing.T) { |
| 13 | testcases := []struct { |
| 14 | filename string |
| 15 | lf *logFile |
| 16 | }{ |
| 17 | { |
| 18 | "program.host.user.log.veyron.INFO.20141204-131502.12345", |
| 19 | &logFile{false, "program", "host", "user", "veyron", "INFO", time.Date(2014, 12, 4, 13, 15, 2, 0, time.Local), 12345}, |
| 20 | }, |
| 21 | { |
| 22 | "prog.test.host-name.user.log.veyron.ERROR.20141204-131502.12345", |
| 23 | &logFile{false, "prog.test", "host-name", "user", "veyron", "ERROR", time.Date(2014, 12, 4, 13, 15, 2, 0, time.Local), 12345}, |
| 24 | }, |
| 25 | } |
| 26 | for _, tc := range testcases { |
| 27 | lf, err := parseFileName(tc.filename) |
| 28 | if err != nil { |
| 29 | t.Errorf("unexpected error for %q: %v", tc.filename, err) |
| 30 | } |
| 31 | if !reflect.DeepEqual(tc.lf, lf) { |
| 32 | t.Errorf("unexpected result: got %+v, expected %+v", lf, tc.lf) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestParseFileNameError(t *testing.T) { |
| 38 | testcases := []string{ |
| 39 | "program.host.user.log.veyron.INFO.20141204-131502", |
| 40 | "prog.test.host-name.user.log.veyron.20141204-131502.12345", |
| 41 | "foo.txt", |
| 42 | } |
| 43 | for _, tc := range testcases { |
| 44 | if _, err := parseFileName(tc); err == nil { |
| 45 | t.Errorf("unexpected success for %q", tc) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestParseFileInfo(t *testing.T) { |
| 51 | tmpdir, err := ioutil.TempDir("", "parse-file-info-") |
| 52 | if err != nil { |
| 53 | t.Fatalf("ioutil.TempDir failed: %v", err) |
| 54 | } |
| 55 | defer os.RemoveAll(tmpdir) |
| 56 | |
| 57 | name := "program.host.user.log.veyron.INFO.20141204-131502.12345" |
| 58 | if err := ioutil.WriteFile(filepath.Join(tmpdir, name), []byte{}, 0644); err != nil { |
| 59 | t.Fatalf("ioutil.WriteFile failed: %v", err) |
| 60 | } |
| 61 | link := "program.INFO" |
| 62 | if err := os.Symlink(name, filepath.Join(tmpdir, link)); err != nil { |
| 63 | t.Fatalf("os.Symlink failed: %v", err) |
| 64 | } |
| 65 | |
| 66 | // Test regular file. |
| 67 | { |
| 68 | fi, err := os.Lstat(filepath.Join(tmpdir, name)) |
| 69 | if err != nil { |
| 70 | t.Fatalf("os.Lstat failed: %v", err) |
| 71 | } |
| 72 | lf, err := parseFileInfo(tmpdir, fi) |
| 73 | if err != nil { |
| 74 | t.Errorf("parseFileInfo(%v, %v) failed: %v", tmpdir, fi, err) |
| 75 | } |
| 76 | expected := &logFile{false, "program", "host", "user", "veyron", "INFO", time.Date(2014, 12, 4, 13, 15, 2, 0, time.Local), 12345} |
| 77 | if !reflect.DeepEqual(lf, expected) { |
| 78 | t.Errorf("unexpected result: got %+v, expected %+v", lf, expected) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Test symlink. |
| 83 | { |
| 84 | fi, err := os.Lstat(filepath.Join(tmpdir, link)) |
| 85 | if err != nil { |
| 86 | t.Fatalf("os.Lstat failed: %v", err) |
| 87 | } |
| 88 | lf, err := parseFileInfo(tmpdir, fi) |
| 89 | if err != nil { |
| 90 | t.Errorf("parseFileInfo(%v, %v) failed: %v", tmpdir, fi, err) |
| 91 | } |
| 92 | expected := &logFile{true, "program", "host", "user", "veyron", "INFO", time.Date(2014, 12, 4, 13, 15, 2, 0, time.Local), 12345} |
| 93 | if !reflect.DeepEqual(lf, expected) { |
| 94 | t.Errorf("unexpected result: got %+v, expected %+v", lf, expected) |
| 95 | } |
| 96 | } |
| 97 | } |