go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/tsmon/registry/registry_test.go (about)

     1  // Copyright 2023 The LUCI 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 registry
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/common/tsmon/field"
    22  	"go.chromium.org/luci/common/tsmon/types"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  )
    27  
    28  type metricDef struct {
    29  	types.MetricInfo
    30  	types.MetricMetadata
    31  	fixedResetTime time.Time
    32  }
    33  
    34  func (m *metricDef) Info() types.MetricInfo         { return m.MetricInfo }
    35  func (m *metricDef) Metadata() types.MetricMetadata { return m.MetricMetadata }
    36  func (m *metricDef) SetFixedResetTime(t time.Time)  { m.fixedResetTime = t }
    37  
    38  func TestAdd(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	Convey("Add", t, func() {
    42  		newMetric := func(name string, fns ...string) *metricDef {
    43  			ret := &metricDef{MetricInfo: types.MetricInfo{Name: name}}
    44  			for _, n := range fns {
    45  				f := field.Field{Name: n, Type: field.IntType}
    46  				ret.MetricInfo.Fields = append(ret.MetricInfo.Fields, f)
    47  			}
    48  			return ret
    49  		}
    50  
    51  		Convey("works", func() {
    52  			Add(newMetric("my/metric_/1"))
    53  			Add(newMetric("my/metriC/2", "field_1"))
    54  			Add(newMetric("my/metric-/3", "field_1", "field_2"))
    55  		})
    56  
    57  		Convey("panics", func() {
    58  			m := func(n string, fns ...string) func() {
    59  				return func() { Add(newMetric(n, fns...)) }
    60  			}
    61  
    62  			Convey("if metric name is invalid", func() {
    63  				So(m(""), ShouldPanicLike, "empty metric name")
    64  				So(m("/"), ShouldPanicLike, "invalid metric name")
    65  				So(m("//"), ShouldPanicLike, "invalid metric name")
    66  				So(m("meric//1"), ShouldPanicLike, "invalid metric name")
    67  				So(m("meric!"), ShouldPanicLike, "invalid metric name")
    68  				So(m("meric#//"), ShouldPanicLike, "invalid metric name")
    69  			})
    70  
    71  			Convey("if metric name is duplicate", func() {
    72  				m("metric")()
    73  				So(m("metric"), ShouldPanicLike, "duplicate metric name")
    74  			})
    75  
    76  			Convey("if field name is invalid", func() {
    77  				So(m("metric/a", ""), ShouldPanicLike, "invalid field name")
    78  				So(m("metric/a", "-"), ShouldPanicLike, "invalid field name")
    79  				So(m("metric/a", "f-1"), ShouldPanicLike, "invalid field name")
    80  				So(m("metric/a", "f#1"), ShouldPanicLike, "invalid field name")
    81  				So(m("metric/a", "1_/"), ShouldPanicLike, "invalid field name")
    82  			})
    83  		})
    84  	})
    85  }