def __init__ ( self, config ) : global controller #self.base_uri = 'http://172.24.42.141/api' self.base_uri = config.SFA_CLAB_URL controller = Api(self.base_uri) #(config.CLAP_API_URL) try: controller.retrieve() except requests.exceptions.MissingSchema as e: raise MalformedURI(self.base_uri, e.message) except requests.exceptions.ConnectionError as e: raise UnexistingURI(self.base_uri, e.message) except ValueError: raise InvalidURI(self.base_uri) # Use of a default user for the C-Lab SFAWrap #self.username='******' #self.password='******' #self.groupname='vct' self.username = config.SFA_CLAB_USER self.password = config.SFA_CLAB_PASSWORD self.groupname = config.SFA_CLAB_GROUP controller.login(self.username, self.password) self.default_template = config.SFA_CLAB_DEFAULT_TEMPLATE
def test_retrieve_base(self): group = self.api.groups.retrieve()[0] name = 'RandomNode-%s' % random_ascii(10) self.test_login() node = self.api.nodes.create(name=name, group=group) controller = Api(node.uri) controller.retrieve() self.assertEqual(self.api, controller) self.assertEqual(self.api._data, controller._data)
def test_caching(self): api = Api(os.environ['CONFINE_SERVER_API'], cache=True) api.nodes.retrieve() self.assertEqual(0, api.cache.hits) self.assertEqual(2, api.cache.misses) nodes = api.nodes.retrieve() self.assertEqual(1, api.cache.hits) self.assertEqual(2, api.cache.misses) for node in nodes: node.retrieve() self.assertEqual(len(nodes) + 2, api.cache.misses) self.assertEqual(1, api.cache.hits) for node in nodes: node.retrieve() self.assertEqual(len(nodes) + 1, api.cache.hits) self.assertEqual(len(nodes) + 2, api.cache.misses) api.cache.invalidate() self.assertEqual(0, api.stats['conditional']) for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) api.cache.remove() for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) self.assertEqual((2 * len(nodes)) + 1, api.cache.hits) self.assertEqual((2 * len(nodes)) + 2, api.cache.misses)
def setUp(self): super(BaseLiveServerTestCase, self).setUp() self.rest = Api(self.live_server_url + '/api/') self.account = Account.objects.create(name='orchestra') self.username = '******' self.password = '******' self.user = User.objects.create_superuser(username='******', password='******', email='*****@*****.**', account=self.account)
class BaseLiveServerTestCase(AppDependencyMixin, LiveServerTestCase): @classmethod def setUpClass(cls): cls.vdisplay = Xvfb() cls.vdisplay.start() cls.selenium = WebDriver() super(BaseLiveServerTestCase, cls).setUpClass() @classmethod def tearDownClass(cls): cls.selenium.quit() cls.vdisplay.stop() super(BaseLiveServerTestCase, cls).tearDownClass() def setUp(self): super(BaseLiveServerTestCase, self).setUp() self.rest = Api(self.live_server_url + '/api/') self.account = Account.objects.create(name='orchestra') self.username = '******' self.password = '******' self.user = User.objects.create_superuser(username='******', password='******', email='*****@*****.**', account=self.account) def admin_login(self): session = SessionStore() session[SESSION_KEY] = self.user.pk session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session.save() ## to set a cookie we need to first visit the domain. self.selenium.get(self.live_server_url + '/admin/') self.selenium.add_cookie(dict( name=settings.SESSION_COOKIE_NAME, value=session.session_key, # path='/', )) def rest_login(self): self.rest.login(username=self.username, password=self.password)
class CouchBaseRetriever: def __init__(self): # connection to CouchBase DB self.cb = Couchbase.connect(bucket=config.COUCHBASE_BUCKET, host=config.COUCHBASE_HOST, port=config.COUCHBASE_PORT) # connection to C-Lab Controller API self.api = Api(config.CLAB_CONTROLLER_API) self.api.retrieve() def get_nodes_monitoring_metric(self, metric): view=View(self.cb, config.DESIGN_VIEW, config.VIEWS[metric], include_docs=True, full_set=True) nodes_info={} for v in view: node_name=v.value['node'] nodes_info[node_name]=v.value if metric == 'availability': return self.get_nodes_availability(nodes_info) return collections.OrderedDict(sorted(nodes_info.items())) def get_nodes_availability(self, nodes_info): nodes = self.api.nodes.retrieve() nodes_availability = {} timestamp = current_epoch_timestamp() for node in nodes: addr = "[{0}]".format(node.mgmt_net.addr) current_state= nodes_info[node.name]['current_state'] if node.name in nodes_info else 'UNKNOWN' up = 1 if current_state=='production' else 0 nodes_availability[node.name]={'node':unicode_normalize(node.name), 'node_addr':addr, 'timestamp':timestamp, 'up':up, 'current_state':current_state} return collections.OrderedDict(sorted(nodes_availability.items()))
def setUp(self): self.api = Api(os.environ['CONFINE_SERVER_API'])
def setUp(self): from orm.api import Api super(BaseLiveServerTestCase, self).setUp() self.rest = Api(self.live_server_url + '/api/') self.rest.enable_logging() self.account = self.create_account(superuser=True)
class BaseLiveServerTestCase(AppDependencyMixin, LiveServerTestCase): @classmethod def setUpClass(cls): # Avoid problems with the overlaping menu when clicking settings.ADMIN_TOOLS_MENU = 'admin_tools.menu.Menu' cls.vdisplay = Xvfb() cls.vdisplay.start() cls.selenium = WebDriver() super(BaseLiveServerTestCase, cls).setUpClass() @classmethod def tearDownClass(cls): cls.selenium.quit() cls.vdisplay.stop() super(BaseLiveServerTestCase, cls).tearDownClass() def create_account(self, username='', superuser=False): if not username: username = '******' % random_ascii(5) password = '******' self.account_password = password if superuser: return Account.objects.create_superuser(username, password=password, email='*****@*****.**') return Account.objects.create_user(username, password=password, email='*****@*****.**') def setUp(self): from orm.api import Api super(BaseLiveServerTestCase, self).setUp() self.rest = Api(self.live_server_url + '/api/') self.rest.enable_logging() self.account = self.create_account(superuser=True) def admin_login(self): session = SessionStore() session[SESSION_KEY] = self.account_id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session.save() ## to set a cookie we need to first visit the domain. self.selenium.get(self.live_server_url + '/admin/') self.selenium.add_cookie(dict( name=settings.SESSION_COOKIE_NAME, value=session.session_key, # path='/', )) def rest_login(self): self.rest.login(username=self.account.username, password=self.account_password) def take_screenshot(self): timestamp = datetime.datetime.now().isoformat().replace(':', '') filename = 'screenshot_%s_%s.png' % (self.id(), timestamp) path = '/home/orchestra/snapshots' self.selenium.save_screenshot(os.path.join(path, filename)) def admin_delete(self, obj): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name delete = reverse('admin:%s_%s_delete' % (app_label, model_name), args=(obj.pk,)) url = self.live_server_url + delete self.selenium.get(url) confirmation = self.selenium.find_element_by_name('post') confirmation.submit() self.assertNotEqual(url, self.selenium.current_url) def admin_disable(self, obj): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name change = reverse('admin:%s_%s_change' % (app_label, model_name), args=(obj.pk,)) url = self.live_server_url + change self.selenium.get(url) is_active = self.selenium.find_element_by_id('id_is_active') is_active.click() save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url) def admin_change_password(self, obj, password): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name change_password = reverse('admin:%s_%s_change_password' % (app_label, model_name), args=(obj.pk,)) url = self.live_server_url + change_password self.selenium.get(url) password_field = self.selenium.find_element_by_id('id_password1') password_field.send_keys(password) password_field = self.selenium.find_element_by_id('id_password2') password_field.send_keys(password) password_field.submit() self.assertNotEqual(url, self.selenium.current_url)
class BaseLiveServerTestCase(AppDependencyMixin, LiveServerTestCase): @classmethod def setUpClass(cls): # Avoid problems with the overlaping menu when clicking settings.ADMIN_TOOLS_MENU = 'admin_tools.menu.Menu' cls.vdisplay = Xvfb() cls.vdisplay.start() cls.selenium = WebDriver() super(BaseLiveServerTestCase, cls).setUpClass() @classmethod def tearDownClass(cls): cls.selenium.quit() cls.vdisplay.stop() super(BaseLiveServerTestCase, cls).tearDownClass() def create_account(self, username='', superuser=False): if not username: username = '******' % random_ascii(5) password = '******' self.account_password = password if superuser: return Account.objects.create_superuser( username, password=password, email='*****@*****.**') return Account.objects.create_user(username, password=password, email='*****@*****.**') def setUp(self): from orm.api import Api super(BaseLiveServerTestCase, self).setUp() self.rest = Api(self.live_server_url + '/api/') self.rest.enable_logging() self.account = self.create_account(superuser=True) def admin_login(self): session = SessionStore() session[SESSION_KEY] = self.account.id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session.save() ## to set a cookie we need to first visit the domain. self.selenium.get(self.live_server_url + '/admin/') self.selenium.add_cookie( dict( name=settings.SESSION_COOKIE_NAME, value=session.session_key, # path='/', )) def rest_login(self): self.rest.login(username=self.account.username, password=self.account_password) def take_screenshot(self): timestamp = datetime.datetime.now().isoformat().replace(':', '') filename = 'screenshot_%s_%s.png' % (self.id(), timestamp) path = '/home/orchestra/snapshots' self.selenium.save_screenshot(os.path.join(path, filename)) def admin_delete(self, obj): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name delete = reverse('admin:%s_%s_delete' % (app_label, model_name), args=(obj.pk, )) url = self.live_server_url + delete self.selenium.get(url) confirmation = self.selenium.find_element_by_name('post') confirmation.submit() self.assertNotEqual(url, self.selenium.current_url) def admin_disable(self, obj): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name change = reverse('admin:%s_%s_change' % (app_label, model_name), args=(obj.pk, )) url = self.live_server_url + change self.selenium.get(url) is_active = self.selenium.find_element_by_id('id_is_active') is_active.click() save = self.selenium.find_element_by_name('_save') save.submit() self.assertNotEqual(url, self.selenium.current_url) def admin_change_password(self, obj, password): opts = obj._meta app_label, model_name = opts.app_label, opts.model_name change_password = reverse('admin:%s_%s_change_password' % (app_label, model_name), args=(obj.pk, )) url = self.live_server_url + change_password self.selenium.get(url) password_field = self.selenium.find_element_by_id('id_password1') password_field.send_keys(password) password_field = self.selenium.find_element_by_id('id_password2') password_field.send_keys(password) password_field.submit() self.assertNotEqual(url, self.selenium.current_url)
class ApiTests(unittest.TestCase): def setUp(self): self.api = Api(os.environ['CONFINE_SERVER_API']) def test_attributes(self): self.assertEqual(['uri'], self.api._data.keys()) def test_self_retrieve(self): self.api.retrieve() self.assertEqual(3, len(self.api._data)) def test_managers(self): self.api.nodes with self.assertRaises(AttributeError): self.api.rata def test_login(self): self.assertRaises(self.api.ResponseStatusError, self.api.login) self.api.username = os.environ['CONFINE_USER'] self.api.password = os.environ['CONFINE_PASSWORD'] self.api.login() auth_header = self.api.DEFAULT_HEADERS['authorization'] self.assertLess(20, auth_header) def test_login_providing_credentials(self): username = os.environ['CONFINE_USER'] password = os.environ['CONFINE_PASSWORD'] self.api.login(username=username, password=password) self.assertEqual(self.api.username, username) self.assertEqual(self.api.password, password) auth_header = self.api.DEFAULT_HEADERS['authorization'] self.assertLess(20, auth_header) def test_logout(self): self.test_login() self.api.logout() self.assertNotIn('authorization', self.api.DEFAULT_HEADERS) def test_retrieve_base(self): group = self.api.groups.retrieve()[0] name = 'RandomNode-%s' % random_ascii(10) self.test_login() node = self.api.nodes.create(name=name, group=group) controller = Api(node.uri) controller.retrieve() self.assertEqual(self.api, controller) self.assertEqual(self.api._data, controller._data) def test_caching(self): api = Api(os.environ['CONFINE_SERVER_API'], cache=True) api.nodes.retrieve() self.assertEqual(0, api.cache.hits) self.assertEqual(2, api.cache.misses) nodes = api.nodes.retrieve() self.assertEqual(1, api.cache.hits) self.assertEqual(2, api.cache.misses) for node in nodes: node.retrieve() self.assertEqual(len(nodes) + 2, api.cache.misses) self.assertEqual(1, api.cache.hits) for node in nodes: node.retrieve() self.assertEqual(len(nodes) + 1, api.cache.hits) self.assertEqual(len(nodes) + 2, api.cache.misses) api.cache.invalidate() self.assertEqual(0, api.stats['conditional']) for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) api.cache.remove() for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) self.assertEqual((2 * len(nodes)) + 1, api.cache.hits) self.assertEqual((2 * len(nodes)) + 2, api.cache.misses)
def split_len(seq, length): """ Returns seq broken in a list of strings of length length """ return [seq[i:i+length] for i in range(0, len(seq), length)] def int_to_hex_str(number, digits): hex_str = ('%.' + str(digits) + 'x') % number err_msg = "Hex representation of %d doesn't fit in %s digits" % (number, digits) assert len(hex_str) <= digits, err_msg return hex_str def ipv6_addr(node_id, slice_id, iface_nr=2): # Here we assume that mgmt iface.nr value is always 2 nr = '10' + int_to_hex_str(iface_nr, 2) node_id = int_to_hex_str(node_id, 4) slice_id = int_to_hex_str(slice_id, 12) ipv6_words = MGMT_IPV6_PREFIX.split(':')[:3] ipv6_words.extend([node_id, nr]) ipv6_words.extend(split_len(slice_id, 4)) return ':'.join(ipv6_words) controller = Api('https://controller.community-lab.net/api/') MGMT_IPV6_PREFIX = controller.testbed_params.mgmt_ipv6_prefix slivers = controller.slivers.retrieve() slivers.retrieve_related('node', 'slice') for sliver in slivers.filter(slice__id=249): print(ipv6_addr(sliver.node.id, sliver.slice.id))
def __init__(self): # connection to CouchBase DB self.cb = Couchbase.connect(bucket=config.COUCHBASE_BUCKET, host=config.COUCHBASE_HOST, port=config.COUCHBASE_PORT) # connection to C-Lab Controller API self.api = Api(config.CLAB_CONTROLLER_API) self.api.retrieve()
class ApiTests(unittest.TestCase): def setUp(self): self.api = Api(os.environ['CONFINE_SERVER_API']) def test_attributes(self): self.assertEqual(['uri'], self.api._data.keys()) def test_self_retrieve(self): self.api.retrieve() self.assertEqual(3, len(self.api._data)) def test_managers(self): self.api.nodes with self.assertRaises(AttributeError): self.api.rata def test_login(self): self.assertRaises(self.api.ResponseStatusError, self.api.login) self.api.username = os.environ['CONFINE_USER'] self.api.password = os.environ['CONFINE_PASSWORD'] self.api.login() auth_header = self.api.DEFAULT_HEADERS['authorization'] self.assertLess(20, auth_header) def test_login_providing_credentials(self): username = os.environ['CONFINE_USER'] password = os.environ['CONFINE_PASSWORD'] self.api.login(username=username, password=password) self.assertEqual(self.api.username, username) self.assertEqual(self.api.password, password) auth_header = self.api.DEFAULT_HEADERS['authorization'] self.assertLess(20, auth_header) def test_logout(self): self.test_login() self.api.logout() self.assertNotIn('authorization', self.api.DEFAULT_HEADERS) def test_retrieve_base(self): group = self.api.groups.retrieve()[0] name = 'RandomNode-%s' % random_ascii(10) self.test_login() node = self.api.nodes.create(name=name, group=group) controller = Api(node.uri) controller.retrieve() self.assertEqual(self.api, controller) self.assertEqual(self.api._data, controller._data) def test_caching(self): api = Api(os.environ['CONFINE_SERVER_API'], cache=True) api.nodes.retrieve() self.assertEqual(0, api.cache.hits) self.assertEqual(2, api.cache.misses) nodes = api.nodes.retrieve() self.assertEqual(1, api.cache.hits) self.assertEqual(2, api.cache.misses) for node in nodes: node.retrieve() self.assertEqual(len(nodes)+2, api.cache.misses) self.assertEqual(1, api.cache.hits) for node in nodes: node.retrieve() self.assertEqual(len(nodes)+1, api.cache.hits) self.assertEqual(len(nodes)+2, api.cache.misses) api.cache.invalidate() self.assertEqual(0, api.stats['conditional']) for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) api.cache.remove() for node in nodes: node.retrieve() self.assertEqual(len(nodes), api.stats['conditional']) self.assertEqual((2*len(nodes))+1, api.cache.hits) self.assertEqual((2*len(nodes))+2, api.cache.misses)