istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/log/uds_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package log
    16  
    17  import (
    18  	"encoding/json"
    19  	"io"
    20  	"net"
    21  	"net/http"
    22  	"path/filepath"
    23  	"reflect"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  type udsServer struct {
    29  	messages []string
    30  }
    31  
    32  func (us *udsServer) handleLog(w http.ResponseWriter, r *http.Request) {
    33  	var body []byte
    34  	if r.Body != nil {
    35  		if data, err := io.ReadAll(r.Body); err == nil {
    36  			body = data
    37  		}
    38  	}
    39  	messages := []string{}
    40  	if err := json.Unmarshal(body, &messages); err != nil {
    41  		w.WriteHeader(http.StatusBadRequest)
    42  	}
    43  	us.messages = append(us.messages, messages...)
    44  }
    45  
    46  func TestUDSLog(t *testing.T) {
    47  	srv := udsServer{messages: make([]string, 0)}
    48  	udsDir := t.TempDir()
    49  	socketPath := filepath.Join(udsDir, "test.sock")
    50  	unixListener, err := net.Listen("unix", socketPath)
    51  	if err != nil {
    52  		t.Fatalf("failed to create uds listener: %v", err)
    53  	}
    54  	loggingOptions := DefaultOptions()
    55  	loggingOptions.JSONEncoding = true
    56  	if err := Configure(loggingOptions.WithTeeToUDS(socketPath, "/")); err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	mux := http.NewServeMux()
    60  	mux.HandleFunc("/", srv.handleLog)
    61  
    62  	go func() {
    63  		if err := http.Serve(unixListener, mux); err != nil {
    64  			t.Error(err)
    65  		}
    66  	}()
    67  
    68  	{
    69  		t.Log("test sending normal log")
    70  
    71  		WithLabels("k", "v").Info("test")
    72  		Warn("test2")
    73  		Sync()
    74  
    75  		// There should be two messages received at server
    76  		// {"msg":"test","k":"v"}
    77  		// {"msg":"test2"}
    78  		if got, want := len(srv.messages), 2; got != want {
    79  			t.Fatalf("number received log messages got %v want %v", got, want)
    80  		}
    81  		type testMessage struct {
    82  			Msg string `json:"msg"`
    83  			K   string `json:"k"`
    84  		}
    85  
    86  		want := []testMessage{
    87  			{Msg: "test", K: "v"},
    88  			{Msg: "test2"},
    89  		}
    90  		got := make([]testMessage, 2)
    91  		json.Unmarshal([]byte(srv.messages[0]), &got[0])
    92  		json.Unmarshal([]byte(srv.messages[1]), &got[1])
    93  		if !reflect.DeepEqual(got, want) {
    94  			t.Errorf("received log messages, got %v want %v", got, want)
    95  		}
    96  	}
    97  
    98  	{
    99  		t.Log("test sending log with specified time")
   100  
   101  		// Clean up all the mssages, and log again. Check that buffer is cleaned up properly.
   102  		srv.messages = make([]string, 0)
   103  		yesterday := time.Now().Add(-time.Hour * 24).Truncate(time.Microsecond)
   104  		defaultScope.LogWithTime(InfoLevel, "test3", yesterday)
   105  		Sync()
   106  		// There should only be one message in the buffer
   107  		if got, want := len(srv.messages), 1; got != want {
   108  			t.Fatalf("number received log messages got %v want %v", got, want)
   109  		}
   110  
   111  		type testMessage struct {
   112  			Msg  string    `json:"msg"`
   113  			Time time.Time `json:"time"`
   114  		}
   115  		var got testMessage
   116  		json.Unmarshal([]byte(srv.messages[0]), &got)
   117  		if got.Time.UnixNano() != yesterday.UnixNano() {
   118  			t.Errorf("received log message with specified time, got %v want %v", got.Time, yesterday)
   119  		}
   120  	}
   121  }