k8s.io/kubernetes@v1.29.3/test/e2e/upgrades/storage/persistent_volumes.go (about)

     1  /*
     2  Copyright 2017 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 storage
    18  
    19  import (
    20  	"context"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	utilerrors "k8s.io/apimachinery/pkg/util/errors"
    24  	"k8s.io/kubernetes/test/e2e/framework"
    25  	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
    26  	e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
    27  	e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
    28  	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
    29  	"k8s.io/kubernetes/test/e2e/upgrades"
    30  
    31  	"github.com/onsi/ginkgo/v2"
    32  )
    33  
    34  // PersistentVolumeUpgradeTest test that a pv is available before and after a cluster upgrade.
    35  type PersistentVolumeUpgradeTest struct {
    36  	pvc *v1.PersistentVolumeClaim
    37  }
    38  
    39  // Name returns the tracking name of the test.
    40  func (PersistentVolumeUpgradeTest) Name() string { return "[sig-storage] persistent-volume-upgrade" }
    41  
    42  const (
    43  	pvTestFile string = "/mnt/volume1/pv_upgrade_test"
    44  	pvTestData string = "keep it pv"
    45  	pvWriteCmd string = "echo \"" + pvTestData + "\" > " + pvTestFile
    46  	pvReadCmd  string = "cat " + pvTestFile
    47  )
    48  
    49  // Setup creates a pv and then verifies that a pod can consume it.  The pod writes data to the volume.
    50  func (t *PersistentVolumeUpgradeTest) Setup(ctx context.Context, f *framework.Framework) {
    51  
    52  	var err error
    53  	e2eskipper.SkipUnlessProviderIs("gce", "gke", "openstack", "aws", "vsphere", "azure")
    54  
    55  	ns := f.Namespace.Name
    56  
    57  	ginkgo.By("Creating a PVC")
    58  	pvcConfig := e2epv.PersistentVolumeClaimConfig{
    59  		StorageClassName: nil,
    60  	}
    61  	t.pvc = e2epv.MakePersistentVolumeClaim(pvcConfig, ns)
    62  	t.pvc, err = e2epv.CreatePVC(ctx, f.ClientSet, ns, t.pvc)
    63  	framework.ExpectNoError(err)
    64  
    65  	ginkgo.By("Consuming the PV before upgrade")
    66  	t.testPod(ctx, f, pvWriteCmd+";"+pvReadCmd)
    67  }
    68  
    69  // Test waits for the upgrade to complete, and then verifies that a pod can still consume the pv
    70  // and that the volume data persists.
    71  func (t *PersistentVolumeUpgradeTest) Test(ctx context.Context, f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) {
    72  	<-done
    73  	ginkgo.By("Consuming the PV after upgrade")
    74  	t.testPod(ctx, f, pvReadCmd)
    75  }
    76  
    77  // Teardown cleans up any remaining resources.
    78  func (t *PersistentVolumeUpgradeTest) Teardown(ctx context.Context, f *framework.Framework) {
    79  	errs := e2epv.PVPVCCleanup(ctx, f.ClientSet, f.Namespace.Name, nil, t.pvc)
    80  	if len(errs) > 0 {
    81  		framework.Failf("Failed to delete 1 or more PVs/PVCs. Errors: %v", utilerrors.NewAggregate(errs))
    82  	}
    83  }
    84  
    85  // testPod creates a pod that consumes a pv and prints it out. The output is then verified.
    86  func (t *PersistentVolumeUpgradeTest) testPod(ctx context.Context, f *framework.Framework, cmd string) {
    87  	pod := e2epod.MakePod(f.Namespace.Name, nil, []*v1.PersistentVolumeClaim{t.pvc}, f.NamespacePodSecurityLevel, cmd)
    88  	expectedOutput := []string{pvTestData}
    89  	e2eoutput.TestContainerOutput(ctx, f, "pod consumes pv", pod, 0, expectedOutput)
    90  }