test_provisioner.py 12.8 KB
Newer Older
Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
1 2 3 4
import mock

from fokia.provisioner import Provisioner

Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
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
test_flavors = [{u'SNF:allow_create': True,
                 u'SNF:disk_template': u'drbd',
                 u'SNF:volume_type': 1,
                 u'disk': 20,
                 u'id': 1,
                 u'links': [{u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/flavors/1',
                             u'rel': u'self'},
                            {u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/flavors/1',
                             u'rel': u'bookmark'}],
                 u'name': u'C1R1024D20drbd',
                 u'ram': 1024,
                 u'vcpus': 1},
                {u'SNF:allow_create': True,
                 u'SNF:disk_template': u'drbd',
                 u'SNF:volume_type': 1,
                 u'disk': 40,
                 u'id': 3,
                 u'links': [{u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/flavors/3',
                             u'rel': u'self'},
                            {u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/flavors/3',
                             u'rel': u'bookmark'}],
                 u'name': u'C1R1024D40drbd',
                 u'ram': 1024,
                 u'vcpus': 1},
                {u'SNF:allow_create': True,
                 u'SNF:disk_template': u'drbd',
                 u'SNF:volume_type': 1,
                 u'disk': 20,
                 u'id': 4,
                 u'links': [{u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/flavors/4',
                             u'rel': u'self'},
                            {u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/flavors/4',
                             u'rel': u'bookmark'}],
                 u'name': u'C1R2048D20drbd',
                 u'ram': 2048,
                 u'vcpus': 1}]
Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
41

Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
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
test_images = [{u'created': u'2015-06-26T11:29:59+00:00',
                u'id': u'0035ac89-a86e-4108-93e8-93e294b74a3d',
                u'is_snapshot': False,
                u'links': [{
                    u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/images/0035ac89-a86e-4108-93e8-93e294b74a3d',
                    u'rel': u'self'},
                    {
                        u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/images/0035ac89-a86e-4108-93e8-93e294b74a3d',
                        u'rel': u'bookmark'},
                    {
                        u'href': u'https://cyclades.okeanos.grnet.gr/image/v1.0/images/0035ac89-a86e-4108-93e8-93e294b74a3d',
                        u'rel': u'alternate'}],
                u'metadata': {
                    u'description': u'ArchLinux 2015.06.26 with openssh and iptables setup - accepting only PubKeyAuth',
                    u'gui': u'No GUI',
                    u'kernel': u'4.0.6-1',
                    u'os': u'archlinux',
                    u'osfamily': u'linux',
                    u'partition_table': u'gpt',
                    u'root_partition': u'1',
                    u'sortorder': u'7400000',
                    u'users': u'root'},
                u'name': u'archlinux-2015.06.26',
                u'progress': 100,
                u'public': True,
                u'status': u'ACTIVE',
                u'tenant_id': u'7e271a0b-1427-45ec-9916-f8e72bfbf3d4',
                u'updated': u'2015-06-26T11:29:59+00:00',
                u'user_id': u'7e271a0b-1427-45ec-9916-f8e72bfbf3d4'},
               {u'created': u'2014-03-11T12:55:07+00:00',
                u'id': u'0099593e-4f6d-48bf-8f03-0cec7fabb05b',
                u'is_snapshot': False,
                u'links': [{
                    u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/images/0099593e-4f6d-48bf-8f03-0cec7fabb05b',
                    u'rel': u'self'},
                    {
                        u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/images/0099593e-4f6d-48bf-8f03-0cec7fabb05b',
                        u'rel': u'bookmark'},
                    {
                        u'href': u'https://cyclades.okeanos.grnet.gr/image/v1.0/images/0099593e-4f6d-48bf-8f03-0cec7fabb05b',
                        u'rel': u'alternate'}],
                u'metadata': {u'description': u'Master clone',
                              u'os': u'ubuntu',
                              u'osfamily': u'linux',
                              u'partition_table': u'msdos',
                              u'root_partition': u'1',
                              u'users': u'user hduser'},
                u'name': u'master_clone',
                u'progress': 100,
                u'public': True,
                u'status': u'ACTIVE',
                u'tenant_id': u'607bc20e-ae01-46ca-8a7d-d37b92e9908f',
                u'updated': u'2014-03-11T12:55:07+00:00',
                u'user_id': u'607bc20e-ae01-46ca-8a7d-d37b92e9908f'}]

