github.com/google/cloudprober@v0.11.3/targets/endpoint/endpoint_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 endpoint
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  )
    21  
    22  func TestEndpointsFromNames(t *testing.T) {
    23  	names := []string{"targetA", "targetB", "targetC"}
    24  	endpoints := EndpointsFromNames(names)
    25  
    26  	for i := range names {
    27  		ep := endpoints[i]
    28  
    29  		if ep.Name != names[i] {
    30  			t.Errorf("Endpoint.Name=%s, want=%s", ep.Name, names[i])
    31  		}
    32  		if ep.Port != 0 {
    33  			t.Errorf("Endpoint.Port=%d, want=0", ep.Port)
    34  		}
    35  		if len(ep.Labels) != 0 {
    36  			t.Errorf("Endpoint.Labels=%v, want={}", ep.Labels)
    37  		}
    38  	}
    39  }
    40  
    41  func TestKey(t *testing.T) {
    42  	for _, test := range []struct {
    43  		name   string
    44  		port   int
    45  		labels map[string]string
    46  		key    string
    47  	}{
    48  		{
    49  			name: "t1",
    50  			port: 80,
    51  			key:  "t1_80",
    52  		},
    53  		{
    54  			name:   "t1",
    55  			port:   80,
    56  			labels: map[string]string{"app": "cloudprober", "dc": "xx"},
    57  			key:    "t1_80_app:cloudprober_dc:xx",
    58  		},
    59  		{
    60  			name:   "t1",
    61  			port:   80,
    62  			labels: map[string]string{"dc": "xx", "app": "cloudprober"},
    63  			key:    "t1_80_app:cloudprober_dc:xx",
    64  		},
    65  	} {
    66  		ep := Endpoint{
    67  			Name:   test.name,
    68  			Port:   test.port,
    69  			Labels: test.labels,
    70  		}
    71  		t.Run(fmt.Sprintf("%v", ep), func(t *testing.T) {
    72  			key := ep.Key()
    73  			if key != test.key {
    74  				t.Errorf("Got key: %s, want: %s", key, test.key)
    75  			}
    76  		})
    77  	}
    78  }