|
| 1 | +/* |
| 2 | +Copyright 2023 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 it |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "flag" |
| 22 | + "fmt" |
| 23 | + "math/rand" |
| 24 | + "os" |
| 25 | + "path/filepath" |
| 26 | + "testing" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 30 | + . "github.com/onsi/ginkgo/v2" |
| 31 | + . "github.com/onsi/gomega" |
| 32 | + "k8s.io/klog/v2" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + stdVolCap = []*csi.VolumeCapability{ |
| 37 | + { |
| 38 | + AccessType: &csi.VolumeCapability_Mount{ |
| 39 | + Mount: &csi.VolumeCapability_MountVolume{}, |
| 40 | + }, |
| 41 | + AccessMode: &csi.VolumeCapability_AccessMode{ |
| 42 | + Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + stdCapRange = &csi.CapacityRange{RequiredBytes: int64(1 * 1024 * 1024 * 1024)} |
| 47 | + testVolumeName = fmt.Sprintf("ibm-powervs-csi-driver-it-%d", rand.New(rand.NewSource(time.Now().UnixNano())).Uint64()) |
| 48 | +) |
| 49 | + |
| 50 | +func TestIntegration(t *testing.T) { |
| 51 | + flag.Parse() |
| 52 | + RegisterFailHandler(Fail) |
| 53 | + RunSpecs(t, "IBM PowerVS CSI Driver Integration Tests") |
| 54 | +} |
| 55 | + |
| 56 | +var _ = Describe("IBM PowerVS CSI Driver", func() { |
| 57 | + |
| 58 | + It("Should create, attach, stage and mount volume, check if it's writable, unmount, unstage, detach, delete, and check if it's deleted", func() { |
| 59 | + |
| 60 | + testCreateAttachWriteReadDetachDelete() |
| 61 | + |
| 62 | + }) |
| 63 | +}) |
| 64 | + |
| 65 | +func testCreateAttachWriteReadDetachDelete() { |
| 66 | + klog.Infof("Creating volume with name %s", testVolumeName) |
| 67 | + start := time.Now() |
| 68 | + resp, err := csiClient.ctrl.CreateVolume(context.Background(), &csi.CreateVolumeRequest{ |
| 69 | + Name: testVolumeName, |
| 70 | + CapacityRange: stdCapRange, |
| 71 | + VolumeCapabilities: stdVolCap, |
| 72 | + Parameters: nil, |
| 73 | + }) |
| 74 | + Expect(err).To(BeNil(), "error during create volume") |
| 75 | + volume := resp.GetVolume() |
| 76 | + Expect(volume).NotTo(BeNil(), "volume is nil") |
| 77 | + klog.Infof("Created volume %s in %v", volume, time.Since(start)) |
| 78 | + |
| 79 | + defer func() { |
| 80 | + klog.Infof("Deleting volume %s", volume.VolumeId) |
| 81 | + start := time.Now() |
| 82 | + _, err = csiClient.ctrl.DeleteVolume(context.Background(), &csi.DeleteVolumeRequest{VolumeId: volume.VolumeId}) |
| 83 | + Expect(err).To(BeNil(), "error during delete volume") |
| 84 | + klog.Infof("Deleted volume %s in %v", volume.VolumeId, time.Since(start)) |
| 85 | + }() |
| 86 | + |
| 87 | + klog.Info("Running ValidateVolumeCapabilities") |
| 88 | + vcResp, err := csiClient.ctrl.ValidateVolumeCapabilities(context.Background(), &csi.ValidateVolumeCapabilitiesRequest{ |
| 89 | + VolumeId: volume.VolumeId, |
| 90 | + VolumeCapabilities: stdVolCap, |
| 91 | + }) |
| 92 | + Expect(err).To(BeNil()) |
| 93 | + klog.Infof("Ran ValidateVolumeCapabilities with response %v", vcResp) |
| 94 | + |
| 95 | + klog.Info("Running NodeGetInfo") |
| 96 | + niResp, err := csiClient.node.NodeGetInfo(context.Background(), &csi.NodeGetInfoRequest{}) |
| 97 | + Expect(err).To(BeNil()) |
| 98 | + klog.Infof("Ran NodeGetInfo with response %v", niResp) |
| 99 | + |
| 100 | + testAttachStagePublishDetach(volume.VolumeId) |
| 101 | +} |
| 102 | + |
| 103 | +func testAttachStagePublishDetach(volumeID string) { |
| 104 | + klog.Infof("Attaching volume %s to node %s", volumeID, nodeID) |
| 105 | + start := time.Now() |
| 106 | + respAttach, err := csiClient.ctrl.ControllerPublishVolume(context.Background(), &csi.ControllerPublishVolumeRequest{ |
| 107 | + VolumeId: volumeID, |
| 108 | + NodeId: nodeID, |
| 109 | + VolumeCapability: stdVolCap[0], |
| 110 | + }) |
| 111 | + Expect(err).To(BeNil(), "failed attaching volume %s to node %s", volumeID, nodeID) |
| 112 | + // assertAttachmentState(volumeID, "attached") |
| 113 | + klog.Infof("Attached volume with response %v in %v", respAttach.PublishContext, time.Since(start)) |
| 114 | + |
| 115 | + defer func() { |
| 116 | + klog.Infof("Detaching volume %s from node %s", volumeID, nodeID) |
| 117 | + start := time.Now() |
| 118 | + _, err = csiClient.ctrl.ControllerUnpublishVolume(context.Background(), &csi.ControllerUnpublishVolumeRequest{ |
| 119 | + VolumeId: volumeID, |
| 120 | + NodeId: nodeID, |
| 121 | + }) |
| 122 | + Expect(err).To(BeNil(), "failed detaching volume %s from node %s", volumeID, nodeID) |
| 123 | + // assertAttachmentState(volumeID, "detached") |
| 124 | + klog.Infof("Detached volume %s from node %s in %v", volumeID, nodeID, time.Since(start)) |
| 125 | + }() |
| 126 | + |
| 127 | + if os.Getenv("TEST_REMOTE_NODE") == "1" { |
| 128 | + testStagePublish(volumeID, respAttach.PublishContext["wwn"]) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func testStagePublish(volumeID, wwn string) { |
| 133 | + // Node Stage |
| 134 | + volDir := filepath.Join("/tmp/", testVolumeName) |
| 135 | + stageDir := filepath.Join(volDir, "stage") |
| 136 | + klog.Infof("Staging volume %s to path %s", volumeID, stageDir) |
| 137 | + start := time.Now() |
| 138 | + _, err := csiClient.node.NodeStageVolume(context.Background(), &csi.NodeStageVolumeRequest{ |
| 139 | + VolumeId: volumeID, |
| 140 | + StagingTargetPath: stageDir, |
| 141 | + VolumeCapability: stdVolCap[0], |
| 142 | + PublishContext: map[string]string{"wwn": wwn}, |
| 143 | + }) |
| 144 | + Expect(err).To(BeNil(), "failed staging volume %s to path %s", volumeID, stageDir) |
| 145 | + klog.Infof("Staged volume %s to path %s in %v", volumeID, stageDir, time.Since(start)) |
| 146 | + |
| 147 | + defer func() { |
| 148 | + klog.Infof("Unstaging volume %s from path %s", volumeID, stageDir) |
| 149 | + start := time.Now() |
| 150 | + _, err = csiClient.node.NodeUnstageVolume(context.Background(), &csi.NodeUnstageVolumeRequest{VolumeId: volumeID, StagingTargetPath: stageDir}) |
| 151 | + Expect(err).To(BeNil(), "failed unstaging volume %s from path %s", volumeID, stageDir) |
| 152 | + err = os.RemoveAll(volDir) |
| 153 | + Expect(err).To(BeNil(), "failed to remove volume directory") |
| 154 | + klog.Infof("Unstaged volume %s from path %s in %v", volumeID, stageDir, time.Since(start)) |
| 155 | + }() |
| 156 | + |
| 157 | + // Node Publish |
| 158 | + publishDir := filepath.Join("/tmp/", testVolumeName, "mount") |
| 159 | + klog.Infof("Publishing volume %s to path %s", volumeID, publishDir) |
| 160 | + start = time.Now() |
| 161 | + _, err = csiClient.node.NodePublishVolume(context.Background(), &csi.NodePublishVolumeRequest{ |
| 162 | + VolumeId: volumeID, |
| 163 | + StagingTargetPath: stageDir, |
| 164 | + TargetPath: publishDir, |
| 165 | + VolumeCapability: stdVolCap[0], |
| 166 | + PublishContext: map[string]string{"wwn": wwn}, |
| 167 | + }) |
| 168 | + Expect(err).To(BeNil(), "failed publishing volume %s to path %s", volumeID, publishDir) |
| 169 | + klog.Infof("Published volume %s to path %s in %v", volumeID, publishDir, time.Since(start)) |
| 170 | + |
| 171 | + defer func() { |
| 172 | + klog.Infof("Unpublishing volume %s from path %s", volumeID, publishDir) |
| 173 | + start := time.Now() |
| 174 | + _, err = csiClient.node.NodeUnpublishVolume(context.Background(), &csi.NodeUnpublishVolumeRequest{ |
| 175 | + VolumeId: volumeID, |
| 176 | + TargetPath: publishDir, |
| 177 | + }) |
| 178 | + Expect(err).To(BeNil(), "failed unpublishing volume %s from path %s", volumeID, publishDir) |
| 179 | + klog.Infof("Unpublished volume %s from path %s in %v", volumeID, publishDir, time.Since(start)) |
| 180 | + }() |
| 181 | +} |
0 commit comments