forked from GoogleCloudPlatform/compute-image-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
184 lines (150 loc) · 4.5 KB
/
logger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package daisy
import (
"bufio"
"bytes"
"fmt"
"regexp"
"sync"
"testing"
"cloud.google.com/go/logging"
"github.com/stretchr/testify/assert"
)
type MockLogger struct {
entries []*LogEntry
mx sync.Mutex
serialPortLogs []string
}
func (l *MockLogger) WriteSerialPortLogs(w *Workflow, instance string, buf bytes.Buffer) {
l.serialPortLogs = append(l.serialPortLogs, buf.String())
}
func (l *MockLogger) ReadSerialPortLogs() []string {
return l.serialPortLogs
}
func (l *MockLogger) WriteLogEntry(e *LogEntry) {
l.mx.Lock()
defer l.mx.Unlock()
l.entries = append(l.entries, e)
}
// f flushes all loggers.
func (l *MockLogger) Flush() {}
func (l *MockLogger) getEntries() []*LogEntry {
l.mx.Lock()
defer l.mx.Unlock()
return l.entries[:]
}
func TestWriteWorkflowInfo(t *testing.T) {
w := New()
w.Name = "Test"
w.Logger = newDaisyLogger(false)
var b bytes.Buffer
w.Logger.(*daisyLog).gcsLogWriter = &syncedWriter{buf: bufio.NewWriter(&b)}
w.LogWorkflowInfo("test %s", "a")
w.Logger.(*daisyLog).gcsLogWriter.Flush()
got := b.String()
want := "\\[Test\\]: \\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}([+-]\\d{2}:\\d{2})|Z test a"
match, err := regexp.MatchString(want, got)
if err != nil {
t.Fatal(err)
}
if !match {
t.Errorf("Wanted to match %s, got %s", want, got)
}
}
func TestWriteStepInfo(t *testing.T) {
w := New()
w.Name = "Test"
w.Logger = newDaisyLogger(false)
var b bytes.Buffer
w.Logger.(*daisyLog).gcsLogWriter = &syncedWriter{buf: bufio.NewWriter(&b)}
w.LogStepInfo("StepName", "StepType", "test %s", "a")
w.Logger.Flush()
got := b.String()
want := "\\[Test.StepName\\]: \\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}([+-]\\d{2}:\\d{2})|Z StepType: test a"
match, _ := regexp.MatchString(want, got)
if !match {
t.Errorf("Wanted to match %s, got %s", want, got)
}
}
type MockCloudLogWriter struct {
entries []*logging.Entry
mx sync.Mutex
}
func (cl *MockCloudLogWriter) Log(e logging.Entry) {
cl.mx.Lock()
defer cl.mx.Unlock()
cl.entries = append(cl.entries, &e)
}
func (cl *MockCloudLogWriter) Flush() error {
return nil
}
func TestSendSerialPortLogsToCloud(t *testing.T) {
w := New()
w.Name = "Test"
w.Logger = newDaisyLogger(false)
cl := &MockCloudLogWriter{}
w.Logger.(*daisyLog).cloudLogger = cl
var buf bytes.Buffer
for i := 0; i < 98*1024; i++ {
buf.WriteString("Serial output\n")
}
w.Logger.WriteSerialPortLogs(w, "instance-name", buf)
// We expect 14 entries
if len(cl.entries) != 14 {
t.Errorf("Wanted %d, got %d", 14, len(cl.entries))
}
assertLogOutput(t, w.Logger.ReadSerialPortLogs(),
[]string{"Serial logs for instance: instance-name\n" + buf.String()})
}
func TestSendSerialPortLogsToCloudMultipleInstances(t *testing.T) {
w := New()
w.Name = "Test"
w.Logger = newDaisyLogger(false)
cl := &MockCloudLogWriter{}
w.Logger.(*daisyLog).cloudLogger = cl
contentOfLogs := []string{
"line1\nline2",
"more log info\t",
}
instanceAnnotatedLogs := []string{
"Serial logs for instance: instance-0\nline1\nline2",
"Serial logs for instance: instance-1\nmore log info\t",
}
for i, log := range contentOfLogs {
var buf bytes.Buffer
buf.WriteString(log)
w.Logger.WriteSerialPortLogs(w, fmt.Sprintf("instance-%d", i), buf)
}
assertLogOutput(t, w.Logger.ReadSerialPortLogs(), instanceAnnotatedLogs)
}
func TestSendSerialPortLogsToCloudDisabled(t *testing.T) {
w := New()
w.Name = "Test"
w.Logger = newDaisyLogger(false)
var buf bytes.Buffer
buf.WriteString("Serial output\n")
w.Logger.WriteSerialPortLogs(w, "instance-name", buf)
assert.Equal(t, len(w.Logger.ReadSerialPortLogs()), 0,
"Don't retain logs if cloud logging disabled.")
}
func assertLogOutput(t *testing.T, actualLogs []string, expectedLogs []string) {
if len(actualLogs) != len(expectedLogs) {
t.Errorf("Expected %d serial logs. Found %d",
len(expectedLogs), len(actualLogs))
}
for _, log := range expectedLogs {
assert.Contains(t, actualLogs, log)
}
}