def on_get(self, req, resp): """ Handles GET requests for Clusters. :param req: Request instance that will be passed through. :type req: falcon.Request :param resp: Response instance that will be passed through. :type resp: falcon.Response """ req.context['model'] = None try: store_manager = cherrypy.engine.publish('get-store-manager')[0] clusters = store_manager.list(Clusters.new()) if clusters.clusters == []: self.logger.debug('Store returned an empty cluster list.') resp.status = falcon.HTTP_200 return except: self.logger.warn( 'Store does not have any clusters. Returning [] and 404.') resp.status = falcon.HTTP_404 return # HACK: Should use model instead resp.status = falcon.HTTP_200 resp.body = json.dumps([cluster.name for cluster in clusters.clusters])
def test_watcher_failed_to_active(self): """ Verify the watcher. """ with mock.patch('commissaire.transport.ansibleapi.Transport') as _tp: _tp().check_host_availability.return_value = (0, {}) q = Queue() test_host = make_new(HOST) test_host.last_check = (datetime.datetime.now() - datetime.timedelta(days=10)).isoformat() test_host.status = 'failed' test_cluster = make_new(CLUSTER) test_cluster.type = C.CLUSTER_TYPE_KUBERNETES test_cluster.hostset = [test_host.address] store_manager = MagicMock(StoreHandlerManager) store_manager.list.side_effect = (Hosts.new( hosts=[test_host]), Clusters.new(clusters=[test_cluster])) store_manager.get.return_value = test_host watcher(q, store_manager, run_once=True) self.assertEquals(2, store_manager.list.call_count) store_manager.save.assert_called_once() self.assertEquals('active', test_host.status)
def cluster_for_host(address, store_manager): """ Checks to see if the the host is part of a cluster. KeyError is raised if the host is not part of a cluster. :param name: Name of a cluster :type name: str :param store_manager: Remote object for remote stores :type store_manager: commissaire.store.StoreHandlerManager :returns: A cluster instance that has the host :rtype: commissaire.model.Model :rasies: KeyError """ for cluster in store_manager.list(Clusters.new()).clusters: if address in cluster.hostset: return cluster raise KeyError
def test_host_status_retrieve_with_container_manager(self): """ Verify retrieving Host status when it is in a container manager cluster. """ with mock.patch('cherrypy.engine.publish') as _publish: manager = mock.MagicMock(StoreHandlerManager) kube_container_mgr = KubeContainerManager({ 'server_url': 'http://127.0.0.1:8080', 'token': 'token' }) # A dummy requests.Response response = requests.Response() response.status_code = 200 response._content='{"use": "kube"}' kube_container_mgr._get = mock.MagicMock(return_value=response) manager.list_container_managers.return_value = [kube_container_mgr] _publish.return_value = [manager] test_host = make_new(HOST) test_cluster = make_new(CLUSTER) test_cluster.type = C.CLUSTER_TYPE_KUBERNETES test_cluster.hostset = [test_host.address] # Verify if the host exists the data is returned manager.get.side_effect = ( test_host, test_cluster) manager.list.return_value = Clusters.new(clusters=[test_cluster]) body = self.simulate_request('/api/v0/host/10.2.0.2/status') self.assertEqual(self.srmock.status, falcon.HTTP_200) result = json.loads(body[0]) self.assertEquals(C.CLUSTER_TYPE_KUBERNETES, result['type']) self.assertEquals('available', result['host']['status']) self.assertEquals({'use': 'kube'}, result['container_manager'])