Skip to content
Snippets Groups Projects
Commit 2771835c authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

RAPI QA: Use RAPI client


Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 502f5236
No related branches found
No related tags found
No related merge requests found
...@@ -281,10 +281,13 @@ def main(): ...@@ -281,10 +281,13 @@ def main():
rapi_user = "ganeti-qa" rapi_user = "ganeti-qa"
rapi_secret = utils.GenerateSecret() rapi_secret = utils.GenerateSecret()
qa_rapi.OpenerFactory.SetCredentials(rapi_user, rapi_secret)
RunEnvTests() RunEnvTests()
SetupCluster(rapi_user, rapi_secret) SetupCluster(rapi_user, rapi_secret)
# Load RAPI certificate
qa_rapi.Setup(rapi_user, rapi_secret)
RunClusterTests() RunClusterTests()
RunOsTests() RunOsTests()
......
...@@ -22,12 +22,17 @@ ...@@ -22,12 +22,17 @@
""" """
import urllib2 import tempfile
from ganeti import utils from ganeti import utils
from ganeti import constants from ganeti import constants
from ganeti import errors from ganeti import errors
from ganeti import serializer from ganeti import serializer
from ganeti import cli
from ganeti import rapi
import ganeti.rapi.client
import ganeti.rapi.client_utils
import qa_config import qa_config
import qa_utils import qa_utils
...@@ -37,58 +42,38 @@ from qa_utils import (AssertEqual, AssertNotEqual, AssertIn, AssertMatch, ...@@ -37,58 +42,38 @@ from qa_utils import (AssertEqual, AssertNotEqual, AssertIn, AssertMatch,
StartSSH) StartSSH)
class OpenerFactory: _rapi_ca = None
"""A factory singleton to construct urllib opener chain. _rapi_client = None
This is needed because qa_config is not initialized yet at module load time
"""
_opener = None
_rapi_user = None
_rapi_secret = None
@classmethod
def SetCredentials(cls, rapi_user, rapi_secret):
"""Set the credentials for authorized access.
"""
cls._rapi_user = rapi_user
cls._rapi_secret = rapi_secret
@classmethod
def Opener(cls):
"""Construct the opener if not yet done.
""" def Setup(username, password):
if not cls._opener: """Configures the RAPI client.
if not cls._rapi_user or not cls._rapi_secret:
raise errors.ProgrammerError("SetCredentials was never called.")
# Create opener which doesn't try to look for proxies and does auth """
master = qa_config.GetMasterNode() global _rapi_ca
host = master["primary"] global _rapi_client
port = qa_config.get("rapi-port", default=constants.DEFAULT_RAPI_PORT)
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, 'https://%s:%s' % (host, port),
cls._rapi_user,
cls._rapi_secret)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
cls._opener = urllib2.build_opener(urllib2.ProxyHandler({}), authhandler)
return cls._opener master = qa_config.GetMasterNode()
# Load RAPI certificate from master node
cmd = ["cat", constants.RAPI_CERT_FILE]
class RapiRequest(urllib2.Request): # Write to temporary file
"""This class supports other methods beside GET/POST. _rapi_ca = tempfile.NamedTemporaryFile()
_rapi_ca.write(qa_utils.GetCommandOutput(master["primary"],
utils.ShellQuoteArgs(cmd)))
_rapi_ca.flush()
""" port = qa_config.get("rapi-port", default=constants.DEFAULT_RAPI_PORT)
cfg_ssl = rapi.client.CertAuthorityVerify(cafile=_rapi_ca.name)
def __init__(self, method, url, headers, data): _rapi_client = rapi.client.GanetiRapiClient(master["primary"], port=port,
urllib2.Request.__init__(self, url, data=data, headers=headers) username=username,
self._method = method password=password,
config_ssl_verification=cfg_ssl,
ignore_proxy=True)
def get_method(self): print "RAPI protocol version: %s" % _rapi_client.GetVersion()
return self._method
INSTANCE_FIELDS = ("name", "os", "pnode", "snodes", INSTANCE_FIELDS = ("name", "os", "pnode", "snodes",
...@@ -119,43 +104,12 @@ def Enabled(): ...@@ -119,43 +104,12 @@ def Enabled():
def _DoTests(uris): def _DoTests(uris):
master = qa_config.GetMasterNode()
host = master["primary"]
port = qa_config.get("rapi-port", default=constants.DEFAULT_RAPI_PORT)
results = [] results = []
for uri, verify, method, body in uris: for uri, verify, method, body in uris:
assert uri.startswith("/") assert uri.startswith("/")
url = "https://%s:%s%s" % (host, port, uri) data = _rapi_client._SendRequest(method, uri, None, body)
headers = {}
if body:
data = serializer.DumpJson(body, indent=False)
headers["Content-Type"] = "application/json"
else:
data = None
if headers or data:
details = []
if headers:
details.append("headers=%s" %
serializer.DumpJson(headers, indent=False).rstrip())
if data:
details.append("data=%s" % data.rstrip())
info = "(%s)" % (", ".join(details), )
else:
info = ""
print "Testing %s %s %s..." % (method, url, info)
req = RapiRequest(method, url, headers, data)
response = OpenerFactory.Opener().open(req)
AssertEqual(response.info()["Content-type"], "application/json")
data = serializer.LoadJson(response.read())
if verify is not None: if verify is not None:
if callable(verify): if callable(verify):
...@@ -305,10 +259,7 @@ def _WaitForRapiJob(job_id): ...@@ -305,10 +259,7 @@ def _WaitForRapiJob(job_id):
("/2/jobs/%s" % job_id, _VerifyJob, "GET", None), ("/2/jobs/%s" % job_id, _VerifyJob, "GET", None),
]) ])
# FIXME: Use "gnt-job watch" until RAPI supports waiting for job rapi.client_utils.PollJob(_rapi_client, job_id, cli.StdioJobPollReportCb())
cmd = ["gnt-job", "watch", str(job_id)]
AssertEqual(StartSSH(master["primary"],
utils.ShellQuoteArgs(cmd)).wait(), 0)
def TestRapiInstanceAdd(node): def TestRapiInstanceAdd(node):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment