This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
main_test.go
83 lines (66 loc) · 1.69 KB
/
main_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
// Copyright 2018 Intel Corporation.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestInitLogger(t *testing.T) {
origStdout := os.Stdout
origStderr := os.Stderr
r, w, err := os.Pipe()
assert.Nil(t, err, "Could not create the pipe: %v", err)
os.Stdout = w
os.Stderr = w
defer func() {
r.Close()
w.Close()
os.Stdout = origStdout
os.Stderr = origStderr
}()
testOutString := "Foo Bar"
initLogger("debug", "container-id", "exec-id", logrus.Fields{}, ioutil.Discard)
logger().Info(testOutString)
outC := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
w.Close()
out := <-outC
assert.Equal(t, out, "", "Expecting %q to be empty", out)
}
func TestInitLoggerFields(t *testing.T) {
assert := assert.New(t)
buf := &bytes.Buffer{}
announceFields := logrus.Fields{
"foo": "bar",
"A": "B",
"sausages": "yummy",
}
initLogger("debug", "container-id", "exec-id", announceFields, buf)
line := buf.String()
assert.True(strings.Contains(line, "level=info"))
assert.True(strings.Contains(line, "msg=announce"))
assert.True(strings.Contains(line, "container=container-id"))
assert.True(strings.Contains(line, "exec-id=exec-id"))
assert.True(strings.Contains(line, "name="+shimName))
assert.True(strings.Contains(line, "source=shim"))
pidPattern := regexp.MustCompile(`pid=\d+`)
matches := pidPattern.FindAllString(line, -1)
assert.NotNil(matches)
for k, v := range announceFields {
assert.True(strings.Contains(line, fmt.Sprintf("%s=%s", k, v)))
}
}