github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/debug/log/kubernetes/kubernetes_test.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/debug/log/kubernetes/kubernetes_test.go
    14  
    15  package kubernetes
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/json"
    20  	"io"
    21  	"os"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/tickoalcantara12/micro/v3/service/debug/log"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestKubernetes(t *testing.T) {
    30  	// TODO: fix local test running
    31  	return
    32  
    33  	if os.Getenv("IN_TRAVIS_CI") == "yes" {
    34  		t.Skip("In Travis CI")
    35  	}
    36  
    37  	k := NewLog(log.Name("micro-network"))
    38  
    39  	r, w, err := os.Pipe()
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	s := os.Stderr
    45  	os.Stderr = w
    46  	meta := make(map[string]string)
    47  
    48  	write := log.Record{
    49  		Timestamp: time.Unix(0, 0).UTC(),
    50  		Message:   "Test log entry",
    51  		Metadata:  meta,
    52  	}
    53  
    54  	meta["foo"] = "bar"
    55  
    56  	k.Write(write)
    57  	b := &bytes.Buffer{}
    58  	w.Close()
    59  	io.Copy(b, r)
    60  	os.Stderr = s
    61  
    62  	var read log.Record
    63  
    64  	if err := json.Unmarshal(b.Bytes(), &read); err != nil {
    65  		t.Fatalf("json.Unmarshal failed: %s", err.Error())
    66  	}
    67  
    68  	assert.Equal(t, write, read, "Write was not equal")
    69  
    70  	records, err := k.Read()
    71  	assert.Nil(t, err, "Read should not error")
    72  	assert.NotNil(t, records, "Read should return records")
    73  
    74  	stream, err := k.Stream()
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	records = nil
    80  
    81  	go stream.Stop()
    82  
    83  	for s := range stream.Chan() {
    84  		records = append(records, s)
    85  	}
    86  
    87  	assert.Equal(t, 0, len(records), "Stream should return nothing")
    88  }