def test_offline(self, check_execute_command, execute_command, cephadm_module): check_execute_command.return_value = '' execute_command.return_value = '', '', 0 if not AsyncMock: # can't run this test if we could not import AsyncMock return mock_connect = AsyncMock(return_value='') with mock.patch("asyncssh.connect", new=mock_connect) as asyncssh_connect: with with_host(cephadm_module, 'test'): asyncssh_connect.side_effect = ConnectionLost('reason') code, out, err = cephadm_module.check_host('test') assert out == '' assert "Host 'test' not found" in err out = wait(cephadm_module, cephadm_module.get_hosts())[0].to_json() assert out == HostSpec('test', '1::4', status='Offline').to_json() asyncssh_connect.return_value = mock.MagicMock() asyncssh_connect.side_effect = None assert CephadmServe(cephadm_module)._check_host('test') is None out = wait(cephadm_module, cephadm_module.get_hosts())[0].to_json() assert out == HostSpec('test', '1::4').to_json()
def mk_spec_and_host(spec_section, hosts, explicit_key, explicit, count): if spec_section == 'hosts': mk_spec = lambda: ServiceSpec('mon', placement=PlacementSpec( hosts=explicit, count=count, )) elif spec_section == 'label': mk_spec = lambda: ServiceSpec('mon', placement=PlacementSpec( label='mylabel', count=count, )) elif spec_section == 'host_pattern': pattern = { 'e': 'notfound', '1': '1', '12': '[1-2]', '123': '*', }[explicit_key] mk_spec = lambda: ServiceSpec('mon', placement=PlacementSpec( host_pattern=pattern, count=count, )) else: assert False hosts = [ HostSpec(h, labels=['mylabel']) if h in explicit else HostSpec(h) for h in hosts ] return mk_spec, hosts
def test_drivegroup_pattern(): dg = DriveGroupSpec(PlacementSpec(host_pattern='node[1-3]'), service_id='foobar', data_devices=DeviceSelection(all=True)) assert dg.placement.filter_matching_hostspecs([ HostSpec('node{}'.format(i)) for i in range(10) ]) == ['node1', 'node2', 'node3']
def test_DriveGroup(test_input): dg = [DriveGroupSpec.from_json(inp) for inp in test_input][0] assert dg.placement.filter_matching_hostspecs([HostSpec('hostname') ]) == ['hostname'] assert dg.service_id == 'testing_drivegroup' assert all([isinstance(x, Device) for x in dg.data_devices.paths]) assert dg.data_devices.paths[0].path == '/dev/sda'
def test_node_assignment4(spec, networks, daemons, expected, expected_add, expected_remove): all_slots, to_add, to_remove = HostAssignment( spec=spec, hosts=[HostSpec(h, labels=['foo']) for h in networks.keys()], daemons=daemons, allow_colo=True, networks=networks, primary_daemon_type='haproxy' if spec.service_type == 'ingress' else spec.service_type, per_host_daemon_type='keepalived' if spec.service_type == 'ingress' else None, ).place() got = [str(p) for p in all_slots] num_wildcard = 0 for i in expected: if i == '*': num_wildcard += 1 else: assert i in got got.remove(i) assert num_wildcard == len(got) got = [str(p) for p in to_add] num_wildcard = 0 for i in expected_add: if i == '*': num_wildcard += 1 else: assert i in got got.remove(i) assert num_wildcard == len(got) assert sorted([d.name() for d in to_remove]) == sorted(expected_remove)
def test_bad_specs(service_type, placement, hosts, daemons, expected): with pytest.raises(OrchestratorValidationError) as e: hosts = HostAssignment(spec=ServiceSpec(service_type, placement=placement), hosts=[HostSpec(h) for h in hosts], get_daemons_func=lambda _: daemons).place() assert str(e.value) == expected
def test_node_assignment4(spec, networks, daemons, expected, expected_add, expected_remove): all_slots, to_add, to_remove = HostAssignment( spec=spec, hosts=[HostSpec(h, labels=['foo']) for h in networks.keys()], daemons=daemons, allow_colo=True, networks=networks, ).place() got = [str(p) for p in all_slots] num_wildcard = 0 for i in expected: if i == '*': num_wildcard += 1 else: assert i in got got.remove(i) assert num_wildcard == len(got) got = [str(p) for p in to_add] num_wildcard = 0 for i in expected_add: if i == '*': num_wildcard += 1 else: assert i in got got.remove(i) assert num_wildcard == len(got) assert sorted([d.name() for d in to_remove]) == sorted(expected_remove)
def test_node_assignment3(service_type, placement, hosts, daemons, expected_len, must_have): hosts = HostAssignment(spec=ServiceSpec(service_type, placement=placement), hosts=[HostSpec(h) for h in hosts], get_daemons_func=lambda _: daemons).place() assert len(hosts) == expected_len for h in must_have: assert h in [h.hostname for h in hosts]
def test_bad_specs(service_type, placement, hosts, daemons, expected): with pytest.raises(OrchestratorValidationError) as e: hosts, to_add, to_remove = HostAssignment( spec=ServiceSpec(service_type, placement=placement), hosts=[HostSpec(h) for h in hosts], daemons=daemons, ).place() assert str(e.value) == expected
def test_node_assignment2(service_type, placement, hosts, daemons, expected_len, in_set): hosts = HostAssignment(spec=ServiceSpec(service_type, placement=placement), hosts=[HostSpec(h, labels=['foo']) for h in hosts], get_daemons_func=lambda _: daemons).place() assert len(hosts) == expected_len for h in [h.hostname for h in hosts]: assert h in in_set
def test_node_assignment2(service_type, placement, hosts, daemons, expected_len, in_set): hosts, to_add, to_remove = HostAssignment( spec=ServiceSpec(service_type, placement=placement), hosts=[HostSpec(h, labels=['foo']) for h in hosts], daemons=daemons, ).place() assert len(hosts) == expected_len for h in [h.hostname for h in hosts]: assert h in in_set
def test_node_assignment3(service_type, placement, hosts, daemons, expected_len, must_have): hosts, to_add, to_remove = HostAssignment( spec=ServiceSpec(service_type, placement=placement), hosts=[HostSpec(h) for h in hosts], daemons=daemons, ).place() assert len(hosts) == expected_len for h in must_have: assert h in [h.hostname for h in hosts]
def test_active_assignment(service_type, placement, hosts, daemons, expected): spec = ServiceSpec(service_type=service_type, service_id=None, placement=placement) hosts = HostAssignment(spec=spec, hosts=[HostSpec(h) for h in hosts], get_daemons_func=lambda _: daemons).place() assert sorted([h.hostname for h in hosts]) in expected
def test_node_assignment(service_type, placement, hosts, daemons, rank_map, post_rank_map, expected, expected_add, expected_remove): spec = None service_id = None allow_colo = False if service_type == 'rgw': service_id = 'realm.zone' allow_colo = True elif service_type == 'mds': service_id = 'myfs' allow_colo = True elif service_type == 'nfs': service_id = 'mynfs' spec = ServiceSpec(service_type=service_type, service_id=service_id, placement=placement, pool='foo') if not spec: spec = ServiceSpec(service_type=service_type, service_id=service_id, placement=placement) all_slots, to_add, to_remove = HostAssignment( spec=spec, hosts=[HostSpec(h, labels=['foo']) for h in hosts], daemons=daemons, allow_colo=allow_colo, rank_map=rank_map, ).place() assert rank_map == post_rank_map got = [str(p) for p in all_slots] num_wildcard = 0 for i in expected: if i == '*': num_wildcard += 1 else: assert i in got got.remove(i) assert num_wildcard == len(got) got = [str(p) for p in to_add] num_wildcard = 0 for i in expected_add: if i == '*': num_wildcard += 1 else: assert i in got got.remove(i) assert num_wildcard == len(got) assert sorted([d.name() for d in to_remove]) == sorted(expected_remove)
def test_odd_mons(service_type, placement, hosts, daemons, expected_count): spec = ServiceSpec(service_type=service_type, service_id=None, placement=placement) hosts = HostAssignment( spec=spec, hosts=[HostSpec(h) for h in hosts], get_daemons_func=lambda _: daemons).place() assert len(hosts) == expected_count
def test_node_assignment(service_type, placement, hosts, daemons, expected): service_id = None if service_type == 'rgw': service_id = 'realm.zone' spec = ServiceSpec(service_type=service_type, service_id=service_id, placement=placement) hosts = HostAssignment(spec=spec, hosts=[HostSpec(h, labels=['foo']) for h in hosts], get_daemons_func=lambda _: daemons).place() assert sorted([h.hostname for h in hosts]) == sorted(expected)
def test_active_assignment(service_type, placement, hosts, daemons, expected, expected_add, expected_remove): spec = ServiceSpec(service_type=service_type, service_id=None, placement=placement) hosts, to_add, to_remove = HostAssignment( spec=spec, hosts=[HostSpec(h) for h in hosts], daemons=daemons, ).place() assert sorted([h.hostname for h in hosts]) in expected assert sorted([h.hostname for h in to_add]) in expected_add assert sorted([h.name() for h in to_remove]) in expected_remove
# flake8: noqa import json import yaml import pytest from ceph.deployment.hostspec import HostSpec, SpecValidationError @pytest.mark.parametrize("test_input,expected", [ ({ "hostname": "foo" }, HostSpec('foo')), ({ "hostname": "foo", "labels": "l1" }, HostSpec('foo', labels=['l1'])), ({ "hostname": "foo", "labels": ["l1", "l2"] }, HostSpec('foo', labels=['l1', 'l2'])), ({ "hostname": "foo", "location": { "rack": "foo" } }, HostSpec('foo', location={'rack': 'foo'})), ]) def test_parse_host_specs(test_input, expected): hs = HostSpec.from_json(test_input) assert hs == expected
def test_orch_ps(_describe_service): # Ensure natural sorting on daemon names (osd.1, osd.2, osd.10) cmd = { 'prefix': 'orch ps' } m = OrchestratorCli('orchestrator', 0, 0) r = m._handle_command(None, cmd) out = 'NAME HOST PORTS STATUS REFRESHED AGE MEM USE MEM LIM VERSION IMAGE ID \n'\ 'osd.1 <unknown> unknown - - - - <unknown> <unknown> \n'\ 'osd.2 <unknown> unknown - - - - <unknown> <unknown> \n'\ 'osd.10 <unknown> unknown - - - - <unknown> <unknown> ' assert r == HandleCommandResult(retval=0, stdout=out, stderr='') hlist = OrchResult([HostSpec("ceph-node-1"), HostSpec("ceph-node-2"), HostSpec("ceph-node-10")]) @mock.patch("orchestrator.OrchestratorCli.get_hosts", return_value=hlist) def test_orch_host_ls(_describe_service): # Ensure natural sorting on hostnames (ceph-node-1, ceph-node-2, ceph-node-10) cmd = { 'prefix': 'orch host ls' } m = OrchestratorCli('orchestrator', 0, 0) r = m._handle_command(None, cmd) out = 'HOST ADDR LABELS STATUS \n'\ 'ceph-node-1 ceph-node-1 \n'\ 'ceph-node-2 ceph-node-2 \n'\ 'ceph-node-10 ceph-node-10 \n'\
def get_hosts_func(label=None, as_hostspec=False): if as_hostspec: return [HostSpec(h) for h in hosts] return hosts