File: test_volume_attachment.py

package info (click to toggle)
python-openstacksdk 4.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,352 kB
  • sloc: python: 122,960; sh: 153; makefile: 23
file content (153 lines) | stat: -rw-r--r-- 5,028 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from openstack.block_storage.v3 import volume as volume_
from openstack.compute.v2 import server as server_
from openstack.compute.v2 import volume_attachment as volume_attachment_
from openstack.tests.functional.compute import base as ft_base


class TestServerVolumeAttachment(ft_base.BaseComputeTest):
    def setUp(self):
        super().setUp()

        if not self.user_cloud.has_service('block-storage'):
            self.skipTest('block-storage service not supported by cloud')

        self.server_name = self.getUniqueString()
        self.volume_name = self.getUniqueString()

        # create the server and volume

        server = self.user_cloud.compute.create_server(
            name=self.server_name,
            flavor_id=self.flavor.id,
            image_id=self.image.id,
            networks='none',
        )
        self.user_cloud.compute.wait_for_server(
            server,
            wait=self._wait_for_timeout,
        )
        self.assertIsInstance(server, server_.Server)
        self.assertEqual(self.server_name, server.name)

        volume = self.user_cloud.block_storage.create_volume(
            name=self.volume_name,
            size=1,
        )
        self.user_cloud.block_storage.wait_for_status(
            volume,
            status='available',
            wait=self._wait_for_timeout,
        )
        self.assertIsInstance(volume, volume_.Volume)
        self.assertEqual(self.volume_name, volume.name)

        self.server = server
        self.volume = volume

    def tearDown(self):
        self.user_cloud.compute.delete_server(self.server.id)
        self.user_cloud.compute.wait_for_delete(
            self.server,
            wait=self._wait_for_timeout,
        )

        self.user_cloud.block_storage.delete_volume(self.volume.id)
        self.user_cloud.block_storage.wait_for_delete(
            self.volume,
            wait=self._wait_for_timeout,
        )

        super().tearDown()

    def test_volume_attachment(self):
        # create the volume attachment

        volume_attachment = self.user_cloud.compute.create_volume_attachment(
            self.server,
            self.volume,
        )
        self.assertIsInstance(
            volume_attachment,
            volume_attachment_.VolumeAttachment,
        )
        self.user_cloud.block_storage.wait_for_status(
            self.volume,
            status='in-use',
            wait=self._wait_for_timeout,
        )

        # list all attached volume attachments (there should only be one)

        volume_attachments = list(
            self.user_cloud.compute.volume_attachments(self.server)
        )
        self.assertEqual(1, len(volume_attachments))
        self.assertIsInstance(
            volume_attachments[0],
            volume_attachment_.VolumeAttachment,
        )

        # update the volume attachment

        volume_attachment = self.user_cloud.compute.update_volume_attachment(
            self.server,
            self.volume,
            delete_on_termination=True,
        )
        self.assertIsInstance(
            volume_attachment,
            volume_attachment_.VolumeAttachment,
        )

        # retrieve details of the (updated) volume attachment

        volume_attachment = self.user_cloud.compute.get_volume_attachment(
            self.server,
            self.volume,
        )
        self.assertIsInstance(
            volume_attachment,
            volume_attachment_.VolumeAttachment,
        )
        self.assertTrue(volume_attachment.delete_on_termination)

        # delete the volume attachment

        result = self.user_cloud.compute.delete_volume_attachment(
            self.server,
            self.volume,
            ignore_missing=False,
        )
        self.assertIsNone(result)

        self.user_cloud.block_storage.wait_for_status(
            self.volume,
            status='available',
            wait=self._wait_for_timeout,
        )

        # Wait for the attachment to be deleted.
        # This is done to prevent a race between the BDM
        # record being deleted and we trying to delete the server.
        self.user_cloud.compute.wait_for_delete(
            volume_attachment,
            wait=self._wait_for_timeout,
        )

        # Verify the server doesn't have any volume attachment
        volume_attachments = list(
            self.user_cloud.compute.volume_attachments(self.server)
        )
        self.assertEqual(0, len(volume_attachments))