3 # Copyright (C) 2007, 2008 Google Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """Remote API QA tests.
27 from ganeti import utils
28 from ganeti import constants
29 from ganeti import errors
30 from ganeti import serializer
36 from qa_utils import AssertEqual, AssertNotEqual, AssertIn, StartSSH
39 # Create opener which doesn't try to look for proxies.
40 NoProxyOpener = urllib2.build_opener(urllib2.ProxyHandler({}))
43 INSTANCE_FIELDS = ("name", "os", "pnode", "snodes",
45 "disk_template", "disk.sizes",
46 "nic.ips", "nic.macs", "nic.modes", "nic.links",
47 "beparams", "hvparams",
48 "oper_state", "oper_ram", "status", "tags")
50 NODE_FIELDS = ("name", "dtotal", "dfree",
51 "mtotal", "mnode", "mfree",
52 "pinst_cnt", "sinst_cnt", "tags")
54 LIST_FIELDS = ("id", "uri")
58 """Return whether remote API tests should be run.
61 return qa_config.TestEnabled('rapi')
65 master = qa_config.GetMasterNode()
66 host = master["primary"]
67 port = qa_config.get("rapi-port", default=constants.DEFAULT_RAPI_PORT)
69 for uri, verify in uris:
70 assert uri.startswith("/")
72 url = "https://%s:%s%s" % (host, port, uri)
74 print "Testing %s ..." % url
76 response = NoProxyOpener.open(url)
78 AssertEqual(response.info()["Content-type"], "application/json")
80 data = serializer.LoadJson(response.read())
82 if verify is not None:
86 AssertEqual(data, verify)
90 """Testing remote API version.
94 ("/version", constants.RAPI_VERSION),
98 def TestEmptyCluster():
99 """Testing remote API on an empty cluster.
102 master_name = qa_config.GetMasterNode()["primary"]
104 def _VerifyInfo(data):
105 AssertIn("name", data)
106 AssertIn("master", data)
107 AssertEqual(data["master"], master_name)
109 def _VerifyNodes(data):
112 "uri": "/2/nodes/%s" % master_name,
114 AssertIn(master_entry, data)
116 def _VerifyNodesBulk(data):
118 for entry in NODE_FIELDS:
119 AssertIn(entry, node)
123 ("/2/info", _VerifyInfo),
125 ("/2/nodes", _VerifyNodes),
126 ("/2/nodes?bulk=1", _VerifyNodesBulk),
127 ("/2/instances", []),
128 ("/2/instances?bulk=1", []),
133 def TestInstance(instance):
134 """Testing getting instance(s) info via remote API.
137 def _VerifyInstance(data):
138 for entry in INSTANCE_FIELDS:
139 AssertIn(entry, data)
141 def _VerifyInstancesList(data):
142 for instance in data:
143 for entry in LIST_FIELDS:
144 AssertIn(entry, instance)
146 def _VerifyInstancesBulk(data):
147 for instance_data in data:
148 _VerifyInstance(instance_data)
151 ("/2/instances/%s" % instance["name"], _VerifyInstance),
152 ("/2/instances", _VerifyInstancesList),
153 ("/2/instances?bulk=1", _VerifyInstancesBulk),
158 """Testing getting node(s) info via remote API.
161 def _VerifyNode(data):
162 for entry in NODE_FIELDS:
163 AssertIn(entry, data)
165 def _VerifyNodesList(data):
167 for entry in LIST_FIELDS:
168 AssertIn(entry, node)
170 def _VerifyNodesBulk(data):
171 for node_data in data:
172 _VerifyNode(node_data)
175 ("/2/nodes/%s" % node["primary"], _VerifyNode),
176 ("/2/nodes", _VerifyNodesList),
177 ("/2/nodes?bulk=1", _VerifyNodesBulk),
181 def TestTags(kind, name, tags):
182 """Tests .../tags resources.
185 if kind == constants.TAG_CLUSTER:
187 elif kind == constants.TAG_NODE:
188 uri = "/2/nodes/%s/tags" % name
189 elif kind == constants.TAG_INSTANCE:
190 uri = "/2/instances/%s/tags" % name
192 raise errors.ProgrammerError("Unknown tag kind")
194 def _VerifyTags(data):
195 # Create copies to modify
201 AssertEqual(should, returned)