...

Source file src/go.opencensus.io/zpages/zpages_test.go

Documentation: go.opencensus.io/zpages

     1  // Copyright 2017, OpenCensus 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  
    16  package zpages
    17  
    18  import (
    19  	"bytes"
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  
    24  	"fmt"
    25  	"net/http"
    26  	"net/http/httptest"
    27  
    28  	"go.opencensus.io/trace"
    29  )
    30  
    31  var (
    32  	tid  = trace.TraceID{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 4, 8, 16, 32, 64, 128}
    33  	sid  = trace.SpanID{1, 2, 4, 8, 16, 32, 64, 128}
    34  	sid2 = trace.SpanID{0, 3, 5, 9, 17, 33, 65, 129}
    35  )
    36  
    37  func TestTraceRows(t *testing.T) {
    38  	now := time.Now()
    39  	later := now.Add(2 * time.Second)
    40  	data := traceDataFromSpans("foo", []*trace.SpanData{{
    41  		SpanContext:  trace.SpanContext{TraceID: tid, SpanID: sid},
    42  		ParentSpanID: sid2,
    43  		Name:         "foo",
    44  		StartTime:    now,
    45  		EndTime:      later,
    46  		Attributes:   map[string]interface{}{"stringkey": "stringvalue", "intkey": 42, "boolkey": true},
    47  		Annotations: []trace.Annotation{
    48  			{Time: now.Add(time.Millisecond), Message: "hello, world", Attributes: map[string]interface{}{"foo": "bar"}},
    49  			{Time: now.Add(1500 * time.Millisecond), Message: "hello, world"},
    50  		},
    51  		MessageEvents: []trace.MessageEvent{
    52  			{Time: now, EventType: 2, MessageID: 0x3, UncompressedByteSize: 0x190, CompressedByteSize: 0x12c},
    53  			{Time: later, EventType: 1, MessageID: 0x1, UncompressedByteSize: 0xc8, CompressedByteSize: 0x64},
    54  		},
    55  		Status: trace.Status{
    56  			Code:    1,
    57  			Message: "d'oh!",
    58  		},
    59  	}})
    60  	fakeTime := "2006/01/02-15:04:05.123456"
    61  	for i := range data.Rows {
    62  		data.Rows[i].Fields[0] = fakeTime
    63  	}
    64  	if want := (traceData{
    65  		Name: "foo",
    66  		Num:  1,
    67  		Rows: []traceRow{
    68  			{Fields: [3]string{fakeTime, "    2.000000", ""}, SpanContext: trace.SpanContext{TraceID: tid, SpanID: sid}, ParentSpanID: sid2},
    69  			{Fields: [3]string{fakeTime, "", `Status{canonicalCode=CANCELLED, description="d'oh!"}`}},
    70  			{Fields: [3]string{fakeTime, "", `Attributes:{boolkey=true, intkey=42, stringkey="stringvalue"}`}},
    71  			{Fields: [3]string{fakeTime, "     .     0", "received message [400 bytes, 300 compressed bytes]"}},
    72  			{Fields: [3]string{fakeTime, "     .  1000", `hello, world  Attributes:{foo="bar"}`}},
    73  			{Fields: [3]string{fakeTime, "    1.499000", "hello, world"}},
    74  			{Fields: [3]string{fakeTime, "     .500000", "sent message [200 bytes, 100 compressed bytes]"}}}}); !reflect.DeepEqual(data, want) {
    75  		t.Errorf("traceRows: got %v want %v\n", data, want)
    76  	}
    77  
    78  	var buf bytes.Buffer
    79  	writeTextTraces(&buf, data)
    80  	if want := `When                       Elapsed(s)   Type
    81  2006/01/02-15:04:05.123456     2.000000 trace_id: 01020304050607080102040810204080 span_id: 0102040810204080 parent_span_id: 0003050911214181
    82  2006/01/02-15:04:05.123456              Status{canonicalCode=CANCELLED, description="d'oh!"}
    83  2006/01/02-15:04:05.123456              Attributes:{boolkey=true, intkey=42, stringkey="stringvalue"}
    84  2006/01/02-15:04:05.123456      .     0 received message [400 bytes, 300 compressed bytes]
    85  2006/01/02-15:04:05.123456      .  1000 hello, world  Attributes:{foo="bar"}
    86  2006/01/02-15:04:05.123456     1.499000 hello, world
    87  2006/01/02-15:04:05.123456      .500000 sent message [200 bytes, 100 compressed bytes]
    88  `; buf.String() != want {
    89  		t.Errorf("writeTextTraces: got %q want %q\n", buf.String(), want)
    90  	}
    91  }
    92  
    93  func TestGetZPages(t *testing.T) {
    94  	mux := http.NewServeMux()
    95  	Handle(mux, "/debug")
    96  	server := httptest.NewServer(mux)
    97  	defer server.Close()
    98  	tests := []string{"/debug/rpcz", "/debug/tracez"}
    99  	for _, tt := range tests {
   100  		t.Run(fmt.Sprintf("GET %s", tt), func(t *testing.T) {
   101  			res, err := http.Get(server.URL + tt)
   102  			if err != nil {
   103  				t.Error(err)
   104  				return
   105  			}
   106  			if got, want := res.StatusCode, http.StatusOK; got != want {
   107  				t.Errorf("res.StatusCode = %d; want %d", got, want)
   108  			}
   109  		})
   110  	}
   111  }
   112  
   113  func TestGetZPages_default(t *testing.T) {
   114  	server := httptest.NewServer(Handler)
   115  	defer server.Close()
   116  	tests := []string{"/rpcz", "/tracez"}
   117  	for _, tt := range tests {
   118  		t.Run(fmt.Sprintf("GET %s", tt), func(t *testing.T) {
   119  			res, err := http.Get(server.URL + tt)
   120  			if err != nil {
   121  				t.Error(err)
   122  				return
   123  			}
   124  			if got, want := res.StatusCode, http.StatusOK; got != want {
   125  				t.Errorf("res.StatusCode = %d; want %d", got, want)
   126  			}
   127  		})
   128  	}
   129  }
   130  

View as plain text