github.com/google/cloudprober@v0.11.3/metrics/metrics_test.go (about)

     1  // Copyright 2019 The Cloudprober 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 metrics
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestParseValueFromString(t *testing.T) {
    22  	var val string
    23  
    24  	// Bad value, should return an error
    25  	val = "234g"
    26  	_, err := ParseValueFromString(val)
    27  	if err == nil {
    28  		t.Errorf("ParseValueFromString(%s) returned no error", val)
    29  	}
    30  
    31  	// Float value
    32  	val = "234"
    33  	v, err := ParseValueFromString(val)
    34  	if err != nil {
    35  		t.Errorf("ParseValueFromString(%s) returned error: %v", val, err)
    36  	}
    37  	if _, ok := v.(*Float); !ok {
    38  		t.Errorf("ParseValueFromString(%s) returned a non-float: %v", val, v)
    39  	}
    40  
    41  	// String value, aggregation disabled
    42  	val = "\"234\""
    43  	v, err = ParseValueFromString(val)
    44  	if err != nil {
    45  		t.Errorf("ParseValueFromString(%s) returned error: %v", val, err)
    46  	}
    47  	if _, ok := v.(String); !ok {
    48  		t.Errorf("ParseValueFromString(%s) returned a non-string: %v", val, v)
    49  	}
    50  
    51  	// Map value
    52  	val = "map:code 200:10 404:1"
    53  	v, err = ParseValueFromString(val)
    54  	if err != nil {
    55  		t.Errorf("ParseValueFromString(%s) returned error: %v", val, err)
    56  	}
    57  	if _, ok := v.(*Map); !ok {
    58  		t.Errorf("ParseValueFromString(%s) returned a non-map: %v", val, v)
    59  	}
    60  
    61  	// Dist value
    62  	val = "dist:sum:899|count:221|lb:-Inf,0.5,2,7.5|bc:34,54,121,12"
    63  	v, err = ParseValueFromString(val)
    64  	if err != nil {
    65  		t.Errorf("ParseValueFromString(%s) returned error: %v", val, err)
    66  	}
    67  	if _, ok := v.(*Distribution); !ok {
    68  		t.Errorf("ParseValueFromString(%s) returned a non-dist: %v", val, v)
    69  	}
    70  }