blob: 3b1005fb55cb8ce8dca4fadb72bf4b1923cbfd63 [file] [log] [blame]
Dan Willemsen18490112018-05-25 16:30:04 -07001// 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
15package paths
16
17import (
18 "context"
19 "io/ioutil"
20 "os"
21 "path/filepath"
22 "reflect"
23 "runtime"
24 "strings"
25 "testing"
26)
27
28func TestSendLog(t *testing.T) {
29 t.Run("Short name", func(t *testing.T) {
30 d, err := ioutil.TempDir("", "s")
31 if err != nil {
32 t.Fatal(err)
33 }
34 defer os.RemoveAll(d)
35 f := filepath.Join(d, "s")
36
37 testSendLog(t, f, getSocketAddr)
38 })
39
40 testLongName := func(t *testing.T, lookup socketAddrFunc) {
41 d, err := ioutil.TempDir("", strings.Repeat("s", 150))
42 if err != nil {
43 t.Fatal(err)
44 }
45 defer os.RemoveAll(d)
46 f := filepath.Join(d, strings.Repeat("s", 10))
47
48 testSendLog(t, f, lookup)
49 }
50
51 // Using a name longer than the ~100 limit of the underlying calls to bind, etc
52 t.Run("Long name", func(t *testing.T) {
53 testLongName(t, getSocketAddr)
54 })
55
56 if runtime.GOOS == "linux" {
57 t.Run("Long name proc fallback", func(t *testing.T) {
58 testLongName(t, procFallback)
59 })
60 }
61
62 t.Run("Long name tmp fallback", func(t *testing.T) {
63 testLongName(t, tmpFallback)
64 })
65}
66
67func testSendLog(t *testing.T, socket string, lookup socketAddrFunc) {
68 recv, err := logListener(context.Background(), socket, lookup)
69 if err != nil {
70 t.Fatal(err)
71 }
72
73 go func() {
74 for i := 0; i < 10; i++ {
75 sendLog(socket, lookup, 0, &LogEntry{
76 Basename: "test",
77 Args: []string{"foo", "bar"},
78 }, make(chan interface{}))
79 }
80 }()
81
82 count := 0
83 for {
84 entry := <-recv
85 if entry == nil {
86 if count != 10 {
87 t.Errorf("Expected 10 logs, got %d", count)
88 }
89 return
90 }
91
92 ref := LogEntry{
93 Basename: "test",
94 Args: []string{"foo", "bar"},
95 }
96 if !reflect.DeepEqual(ref, *entry) {
97 t.Fatalf("Bad log entry: %v", entry)
98 }
99 count++
100
101 if count == 10 {
102 return
103 }
104 }
105}
106
107func TestSendLogError(t *testing.T) {
108 d, err := ioutil.TempDir("", "log_socket")
109 if err != nil {
110 t.Fatal(err)
111 }
112 defer os.RemoveAll(d)
113
114 // Missing log sockets should not block waiting for the timeout to elapse
115 t.Run("Missing file", func(t *testing.T) {
116 sendLog(filepath.Join(d, "missing"), getSocketAddr, 0, &LogEntry{}, make(chan interface{}))
117 })
118
119 // Non-sockets should not block waiting for the timeout to elapse
120 t.Run("Regular file", func(t *testing.T) {
121 f := filepath.Join(d, "file")
122 if fp, err := os.Create(f); err == nil {
123 fp.Close()
124 } else {
125 t.Fatal(err)
126 }
127
128 sendLog(f, getSocketAddr, 0, &LogEntry{}, make(chan interface{}))
129 })
130
131 // If the reader is stuck, we should be able to make progress
132 t.Run("Reader not reading", func(t *testing.T) {
133 f := filepath.Join(d, "sock1")
134
135 ln, err := listen(f, getSocketAddr)
136 if err != nil {
137 t.Fatal(err)
138 }
139 defer ln.Close()
140
141 done := make(chan bool, 1)
142 go func() {
143 for i := 0; i < 10; i++ {
144 sendLog(f, getSocketAddr, timeoutDuration, &LogEntry{
145 // Ensure a relatively large payload
146 Basename: strings.Repeat(" ", 100000),
147 }, make(chan interface{}))
148 }
149 done <- true
150 }()
151
152 <-done
153 })
154}