micro/debug/log/kubernetes/kubernetes_test.go

57 lines
1.0 KiB
Go
Raw Normal View History

2019-12-17 15:11:26 +03:00
package kubernetes
import (
"bytes"
"encoding/json"
2019-12-17 15:11:26 +03:00
"io"
"os"
"testing"
"time"
"github.com/micro/go-micro/debug/log"
"github.com/stretchr/testify/assert"
)
func TestKubernetes(t *testing.T) {
k := New()
2019-12-17 15:11:26 +03:00
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
s := os.Stderr
os.Stderr = w
meta := make(map[string]string)
write := log.Record{
2019-12-17 20:36:01 +03:00
Timestamp: time.Unix(0, 0).UTC(),
2019-12-18 19:02:11 +03:00
Message: "Test log entry",
2019-12-17 15:11:26 +03:00
Metadata: meta,
}
meta["foo"] = "bar"
k.Write(write)
2019-12-17 15:11:26 +03:00
b := &bytes.Buffer{}
w.Close()
io.Copy(b, r)
os.Stderr = s
var read log.Record
if err := json.Unmarshal(b.Bytes(), &read); err != nil {
t.Fatalf("json.Unmarshal failed: %s", err.Error())
}
assert.Equal(t, write, read, "Write was not equal")
2019-12-17 15:11:26 +03:00
2019-12-17 20:24:01 +03:00
_, err = k.Read()
assert.Error(t, err, "Read should be unimplemented")
2019-12-17 15:11:26 +03:00
2019-12-17 20:24:01 +03:00
stream, err := k.Stream()
if err != nil {
t.Error(err)
}
2019-12-17 15:11:26 +03:00
records := []log.Record{}
2019-12-17 20:24:01 +03:00
go stream.Stop()
for s := range stream.Chan() {
2019-12-17 15:11:26 +03:00
records = append(records, s)
}
2019-12-17 20:24:01 +03:00
assert.Equal(t, 0, len(records), "Stream should return nothing")
2019-12-17 15:11:26 +03:00
}