test_projects = [{u'creation_date': u'2015-06-09T09:46:44.327826+00:00',
                  u'description': u'',
                  u'end_date': u'2015-11-30T00:00:00+00:00',
                  u'homepage': u'',
                  u'id': u'6ff62e8e-0ce9-41f7-ad99-13a18ecada5f',
                  u'join_policy': u'moderated',
                  u'leave_policy': u'auto',
                  u'max_members': 9223372036854775807,
                  u'name': u'lambda.grnet.gr',
                  u'owner': u'69c6686c-4e3e-407e-96b4-c21ef7d5def5',
                  u'private': False,
                  u'resources': {u'astakos.pending_app': {u'member_capacity': 0,
                                                          u'project_capacity': 0},
                                 u'cyclades.cpu': {u'member_capacity': 80,
                                                   u'project_capacity': 80},
                                 u'cyclades.disk': {u'member_capacity': 1073741824000,
                                                    u'project_capacity': 1073741824000},
                                 u'cyclades.floating_ip': {u'member_capacity': 10,
                                                           u'project_capacity': 10},
                                 u'cyclades.network.private': {u'member_capacity': 5,
                                                               u'project_capacity': 5},
                                 u'cyclades.ram': {u'member_capacity': 128849018880,
                                                   u'project_capacity': 128849018880},
                                 u'cyclades.vm': {u'member_capacity': 20,
                                                  u'project_capacity': 20},
                                 u'pithos.diskspace': {u'member_capacity': 0,
                                                       u'project_capacity': 0}},
                  u'state': u'active',
                  u'system_project': False}]

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
test_quotas = { '6ff62e8e-0ce9-41f7-ad99-13a18ecada5f':
             {'cyclades.disk':
                  {'project_limit': 1288490188800, 'project_pending': 0, 'project_usage': 64424509440, 'usage': 0, 'limit': 322122547200, 'pending': 0},
              'cyclades.vm':
                  {'project_limit': 60, 'project_pending': 0, 'project_usage': 2, 'usage': 0, 'limit': 15, 'pending': 0},
              'pithos.diskspace':
                  {'project_limit': 429496729600, 'project_pending': 0, 'project_usage': 0, 'usage': 0, 'limit': 107374182400, 'pending': 0},
              'cyclades.ram':
                  {'project_limit': 128849018880, 'project_pending': 0, 'project_usage': 12884901888, 'usage': 0, 'limit': 32212254720, 'pending': 0},
              'cyclades.cpu':
                  {'project_limit': 120, 'project_pending': 0, 'project_usage': 12, 'usage': 0, 'limit': 30, 'pending': 0},
              'cyclades.floating_ip':
                  {'project_limit': 10, 'project_pending': 0, 'project_usage': 6, 'usage': 3, 'limit': 4, 'pending': 0},
              'cyclades.network.private':
                  {'project_limit': 10, 'project_pending': 0, 'project_usage': 7, 'usage': 0, 'limit': 4, 'pending': 0},
              'astakos.pending_app':
                  {'project_limit': 0, 'project_pending': 0, 'project_usage': 0, 'usage': 0, 'limit': 0, 'pending': 0}} }
Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
test_ip = {u'floating_network_id':
           u'2186', u'user_id':
           u'9819231a-e9e2-40f7-93f1-e2e4cb50cc33',
           u'deleted': False, u'tenant_id':
           u'9819231a-e9e2-40f7-93f1-e2e4cb50cc33',
           u'instance_id': None, u'fixed_ip_address': None,
           u'floating_ip_address':
           u'83.212.116.58',
           u'port_id': None,
           u'id': u'684011'}

