...

Source file src/k8s.io/client-go/tools/cache/index_test.go

Documentation: k8s.io/client-go/tools/cache

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cache
    18  
    19  import (
    20  	"k8s.io/apimachinery/pkg/util/sets"
    21  	"strings"
    22  	"testing"
    23  
    24  	"k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  )
    27  
    28  func testIndexFunc(obj interface{}) ([]string, error) {
    29  	pod := obj.(*v1.Pod)
    30  	return []string{pod.Labels["foo"]}, nil
    31  }
    32  
    33  func TestGetIndexFuncValues(t *testing.T) {
    34  	index := NewIndexer(MetaNamespaceKeyFunc, Indexers{"testmodes": testIndexFunc})
    35  
    36  	pod1 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "one", Labels: map[string]string{"foo": "bar"}}}
    37  	pod2 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "two", Labels: map[string]string{"foo": "bar"}}}
    38  	pod3 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "tre", Labels: map[string]string{"foo": "biz"}}}
    39  
    40  	index.Add(pod1)
    41  	index.Add(pod2)
    42  	index.Add(pod3)
    43  
    44  	keys := index.ListIndexFuncValues("testmodes")
    45  	if len(keys) != 2 {
    46  		t.Errorf("Expected 2 keys but got %v", len(keys))
    47  	}
    48  
    49  	for _, key := range keys {
    50  		if key != "bar" && key != "biz" {
    51  			t.Errorf("Expected only 'bar' or 'biz' but got %s", key)
    52  		}
    53  	}
    54  }
    55  
    56  func testUsersIndexFunc(obj interface{}) ([]string, error) {
    57  	pod := obj.(*v1.Pod)
    58  	usersString := pod.Annotations["users"]
    59  
    60  	return strings.Split(usersString, ","), nil
    61  }
    62  
    63  func TestMultiIndexKeys(t *testing.T) {
    64  	index := NewIndexer(MetaNamespaceKeyFunc, Indexers{"byUser": testUsersIndexFunc})
    65  
    66  	pod1 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "one", Annotations: map[string]string{"users": "ernie,bert"}}}
    67  	pod2 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "two", Annotations: map[string]string{"users": "bert,oscar"}}}
    68  	pod3 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "tre", Annotations: map[string]string{"users": "ernie,elmo"}}}
    69  
    70  	index.Add(pod1)
    71  	index.Add(pod2)
    72  	index.Add(pod3)
    73  
    74  	expected := map[string]sets.String{}
    75  	expected["ernie"] = sets.NewString("one", "tre")
    76  	expected["bert"] = sets.NewString("one", "two")
    77  	expected["elmo"] = sets.NewString("tre")
    78  	expected["oscar"] = sets.NewString("two")
    79  	expected["elmo1"] = sets.NewString()
    80  	{
    81  		for k, v := range expected {
    82  			found := sets.String{}
    83  			indexResults, err := index.ByIndex("byUser", k)
    84  			if err != nil {
    85  				t.Errorf("Unexpected error %v", err)
    86  			}
    87  			for _, item := range indexResults {
    88  				found.Insert(item.(*v1.Pod).Name)
    89  			}
    90  			if !found.Equal(v) {
    91  				t.Errorf("missing items, index %s, expected %v but found %v", k, v.List(), found.List())
    92  			}
    93  		}
    94  	}
    95  
    96  	index.Delete(pod3)
    97  	erniePods, err := index.ByIndex("byUser", "ernie")
    98  	if err != nil {
    99  		t.Errorf("unexpected error: %v", err)
   100  	}
   101  	if len(erniePods) != 1 {
   102  		t.Errorf("Expected 1 pods but got %v", len(erniePods))
   103  	}
   104  	for _, erniePod := range erniePods {
   105  		if erniePod.(*v1.Pod).Name != "one" {
   106  			t.Errorf("Expected only 'one' but got %s", erniePod.(*v1.Pod).Name)
   107  		}
   108  	}
   109  
   110  	elmoPods, err := index.ByIndex("byUser", "elmo")
   111  	if err != nil {
   112  		t.Errorf("unexpected error: %v", err)
   113  	}
   114  	if len(elmoPods) != 0 {
   115  		t.Errorf("Expected 0 pods but got %v", len(elmoPods))
   116  	}
   117  
   118  	copyOfPod2 := pod2.DeepCopy()
   119  	copyOfPod2.Annotations["users"] = "oscar"
   120  	index.Update(copyOfPod2)
   121  	bertPods, err := index.ByIndex("byUser", "bert")
   122  	if err != nil {
   123  		t.Errorf("unexpected error: %v", err)
   124  	}
   125  	if len(bertPods) != 1 {
   126  		t.Errorf("Expected 1 pods but got %v", len(bertPods))
   127  	}
   128  	for _, bertPod := range bertPods {
   129  		if bertPod.(*v1.Pod).Name != "one" {
   130  			t.Errorf("Expected only 'one' but got %s", bertPod.(*v1.Pod).Name)
   131  		}
   132  	}
   133  }
   134  

View as plain text