github.com/google/cloudprober@v0.11.3/surfacers/datadog/datadog_test.go (about)

     1  // Copyright 2021 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 datadog
    16  
    17  import (
    18  	"context"
    19  	"reflect"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/google/cloudprober/logger"
    24  	"github.com/google/cloudprober/metrics"
    25  )
    26  
    27  func newTestDDSurfacer() DDSurfacer {
    28  	l, _ := logger.New(context.TODO(), "test-logger")
    29  
    30  	return DDSurfacer{
    31  		l:      l,
    32  		prefix: "testPrefix",
    33  	}
    34  }
    35  
    36  func TestEmLabelsToTags(t *testing.T) {
    37  	timestamp := time.Now()
    38  
    39  	tests := map[string]struct {
    40  		em   *metrics.EventMetrics
    41  		want []string
    42  	}{
    43  		"no label": {
    44  			em:   metrics.NewEventMetrics(timestamp),
    45  			want: []string{},
    46  		},
    47  		"one label": {
    48  			em:   metrics.NewEventMetrics(timestamp).AddLabel("ptype", "sysvars"),
    49  			want: []string{"ptype:sysvars"},
    50  		},
    51  		"three labels": {
    52  			em: metrics.NewEventMetrics(timestamp).AddLabel("label1", "value1").
    53  				AddLabel("label2", "value2").
    54  				AddLabel("label3", "value3"),
    55  			want: []string{"label1:value1", "label2:value2", "label3:value3"},
    56  		},
    57  	}
    58  
    59  	for name, tc := range tests {
    60  		t.Run(name, func(t *testing.T) {
    61  			got := emLabelsToTags(tc.em)
    62  			if !reflect.DeepEqual(got, tc.want) {
    63  				// if got != tc.want {
    64  				t.Errorf("got: %v, want %v %v %v", got, tc.want, reflect.TypeOf(got), reflect.TypeOf(tc.want))
    65  			}
    66  		})
    67  	}
    68  }