Skip to content

Commit 9832720

Browse files
committed
Introduced a basic logger to the integration tests framework
Signed-off-by: Marco Pracucci <marco@pracucci.com>
1 parent 51f7047 commit 9832720

File tree

3 files changed

+68
-28
lines changed

3 files changed

+68
-28
lines changed

integration/e2e/logger.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package e2e
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"strings"
8+
"time"
9+
10+
"github.com/go-kit/kit/log"
11+
)
12+
13+
// Global logger to use in integration tests. We use a global logger to simplify
14+
// writing integration tests and avoiding having to pass the logger instance
15+
// every time.
16+
var logger log.Logger
17+
18+
func init() {
19+
logger = NewLogger(os.Stdout)
20+
}
21+
22+
type Logger struct {
23+
w io.Writer
24+
}
25+
26+
func NewLogger(w io.Writer) *Logger {
27+
return &Logger{
28+
w: w,
29+
}
30+
}
31+
32+
func (l *Logger) Log(keyvals ...interface{}) error {
33+
log := strings.Builder{}
34+
log.WriteString(time.Now().Format("15:04:05"))
35+
36+
for _, v := range keyvals {
37+
log.WriteString(" " + fmt.Sprint(v))
38+
}
39+
40+
log.WriteString("\n")
41+
l.w.Write([]byte(log.String()))
42+
return nil
43+
}

