k8s.io/apiserver@v0.31.1/pkg/audit/policy/util_test.go (about)

     1  /*
     2  Copyright 2018 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 policy
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"k8s.io/apiserver/pkg/apis/audit"
    25  )
    26  
    27  func TestInvertStages(t *testing.T) {
    28  	for _, test := range []struct {
    29  		desc           string
    30  		stages         []audit.Stage
    31  		expectedStages []audit.Stage
    32  	}{
    33  		{
    34  			desc: "should remove one",
    35  			stages: []audit.Stage{
    36  				audit.StageResponseStarted,
    37  			},
    38  			expectedStages: []audit.Stage{
    39  				audit.StageRequestReceived,
    40  				audit.StageResponseComplete,
    41  				audit.StagePanic,
    42  			},
    43  		},
    44  		{
    45  			desc: "should remove both",
    46  			stages: []audit.Stage{
    47  				audit.StageResponseStarted,
    48  				audit.StageRequestReceived,
    49  			},
    50  			expectedStages: []audit.Stage{
    51  				audit.StageResponseComplete,
    52  				audit.StagePanic,
    53  			},
    54  		},
    55  		{
    56  			desc:   "should remove none",
    57  			stages: []audit.Stage{},
    58  			expectedStages: []audit.Stage{
    59  				audit.StageResponseComplete,
    60  				audit.StageResponseStarted,
    61  				audit.StageRequestReceived,
    62  				audit.StagePanic,
    63  			},
    64  		},
    65  		{
    66  			desc: "should remove all",
    67  			stages: []audit.Stage{
    68  				audit.StageResponseComplete,
    69  				audit.StageResponseStarted,
    70  				audit.StageRequestReceived,
    71  				audit.StagePanic,
    72  			},
    73  			expectedStages: []audit.Stage{},
    74  		},
    75  	} {
    76  		t.Run(test.desc, func(t *testing.T) {
    77  			e := InvertStages(test.stages)
    78  			require.ElementsMatch(t, e, test.expectedStages)
    79  		})
    80  	}
    81  }