micro/debug/log/kubernetes/kubernetes_test.go

75 lines
1.2 KiB
Go
Raw Normal View History

2019-12-17 12:11:26 +00:00
package kubernetes
import (
"bytes"
"encoding/json"
2019-12-17 12:11:26 +00:00
"io"
"os"
"testing"
"time"
"github.com/micro/go-micro/v2/debug/log"
2019-12-17 12:11:26 +00:00
"github.com/stretchr/testify/assert"
)
func TestKubernetes(t *testing.T) {
2019-12-20 23:34:08 +00:00
// TODO: fix local test running
return
if os.Getenv("IN_TRAVIS_CI") == "yes" {
t.Skip("In Travis CI")
}
2019-12-20 23:34:08 +00:00
2019-12-20 23:36:16 +00:00
k := NewLog(log.Name("micro-network"))
2019-12-17 12:11:26 +00:00
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
2019-12-20 23:34:08 +00:00
2019-12-17 12:11:26 +00:00
s := os.Stderr
os.Stderr = w
meta := make(map[string]string)
2019-12-20 23:34:08 +00:00
write := log.Record{
2019-12-17 17:36:01 +00:00
Timestamp: time.Unix(0, 0).UTC(),
2019-12-18 16:02:11 +00:00
Message: "Test log entry",
2019-12-17 12:11:26 +00:00
Metadata: meta,
}
2019-12-20 23:34:08 +00:00
meta["foo"] = "bar"
2019-12-20 23:34:08 +00:00
k.Write(write)
2019-12-17 12:11:26 +00:00
b := &bytes.Buffer{}
w.Close()
io.Copy(b, r)
os.Stderr = s
2019-12-20 23:34:08 +00:00
var read log.Record
2019-12-20 23:34:08 +00:00
if err := json.Unmarshal(b.Bytes(), &read); err != nil {
t.Fatalf("json.Unmarshal failed: %s", err.Error())
}
2019-12-20 23:34:08 +00:00
assert.Equal(t, write, read, "Write was not equal")
2019-12-17 12:11:26 +00:00
records, err := k.Read()
assert.Nil(t, err, "Read should not error")
assert.NotNil(t, records, "Read should return records")
2019-12-17 12:11:26 +00:00
2019-12-17 17:24:01 +00:00
stream, err := k.Stream()
if err != nil {
2019-12-20 23:34:08 +00:00
t.Fatal(err)
2019-12-17 17:24:01 +00:00
}
2019-12-20 23:34:08 +00:00
records = nil
2019-12-17 17:24:01 +00:00
go stream.Stop()
2019-12-20 23:34:08 +00:00
2019-12-17 17:24:01 +00:00
for s := range stream.Chan() {
2019-12-17 12:11:26 +00:00
records = append(records, s)
}
2019-12-20 23:34:08 +00:00
assert.Equal(t, 0, len(records), "Stream should return nothing")
2019-12-17 12:11:26 +00:00
}