github.com/intel/goresctrl@v0.5.0/pkg/kubernetes/annotations_test.go (about)

     1  /*
     2  Copyright 2021 Intel Corporation
     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 kubernetes
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  // TestContainerClassFromAnnotations: unit test for ContainerClassFromAnnotations.
    24  func TestContainerClassFromAnnotations(t *testing.T) {
    25  	aContainerAnnotation := "io.kubernetes.cri.a-class"
    26  	aPodAnnotation := "a.resources.beta.kubernetes.io/pod"
    27  	aPodAnnotationContainerPrefix := "a.resources.beta.kubernetes.io/container."
    28  	bContainerAnnotation := "io.kubernetes.cri.b-class"
    29  	bPodAnnotation := "b.resources.beta.kubernetes.io/pod"
    30  	bPodAnnotationContainerPrefix := "b.resources.beta.kubernetes.io/container."
    31  	allContainerAnnotations := map[string]string{
    32  		aContainerAnnotation: "a-container-class",
    33  		bContainerAnnotation: "b-container-class",
    34  	}
    35  	allPodAnnotations := map[string]string{
    36  		aPodAnnotation: "a-pod-class",
    37  		aPodAnnotationContainerPrefix + "special-container": "a-pod-container-class",
    38  		bPodAnnotation: "b-pod-class",
    39  		bPodAnnotationContainerPrefix + "special-container": "b-pod-container-class",
    40  	}
    41  	tcases := []struct {
    42  		name string // the name of the test case
    43  		// inputs
    44  		lookForCA  string            // container annotation to look for
    45  		lookForPA  string            // pod annotation to look for
    46  		lookForPAC string            // pod annotation container prefix to look for
    47  		cName      string            // container name
    48  		cAnns      map[string]string // container annotations
    49  		pAnns      map[string]string // pod annotations
    50  		// outputs
    51  		expectedClass  string
    52  		expectedOrigin ClassOrigin
    53  	}{
    54  		{
    55  			name:           "all empty",
    56  			expectedOrigin: ClassOriginNotFound,
    57  		},
    58  		{
    59  			name:           "container annotation overrides all pod annotations",
    60  			lookForCA:      aContainerAnnotation,
    61  			lookForPA:      aPodAnnotation,
    62  			lookForPAC:     aPodAnnotationContainerPrefix,
    63  			cName:          "special-container",
    64  			cAnns:          allContainerAnnotations,
    65  			pAnns:          allPodAnnotations,
    66  			expectedClass:  "a-container-class",
    67  			expectedOrigin: ClassOriginContainerAnnotation,
    68  		},
    69  		{
    70  			name:           "container prefix overrides default pod annotation",
    71  			lookForCA:      "not.existing.container.annotation",
    72  			lookForPA:      bPodAnnotation,
    73  			lookForPAC:     bPodAnnotationContainerPrefix,
    74  			cName:          "special-container",
    75  			cAnns:          allContainerAnnotations,
    76  			pAnns:          allPodAnnotations,
    77  			expectedClass:  "b-pod-container-class",
    78  			expectedOrigin: ClassOriginPodAnnotation,
    79  		},
    80  		{
    81  			name:           "default pod annotation",
    82  			lookForCA:      "not.existing.container.annotation",
    83  			lookForPA:      bPodAnnotation,
    84  			lookForPAC:     bPodAnnotationContainerPrefix,
    85  			cName:          "ordinary-container",
    86  			cAnns:          allContainerAnnotations,
    87  			pAnns:          allPodAnnotations,
    88  			expectedClass:  "b-pod-class",
    89  			expectedOrigin: ClassOriginPodAnnotation,
    90  		},
    91  	}
    92  	for _, tc := range tcases {
    93  		t.Run(tc.name, func(t *testing.T) {
    94  			observedClass, observedOrigin := ContainerClassFromAnnotations(
    95  				tc.lookForCA, tc.lookForPA, tc.lookForPAC,
    96  				tc.cName, tc.cAnns, tc.pAnns)
    97  			if observedClass != tc.expectedClass {
    98  				t.Errorf("expected class %q, observed %q", tc.expectedClass, observedClass)
    99  			}
   100  			if observedOrigin != tc.expectedOrigin {
   101  				t.Errorf("expected origin %q, observed %q", tc.expectedOrigin, observedOrigin)
   102  			}
   103  		})
   104  	}
   105  
   106  }