File: test_volume_attachment.py

package info (click to toggle)
python-openstacksdk 4.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 13,904 kB
  • sloc: python: 127,036; sh: 153; makefile: 23
file content (133 lines) | stat: -rw-r--r-- 4,856 bytes parent folder | download | duplicates (2)
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
# 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


class TestServerVolumeAttachment(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.addCleanup(self._delete_server, server)
        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.addCleanup(self._delete_volume, volume)
        self.assertIsInstance(volume, _volume.Volume)
        self.assertEqual(self.volume_name, volume.name)

        self.server = server
        self.volume = volume

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

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

    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))