Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / vmapi / tests.py @ 0c09b1c0

History | View | Annotate | Download (4.3 kB)

1 724e17a6 Kostas Papadimitriou
# -*- coding: utf8 -*-
2 724e17a6 Kostas Papadimitriou
# Copyright 2012 GRNET S.A. All rights reserved.
3 724e17a6 Kostas Papadimitriou
#
4 724e17a6 Kostas Papadimitriou
# Redistribution and use in source and binary forms, with or
5 724e17a6 Kostas Papadimitriou
# without modification, are permitted provided that the following
6 724e17a6 Kostas Papadimitriou
# conditions are met:
7 724e17a6 Kostas Papadimitriou
#
8 724e17a6 Kostas Papadimitriou
#   1. Redistributions of source code must retain the above
9 724e17a6 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
10 724e17a6 Kostas Papadimitriou
#      disclaimer.
11 724e17a6 Kostas Papadimitriou
#
12 724e17a6 Kostas Papadimitriou
#   2. Redistributions in binary form must reproduce the above
13 724e17a6 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
14 724e17a6 Kostas Papadimitriou
#      disclaimer in the documentation and/or other materials
15 724e17a6 Kostas Papadimitriou
#      provided with the distribution.
16 724e17a6 Kostas Papadimitriou
#
17 724e17a6 Kostas Papadimitriou
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 724e17a6 Kostas Papadimitriou
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 724e17a6 Kostas Papadimitriou
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 724e17a6 Kostas Papadimitriou
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 724e17a6 Kostas Papadimitriou
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 724e17a6 Kostas Papadimitriou
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 724e17a6 Kostas Papadimitriou
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 724e17a6 Kostas Papadimitriou
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 724e17a6 Kostas Papadimitriou
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 724e17a6 Kostas Papadimitriou
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 724e17a6 Kostas Papadimitriou
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 724e17a6 Kostas Papadimitriou
# POSSIBILITY OF SUCH DAMAGE.
29 724e17a6 Kostas Papadimitriou
#
30 724e17a6 Kostas Papadimitriou
# The views and conclusions contained in the software and
31 724e17a6 Kostas Papadimitriou
# documentation are those of the authors and should not be
32 724e17a6 Kostas Papadimitriou
# interpreted as representing official policies, either expressed
33 724e17a6 Kostas Papadimitriou
# or implied, of GRNET S.A.
34 724e17a6 Kostas Papadimitriou
35 724e17a6 Kostas Papadimitriou
from django.test import TestCase
36 724e17a6 Kostas Papadimitriou
from django.utils import simplejson as json
37 724e17a6 Kostas Papadimitriou
38 d2c9adac Christos Stavrakakis
from synnefo.lib import join_urls
39 a9e5e76a Kostas Papadimitriou
from synnefo.vmapi import settings
40 a9e5e76a Kostas Papadimitriou
41 5b6af211 Georgios D. Tsoukalas
from synnefo.cyclades_settings import cyclades_services, BASE_HOST
42 5b6af211 Georgios D. Tsoukalas
from synnefo.lib.services import get_service_path
43 5b6af211 Georgios D. Tsoukalas
from synnefo.lib import join_urls
44 5b6af211 Georgios D. Tsoukalas
45 5b6af211 Georgios D. Tsoukalas
46 5b6af211 Georgios D. Tsoukalas
class VMAPITest(TestCase):
47 5b6af211 Georgios D. Tsoukalas
    def setUp(self, *args, **kwargs):
48 5b6af211 Georgios D. Tsoukalas
        super(VMAPITest, self).setUp(*args, **kwargs)
49 5b6af211 Georgios D. Tsoukalas
        self.api_path = get_service_path(cyclades_services, 'vmapi',
50 5b6af211 Georgios D. Tsoukalas
                                         version='v1.0')
51 5b6af211 Georgios D. Tsoukalas
    def myget(self, path, *args, **kwargs):
52 5b6af211 Georgios D. Tsoukalas
        path = join_urls(self.api_path, path)
53 5b6af211 Georgios D. Tsoukalas
        return self.client.get(path, *args, **kwargs)
54 5b6af211 Georgios D. Tsoukalas
55 5b6af211 Georgios D. Tsoukalas
    def myput(self, path, *args, **kwargs):
56 5b6af211 Georgios D. Tsoukalas
        path = join_urls(self.api_path, path)
57 5b6af211 Georgios D. Tsoukalas
        return self.client.put(path, *args, **kwargs)
