-
Notifications
You must be signed in to change notification settings - Fork 15
/
logstreamer_test.go
111 lines (90 loc) · 2.6 KB
/
logstreamer_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
package logstreamer
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strings"
"testing"
)
func TestLogstreamerOk(t *testing.T) {
// Create a logger (your app probably already has one)
logger := log.New(os.Stdout, "--> ", log.Ldate|log.Ltime)
// Setup a streamer that we'll pipe cmd.Stdout to
logStreamerOut := NewLogstreamer(logger, "stdout", false)
defer logStreamerOut.Close()
// Setup a streamer that we'll pipe cmd.Stderr to.
// We want to record/buffer anything that's written to this (3rd argument true)
logStreamerErr := NewLogstreamer(logger, "stderr", true)
defer logStreamerErr.Close()
// Execute something that succeeds
cmd := exec.Command(
"ls",
"-al",
)
cmd.Stderr = logStreamerErr
cmd.Stdout = logStreamerOut
// Reset any error we recorded
logStreamerErr.FlushRecord()
// Execute command
err := cmd.Start()
// Failed to spawn?
if err != nil {
t.Fatal("ERROR could not spawn command.", err.Error())
}
// Failed to execute?
err = cmd.Wait()
if err != nil {
t.Fatal("ERROR command finished with error. ", err.Error(), logStreamerErr.FlushRecord())
}
}
func TestLogstreamerErr(t *testing.T) {
// Create a logger (your app probably already has one)
logger := log.New(os.Stdout, "--> ", log.Ldate|log.Ltime)
// Setup a streamer that we'll pipe cmd.Stdout to
logStreamerOut := NewLogstreamer(logger, "stdout", false)
defer logStreamerOut.Close()
// Setup a streamer that we'll pipe cmd.Stderr to.
// We want to record/buffer anything that's written to this (3rd argument true)
logStreamerErr := NewLogstreamer(logger, "stderr", true)
defer logStreamerErr.Close()
// Execute something that succeeds
cmd := exec.Command(
"ls",
"nonexisting",
)
cmd.Stderr = logStreamerErr
cmd.Stdout = logStreamerOut
// Reset any error we recorded
logStreamerErr.FlushRecord()
// Execute command
err := cmd.Start()
// Failed to spawn?
if err != nil {
logger.Print("ERROR could not spawn command. ")
}
// Failed to execute?
err = cmd.Wait()
if err != nil {
fmt.Printf("Good. command finished with %s. %s. \n", err.Error(), logStreamerErr.FlushRecord())
} else {
t.Fatal("This command should have failed")
}
}
func TestLogstreamerFlush(t *testing.T) {
const text = "Text without newline"
var buffer bytes.Buffer
byteWriter := bufio.NewWriter(&buffer)
logger := log.New(byteWriter, "", 0)
logStreamerOut := NewLogstreamer(logger, "", false)
defer logStreamerOut.Close()
logStreamerOut.Write([]byte(text))
logStreamerOut.Flush()
byteWriter.Flush()
s := strings.TrimSpace(string(buffer.Bytes()))
if s != text {
t.Fatalf("Expected '%s', got '%s'.", text, s)
}
}