k8s.io/kubernetes@v1.29.3/test/e2e/storage/csi_mock/csi_service_account_token.go (about)

     1  /*
     2  Copyright 2022 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 csi_mock
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/onsi/ginkgo/v2"
    24  	storagev1 "k8s.io/api/storage/v1"
    25  	"k8s.io/kubernetes/test/e2e/framework"
    26  	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
    27  	"k8s.io/kubernetes/test/e2e/storage/utils"
    28  	admissionapi "k8s.io/pod-security-admission/api"
    29  	utilptr "k8s.io/utils/pointer"
    30  )
    31  
    32  var _ = utils.SIGDescribe("CSI Mock volume service account token", func() {
    33  	f := framework.NewDefaultFramework("csi-mock-volumes-service-token")
    34  	f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
    35  	m := newMockDriverSetup(f)
    36  
    37  	ginkgo.Context("CSIServiceAccountToken", func() {
    38  		var (
    39  			err error
    40  		)
    41  		tests := []struct {
    42  			desc                  string
    43  			deployCSIDriverObject bool
    44  			tokenRequests         []storagev1.TokenRequest
    45  		}{
    46  			{
    47  				desc:                  "token should not be plumbed down when csiServiceAccountTokenEnabled=false",
    48  				deployCSIDriverObject: true,
    49  				tokenRequests:         nil,
    50  			},
    51  			{
    52  				desc:                  "token should not be plumbed down when CSIDriver is not deployed",
    53  				deployCSIDriverObject: false,
    54  				tokenRequests:         []storagev1.TokenRequest{{}},
    55  			},
    56  			{
    57  				desc:                  "token should be plumbed down when csiServiceAccountTokenEnabled=true",
    58  				deployCSIDriverObject: true,
    59  				tokenRequests:         []storagev1.TokenRequest{{ExpirationSeconds: utilptr.Int64Ptr(60 * 10)}},
    60  			},
    61  		}
    62  		for _, test := range tests {
    63  			test := test
    64  			csiServiceAccountTokenEnabled := test.tokenRequests != nil
    65  			ginkgo.It(test.desc, func(ctx context.Context) {
    66  				m.init(ctx, testParameters{
    67  					registerDriver:    test.deployCSIDriverObject,
    68  					tokenRequests:     test.tokenRequests,
    69  					requiresRepublish: &csiServiceAccountTokenEnabled,
    70  				})
    71  
    72  				ginkgo.DeferCleanup(m.cleanup)
    73  
    74  				_, _, pod := m.createPod(ctx, pvcReference)
    75  				if pod == nil {
    76  					return
    77  				}
    78  				err = e2epod.WaitForPodNameRunningInNamespace(ctx, m.cs, pod.Name, pod.Namespace)
    79  				framework.ExpectNoError(err, "Failed to start pod: %v", err)
    80  
    81  				// sleep to make sure RequiresRepublish triggers more than 1 NodePublishVolume
    82  				numNodePublishVolume := 1
    83  				if test.deployCSIDriverObject && csiServiceAccountTokenEnabled {
    84  					time.Sleep(5 * time.Second)
    85  					numNodePublishVolume = 2
    86  				}
    87  
    88  				ginkgo.By("Deleting the previously created pod")
    89  				err = e2epod.DeletePodWithWait(ctx, m.cs, pod)
    90  				framework.ExpectNoError(err, "while deleting")
    91  
    92  				ginkgo.By("Checking CSI driver logs")
    93  				err = checkPodLogs(ctx, m.driver.GetCalls, pod, false, false, false, test.deployCSIDriverObject && csiServiceAccountTokenEnabled, numNodePublishVolume)
    94  				framework.ExpectNoError(err)
    95  			})
    96  		}
    97  	})
    98  })