// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zpages
import (
"bytes"
"html/template"
"testing"
)
const tmplBody = `
{{.Method}} |
|
{{.CountMinute|count}} |
{{.CountHour|count}} |
{{.CountTotal|count}} | |
{{.AvgLatencyMinute|ms}} |
{{.AvgLatencyHour|ms}} |
{{.AvgLatencyTotal|ms}} | |
{{.RPCRateMinute|rate}} |
{{.RPCRateHour|rate}} |
{{.RPCRateTotal|rate}} | |
{{.InputRateMinute|datarate}} |
{{.InputRateHour|datarate}} |
{{.InputRateTotal|datarate}} | |
{{.OutputRateMinute|datarate}} |
{{.OutputRateHour|datarate}} |
{{.OutputRateTotal|datarate}} | |
{{.ErrorsMinute|count}} |
{{.ErrorsHour|count}} |
{{.ErrorsTotal|count}} | |
`
var tmpl = template.Must(template.New("countTest").Funcs(templateFunctions).Parse(tmplBody))
func TestTemplateFuncs(t *testing.T) {
buf := new(bytes.Buffer)
sshot := &statSnapshot{
Method: "Foo",
CountMinute: 1e9,
CountHour: 5000,
CountTotal: 1e12,
AvgLatencyMinute: 10000,
AvgLatencyHour: 1000,
AvgLatencyTotal: 20000,
RPCRateMinute: 2000,
RPCRateHour: 5000,
RPCRateTotal: 75000,
InputRateMinute: 75000,
InputRateHour: 75000,
InputRateTotal: 75000,
OutputRateMinute: 75000,
OutputRateHour: 75000,
OutputRateTotal: 75000,
ErrorsMinute: 120000000,
ErrorsHour: 75000000,
ErrorsTotal: 7500000,
}
if err := tmpl.Execute(buf, sshot); err != nil {
t.Fatalf("Failed to execute template: %v", err)
}
want := `
Foo |
|
1.000 G |
5000 |
1.000 T | |
0.010 |
0.001 |
0.020 | |
2000.000 |
5000.000 |
75000.000 | |
0.075 |
0.075 |
0.075 | |
0.075 |
0.075 |
0.075 | |
120.000 M |
75.000 M |
7.500 M | |
`
if g, w := buf.String(), want; g != w {
t.Errorf("Output mismatch:\nGot:\n\t%s\nWant:\n\t%s", g, w)
}
}