Dan Willemsen | a14704c | 2017-10-28 22:57:22 -0700 | [diff] [blame^] | 1 | // Copyright 2018 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package paths |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "io/ioutil" |
| 20 | "net" |
| 21 | "os" |
| 22 | "path/filepath" |
| 23 | "reflect" |
| 24 | "strings" |
| 25 | "testing" |
| 26 | "time" |
| 27 | ) |
| 28 | |
| 29 | func TestSendLog(t *testing.T) { |
| 30 | d, err := ioutil.TempDir("", "log_socket") |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | defer os.RemoveAll(d) |
| 35 | f := filepath.Join(d, "sock") |
| 36 | |
| 37 | ctx, _ := context.WithTimeout(context.Background(), 2*timeoutDuration) |
| 38 | |
| 39 | recv, err := LogListener(ctx, f) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | |
| 44 | go func() { |
| 45 | for i := 0; i < 10; i++ { |
| 46 | SendLog(f, &LogEntry{ |
| 47 | Basename: "test", |
| 48 | Args: []string{"foo", "bar"}, |
| 49 | }, make(chan interface{})) |
| 50 | } |
| 51 | }() |
| 52 | |
| 53 | count := 0 |
| 54 | for { |
| 55 | select { |
| 56 | case entry := <-recv: |
| 57 | ref := LogEntry{ |
| 58 | Basename: "test", |
| 59 | Args: []string{"foo", "bar"}, |
| 60 | } |
| 61 | if !reflect.DeepEqual(ref, *entry) { |
| 62 | t.Fatalf("Bad log entry: %v", entry) |
| 63 | } |
| 64 | count++ |
| 65 | |
| 66 | if count == 10 { |
| 67 | return |
| 68 | } |
| 69 | case <-ctx.Done(): |
| 70 | t.Error("Hit timeout before receiving all logs") |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestSendLogError(t *testing.T) { |
| 76 | d, err := ioutil.TempDir("", "log_socket") |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | defer os.RemoveAll(d) |
| 81 | |
| 82 | t.Run("Missing file", func(t *testing.T) { |
| 83 | start := time.Now() |
| 84 | SendLog(filepath.Join(d, "missing"), &LogEntry{}, make(chan interface{})) |
| 85 | elapsed := time.Since(start) |
| 86 | if elapsed > timeoutDuration { |
| 87 | t.Errorf("Should have been << timeout (%s), but was %s", timeoutDuration, elapsed) |
| 88 | } |
| 89 | }) |
| 90 | |
| 91 | t.Run("Regular file", func(t *testing.T) { |
| 92 | f := filepath.Join(d, "file") |
| 93 | if fp, err := os.Create(f); err == nil { |
| 94 | fp.Close() |
| 95 | } else { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | |
| 99 | start := time.Now() |
| 100 | SendLog(f, &LogEntry{}, make(chan interface{})) |
| 101 | elapsed := time.Since(start) |
| 102 | if elapsed > timeoutDuration { |
| 103 | t.Errorf("Should have been << timeout (%s), but was %s", timeoutDuration, elapsed) |
| 104 | } |
| 105 | }) |
| 106 | |
| 107 | t.Run("Reader not reading", func(t *testing.T) { |
| 108 | f := filepath.Join(d, "sock1") |
| 109 | |
| 110 | ln, err := net.Listen("unix", f) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | defer ln.Close() |
| 115 | |
| 116 | done := make(chan bool, 1) |
| 117 | go func() { |
| 118 | for i := 0; i < 1000; i++ { |
| 119 | SendLog(f, &LogEntry{ |
| 120 | // Ensure a relatively large payload |
| 121 | Basename: strings.Repeat(" ", 100000), |
| 122 | }, make(chan interface{})) |
| 123 | } |
| 124 | done <- true |
| 125 | }() |
| 126 | |
| 127 | select { |
| 128 | case <-done: |
| 129 | break |
| 130 | case <-time.After(10 * timeoutDuration): |
| 131 | t.Error("Should have finished") |
| 132 | } |
| 133 | }) |
| 134 | } |