Skip to content

Commit 3e4f1a7

Browse files
committed
Add a e2e test for controller and node expand volume
1 parent f1ad330 commit 3e4f1a7

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

test/e2e/tests/single_zone_e2e_test.go

+111
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ package tests
1717
import (
1818
"context"
1919
"fmt"
20+
"path/filepath"
2021
"strings"
2122
"time"
2223

2324
"k8s.io/apimachinery/pkg/util/uuid"
2425
"k8s.io/apimachinery/pkg/util/wait"
26+
"k8s.io/klog"
2527
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
2628
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
29+
testutils "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e/utils"
2730

2831
csi "github.com/container-storage-interface/spec/lib/go/csi"
2932
. "github.com/onsi/ginkgo"
@@ -98,6 +101,114 @@ var _ = Describe("GCE PD CSI Driver", func() {
98101

99102
})
100103

104+
It("Should resize controller and node", func() {
105+
testContext := getRandomTestContext()
106+
107+
p, z, _ := testContext.Instance.GetIdentity()
108+
client := testContext.Client
109+
instance := testContext.Instance
110+
111+
// Create Disk
112+
volName := testNamePrefix + string(uuid.NewUUID())
113+
volID, err := client.CreateVolume(volName, nil, defaultSizeGb,
114+
&csi.TopologyRequirement{
115+
Requisite: []*csi.Topology{
116+
{
117+
Segments: map[string]string{common.TopologyKeyZone: z},
118+
},
119+
},
120+
})
121+
Expect(err).To(BeNil(), "CreateVolume failed with error: %v", err)
122+
123+
// Validate Disk Created
124+
cloudDisk, err := computeService.Disks.Get(p, z, volName).Do()
125+
Expect(err).To(BeNil(), "Could not get disk from cloud directly")
126+
Expect(cloudDisk.Type).To(ContainSubstring(standardDiskType))
127+
Expect(cloudDisk.Status).To(Equal(readyState))
128+
Expect(cloudDisk.SizeGb).To(Equal(defaultSizeGb))
129+
Expect(cloudDisk.Name).To(Equal(volName))
130+
131+
defer func() {
132+
// Delete Disk
133+
client.DeleteVolume(volID)
134+
Expect(err).To(BeNil(), "DeleteVolume failed")
135+
136+
// Validate Disk Deleted
137+
_, err = computeService.Disks.Get(p, z, volName).Do()
138+
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
139+
}()
140+
141+
// Attach Disk
142+
err = client.ControllerPublishVolume(volID, instance.GetNodeID())
143+
Expect(err).To(BeNil(), "Controller publish volume failed")
144+
145+
defer func() {
146+
// Detach Disk
147+
err = client.ControllerUnpublishVolume(volID, instance.GetNodeID())
148+
if err != nil {
149+
klog.Errorf("Failed to detach disk: %v", err)
150+
}
151+
152+
}()
153+
154+
// Stage Disk
155+
stageDir := filepath.Join("/tmp/", volName, "stage")
156+
err = client.NodeStageVolume(volID, stageDir)
157+
Expect(err).To(BeNil(), "Node Stage volume failed")
158+
159+
defer func() {
160+
// Unstage Disk
161+
err = client.NodeUnstageVolume(volID, stageDir)
162+
if err != nil {
163+
klog.Errorf("Failed to unstage volume: %v", err)
164+
}
165+
fp := filepath.Join("/tmp/", volName)
166+
err = testutils.RmAll(instance, fp)
167+
if err != nil {
168+
klog.Errorf("Failed to rm file path %s: %v", fp, err)
169+
}
170+
}()
171+
172+
// Mount Disk
173+
publishDir := filepath.Join("/tmp/", volName, "mount")
174+
err = client.NodePublishVolume(volID, stageDir, publishDir)
175+
Expect(err).To(BeNil(), "Node publish volume failed")
176+
177+
defer func() {
178+
// Unmount Disk
179+
err = client.NodeUnpublishVolume(volID, publishDir)
180+
if err != nil {
181+
klog.Errorf("NodeUnpublishVolume failed with error: %v", err)
182+
}
183+
}()
184+
185+
// Verify pre-resize fs size
186+
sizeGb, err := testutils.GetFSSizeInGb(instance, publishDir)
187+
Expect(err).To(BeNil(), "Failed to get FSSize in GB")
188+
Expect(sizeGb).To(Equal(defaultSizeGb))
189+
190+
// Resize controller
191+
var newSizeGb int64 = 10
192+
err = client.ControllerExpandVolume(volID, newSizeGb)
193+
194+
Expect(err).To(BeNil(), "Controller expand volume failed")
195+
196+
// Verify cloud size
197+
cloudDisk, err = computeService.Disks.Get(p, z, volName).Do()
198+
Expect(err).To(BeNil(), "Get cloud disk failed")
199+
Expect(cloudDisk.SizeGb).To(Equal(newSizeGb))
200+
201+
// Resize node
202+
err = client.NodeExpandVolume(volID, publishDir, newSizeGb)
203+
Expect(err).To(BeNil(), "Node expand volume failed")
204+
205+
// Verify disk size
206+
sizeGb, err = testutils.GetFSSizeInGb(instance, publishDir)
207+
Expect(err).To(BeNil(), "Failed to get FSSize in GB")
208+
Expect(sizeGb).To(Equal(newSizeGb))
209+
210+
})
211+
101212
It("Should create disks in correct zones when topology is specified", func() {
102213
Expect(testContexts).ToNot(BeEmpty())
103214
testContext := getRandomTestContext()

test/e2e/utils/utils.go

+15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"math/rand"
2121
"os"
2222
"path"
23+
"strconv"
24+
"strings"
2325
"time"
2426

2527
"golang.org/x/oauth2/google"
@@ -167,6 +169,19 @@ func ReadFile(instance *remote.InstanceInfo, filePath string) (string, error) {
167169
return output, nil
168170
}
169171

172+
func GetFSSizeInGb(instance *remote.InstanceInfo, path string) (int64, error) {
173+
output, err := instance.SSHNoSudo("df", "--output=size", "-BG", path, "|", "awk", "'NR==2'")
174+
if err != nil {
175+
return -1, fmt.Errorf("failed to get size of path %s. Output: %v, error: %v", path, output, err)
176+
}
177+
output = strings.TrimSuffix(strings.TrimSpace(output), "G")
178+
n, err := strconv.ParseInt(output, 10, 64)
179+
if err != nil {
180+
return -1, fmt.Errorf("failed to parse size %s into int", output)
181+
}
182+
return n, nil
183+
}
184+
170185
func RmAll(instance *remote.InstanceInfo, filePath string) error {
171186
output, err := instance.SSH("rm", "-rf", filePath)
172187
if err != nil {

test/remote/client-wrappers.go

+23
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ func (c *CsiClient) NodePublishVolume(volumeId, stageDir, publishDir string) err
179179
return err
180180
}
181181

182+
func (c *CsiClient) ControllerExpandVolume(volumeId string, sizeGb int64) error {
183+
controllerExpandReq := &csipb.ControllerExpandVolumeRequest{
184+
VolumeId: volumeId,
185+
CapacityRange: &csipb.CapacityRange{
186+
RequiredBytes: common.GbToBytes(sizeGb),
187+
},
188+
}
189+
_, err := c.ctrlClient.ControllerExpandVolume(context.Background(), controllerExpandReq)
190+
return err
191+
}
192+
193+
func (c *CsiClient) NodeExpandVolume(volumeId, volumePath string, sizeGb int64) error {
194+
nodeExpandReq := &csipb.NodeExpandVolumeRequest{
195+
VolumeId: volumeId,
196+
VolumePath: volumePath,
197+
CapacityRange: &csipb.CapacityRange{
198+
RequiredBytes: common.GbToBytes(sizeGb),
199+
},
200+
}
201+
_, err := c.nodeClient.NodeExpandVolume(context.Background(), nodeExpandReq)
202+
return err
203+
}
204+
182205
func (c *CsiClient) NodeGetInfo() (*csipb.NodeGetInfoResponse, error) {
183206
resp, err := c.nodeClient.NodeGetInfo(context.Background(), &csipb.NodeGetInfoRequest{})
184207
return resp, err

test/remote/ssh.go

+1
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@ func runSSHCommand(cmd string, args ...string) (string, error) {
108108
if err != nil {
109109
return string(output), fmt.Errorf("command [%s %s] failed with error: %v", cmd, strings.Join(args, " "), err)
110110
}
111+
klog.V(4).Infof("SSH command: %v had output: %s", cmd, string(output))
111112
return string(output), nil
112113
}

0 commit comments

Comments
 (0)