@@ -15,9 +15,15 @@ limitations under the License.
15
15
package tests
16
16
17
17
import (
18
+ "path/filepath"
19
+ "strings"
20
+
21
+ csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
18
22
. "github.com/onsi/ginkgo"
19
23
. "github.com/onsi/gomega"
24
+ "k8s.io/apimachinery/pkg/util/uuid"
20
25
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
26
+ gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
21
27
testutils "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e/utils"
22
28
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/remote"
23
29
)
@@ -57,4 +63,153 @@ var _ = Describe("GCE PD CSI Driver Multi-Zone", func() {
57
63
58
64
})
59
65
66
+ It ("Should successfully run through entire lifecycle of an RePD volume on instances in 2 zones" , func () {
67
+ // Create new driver and client
68
+
69
+ Expect (testInstances ).NotTo (BeEmpty ())
70
+
71
+ zoneToContext := map [string ]* remote.TestContext {}
72
+ zones := []string {}
73
+
74
+ for _ , i := range testInstances {
75
+ _ , z , _ := i .GetIdentity ()
76
+ // Zone hasn't been seen before
77
+ if _ , ok := zoneToContext [z ]; ! ok {
78
+ c , err := testutils .GCEClientAndDriverSetup (i )
79
+ Expect (err ).To (BeNil (), "Set up new Driver and Client failed with error" )
80
+ zoneToContext [z ] = c
81
+ zones = append (zones , z )
82
+
83
+ defer func () {
84
+ err := remote .TeardownDriverAndClient (c )
85
+ Expect (err ).To (BeNil (), "Teardown Driver and Client failed with error" )
86
+ }()
87
+ }
88
+ if len (zoneToContext ) == 2 {
89
+ break
90
+ }
91
+ }
92
+
93
+ Expect (len (zoneToContext )).To (Equal (2 ), "Must have instances in exactly 2 zones" )
94
+
95
+ controllerContext := zoneToContext [zones [0 ]]
96
+ controllerClient := controllerContext .Client
97
+ controllerInstance := controllerContext .Instance
98
+
99
+ p , _ , _ := controllerInstance .GetIdentity ()
100
+
101
+ region , err := common .GetRegionFromZones (zones )
102
+ Expect (err ).To (BeNil (), "Failed to get region from zones" )
103
+
104
+ // Create Disk
105
+ volName := testNamePrefix + string (uuid .NewUUID ())
106
+ volId , err := controllerClient .CreateVolume (volName , map [string ]string {
107
+ common .ParameterKeyReplicationType : "regional-pd" ,
108
+ }, defaultSizeGb , & csi.TopologyRequirement {
109
+ Requisite : []* csi.Topology {
110
+ {
111
+ Segments : map [string ]string {common .TopologyKeyZone : zones [0 ]},
112
+ },
113
+ {
114
+ Segments : map [string ]string {common .TopologyKeyZone : zones [1 ]},
115
+ },
116
+ },
117
+ })
118
+ Expect (err ).To (BeNil (), "CreateVolume failed with error: %v" , err )
119
+
120
+ // TODO: Validate Disk Created
121
+ cloudDisk , err := betaComputeService .RegionDisks .Get (p , region , volName ).Do ()
122
+ Expect (err ).To (BeNil (), "Could not get disk from cloud directly" )
123
+ Expect (cloudDisk .Type ).To (ContainSubstring (standardDiskType ))
124
+ Expect (cloudDisk .Status ).To (Equal (readyState ))
125
+ Expect (cloudDisk .SizeGb ).To (Equal (defaultSizeGb ))
126
+ Expect (cloudDisk .Name ).To (Equal (volName ))
127
+
128
+ defer func () {
129
+ // Delete Disk
130
+ controllerClient .DeleteVolume (volId )
131
+ Expect (err ).To (BeNil (), "DeleteVolume failed" )
132
+
133
+ // TODO: Validate Disk Deleted
134
+ _ , err = betaComputeService .RegionDisks .Get (p , region , volName ).Do ()
135
+ Expect (gce .IsGCEError (err , "notFound" )).To (BeTrue (), "Expected disk to not be found" )
136
+ }()
137
+
138
+ // For each of the two instances
139
+ i := 0
140
+ for _ , testContext := range zoneToContext {
141
+ readOnly := false
142
+ if i >= 1 {
143
+ readOnly = true
144
+ }
145
+ testAttachWriteReadDetach (volId , volName , testContext .Instance , testContext .Client , readOnly )
146
+ i = i + 1
147
+ }
148
+
149
+ })
150
+
60
151
})
152
+
153
+ func testAttachWriteReadDetach (volId string , volName string , instance * remote.InstanceInfo , client * remote.CsiClient , readOnly bool ) {
154
+ var err error
155
+
156
+ // Attach Disk
157
+ err = client .ControllerPublishVolume (volId , instance .GetNodeID ())
158
+ Expect (err ).To (BeNil (), "ControllerPublishVolume failed with error" )
159
+
160
+ defer func () {
161
+
162
+ // Detach Disk
163
+ err = client .ControllerUnpublishVolume (volId , instance .GetNodeID ())
164
+ Expect (err ).To (BeNil (), "ControllerUnpublishVolume failed with error" )
165
+ }()
166
+
167
+ // Stage Disk
168
+ stageDir := filepath .Join ("/tmp/" , volName , "stage" )
169
+ client .NodeStageVolume (volId , stageDir )
170
+ Expect (err ).To (BeNil (), "NodeStageVolume failed with error" )
171
+
172
+ defer func () {
173
+ // Unstage Disk
174
+ err = client .NodeUnstageVolume (volId , stageDir )
175
+ Expect (err ).To (BeNil (), "NodeUnstageVolume failed with error" )
176
+ err = testutils .RmAll (instance , filepath .Join ("/tmp/" , volName ))
177
+ Expect (err ).To (BeNil (), "Failed to remove temp directory" )
178
+ }()
179
+
180
+ // Mount Disk
181
+ publishDir := filepath .Join ("/tmp/" , volName , "mount" )
182
+ err = client .NodePublishVolume (volId , stageDir , publishDir )
183
+ Expect (err ).To (BeNil (), "NodePublishVolume failed with error" )
184
+ err = testutils .ForceChmod (instance , filepath .Join ("/tmp/" , volName ), "777" )
185
+ Expect (err ).To (BeNil (), "Chmod failed with error" )
186
+ testFileContents := "test"
187
+ if ! readOnly {
188
+ // Write a file
189
+ testFile := filepath .Join (publishDir , "testfile" )
190
+ err = testutils .WriteFile (instance , testFile , testFileContents )
191
+ Expect (err ).To (BeNil (), "Failed to write file" )
192
+ }
193
+
194
+ // Unmount Disk
195
+ err = client .NodeUnpublishVolume (volId , publishDir )
196
+ Expect (err ).To (BeNil (), "NodeUnpublishVolume failed with error" )
197
+
198
+ // Mount disk somewhere else
199
+ secondPublishDir := filepath .Join ("/tmp/" , volName , "secondmount" )
200
+ err = client .NodePublishVolume (volId , stageDir , secondPublishDir )
201
+ Expect (err ).To (BeNil (), "NodePublishVolume failed with error" )
202
+ err = testutils .ForceChmod (instance , filepath .Join ("/tmp/" , volName ), "777" )
203
+ Expect (err ).To (BeNil (), "Chmod failed with error" )
204
+
205
+ // Read File
206
+ secondTestFile := filepath .Join (secondPublishDir , "testfile" )
207
+ readContents , err := testutils .ReadFile (instance , secondTestFile )
208
+ Expect (err ).To (BeNil (), "ReadFile failed with error" )
209
+ Expect (strings .TrimSpace (string (readContents ))).To (Equal (testFileContents ))
210
+
211
+ // Unmount Disk
212
+ err = client .NodeUnpublishVolume (volId , secondPublishDir )
213
+ Expect (err ).To (BeNil (), "NodeUnpublishVolume failed with error" )
214
+
215
+ }
0 commit comments