github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/util/label_copier_test.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     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 util
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"k8s.io/heapster/metrics/core"
    22  )
    23  
    24  func TestDefault(t *testing.T) {
    25  	actual := initializeAndCopy(t,
    26  		",",
    27  		[]string{},
    28  		[]string{})
    29  
    30  	expected := map[string]string{
    31  		core.LabelLabels.Key: "colour:red," + core.LabelLabels.Key + ":preorder;configurable,name:bike,price:too_high,weight:10kg",
    32  		"somelabel":          "somevalue",
    33  	}
    34  	assert.Equal(t, expected, actual)
    35  }
    36  
    37  func TestSeparator(t *testing.T) {
    38  	actual := initializeAndCopy(t,
    39  		"-",
    40  		[]string{},
    41  		[]string{})
    42  
    43  	expected := map[string]string{
    44  		core.LabelLabels.Key: "colour:red-" + core.LabelLabels.Key + ":preorder;configurable-name:bike-price:too_high-weight:10kg",
    45  		"somelabel":          "somevalue",
    46  	}
    47  	assert.Equal(t, expected, actual)
    48  }
    49  
    50  func TestStoredLabels(t *testing.T) {
    51  	actual := initializeAndCopy(t,
    52  		",",
    53  		[]string{"name", "price", "copiedlabels=" + core.LabelLabels.Key, "unknown"},
    54  		[]string{})
    55  
    56  	expected := map[string]string{
    57  		"name":               "bike",
    58  		"price":              "too_high",
    59  		"copiedlabels":       "preorder;configurable",
    60  		core.LabelLabels.Key: "colour:red," + core.LabelLabels.Key + ":preorder;configurable,name:bike,price:too_high,weight:10kg",
    61  		"somelabel":          "somevalue",
    62  	}
    63  	assert.Equal(t, expected, actual)
    64  }
    65  
    66  func TestIgnoredLabels(t *testing.T) {
    67  	actual := initializeAndCopy(t,
    68  		",",
    69  		[]string{},
    70  		[]string{"colour", "weight", "unknown"})
    71  
    72  	expected := map[string]string{
    73  		core.LabelLabels.Key: core.LabelLabels.Key + ":preorder;configurable,name:bike,price:too_high",
    74  		"somelabel":          "somevalue",
    75  	}
    76  	assert.Equal(t, expected, actual)
    77  }
    78  
    79  func TestAll(t *testing.T) {
    80  	actual := initializeAndCopy(t,
    81  		"-",
    82  		[]string{"name", "colour", "copiedlabels=" + core.LabelLabels.Key, "price", "weight", "unknown"},
    83  		[]string{"colour", core.LabelLabels.Key, "price", "unknown"})
    84  
    85  	expected := map[string]string{
    86  		"name":               "bike",
    87  		"colour":             "red",
    88  		"price":              "too_high",
    89  		"weight":             "10kg",
    90  		"copiedlabels":       "preorder;configurable",
    91  		core.LabelLabels.Key: "name:bike-weight:10kg",
    92  		"somelabel":          "somevalue",
    93  	}
    94  	assert.Equal(t, expected, actual)
    95  }
    96  
    97  func initializeAndCopy(t *testing.T, separator string, storedLabels []string, ignoredLabels []string) map[string]string {
    98  	lc, err := NewLabelCopier(separator, storedLabels, ignoredLabels)
    99  	if err != nil {
   100  		t.Fatalf("Could not create LabelCopier: %v", err)
   101  	}
   102  
   103  	labels := map[string]string{
   104  		"name":               "bike",
   105  		"colour":             "red",
   106  		"price":              "too_high",
   107  		"weight":             "10kg",
   108  		core.LabelLabels.Key: "preorder;configurable",
   109  	}
   110  
   111  	out := map[string]string{
   112  		"somelabel": "somevalue",
   113  	}
   114  
   115  	lc.Copy(labels, out)
   116  	return out
   117  }