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 _list_host(self, model_instance): """ Lists data at a location in a store and returns back model instances. :param model_instance: Model instance to search for and list :type model_instance: commissaire.model.Model :returns: A list of models :rtype: list """ hosts = [] path = _model_mapper[model_instance.__class__.__name__] items = self._store.get(self._endpoint + path).json() for item in items.get('items'): try: hosts.append(self._format_model(item, Host.new(), True)) except (TypeError, KeyError): # TODO: Add logging pass return Hosts.new(hosts=hosts)
def _list_host(self, model_instance): """ Lists data at a location in a store and returns back model instances. :param model_instance: Model instance to search for and list :type model_instance: commissaire.model.Model :returns: A list of models :rtype: list """ hosts = [] path = _model_mapper[model_instance.__class__.__name__] items = self._store.get(self._endpoint + path).json() for item in items.get('items'): try: hosts.append(self._format_model(item, Host.new(), True)) except (TypeError, KeyError): # TODO: Add logging pass return Hosts.new(hosts=hosts)
def test_watcher_without_a_cluster(self): """ Verify the watcher without a cluster. """ 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() store_manager = MagicMock(StoreHandlerManager) store_manager.list.return_value = Hosts.new(hosts=[test_host]) store_manager.get.return_value = test_host watcher(q, store_manager, run_once=True) store_manager.list.assert_called_once() store_manager.save.assert_called_once()
def test_hosts_defaults_values(self): """ Verify Hosts model fills default values when missing. """ hosts = Hosts.new() self.assertEquals(Hosts._attribute_defaults['hosts'], hosts.hosts)
' "cpus": 0, "memory": 0, "space": 0,' ' "last_check": ""}') #: Credential JSON for tests HOST_CREDS_JSON = '{"remote_user": "******", "ssh_priv_key": "dGVzdAo="}' #: HostStatus JSON for tests HOST_STATUS_JSON = ( '{"type": "host_only", "container_manager": {}, "commissaire": ' '{"status": "available", "last_check": "2016-07-29T20:39:50.529454"}}') #: Host model for most tests HOST = Host.new(ssh_priv_key='dGVzdAo=', remote_user='******', **json.loads(HOST_JSON)) #: HostStatus model for most tests HOST_STATUS = HostStatus.new(**json.loads(HOST_STATUS_JSON)) #: Hosts model for most tests HOSTS = Hosts.new(hosts=[HOST]) #: Cluster model for most tests CLUSTER = Cluster.new( name='cluster', status='ok', hostset=[], ) #: Cluster model with HOST for most tests CLUSTER_WITH_HOST = Cluster.new( name='cluster', status='ok', hostset=[HOST], ) #: Cluster model with flattened HOST for tests CLUSTER_WITH_FLAT_HOST = Cluster.new( name='cluster',
def test_hosts_defaults_values(self): """ Verify Hosts model fills default values when missing. """ hosts = Hosts.new() self.assertEquals(Hosts._attribute_defaults['hosts'], hosts.hosts)
#: Response JSON for a single host HOST_JSON = ( '{"address": "10.2.0.2",' ' "status": "available", "os": "atomic",' ' "cpus": 2, "memory": 11989228, "space": 487652,' ' "last_check": "2015-12-17T15:48:18.710454"}') #: Credential JSON for tests HOST_CREDS_JSON = '{"remote_user": "******", "ssh_priv_key": "dGVzdAo="}' #: Host model for most tests HOST = Host.new( ssh_priv_key='dGVzdAo=', remote_user='******', **json.loads(HOST_JSON)) #: Hosts model for most tests HOSTS = Hosts.new( hosts=[HOST] ) #: Cluster model for most tests CLUSTER = Cluster.new( name='cluster', status='ok', hostset=[], ) #: Cluster model with HOST for most tests CLUSTER_WITH_HOST = Cluster.new( name='cluster', status='ok', hostset=[HOST], ) #: Cluster model with flattened HOST for tests CLUSTER_WITH_FLAT_HOST = Cluster.new(