github.com/kubewharf/katalyst-core@v0.5.3/cmd/katalyst-controller/app/options/controllerbase_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 options
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	v1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  func TestWorkloadProfilingOptions(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	workload := &v1.Pod{
    31  		ObjectMeta: metav1.ObjectMeta{
    32  			Name:      "pod",
    33  			Namespace: "ns",
    34  			Annotations: map[string]string{
    35  				"aa": "bb",
    36  				"cc": "dd",
    37  				"ee": "",
    38  			},
    39  			Labels: map[string]string{
    40  				"11": "22",
    41  				"33": "44",
    42  				"55": "",
    43  			},
    44  		},
    45  	}
    46  
    47  	tests := []struct {
    48  		name    string
    49  		options WorkloadProfilingOptions
    50  		matched bool
    51  	}{
    52  		{
    53  			name:    "1",
    54  			options: WorkloadProfilingOptions{},
    55  			matched: false,
    56  		},
    57  		{
    58  			name: "2",
    59  			options: WorkloadProfilingOptions{
    60  				ExplicitChecking: true,
    61  				AntiNamespaces:   []string{"ns"},
    62  			},
    63  			matched: false,
    64  		},
    65  		{
    66  			name: "3",
    67  			options: WorkloadProfilingOptions{
    68  				ExplicitChecking: true,
    69  				Namespaces:       []string{"ns"},
    70  				AnnoSelector:     "aa=bb",
    71  				LabelSelector:    "11=22",
    72  			},
    73  			matched: true,
    74  		},
    75  		{
    76  			name: "4",
    77  			options: WorkloadProfilingOptions{
    78  				ExplicitChecking: true,
    79  				Namespaces:       []string{"ns"},
    80  				AnnoSelector:     "aa!=bb",
    81  				LabelSelector:    "11=22",
    82  			},
    83  			matched: false,
    84  		},
    85  		{
    86  			name: "5",
    87  			options: WorkloadProfilingOptions{
    88  				ExplicitChecking: true,
    89  				Namespaces:       []string{"ns"},
    90  				AnnoSelector:     "ff!=gg",
    91  				LabelSelector:    "11=22",
    92  			},
    93  			matched: true,
    94  		},
    95  		{
    96  			name: "6",
    97  			options: WorkloadProfilingOptions{
    98  				ExplicitChecking: true,
    99  				AnnoSelector:     "aa=bb,cc=dd",
   100  				LabelSelector:    "11=22,33=44",
   101  			},
   102  			matched: true,
   103  		},
   104  		{
   105  			name: "7",
   106  			options: WorkloadProfilingOptions{
   107  				ExplicitChecking: true,
   108  				AnnoSelector:     "aa=bb,cc=ff",
   109  				LabelSelector:    "11=22",
   110  			},
   111  			matched: false,
   112  		},
   113  		{
   114  			name: "8",
   115  			options: WorkloadProfilingOptions{
   116  				ExplicitChecking: true,
   117  				AnnoSelector:     "aa=bb,ee",
   118  				LabelSelector:    "11=22,55",
   119  			},
   120  			matched: true,
   121  		},
   122  		{
   123  			name: "9",
   124  			options: WorkloadProfilingOptions{
   125  				ExplicitChecking: true,
   126  				AnnoSelector:     "aa=bb,ff",
   127  				LabelSelector:    "11=22,55",
   128  			},
   129  			matched: false,
   130  		},
   131  		{
   132  			name: "10",
   133  			options: WorkloadProfilingOptions{
   134  				ExplicitChecking: true,
   135  				AnnoSelector:     "aa",
   136  				LabelSelector:    "11=22,55",
   137  			},
   138  			matched: true,
   139  		},
   140  		{
   141  			name: "11",
   142  			options: WorkloadProfilingOptions{
   143  				ExplicitChecking: true,
   144  				AnnoSelector:     "aa=bb,ee=",
   145  				LabelSelector:    "11=22,55",
   146  			},
   147  			matched: true,
   148  		},
   149  		{
   150  			name: "12",
   151  			options: WorkloadProfilingOptions{
   152  				ExplicitChecking: true,
   153  				AnnoSelector:     "aa=bb,ee,ff!=",
   154  				LabelSelector:    "11=22,55",
   155  			},
   156  			matched: true,
   157  		},
   158  		{
   159  			name: "13",
   160  			options: WorkloadProfilingOptions{
   161  				ExplicitChecking: true,
   162  				AnnoSelector:     "aa=bb,ee!=,ff!=",
   163  				LabelSelector:    "11=22,55",
   164  			},
   165  			matched: false,
   166  		},
   167  		{
   168  			name: "14",
   169  			options: WorkloadProfilingOptions{
   170  				ExplicitChecking: true,
   171  				AnnoSelector:     "aa=bb,ee!=",
   172  				LabelSelector:    "11=22",
   173  			},
   174  			matched: false,
   175  		},
   176  		{
   177  			name: "15",
   178  			options: WorkloadProfilingOptions{
   179  				ExplicitChecking: true,
   180  				AnnoSelector:     "aa=bb,!cc",
   181  				LabelSelector:    "11=22",
   182  			},
   183  			matched: false,
   184  		},
   185  		{
   186  			name: "15",
   187  			options: WorkloadProfilingOptions{
   188  				ExplicitChecking: true,
   189  				AnnoSelector:     "aa=bb,!ee",
   190  				LabelSelector:    "11=22",
   191  			},
   192  			matched: false,
   193  		},
   194  		{
   195  			name: "15",
   196  			options: WorkloadProfilingOptions{
   197  				ExplicitChecking: true,
   198  				AnnoSelector:     "aa=bb,!ff",
   199  				LabelSelector:    "11=22",
   200  			},
   201  			matched: true,
   202  		},
   203  	}
   204  	for _, tt := range tests {
   205  		tt := tt
   206  		t.Run(tt.name, func(t *testing.T) {
   207  			t.Parallel()
   208  
   209  			f, ok, err := tt.options.getWorkloadEnableFunc()
   210  			assert.NoError(t, err)
   211  			if ok {
   212  				assert.Equal(t, tt.matched, f(workload))
   213  			}
   214  		})
   215  	}
   216  }