Skip to content

Commit f29a088

Browse files
Enrico Usaienrico-usai
authored andcommitted
Improve method to attach a volume to an instance waiting to be attached
Note: I replaced the attach_volume boto3 call with Volume.attach_to_instance method because it contains the state of the attachment in the response. Signed-off-by: Enrico Usai <[email protected]>
1 parent f886e7a commit f29a088

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

tests/integration-tests/tests/storage/snapshots_factory.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import boto3
1717
from fabric import Connection
1818
from retrying import retry
19+
from time_utils import minutes, seconds
1920
from utils import random_alphanumeric
2021

2122
SnapshotConfig = namedtuple("ClusterConfig", ["ssh_key", "key_name", "vpc_id", "head_node_subnet_id"])
@@ -154,9 +155,19 @@ def _open_ssh_connection(self):
154155
raise ConnectionError()
155156
return ssh_conn
156157

158+
@retry(retry_on_result=lambda state: state != "attached", wait_fixed=seconds(2), stop_max_delay=minutes(5))
159+
def _wait_volume_attached(self):
160+
vol = self.ec2.Volume(self.volume.id)
161+
attachment_state = next(
162+
(attachment["State"] for attachment in vol.attachments if attachment["InstanceId"] == self.instance.id), ""
163+
)
164+
return attachment_state
165+
157166
def _attach_volume(self):
158-
result = self.boto_client.attach_volume(VolumeId=self.volume.id, InstanceId=self.instance.id, Device="/dev/sdf")
167+
result = self.volume.attach_to_instance(InstanceId=self.instance.id, Device="/dev/sdf")
159168
logging.info("Attach Volume Result: %s", result)
169+
self._wait_volume_attached()
170+
logging.info("Volume attached")
160171

161172
def _create_volume(self, subnet):
162173
vol = self.ec2.create_volume(

0 commit comments

Comments
 (0)