github.com/google/cloudprober@v0.11.3/probes/options/labels_test.go (about)

     1  // Copyright 2017-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 options
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/golang/protobuf/proto"
    22  	configpb "github.com/google/cloudprober/probes/proto"
    23  	"github.com/google/cloudprober/targets/endpoint"
    24  )
    25  
    26  var configWithAdditionalLabels = &configpb.ProbeDef{
    27  	AdditionalLabel: []*configpb.AdditionalLabel{
    28  		{
    29  			Key:   proto.String("src_zone"),
    30  			Value: proto.String("zoneA"),
    31  		},
    32  		{
    33  			Key:   proto.String("dst_zone"),
    34  			Value: proto.String("@target.label.zone@"),
    35  		},
    36  		{
    37  			Key:   proto.String("dst_zone_label"),
    38  			Value: proto.String("zone:@target.label.zone@"),
    39  		},
    40  		{
    41  			Key:   proto.String("dst_name"),
    42  			Value: proto.String("@target.name@"),
    43  		},
    44  		{
    45  			Key:   proto.String("dst"),
    46  			Value: proto.String("@target.label.zone@:@target.name@:@target.port@"),
    47  		},
    48  		{
    49  			Key:   proto.String("bad_label"),
    50  			Value: proto.String("@target.metadata@:@unknown@"),
    51  		},
    52  		{
    53  			Key:   proto.String("incomplete_label"),
    54  			Value: proto.String("@target.label.zone@:@target.name"),
    55  		},
    56  	},
    57  }
    58  
    59  func TestParseAdditionalLabel(t *testing.T) {
    60  	expected := []*AdditionalLabel{
    61  		{
    62  			Key:         "src_zone",
    63  			staticValue: "zoneA",
    64  			valueParts:  []string{"zoneA"},
    65  		},
    66  		{
    67  			Key:        "dst_zone",
    68  			valueParts: []string{"", "target.label.zone", ""},
    69  			tokens:     []targetToken{{tokenType: label, labelKey: "zone"}},
    70  		},
    71  		{
    72  			Key:        "dst_zone_label",
    73  			valueParts: []string{"zone:", "target.label.zone", ""},
    74  			tokens:     []targetToken{{tokenType: label, labelKey: "zone"}},
    75  		},
    76  		{
    77  			Key:        "dst_name",
    78  			valueParts: []string{"", "target.name", ""},
    79  			tokens:     []targetToken{{tokenType: name}},
    80  		},
    81  		{
    82  			Key:        "dst",
    83  			valueParts: []string{"", "target.label.zone", ":", "target.name", ":", "target.port", ""},
    84  			tokens:     []targetToken{{tokenType: label, labelKey: "zone"}, {tokenType: name}, {tokenType: port}},
    85  		},
    86  		{
    87  			Key:         "bad_label",
    88  			staticValue: "@target.metadata@:@unknown@",
    89  			valueParts:  []string{"", "target.metadata", ":", "unknown", ""},
    90  		},
    91  		{
    92  			Key:        "incomplete_label",
    93  			valueParts: []string{"", "target.label.zone", ":", "@target.name"},
    94  			tokens:     []targetToken{{tokenType: label, labelKey: "zone"}},
    95  		},
    96  	}
    97  
    98  	for i, alpb := range configWithAdditionalLabels.GetAdditionalLabel() {
    99  		t.Run(alpb.GetKey(), func(t *testing.T) {
   100  			al := ParseAdditionalLabel(alpb)
   101  			if !reflect.DeepEqual(al, expected[i]) {
   102  				t.Errorf("Additional labels not parsed correctly. Got=\n%#v\nWanted=\n%#v", al, expected[i])
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func TestUpdateAdditionalLabel(t *testing.T) {
   109  	aLabels := parseAdditionalLabels(configWithAdditionalLabels)
   110  
   111  	// Verify that we got the correct additional lables and also update them while
   112  	// iterating over them.
   113  	for _, al := range aLabels {
   114  		al.UpdateForTarget(endpoint.Endpoint{Name: "target1", Labels: map[string]string{}, Port: 80})
   115  		al.UpdateForTarget(endpoint.Endpoint{Name: "target2", Labels: map[string]string{"zone": "zoneB"}, Port: 8080})
   116  	}
   117  
   118  	expectedLabels := map[string][][2]string{
   119  		"target1": {
   120  			{"src_zone", "zoneA"},
   121  			{"dst_zone", ""},
   122  			{"dst_zone_label", "zone:"},
   123  			{"dst_name", "target1"},
   124  			{"dst", ":target1:80"},
   125  			{"bad_label", "@target.metadata@:@unknown@"},
   126  			{"incomplete_label", ":@target.name"},
   127  		},
   128  		"target2": {
   129  			{"src_zone", "zoneA"},
   130  			{"dst_zone", "zoneB"},
   131  			{"dst_zone_label", "zone:zoneB"},
   132  			{"dst_name", "target2"},
   133  			{"dst", "zoneB:target2:8080"},
   134  			{"bad_label", "@target.metadata@:@unknown@"},
   135  			{"incomplete_label", "zoneB:@target.name"},
   136  		},
   137  	}
   138  
   139  	for target, labels := range expectedLabels {
   140  		var gotLabels [][2]string
   141  
   142  		for _, al := range aLabels {
   143  			k, v := al.KeyValueForTarget(target)
   144  			gotLabels = append(gotLabels, [2]string{k, v})
   145  		}
   146  
   147  		if !reflect.DeepEqual(gotLabels, labels) {
   148  			t.Errorf("Didn't get expected labels for the target: %s. Got=%v, Expected=%v", target, gotLabels, labels)
   149  		}
   150  	}
   151  }