58 5b6af211 Georgios D. Tsoukalas
59 5b6af211 Georgios D. Tsoukalas
    def mypost(self, path, *args, **kwargs):
60 5b6af211 Georgios D. Tsoukalas
        path = join_urls(self.api_path, path)
61 5b6af211 Georgios D. Tsoukalas
        return self.client.post(path, *args, **kwargs)
62 5b6af211 Georgios D. Tsoukalas
63 5b6af211 Georgios D. Tsoukalas
    def mydelete(self, path, *args, **kwargs):
64 5b6af211 Georgios D. Tsoukalas
        path = join_urls(self.api_path, path)
65 5b6af211 Georgios D. Tsoukalas
        return self.client.delete(path, *args, **kwargs)
66 5b6af211 Georgios D. Tsoukalas
67 68b952f9 Christos Stavrakakis
68 5b6af211 Georgios D. Tsoukalas
class TestServerParams(VMAPITest):
69 724e17a6 Kostas Papadimitriou
70 724e17a6 Kostas Papadimitriou
    def test_cache_backend(self):
71 027e437a Kostas Papadimitriou
        from synnefo.vmapi import backend
72 724e17a6 Kostas Papadimitriou
        backend.set("test", 1)
73 724e17a6 Kostas Papadimitriou
        self.assertEqual(backend.get("test"), 1)
74 724e17a6 Kostas Papadimitriou
        backend.set("test", None)
75 724e17a6 Kostas Papadimitriou
        self.assertEqual(backend.get("test"), None)
76 724e17a6 Kostas Papadimitriou
77 724e17a6 Kostas Papadimitriou
    def test_get_key(self):
78 027e437a Kostas Papadimitriou
        from synnefo.vmapi import get_key
79 027e437a Kostas Papadimitriou
        self.assertEqual(get_key("snf-1", "12345"), "vmapi_snf-1_12345")
80 027e437a Kostas Papadimitriou
        self.assertEqual(get_key("snf-1", None, "12345"), "vmapi_snf-1_12345")
81 724e17a6 Kostas Papadimitriou
82 724e17a6 Kostas Papadimitriou
    def test_params_create(self):
83 027e437a Kostas Papadimitriou
        from synnefo.vmapi.models import create_server_params
84 027e437a Kostas Papadimitriou
        from synnefo.vmapi import backend
85 724e17a6 Kostas Papadimitriou
        try:
86 724e17a6 Kostas Papadimitriou
            from synnefo.db.models import VirtualMachine
87 724e17a6 Kostas Papadimitriou
        except ImportError:
88 724e17a6 Kostas Papadimitriou
            print "Skipping test_params_create"
89 724e17a6 Kostas Papadimitriou
            return
90 724e17a6 Kostas Papadimitriou
91 724e17a6 Kostas Papadimitriou
        # mimic server creation signal called
92 724e17a6 Kostas Papadimitriou
        vm = VirtualMachine()
93 724e17a6 Kostas Papadimitriou
        params = {'password': 'X^942Jjfdsa', 'personality': {}}
94 724e17a6 Kostas Papadimitriou
        uuid = create_server_params(sender=vm, created_vm_params=params)
95 724e17a6 Kostas Papadimitriou
96 b5eef933 Christos Stavrakakis
        self.assertEqual(vm.config_url,
97 5b6af211 Georgios D. Tsoukalas
                         join_urls(BASE_HOST, self.api_path,
98 5b6af211 Georgios D. Tsoukalas
                                   'server-params/%s' % uuid))
99 027e437a Kostas Papadimitriou
        key = "vmapi_%s" % uuid
100 724e17a6 Kostas Papadimitriou
        self.assertEqual(type(backend.get(key)), str)
101 724e17a6 Kostas Papadimitriou
        data = json.loads(backend.get(key))
102 724e17a6 Kostas Papadimitriou
        self.assertEqual('password' in data, True)
103 724e17a6 Kostas Papadimitriou
        self.assertEqual('personality' in data, True)
104 724e17a6 Kostas Papadimitriou
        self.assertEqual(data.get('password'), 'X^942Jjfdsa')
105 724e17a6 Kostas Papadimitriou
106 5b6af211 Georgios D. Tsoukalas
        response = self.myget('server-params/%s' % uuid)
107 a9e5e76a Kostas Papadimitriou
        self.assertEqual(response.status_code, 200)
108 5b6af211 Georgios D. Tsoukalas
        response = self.myget('server-params/%s' % uuid)
109 a9e5e76a Kostas Papadimitriou
        self.assertEqual(response.status_code, 404)