@@ -3,11 +3,14 @@ package v1
33import (
44 "testing"
55
6+ golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
67 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
78 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack"
89 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
10+ "github.com/opentelekomcloud/gophertelekomcloud/openstack/blockstorage/v2/volumes"
911 "github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
1012 "github.com/opentelekomcloud/gophertelekomcloud/openstack/ecs/v1/cloudservers"
13+ "github.com/opentelekomcloud/gophertelekomcloud/openstack/ecs/v1/disk"
1114 "github.com/opentelekomcloud/gophertelekomcloud/openstack/ims/v2/images"
1215 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
1316)
@@ -182,3 +185,85 @@ func TestCloudServersIPV6(t *testing.T) {
182185 }
183186 th .AssertEquals (t , true , ipv6enabled )
184187}
188+
189+ func TestCloudServerVolumeLifecycle (t * testing.T ) {
190+ client , err := clients .NewComputeV1Client ()
191+ th .AssertNoErr (t , err )
192+
193+ clientEvs , err := clients .NewBlockStorageV2Client ()
194+ th .AssertNoErr (t , err )
195+
196+ az := clients .EnvOS .GetEnv ("AVAILABILITY_ZONE" )
197+ if az == "" {
198+ t .Skip ("OS_AVAILABILITY_ZONE env vars is missing but ECSv1 test requires" )
199+ }
200+ createVolumeOpts := volumes.CreateOpts {
201+ Size : 40 ,
202+ Name : tools .RandomString ("tf-evs-disk-" , 4 ),
203+ VolumeType : "SSD" ,
204+ AvailabilityZone : az ,
205+ }
206+
207+ vol , err := volumes .Create (clientEvs , createVolumeOpts ).Extract ()
208+ th .AssertNoErr (t , err )
209+
210+ err = waitForEvsAvailable (clientEvs , 100 , vol .ID )
211+ th .AssertNoErr (t , err )
212+
213+ t .Cleanup (func () {
214+ err = volumes .Delete (clientEvs , vol .ID , volumes.DeleteOpts {}).ExtractErr ()
215+ th .AssertNoErr (t , err )
216+ })
217+
218+ // Get ECSv1 createOpts
219+ createOpts := openstack .GetCloudServerCreateOpts (t )
220+
221+ // Check ECSv1 createOpts
222+ openstack .DryRunCloudServerConfig (t , client , createOpts )
223+ t .Logf ("CreateOpts are ok for creating a cloudServer" )
224+
225+ // Create ECSv1 instance
226+ ecs := openstack .CreateCloudServer (t , client , createOpts )
227+
228+ t .Cleanup (func () {
229+ openstack .DeleteCloudServer (t , client , ecs .ID )
230+ })
231+
232+ t .Logf ("Attaching volume to cloudserver: %s" , vol .ID )
233+ attach , err := disk .Attach (client , disk.CreateOpts {
234+ ServerID : ecs .ID ,
235+ VolumeAttachment : & disk.VolumeAttachment {
236+ VolumeID : vol .ID ,
237+ },
238+ })
239+ th .AssertNoErr (t , err )
240+
241+ err = cloudservers .WaitForJobSuccess (client , 120 , attach .JobID )
242+ th .AssertNoErr (t , err )
243+
244+ t .Logf ("Get all attached volumes to cloudserver: %s" , ecs .ID )
245+ attachments , err := disk .GetAttachments (client , ecs .ID )
246+
247+ tools .PrintResource (t , attachments )
248+
249+ t .Logf ("Force Detaching volume from cloudserver: %s" , vol .ID )
250+ detach , err := disk .Detach (client , ecs .ID , vol .ID , 1 )
251+ th .AssertNoErr (t , err )
252+
253+ err = cloudservers .WaitForJobSuccess (client , 120 , detach .JobID )
254+ th .AssertNoErr (t , err )
255+ }
256+
257+ func waitForEvsAvailable (client * golangsdk.ServiceClient , secs int , volId string ) error {
258+ return golangsdk .WaitFor (secs , func () (bool , error ) {
259+ vol , err := volumes .Get (client , volId ).Extract ()
260+ if err != nil {
261+ return false , err
262+ }
263+
264+ if vol .Status == "available" {
265+ return true , nil
266+ }
267+ return false , nil
268+ })
269+ }
0 commit comments