integration/e2e/scenario.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewScenario(networkName string) (*Scenario, error) {
5454

5555
// Setup the docker network.
5656
if out, err := RunCommandAndGetOutput("docker", "network", "create", networkName); err != nil {
57-
fmt.Println(string(out))
57+
logger.Log(string(out))
5858
s.clean()
5959
return nil, errors.Wrapf(err, "create docker network '%s'", networkName)
6060
}
@@ -90,8 +90,7 @@ func (s *Scenario) StartAndWaitReady(services ...Service) error {
9090

9191
func (s *Scenario) Start(services ...Service) error {
9292
for _, service := range services {
93-
// TODO(bwplotka): Some basic logger would be nice.
94-
fmt.Println("Starting", service.Name())
93+
logger.Log("Starting", service.Name())
9594

9695
// Ensure another service with the same name doesn't exist.
9796
if s.isRegistered(service.Name()) {
@@ -153,15 +152,15 @@ func (s *Scenario) Close() {
153152
// TODO(bwplotka): Add comments.
154153
func (s *Scenario) clean() {
155154
if err := os.RemoveAll(s.sharedDir); err != nil {
156-
fmt.Println("error while removing sharedDir", s.sharedDir, "err:", err)
155+
logger.Log("error while removing sharedDir", s.sharedDir, "err:", err)
157156
}
158157
}
159158

160159
func (s *Scenario) shutdown() {
161160
// Kill the services in the opposite order.
162161
for i := len(s.services) - 1; i >= 0; i-- {
163162
if err := s.services[i].Kill(); err != nil {
164-
fmt.Println("Unable to kill service", s.services[i].Name(), ":", err.Error())
163+
logger.Log("Unable to kill service", s.services[i].Name(), ":", err.Error())
165164
}
166165
}
167166

@@ -181,31 +180,31 @@ func (s *Scenario) shutdown() {
181180
}
182181

183182
if out, err = RunCommandAndGetOutput("docker", "rm", "--force", containerID); err != nil {
184-
fmt.Println(string(out))
185-
fmt.Println("Unable to cleanup leftover container", containerID, ":", err.Error())
183+
logger.Log(string(out))
184+
logger.Log("Unable to cleanup leftover container", containerID, ":", err.Error())
186185
}
187186
}
188187
} else {
189-
fmt.Println(string(out))
190-
fmt.Println("Unable to cleanup leftover containers:", err.Error())
188+
logger.Log(string(out))
189+
logger.Log("Unable to cleanup leftover containers:", err.Error())
191190
}
192191

193192
// Teardown the docker network. In case the network does not exists (ie. this function
194193
// is called during the setup of the scenario) we skip the removal in order to not log
195194
// an error which may be misleading.
196195
if ok, err := existDockerNetwork(s.networkName); ok || err != nil {
197196
if out, err := RunCommandAndGetOutput("docker", "network", "rm", s.networkName); err != nil {
198-
fmt.Println(string(out))
199-
fmt.Println("Unable to remove docker network", s.networkName, ":", err.Error())
197+
logger.Log(string(out))
198+
logger.Log("Unable to remove docker network", s.networkName, ":", err.Error())
200199
}
201200
}
202201
}
203202

204203
func existDockerNetwork(networkName string) (bool, error) {
205204
out, err := RunCommandAndGetOutput("docker", "network", "ls", "--quiet", "--filter", fmt.Sprintf("name=%s", networkName))
206205
if err != nil {
207-
fmt.Println(string(out))
208-
fmt.Println("Unable to check if docker network", networkName, "exists:", err.Error())
206+
logger.Log(string(out))
207+
logger.Log("Unable to check if docker network", networkName, "exists:", err.Error())
209208
}
210209

211210
return strings.TrimSpace(string(out)) != "", nil

integration/e2e/service.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"io"
87
"io/ioutil"
98
"math"
10-
"os"
119
"os/exec"
1210
"regexp"
1311
"strconv"
1412
"strings"
1513
"time"
1614

15+
"github.com/go-kit/kit/log"
1716
"github.com/pkg/errors"
1817
io_prometheus_client "github.com/prometheus/client_model/go"
1918
"github.com/prometheus/common/expfmt"
@@ -104,8 +103,8 @@ func (s *ConcreteService) Start(networkName, sharedDir string) (err error) {
104103
}()
105104

106105
cmd := exec.Command("docker", s.buildDockerRunArgs(networkName, sharedDir)...)
107-
cmd.Stdout = &LinePrefixWriter{prefix: s.name + ": ", wrapped: os.Stdout}
108-
cmd.Stderr = &LinePrefixWriter{prefix: s.name + ": ", wrapped: os.Stderr}
106+
cmd.Stdout = &LinePrefixLogger{prefix: s.name + ": ", logger: logger}
107+
cmd.Stderr = &LinePrefixLogger{prefix: s.name + ": ", logger: logger}
109108
if err = cmd.Start(); err != nil {
110109
return err
111110
}
@@ -142,7 +141,7 @@ func (s *ConcreteService) Start(networkName, sharedDir string) (err error) {
142141
}
143142
s.networkPortsContainerToLocal[containerPort] = localPort
144143
}
145-
fmt.Println("Ports for container:", s.containerName(), "Mapping:", s.networkPortsContainerToLocal)
144+
logger.Log("Ports for container:", s.containerName(), "Mapping:", s.networkPortsContainerToLocal)
146145
return nil
147146
}
148147

@@ -151,10 +150,10 @@ func (s *ConcreteService) Stop() error {
151150
return nil
152151
}
153152

154-
fmt.Println("Stopping", s.name)
153+
logger.Log("Stopping", s.name)
155154

156155
if out, err := RunCommandAndGetOutput("docker", "stop", "--time=30", s.containerName()); err != nil {
157-
fmt.Println(string(out))
156+
logger.Log(string(out))
158157
return err
159158
}
160159
s.usedNetworkName = ""
@@ -167,10 +166,10 @@ func (s *ConcreteService) Kill() error {
167166
return nil
168167
}
169168

170-
fmt.Println("Killing", s.name)
169+
logger.Log("Killing", s.name)
171170

172171
if out, err := RunCommandAndGetOutput("docker", "stop", "--time=0", s.containerName()); err != nil {
173-
fmt.Println(string(out))
172+
logger.Log(string(out))
174173
return err
175174
}
176175
s.usedNetworkName = ""
@@ -405,12 +404,12 @@ func (p *CmdReadinessProbe) Ready(service *ConcreteService) error {
405404
return err
406405
}
407406

408-
type LinePrefixWriter struct {
409-
prefix string
410-
wrapped io.Writer
407+
type LinePrefixLogger struct {
408+
prefix string
409+
logger log.Logger
411410
}
412411

413-
func (w *LinePrefixWriter) Write(p []byte) (n int, err error) {
412+
func (w *LinePrefixLogger) Write(p []byte) (n int, err error) {
414413
for _, line := range strings.Split(string(p), "\n") {
415414
// Skip empty lines
416415
line = strings.TrimSpace(line)
@@ -419,8 +418,7 @@ func (w *LinePrefixWriter) Write(p []byte) (n int, err error) {
419418
}
420419

421420
// Write the prefix + line to the wrapped writer
422-
_, err := w.wrapped.Write([]byte(w.prefix + line + "\n"))
423-
if err != nil {
421+
if err := w.logger.Log(w.prefix + line); err != nil {
424422
return 0, err
425423
}
426424
}

0 commit comments

Comments
 (0)