github.com/ph/moby@v1.13.1/pkg/tailfile/tailfile_test.go (about) 1 package tailfile 2 3 import ( 4 "io/ioutil" 5 "os" 6 "testing" 7 ) 8 9 func TestTailFile(t *testing.T) { 10 f, err := ioutil.TempFile("", "tail-test") 11 if err != nil { 12 t.Fatal(err) 13 } 14 defer f.Close() 15 defer os.RemoveAll(f.Name()) 16 testFile := []byte(`first line 17 second line 18 third line 19 fourth line 20 fifth line 21 next first line 22 next second line 23 next third line 24 next fourth line 25 next fifth line 26 last first line 27 next first line 28 next second line 29 next third line 30 next fourth line 31 next fifth line 32 next first line 33 next second line 34 next third line 35 next fourth line 36 next fifth line 37 last second line 38 last third line 39 last fourth line 40 last fifth line 41 truncated line`) 42 if _, err := f.Write(testFile); err != nil { 43 t.Fatal(err) 44 } 45 if _, err := f.Seek(0, os.SEEK_SET); err != nil { 46 t.Fatal(err) 47 } 48 expected := []string{"last fourth line", "last fifth line"} 49 res, err := TailFile(f, 2) 50 if err != nil { 51 t.Fatal(err) 52 } 53 for i, l := range res { 54 t.Logf("%s", l) 55 if expected[i] != string(l) { 56 t.Fatalf("Expected line %s, got %s", expected[i], l) 57 } 58 } 59 } 60 61 func TestTailFileManyLines(t *testing.T) { 62 f, err := ioutil.TempFile("", "tail-test") 63 if err != nil { 64 t.Fatal(err) 65 } 66 defer f.Close() 67 defer os.RemoveAll(f.Name()) 68 testFile := []byte(`first line 69 second line 70 truncated line`) 71 if _, err := f.Write(testFile); err != nil { 72 t.Fatal(err) 73 } 74 if _, err := f.Seek(0, os.SEEK_SET); err != nil { 75 t.Fatal(err) 76 } 77 expected := []string{"first line", "second line"} 78 res, err := TailFile(f, 10000) 79 if err != nil { 80 t.Fatal(err) 81 } 82 for i, l := range res { 83 t.Logf("%s", l) 84 if expected[i] != string(l) { 85 t.Fatalf("Expected line %s, got %s", expected[i], l) 86 } 87 } 88 } 89 90 func TestTailEmptyFile(t *testing.T) { 91 f, err := ioutil.TempFile("", "tail-test") 92 if err != nil { 93 t.Fatal(err) 94 } 95 defer f.Close() 96 defer os.RemoveAll(f.Name()) 97 res, err := TailFile(f, 10000) 98 if err != nil { 99 t.Fatal(err) 100 } 101 if len(res) != 0 { 102 t.Fatal("Must be empty slice from empty file") 103 } 104 } 105 106 func TestTailNegativeN(t *testing.T) { 107 f, err := ioutil.TempFile("", "tail-test") 108 if err != nil { 109 t.Fatal(err) 110 } 111 defer f.Close() 112 defer os.RemoveAll(f.Name()) 113 testFile := []byte(`first line 114 second line 115 truncated line`) 116 if _, err := f.Write(testFile); err != nil { 117 t.Fatal(err) 118 } 119 if _, err := f.Seek(0, os.SEEK_SET); err != nil { 120 t.Fatal(err) 121 } 122 if _, err := TailFile(f, -1); err != ErrNonPositiveLinesNumber { 123 t.Fatalf("Expected ErrNonPositiveLinesNumber, got %s", err) 124 } 125 if _, err := TailFile(f, 0); err != ErrNonPositiveLinesNumber { 126 t.Fatalf("Expected ErrNonPositiveLinesNumber, got %s", err) 127 } 128 } 129 130 func BenchmarkTail(b *testing.B) { 131 f, err := ioutil.TempFile("", "tail-test") 132 if err != nil { 133 b.Fatal(err) 134 } 135 defer f.Close() 136 defer os.RemoveAll(f.Name()) 137 for i := 0; i < 10000; i++ { 138 if _, err := f.Write([]byte("tailfile pretty interesting line\n")); err != nil { 139 b.Fatal(err) 140 } 141 } 142 b.ResetTimer() 143 for i := 0; i < b.N; i++ { 144 if _, err := TailFile(f, 1000); err != nil { 145 b.Fatal(err) 146 } 147 } 148 }