github.com/intel/goresctrl@v0.5.0/pkg/rdt/kubernetes_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 rdt
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestContainerClassFromAnnotations(t *testing.T) {
    24  	mockRdt := &control{
    25  		classes: make(map[string]*ctrlGroup),
    26  	}
    27  
    28  	containerName := "test-container"
    29  	containerAnnotations := map[string]string{}
    30  	podAnnotations := map[string]string{}
    31  
    32  	// Helper function for checking test cases
    33  	tc := func(expectError bool, expectedClsName string) {
    34  		cls, err := ContainerClassFromAnnotations(containerName, containerAnnotations, podAnnotations)
    35  		if !expectError && err != nil {
    36  			t.Errorf("unexpected error: %v", err)
    37  		} else if expectError && err == nil {
    38  			t.Errorf("unexpected success setting RDT class to %q", cls)
    39  		} else if cls != expectedClsName {
    40  			t.Errorf("invalid rdt class, expecting %q, got %q", expectedClsName, cls)
    41  		}
    42  	}
    43  
    44  	//
    45  	// 1. Test container annotation
    46  	//
    47  
    48  	// Should succeed when rdt is uninitialized but annotations are empty
    49  	rdt = nil
    50  	tc(false, "")
    51  
    52  	// Should fail when rdt is uninitialized but annotations point to a class
    53  	containerAnnotations = map[string]string{RdtContainerAnnotation: "class-1"}
    54  	podAnnotations = map[string]string{
    55  		RdtPodAnnotationContainerPrefix + containerName: "class-2",
    56  		RdtPodAnnotation: "class-3"}
    57  	tc(true, "")
    58  
    59  	// Mock configured rdt which enables the functionality
    60  	rdt = mockRdt
    61  
    62  	// Should fail with an empty set of classes
    63  	tc(true, "")
    64  
    65  	// Should succeed when the class exists but no configuration has been set ("discovery mode")
    66  	mockRdt.classes = map[string]*ctrlGroup{"": nil, "class-1": nil, "class-2": nil, "class-3": nil}
    67  	tc(false, "class-1")
    68  
    69  	// Should succeed with default class config
    70  	mockRdt.conf.Classes = classSet{"class-1": &classConfig{}, "class-2": &classConfig{}, "class-3": &classConfig{}}
    71  	tc(false, "class-1")
    72  
    73  	// Should fail when container annotation has been denied in class ocnfig
    74  	mockRdt.conf.Classes["class-1"].Kubernetes.DenyContainerAnnotation = true
    75  	tc(true, "")
    76  
    77  	// Test invalid class name
    78  	containerAnnotations[RdtContainerAnnotation] = "foo/bar"
    79  	tc(true, "")
    80  
    81  	//
    82  	// 2. Test per-container Pod annotation
    83  	//
    84  	delete(containerAnnotations, RdtContainerAnnotation)
    85  	tc(false, "class-2")
    86  
    87  	// Should fail when pod annotations for the class are denied
    88  	mockRdt.conf.Classes["class-2"].Kubernetes.DenyPodAnnotation = true
    89  	tc(true, "")
    90  
    91  	//
    92  	// 3. Test pod-wide Pod annotation
    93  	//
    94  	delete(podAnnotations, RdtPodAnnotationContainerPrefix+containerName)
    95  	tc(false, "class-3")
    96  
    97  	// Should fail when pod annotations for the class are denied
    98  	mockRdt.conf.Classes["class-3"].Kubernetes.DenyPodAnnotation = true
    99  	tc(true, "")
   100  
   101  	//
   102  	// Test empty annotations
   103  	//
   104  	delete(podAnnotations, RdtPodAnnotation)
   105  	tc(false, "")
   106  }