@@ -2,14 +2,19 @@ package utilities
22
33import (
44 "context"
5+ "errors"
6+ "fmt"
7+ "math"
8+ "strconv"
59 "strings"
610 "testing"
711 "time"
812
913 "github.com/stretchr/testify/require"
14+ "go.opentelemetry.io/otel/metric"
1015)
1116
12- func TestInitVersionMetrics (t * testing.T ) {
17+ func TestVersionInitVersionMetrics (t * testing.T ) {
1318 ctx , cancel := context .WithTimeout (context .Background (), time .Second )
1419 defer cancel ()
1520
@@ -28,31 +33,63 @@ func TestInitVersionMetrics(t *testing.T) {
2833 }
2934 }
3035
31- const ver = "rc2.187.3-rc.23-g33b87ae0"
36+ const validVer = "rc2.187.3-rc.23-g33b87ae0"
3237
3338 {
34- vi , err := parseSemver (ver )
39+ vi , err := parseSemver (validVer )
3540 require .NoError (t , err )
3641 check (vi )
3742 }
3843
3944 {
40- err := initVersionMetrics (ctx , ver )
45+ err := initVersionMetrics (ctx , validVer )
4146 require .NoError (t , err )
4247 }
4348
49+ {
50+ err := initVersionMetrics (ctx , "invalid" )
51+ require .Error (t , err )
52+ const exp = "initVersionMetrics: unable to parse version"
53+ if got := err .Error (); ! strings .Contains (got , exp ) {
54+ t .Fatalf ("exp err %q to contain %q" , got , exp )
55+ }
56+ }
57+
58+ {
59+ max := strconv .AppendUint (nil , math .MaxUint64 , 10 )
60+ ver := fmt .Sprintf ("%v.%v.%s" , 2 , 187 , max )
61+ err := initVersionMetrics (ctx , ver )
62+ require .Error (t , err )
63+ if exp , got := "math.MaxInt64" , err .Error (); ! strings .Contains (got , exp ) {
64+ t .Fatalf ("exp err %q to contain %q" , got , exp )
65+ }
66+ }
67+
68+ {
69+ sentinel := errors .New ("otel-sentinel" )
70+ errFn := func (name string , options ... metric.Int64GaugeOption ) (metric.Int64Gauge , error ) {
71+ return nil , sentinel
72+ }
73+
74+ err := initGauge (ctx , "metric" , 1 , errFn )
75+ require .Error (t , err )
76+ if exp , got := sentinel .Error (), err .Error (); ! strings .Contains (got , exp ) {
77+ t .Fatalf ("exp err %q to contain %q" , got , exp )
78+ }
79+ }
80+
4481 func () {
4582 prev := Version
4683 defer func () { Version = prev }()
47- Version = ver
84+ Version = validVer
4885
4986 err := InitVersionMetrics (ctx )
5087 require .NoError (t , err )
5188 }()
5289
5390}
5491
55- func TestParseSemver (t * testing.T ) {
92+ func TestVersionParseSemver (t * testing.T ) {
5693 cases := []struct {
5794 str , err string
5895 maj , min , pat , rc uint64
@@ -115,6 +152,9 @@ func TestParseSemver(t *testing.T) {
115152 {str : "2.165.1-rc.1" , maj : 2 , min : 165 , pat : 1 , rc : 1 },
116153 {str : "2.165.1-rc1" , maj : 2 , min : 165 , pat : 1 , rc : 1 },
117154
155+ {str : "2.165.1-rc.1.5" , maj : 2 , min : 165 , pat : 1 , rc : 1 },
156+ {str : "2.165.1-rc1.5" , maj : 2 , min : 165 , pat : 1 , rc : 1 },
157+
118158 {str : "" , err : "Invalid Semantic Version" },
119159 {str : "abc" , err : "Invalid Semantic Version" },
120160 }
@@ -144,7 +184,7 @@ func TestParseSemver(t *testing.T) {
144184 t .Fatal ("exp non-nil err" )
145185 }
146186 if exp , got := tc .err , err .Error (); ! strings .Contains (got , exp ) {
147- t .Fatalf ("exp " )
187+ t .Fatalf ("exp err %q to contain %q" , got , exp )
148188 }
149189 continue
150190 }
0 commit comments