Revision e6ce18ac

b/qa/qa_rapi.py
33 33
import qa_utils
34 34
import qa_error
35 35

  
36
from qa_utils import AssertEqual, AssertNotEqual, AssertIn, StartSSH
36
from qa_utils import (AssertEqual, AssertNotEqual, AssertIn, AssertMatch,
37
                      StartSSH)
37 38

  
38 39

  
39
# Create opener which doesn't try to look for proxies.
40
NoProxyOpener = urllib2.build_opener(urllib2.ProxyHandler({}))
40
class OpenerFactory:
41
  """A factory singleton to construct urllib opener chain.
42

  
43
  This is needed because qa_config is not initialized yet at module load time
44

  
45
  """
46
  _opener = None
47

  
48
  @classmethod
49
  def Opener(cls):
50
    """Construct the opener if not yet done.
51

  
52
    """
53
    if not cls._opener:
54
      # Create opener which doesn't try to look for proxies and does auth
55
      master = qa_config.GetMasterNode()
56
      host = master["primary"]
57
      port = qa_config.get("rapi-port", default=constants.DEFAULT_RAPI_PORT)
58
      passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
59
      passman.add_password(None, 'https://%s:%s' % (host, port),
60
                           qa_config.get("rapi-user", default=""),
61
                           qa_config.get("rapi-pass", default=""))
62
      authhandler = urllib2.HTTPBasicAuthHandler(passman)
63
      cls._opener = urllib2.build_opener(urllib2.ProxyHandler({}), authhandler)
64

  
65
    return cls._opener
66

  
67

  
68
class RapiRequest(urllib2.Request):
69
  """This class supports other methods beside GET/POST.
70

  
71
  """
72

  
73
  def __init__(self, url, data=None, headers={}, origin_req_host=None,
74
               unverifiable=False, method="GET"):
75
    urllib2.Request.__init__(self, url, data, headers, origin_req_host,
76
                             unverifiable)
77
    self._method = method
78

  
79
  def get_method(self):
80
    return self._method
41 81

  
42 82

  
43 83
INSTANCE_FIELDS = ("name", "os", "pnode", "snodes",
......
66 106
  host = master["primary"]
67 107
  port = qa_config.get("rapi-port", default=constants.DEFAULT_RAPI_PORT)
68 108

  
69
  for uri, verify in uris:
109
  for uri, verify, method in uris:
70 110
    assert uri.startswith("/")
71 111

  
72 112
    url = "https://%s:%s%s" % (host, port, uri)
73 113

  
74 114
    print "Testing %s ..." % url
75 115

  
76
    response = NoProxyOpener.open(url)
116
    req = RapiRequest(url, method=method)
117
    response = OpenerFactory.Opener().open(req)
77 118

  
78 119
    AssertEqual(response.info()["Content-type"], "application/json")
79 120

  
......
91 132

  
92 133
  """
93 134
  _DoTests([
94
    ("/version", constants.RAPI_VERSION),
135
    ("/version", constants.RAPI_VERSION, 'GET'),
95 136
    ])
96 137

  
97 138

  
......
119 160
        AssertIn(entry, node)
120 161

  
121 162
  _DoTests([
122
    ("/", None),
123
    ("/2/info", _VerifyInfo),
124
    ("/2/tags", None),
125
    ("/2/nodes", _VerifyNodes),
126
    ("/2/nodes?bulk=1", _VerifyNodesBulk),
127
    ("/2/instances", []),
128
    ("/2/instances?bulk=1", []),
129
    ("/2/os", None),
163
    ("/", None, 'GET'),
164
    ("/2/info", _VerifyInfo, 'GET'),
165
    ("/2/tags", None, 'GET'),
166
    ("/2/nodes", _VerifyNodes, 'GET'),
167
    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET'),
168
    ("/2/instances", [], 'GET'),
169
    ("/2/instances?bulk=1", [], 'GET'),
170
    ("/2/os", None, 'GET'),
130 171
    ])
131 172

  
132 173

  
......
147 188
    for instance_data in data:
148 189
      _VerifyInstance(instance_data)
149 190

  
191
  def _VerifyReturnsJob(data):
192
    AssertMatch(data, r'^\d+$')
193

  
150 194
  _DoTests([
151
    ("/2/instances/%s" % instance["name"], _VerifyInstance),
152
    ("/2/instances", _VerifyInstancesList),
153
    ("/2/instances?bulk=1", _VerifyInstancesBulk),
195
    ("/2/instances/%s" % instance["name"], _VerifyInstance, 'GET'),
196
    ("/2/instances", _VerifyInstancesList, 'GET'),
197
    ("/2/instances?bulk=1", _VerifyInstancesBulk, 'GET'),
198
    ("/2/instances/%s/activate-disks" % instance["name"], _VerifyReturnsJob, 'PUT'),
199
    ("/2/instances/%s/deactivate-disks" % instance["name"], _VerifyReturnsJob, 'PUT'),
154 200
    ])
155 201

  
156 202

  
......
172 218
      _VerifyNode(node_data)
173 219

  
174 220
  _DoTests([
175
    ("/2/nodes/%s" % node["primary"], _VerifyNode),
176
    ("/2/nodes", _VerifyNodesList),
177
    ("/2/nodes?bulk=1", _VerifyNodesBulk),
221
    ("/2/nodes/%s" % node["primary"], _VerifyNode, 'GET'),
222
    ("/2/nodes", _VerifyNodesList, 'GET'),
223
    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET'),
178 224
    ])
179 225

  
180 226

  
......
201 247
    AssertEqual(should, returned)
202 248

  
203 249
  _DoTests([
204
    (uri, _VerifyTags),
250
    (uri, _VerifyTags, 'GET'),
205 251
    ])
b/qa/qa_utils.py
24 24
"""
25 25

  
26 26
import os
27
import re
27 28
import sys
28 29
import subprocess
29 30

  
......
92 93
    raise qa_error.Error('%r != %r' % (first, second))
93 94

  
94 95

  
96
def AssertMatch(string, pattern):
97
  """Raises an error when string doesn't match regexp pattern.
98

  
99
  """
100
  if not re.match(pattern, string):
101
    raise qa_error.Error("%r doesn't match /%r/" % (string, pattern))
102

  
103

  
95 104
def GetSSHCommand(node, cmd, strict=True):
96 105
  """Builds SSH command to be executed.
97 106

  

Also available in: Unified diff