def test_use_newly_added_node(self, session, monkeypatch): monkeypatch.setattr(util, "is_node_healthy", lambda node: True) n1 = Node("chacra01.ceph.com") n1.last_used = datetime.datetime.now() n2 = Node("chacra02.ceph.com") session.commit() assert n2 == util.get_next_node()
def test_down_count_is_incremented(self, m_get, session): m_get.return_value.ok = False node = Node("chacra.ceph.com") session.commit() util.is_node_healthy(node) node = Node.get(1) assert node.down_count == 1
def test_get_multiple_nodes(self, session): Node("chacra01.ceph.com") Node("chacra02.ceph.com") session.commit() result = session.app.get("/api/nodes/") assert set(result.json.keys()) == set( ["chacra01.ceph.com", "chacra02.ceph.com"])
def test_pick_last_used_node(self, session, monkeypatch): monkeypatch.setattr(util, "is_node_healthy", lambda node: True) n1 = Node("chacra01.ceph.com") n1.last_used = datetime.datetime.now() - datetime.timedelta(days=1) n2 = Node("chacra02.ceph.com") n2.last_used = datetime.datetime.now() session.commit() assert n1 == util.get_next_node()
def test_node_exceeds_down_count_limit(self, m_get, session): m_get.return_value.ok = False node = Node("chacra.ceph.com") node.down_count = 2 session.commit() util.is_node_healthy(node) node = Node.get(1) assert not node.healthy
def test_down_count_is_cleared_when_healthy(self, m_get, session): m_get.return_value.ok = True node = Node("chacra.ceph.com") node.down_count = 2 session.commit() util.is_node_healthy(node) node = Node.get(1) assert node.down_count == 0
def test_updates_last_check_time(self, session, monkeypatch): monkeypatch.setattr(nodes, "check_node_health", mock_check_node_health(True)) session.app.post("/api/nodes/chacra.ceph.com/") n = Node.get(1) last_check = n.last_check.time() session.app.post("/api/nodes/chacra.ceph.com/") n = Node.get(1) assert n.last_check.time() > last_check
def test_last_check_is_updated(self, m_get, session): m_get.return_value.ok = True node = Node("chacra.ceph.com") last_check = datetime.datetime.now() node.last_check = datetime.datetime.now() session.commit() util.is_node_healthy(node) node = Node.get(1) assert node.last_check.time() > last_check.time()
def test_updates_healthy(self, session, monkeypatch): monkeypatch.setattr(nodes, "check_node_health", mock_check_node_health(True)) session.app.post("/api/nodes/chacra.ceph.com/") n = Node.get(1) n.healthy = False session.commit() session.app.post("/api/nodes/chacra.ceph.com/") n = Node.get(1) assert n.healthy
def test_healthy_is_true_when_rejoining_pool(self, m_get, session): """ Tests the scenario where a node has been marked down, but is now up again and needs to rejoin the pool. """ m_get.return_value.ok = True node = Node("chacra.ceph.com") node.down_count = 3 node.healthy = False session.commit() util.is_node_healthy(node) node = Node.get(1) assert node.down_count == 0 assert node.healthy
def test_sets_last_used_on_selection(self, session, monkeypatch): monkeypatch.setattr(util, "is_node_healthy", lambda node: True) node = Node("chacra.ceph.com") session.commit() last_used = node.last_used next_node = util.get_next_node() assert next_node.last_used.time() > last_used.time()
def test_node_fails_initial_check(self, session, monkeypatch): monkeypatch.setattr(nodes, "check_node_health", mock_check_node_health(False)) session.app.post("/api/nodes/chacra.ceph.com/") n = Node.get(1) assert n.host == "chacra.ceph.com" assert not n.healthy
def test_get_next_node_fails(self, session, monkeypatch): monkeypatch.setattr(nodes, "check_node_health", mock_check_node_health(True)) Node("chacra.ceph.com") session.commit() def _get_next_node(): return None monkeypatch.setattr(nodes, "get_next_node", _get_next_node) result = session.app.get("/api/nodes/next/", expect_errors=True) assert result.status_int == 404
def test_get_next_node_succeeds(self, session, monkeypatch): monkeypatch.setattr(nodes, "check_node_health", mock_check_node_health(True)) Node("chacra.ceph.com") session.commit() def _get_next_node(): node = Node.get(1) return node monkeypatch.setattr(nodes, "get_next_node", _get_next_node) result = session.app.get("/api/nodes/next/") assert result.body == "https://chacra.ceph.com/"
def test_create_node(self, session, monkeypatch): monkeypatch.setattr(nodes, "check_node_health", mock_check_node_health(True)) session.app.post("/api/nodes/chacra.ceph.com/") n = Node.get(1) assert n.host == "chacra.ceph.com"
def _get_next_node(): node = Node.get(1) return node
def test_no_healthy_nodes(self, session): node = Node("chacra.ceph.com") node.healthy = False session.commit() assert not util.get_next_node()
def test_node_raises_requests_exception(self, exc, session, monkeypatch): monkeypatch.setattr(requests, "get", lambda *a, **kw: request_exception(exc)) healthy = util.check_node_health(Node("chacra.ceph.com")) assert healthy is False
def test_get_one_node(self, session): Node("chacra.ceph.com") session.commit() result = session.app.get("/api/nodes/") assert result.json.keys() == ["chacra.ceph.com"]
def test_unhealthy(self, session, monkeypatch): response = RequestsResponse(ok=False) monkeypatch.setattr(requests, "get", lambda *a, **kw: response) healthy = util.check_node_health(Node("chacra.ceph.com")) assert healthy is False
def test_finds_a_node(self, session, monkeypatch): monkeypatch.setattr(util, "is_node_healthy", lambda node: True) node = Node("chacra.ceph.com") session.commit() next_node = util.get_next_node() assert next_node == node
def test_node_is_not_healthy(self, m_get, session): m_get.return_value.ok = False node = Node("chacra.ceph.com") session.commit() assert not util.is_node_healthy(node)