File: tests.py

package info (click to toggle)
horizon 2012.1.1-10
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: python: 12,012; sh: 471; makefile: 172; xml: 8
file content (156 lines) | stat: -rw-r--r-- 6,353 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
154
155
156
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
#
#    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 copy import deepcopy
from django import http
from django.core.urlresolvers import reverse
from mox import IsA
from novaclient import exceptions as novaclient_exceptions

from horizon import api
from horizon import test


class AdminInstancesAndVolumesViewTest(test.BaseAdminViewTests):
    def test_attached_volume(self):
        """ When the user has admin rights, all volumes are returned by
        api.volume_list(), including those with attachments to
        instances that are not owned by the current tenant.
        """
        instance_not_returned_by_server_list = "5"
        volumes = deepcopy(self.volumes.list())
        attached_volume = deepcopy(self.volumes.list()[0])
        attached_volume.id = "2"
        attached_volume.display_name = "Volume2 name"
        attached_volume.size = "80"
        attached_volume.status = "in-use"
        attached_volume.attachments = [{"server_id":
                                        instance_not_returned_by_server_list,
                                        "device": "/dev/hdn"}]
        volumes.append(attached_volume)

        self.mox.StubOutWithMock(api, 'server_list')
        self.mox.StubOutWithMock(api, 'volume_list')
        api.server_list(IsA(http.HttpRequest)).AndReturn(self.servers.list())
        api.volume_list(IsA(http.HttpRequest)).AndReturn(volumes)

        self.mox.ReplayAll()

        res = self.client.get(
            reverse('horizon:nova:instances_and_volumes:index'))

        self.assertTemplateUsed(res,
            'nova/instances_and_volumes/index.html')
        instances = res.context['instances_table'].data
        resp_volumes = res.context['volumes_table'].data

        self.assertItemsEqual(instances, self.servers.list())
        self.assertItemsEqual(resp_volumes, volumes)

        self.assertContains(res, ">Volume name<", 1, 200)
        self.assertContains(res, ">40 GB<", 1, 200)
        self.assertContains(res, ">Available<", 1, 200)

        self.assertContains(res, ">Volume2 name<", 1, 200)
        self.assertContains(res, ">80 GB<", 1, 200)
        self.assertContains(res, ">In-Use<", 1, 200)
        self.assertContains(res,
                            ">Instance UNKNOWN (" +
                            instance_not_returned_by_server_list +
                            ")</a>&nbsp;on /dev/hdn",
                            1, 200)


class InstancesAndVolumesViewTest(test.TestCase):
    def test_index(self):
        self.mox.StubOutWithMock(api, 'server_list')
        self.mox.StubOutWithMock(api, 'volume_list')
        api.server_list(IsA(http.HttpRequest)).AndReturn(self.servers.list())
        api.volume_list(IsA(http.HttpRequest)).AndReturn(self.volumes.list())

        self.mox.ReplayAll()

        res = self.client.get(
            reverse('horizon:nova:instances_and_volumes:index'))

        self.assertTemplateUsed(res,
            'nova/instances_and_volumes/index.html')
        instances = res.context['instances_table'].data
        volumes = res.context['volumes_table'].data

        self.assertItemsEqual(instances, self.servers.list())
        self.assertItemsEqual(volumes, self.volumes.list())

    def test_attached_volume(self):
        volumes = deepcopy(self.volumes.list())
        attached_volume = deepcopy(self.volumes.list()[0])
        attached_volume.id = "2"
        attached_volume.display_name = "Volume2 name"
        attached_volume.size = "80"
        attached_volume.status = "in-use"
        attached_volume.attachments = [{"server_id": "1",
                                        "device": "/dev/hdn"}]
        volumes.append(attached_volume)

        self.mox.StubOutWithMock(api, 'server_list')
        self.mox.StubOutWithMock(api, 'volume_list')
        api.server_list(IsA(http.HttpRequest)).AndReturn(self.servers.list())
        api.volume_list(IsA(http.HttpRequest)).AndReturn(volumes)

        self.mox.ReplayAll()

        res = self.client.get(
            reverse('horizon:nova:instances_and_volumes:index'))

        self.assertTemplateUsed(res,
            'nova/instances_and_volumes/index.html')
        instances = res.context['instances_table'].data
        resp_volumes = res.context['volumes_table'].data

        self.assertItemsEqual(instances, self.servers.list())
        self.assertItemsEqual(resp_volumes, volumes)

        self.assertContains(res, ">Volume name<", 1, 200)
        self.assertContains(res, ">40 GB<", 1, 200)
        self.assertContains(res, ">Available<", 1, 200)

        self.assertContains(res, ">Volume2 name<", 1, 200)
        self.assertContains(res, ">80 GB<", 1, 200)
        self.assertContains(res, ">In-Use<", 1, 200)
        self.assertContains(res,
                            ">Instance server_1 (1)</a>&nbsp;on /dev/hdn",
                            1, 200)

    def test_index_server_list_exception(self):
        self.mox.StubOutWithMock(api, 'server_list')
        self.mox.StubOutWithMock(api, 'volume_list')
        exception = novaclient_exceptions.ClientException('apiException')
        api.server_list(IsA(http.HttpRequest)).AndRaise(exception)
        api.volume_list(IsA(http.HttpRequest)).AndReturn(self.volumes.list())
        api.server_list(IsA(http.HttpRequest)).AndReturn(self.servers.list())

        self.mox.ReplayAll()

        res = self.client.get(
                reverse('horizon:nova:instances_and_volumes:index'))

        self.assertTemplateUsed(res,
                'nova/instances_and_volumes/index.html')
        self.assertEqual(len(res.context['instances_table'].data), 0)