test_vm = {u'addresses': {}, u'links': [{u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/servers/665007', u'rel': u'self'},
          {u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/servers/665007', u'rel': u'bookmark'}], u'image':
          {u'id': u'0e399015-8723-4c78-8198-75bdf693cdde', u'links': [
                            {u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/images/0e399015-8723-4c78-8198-75bdf693cdde', u'rel': u'self'},
                            {u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/images/0e399015-8723-4c78-8198-75bdf693cdde', u'rel': u'bookmark'},
                            {u'href': u'https://cyclades.okeanos.grnet.gr/image/v1.0/images/0e399015-8723-4c78-8198-75bdf693cdde', u'rel': u'alternate'}]},
           u'suspended': False, u'flavor': {u'id': 3, u'links': [{u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/flavors/3', u'rel': u'self'},
          {u'href': u'https://cyclades.okeanos.grnet.gr/compute/v2.0/flavors/3', u'rel': u'bookmark'}]}, u'id': 665007, u'security_groups': [
          {u'name': u'default'}], u'attachments': [], u'user_id': u'9819231a-e9e2-40f7-93f1-e2e4cb50cc33', u'accessIPv4': u'', u'accessIPv6': u'', u'progress': 0, u'config_drive': u'', u'status': u'BUILD', u'updated': u'2015-07-10T07:13:25.973280+00:00', u'hostId': u'', u'SNF:fqdn': u'snf-665007.vm.okeanos.grnet.gr', u'deleted': False, u'key_name': None, u'name': u'to mikro debian sto livadi', u'adminPass': u'q0WVXWIjc4', u'tenant_id': u'6ff62e8e-0ce9-41f7-ad99-13a18ecada5f', u'created': u'2015-07-10T07:13:24.862714+00:00', u'SNF:task_state': u'BUILDING', u'volumes': [50722], u'diagnostics': [], u'metadata': {u'os': u'debian', u'users': u'root ckaner'}, u'SNF:port_forwarding': {}}

Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
166 167 168 169
def test_find_flavor():
    with mock.patch('fokia.provisioner.astakos'), \
         mock.patch('fokia.provisioner.KamakiConfig'), \
         mock.patch('fokia.provisioner.cyclades'):
Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
170
        provisioner = Provisioner("lambda")
Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
171 172 173 174 175 176 177 178 179 180
        provisioner.astakos.get_projects.return_value = test_projects
        provisioner.cyclades.list_images.return_value = test_images
        provisioner.cyclades.list_flavors.return_value = test_flavors

        provisioner.create_vm(vm_name="tost", project_name="lambda.grnet.gr",
                              project_mode="supahpower", image_name="archlinux")
        provisioner.cyclades.create_server.assert_called_with(
            name='tost', image_id=u'0035ac89-a86e-4108-93e8-93e294b74a3d', flavor_id=3,
            project_id=u'6ff62e8e-0ce9-41f7-ad99-13a18ecada5f', networks=[], personality=[])

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
def test_check_all_resources():
    with mock.patch('fokia.provisioner.astakos'), \
         mock.patch('fokia.provisioner.KamakiConfig'), \
         mock.patch('fokia.provisioner.cyclades'):
        provisioner = Provisioner("lambda")
        provisioner.astakos.get_projects.return_value = test_projects
        provisioner.astakos.get_quotas.return_value = test_quotas
        provisioner.check_all_resources(test_quotas,  project_id=u'6ff62e8e-0ce9-41f7-ad99-13a18ecada5f',
                                                      slaves=2,
                                                      cluster_size=3,
                                                      vcpus=12,
                                                      ram=4096*3,
                                                      disk=180,
                                                      ip_request=1,
                                                      network_request=1)
Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
def test_create_vpn():
    with mock.patch('fokia.provisioner.astakos'), \
         mock.patch('fokia.provisioner.KamakiConfig'), \
         mock.patch('fokia.provisioner.cyclades'):
        provisioner = Provisioner("lambda")
        provisioner.network_client.create_network = test_ip
        provisioner.reserve_ip()

def test_create_vm():
    with mock.patch('fokia.provisioner.astakos'), \
         mock.patch('fokia.provisioner.KamakiConfig'), \
         mock.patch('fokia.provisioner.cyclades'):
        provisioner = Provisioner("lambda")
        provisioner.cyclades.create_server = test_vm

def test_connect_vm():
    with mock.patch('fokia.provisioner.astakos'), \
         mock.patch('fokia.provisioner.KamakiConfig'), \
         mock.patch('fokia.provisioner.cyclades'):
        provisioner = Provisioner("lambda")
        provisioner.network_client.create_port = True

Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
219 220
if __name__ == "__main__":
    test_find_flavor()