...

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

Documentation: go.opencensus.io/zpages

     1  // Copyright 2018, 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  package zpages
    16  
    17  import (
    18  	"bytes"
    19  	"html/template"
    20  	"testing"
    21  )
    22  
    23  const tmplBody = `
    24          <td><b>{{.Method}}</b></td>
    25          <td></td>
    26          <td align="right">{{.CountMinute|count}}</td>
    27          <td align="right">{{.CountHour|count}}</td>
    28          <td align="right">{{.CountTotal|count}}</td><td></td>
    29          <td align="right">{{.AvgLatencyMinute|ms}}</td>
    30          <td align="right">{{.AvgLatencyHour|ms}}</td>
    31          <td align="right">{{.AvgLatencyTotal|ms}}</td><td></td>
    32          <td align="right">{{.RPCRateMinute|rate}}</td>
    33          <td align="right">{{.RPCRateHour|rate}}</td>
    34          <td align="right">{{.RPCRateTotal|rate}}</td><td></td>
    35          <td align="right">{{.InputRateMinute|datarate}}</td>
    36          <td align="right">{{.InputRateHour|datarate}}</td>
    37          <td align="right">{{.InputRateTotal|datarate}}</td><td></td>
    38          <td align="right">{{.OutputRateMinute|datarate}}</td>
    39          <td align="right">{{.OutputRateHour|datarate}}</td>
    40          <td align="right">{{.OutputRateTotal|datarate}}</td><td></td>
    41          <td align="right">{{.ErrorsMinute|count}}</td>
    42          <td align="right">{{.ErrorsHour|count}}</td>
    43          <td align="right">{{.ErrorsTotal|count}}</td><td></td>
    44  `
    45  
    46  var tmpl = template.Must(template.New("countTest").Funcs(templateFunctions).Parse(tmplBody))
    47  
    48  func TestTemplateFuncs(t *testing.T) {
    49  	buf := new(bytes.Buffer)
    50  	sshot := &statSnapshot{
    51  		Method:           "Foo",
    52  		CountMinute:      1e9,
    53  		CountHour:        5000,
    54  		CountTotal:       1e12,
    55  		AvgLatencyMinute: 10000,
    56  		AvgLatencyHour:   1000,
    57  		AvgLatencyTotal:  20000,
    58  		RPCRateMinute:    2000,
    59  		RPCRateHour:      5000,
    60  		RPCRateTotal:     75000,
    61  		InputRateMinute:  75000,
    62  		InputRateHour:    75000,
    63  		InputRateTotal:   75000,
    64  		OutputRateMinute: 75000,
    65  		OutputRateHour:   75000,
    66  		OutputRateTotal:  75000,
    67  		ErrorsMinute:     120000000,
    68  		ErrorsHour:       75000000,
    69  		ErrorsTotal:      7500000,
    70  	}
    71  	if err := tmpl.Execute(buf, sshot); err != nil {
    72  		t.Fatalf("Failed to execute template: %v", err)
    73  	}
    74  	want := `
    75          <td><b>Foo</b></td>
    76          <td></td>
    77          <td align="right">1.000 G </td>
    78          <td align="right">5000</td>
    79          <td align="right">1.000 T </td><td></td>
    80          <td align="right">0.010</td>
    81          <td align="right">0.001</td>
    82          <td align="right">0.020</td><td></td>
    83          <td align="right">2000.000</td>
    84          <td align="right">5000.000</td>
    85          <td align="right">75000.000</td><td></td>
    86          <td align="right">0.075</td>
    87          <td align="right">0.075</td>
    88          <td align="right">0.075</td><td></td>
    89          <td align="right">0.075</td>
    90          <td align="right">0.075</td>
    91          <td align="right">0.075</td><td></td>
    92          <td align="right">120.000 M </td>
    93          <td align="right">75.000 M </td>
    94          <td align="right">7.500 M </td><td></td>
    95  `
    96  	if g, w := buf.String(), want; g != w {
    97  		t.Errorf("Output mismatch:\nGot:\n\t%s\nWant:\n\t%s", g, w)
    98  	}
    99  }
   100  

View as plain text