def test_load_all():
    tendrlNS = init()
    NS._int.reconnect = type("Dummy", (object, ), {})
    NS._int.client = importlib.import_module(
        "tendrl.commons.tests.fixtures.client").Client()
    with patch.object(__builtin__, 'hasattr', has_attr):
        obj = BaseObject_Child()
        obj._ns = tendrlNS
        with patch.object(dummy_client,
                          "read",
                          return_value=maps.NamedDict(leaves={})):
            obj.load_all()
        with patch.object(dummy_client,
                          "read",
                          return_value=maps.NamedDict(
                              leaves=[maps.NamedDict(key='test_value')])):
            with patch.object(BaseObject_Child, 'load', return_value="tst"):
                ret = obj.load_all()
                assert isinstance(ret, list)
        with patch.object(dummy_client, "read", read):
            with pytest.raises(etcd.EtcdConnectionFailed):
                obj.load_all()
        with patch.object(dummy_client, "read", read_fn):
            ret = obj.load_all()
            assert ret is None
예제 #2
0
def _read(*args, **kwargs):
    test_job._complete._flag = True
    global status_flag
    global status_valid
    if args[1] == "/queue/job/status" and status_flag == 0:
        status_flag = 1
        return maps.NamedDict(leaves=[maps.NamedDict(key="test/job")],
                              value="finished")
    elif args[1] == "/queue/job/status" and status_flag == 1:
        status_flag = 2
        return maps.NamedDict(leaves=[maps.NamedDict(key="test/job")],
                              value="unfinished")
    elif args[1] == "/queue/job/status" and status_flag == 2:
        raise etcd.EtcdKeyNotFound
    elif args[1] == "/queue" or args[1] == "/queue/job/locked_by":
        return maps.NamedDict(leaves=[maps.NamedDict(key="test/job")],
                              value=False)
    elif args[1] == "/queue/job/valid_until" and status_valid == 0:
        status_valid = 1
        return maps.NamedDict(leaves=[maps.NamedDict(key="test/job")],
                              value=False)
    elif args[1] == "/queue/job/valid_until" and status_valid == 1:
        return maps.NamedDict(
            leaves=[maps.NamedDict(key="test/job")],
            value=(time_utils.now() - datetime.datetime(
                1970, 1, 1).replace(tzinfo=utc)).total_seconds())
예제 #3
0
def test_create_node_id(patch_write, patch_read, patch_client):
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    patch_client.return_value = etcd.Client()
    setattr(NS, "_int", maps.NamedDict())
    NS._int.etcd_kwargs = {'port': 1, 'host': 2, 'allow_reconnect': True}
    NS._int.client = etcd.Client(**NS._int.etcd_kwargs)
    NS._int.wclient = etcd.Client(**NS._int.etcd_kwargs)
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict()
    NS.config.data['tags'] = "test"
    node_context = NodeContext(machine_id="Test_Machine_id",
                               node_id="Test_Node_id",
                               fqdn="Test_fqdn")
    f = tempfile.TemporaryFile()
    with patch.object(__builtin__, "open") as mock_open:
        mock_open.return_value = f
        with patch.object(os, "makedirs", return_value=True) as mock_makedirs:
            node_context._create_node_id()
    f.close()
    with pytest.raises(IOError):
        with patch.object(os, "makedirs", return_value=True) as mock_makedirs:
            node_context._create_node_id()
    f = tempfile.TemporaryFile()
    with patch.object(__builtin__, "open") as mock_open:
        mock_open.return_value = f
        with patch.object(os.path, "exists",
                          return_value=False) as mock_exists:
            with patch.object(os, "makedirs",
                              return_value=True) as mock_makedirs:
                node_context._create_node_id()
    f.close()
def test_get_node_ips():
    param = maps.NamedDict()
    param["Cluster.node_configuration"] = {
        "test_node": maps.NamedDict(role="mon", provisioning_ip="test_ip")
    }
    ret = gluster_help.get_node_ips(param)
    assert ret[0] == "test_ip"
def test_sync_volume_utilization_details_with_stopped_volume():
    setattr(NS, "publisher_id", "gluster-integration")
    setattr(NS, "gluster", maps.NamedDict())
    NS.gluster["objects"] = maps.NamedDict()
    obj = importlib.import_module(
        'tendrl.gluster_integration.objects.utilization'
    )
    for obj_cls in inspect.getmembers(obj, inspect.isclass):
        NS.gluster.objects["Utilization"] = obj_cls[1]
    volume = Volume(
        vol_id='vol-id',
        vol_type='Replicate',
        name='vol1',
        status='Stopped',
        state='up',
        brick_count=3,
        replica_count=3,
        subvol_count=1,
    )
    utilization.sync_utilization_details([volume])
    with mock.patch.object(Volume, 'save') as vol_save_mock:
        assert not vol_save_mock.called

    with mock.patch.object(Utilization, 'save') as util_save_mock:
        assert not util_save_mock.called
예제 #6
0
def load(*args):
    obj = importlib.import_module("tendrl.commons.tests.fixtures.client")
    obj = obj.Client()
    if args[0] == "tag":
        obj.payload = maps.NamedDict(
            type="Test_type",
            tags=["Test_tag"],
            run="tendrl.commons.objects.node.flows.test_flow",
            parameters="Test_param")
    elif args[0] == "node":
        obj.payload = maps.NamedDict(
            type="Test_type",
            tags=["Test_tag"],
            node_ids=["Test_node"],
            run="tendrl.commons.objects.node.flows.test_flow",
            parameters="Test_param")
    elif args[0] == "no_obj_name":
        obj.payload = maps.NamedDict(type="Test_type",
                                     tags=["Test_tag"],
                                     run="tendrl.commons.flows.test_flow",
                                     parameters="Test_param")
    else:
        obj.payload = maps.NamedDict(type="Test_type")
    obj.status = "new"
    obj.job_id = 1
    return obj
예제 #7
0
def init(patch_etcd_utils_read,
         patch_get_node_id,
         patch_read,
         patch_client):
    patch_get_node_id.return_value = 1
    patch_read.return_value = etcd.Client()
    patch_client.return_value = etcd.Client()
    setattr(__builtin__, "NS", maps.NamedDict())
    setattr(NS, "_int", maps.NamedDict())
    NS._int.etcd_kwargs = {
        'port': 1,
        'host': 2,
        'allow_reconnect': True}
    NS._int.client = etcd.Client(**NS._int.etcd_kwargs)
    NS._int.wclient = etcd.Client(**NS._int.etcd_kwargs)
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict()
    NS.config.data['tags'] = "test"
    NS.config.data['logging_socket_path'] = "test"
    NS.publisher_id = "node_context"
    NS.config.data['etcd_port'] = 8085
    NS.config.data['etcd_connection'] = "Test Connection"
    patch_etcd_utils_read.return_value = maps.NamedDict(
        value='{"status": "UP",'
              '"pkey": "tendrl-node-test",'
              '"node_id": "test_node_id",'
              '"ipv4_addr": "test_ip",'
              '"tags": "[\\"my_tag\\"]",'
              '"sync_status": "done",'
              '"locked_by": "fd",'
              '"fqdn": "tendrl-node-test",'
              '"last_sync": "date"}')
    tendrlNS = TendrlNS()
    return tendrlNS
예제 #8
0
def test_run():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.publisher_id = "node_agent"
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict(logging_socket_path="test/path")
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    authorize_key = AuthorizeKey("ssh-rsa", "user_name")
    authorize_key.attributes["_raw_params"] = "test_params"
    with patch.object(ansible_module_runner, 'AnsibleRunner',
                      ansible) as mock_ansible:
        with pytest.raises(ansible_module_runner.AnsibleModuleNotFound):
            ret = authorize_key.run()
    with patch.object(ansible_module_runner.AnsibleRunner, 'run',
                      run) as mock_run:
        ret = authorize_key.run()
        assert ret[0] is False
    with patch.object(ansible_module_runner.AnsibleRunner, 'run') as mock_run:
        mock_run.return_value = ansible_run(True)
        ret = authorize_key.run()
        assert ret[0] is True
    with patch.object(ansible_module_runner.AnsibleRunner, 'run') as mock_run:
        mock_run.return_value = ansible_run(False)
        ret = authorize_key.run()
        assert ret[0] is False
예제 #9
0
def read(param):
    if "subvolume1" in param:
        raise etcd.EtcdKeyNotFound
    else:
        keys = maps.NamedDict(
            key="sub_vol1/dhcp123-12.lab.abc.com:_gluster_b1")
        return maps.NamedDict(leaves=[keys])
예제 #10
0
def test_validate_ns_obj_definitions():
    tendrlNS = init()
    raw_ns = "namespace.tendrl"
    defs = tendrlNS.current_ns.definitions.get_parsed_defs()[raw_ns]
    defs_temp = defs
    defs_temp["objects"]["TestObject"] = maps.NamedDict()
    with pytest.raises(Exception):
        tendrlNS._validate_ns_obj_definitions(raw_ns, defs_temp)
    tendrlNS.current_ns.objects["_Node"]["atoms"]["Test"] =  \
        "Test atom class"
    with pytest.raises(Exception):
        tendrlNS._validate_ns_obj_definitions(raw_ns, defs)
    tendrlNS_temp = init()
    tendrlNS_temp.current_ns.objects["_Node"]["flows"]["Test"] = \
        "Test flow class"
    with pytest.raises(Exception):
        tendrlNS_temp._validate_ns_obj_definitions(raw_ns, defs)
    tendrlNS.current_ns.objects["Test"] = "Test Object"
    with pytest.raises(Exception):
        tendrlNS._validate_ns_obj_definitions(raw_ns, defs)
    tendrlNS_temp = init()
    defs = tendrlNS_temp.current_ns.definitions.get_parsed_defs()[raw_ns]
    defs["objects"]["Node"]["atoms"]["Test"] = \
        "Test atom class"
    with pytest.raises(Exception):
        tendrlNS_temp._validate_ns_obj_definitions(raw_ns, defs)
    defs = tendrlNS_temp.current_ns.definitions.get_parsed_defs()[raw_ns]
    defs["objects"]["Node"]["flows"] = maps.NamedDict()
    defs["objects"]["Node"]["flows"]["Test"] = "Test flow class"
    with pytest.raises(Exception):
        tendrlNS_temp._validate_ns_obj_definitions(raw_ns, defs)
    defs = maps.NamedDict()
    tendrlNS.current_ns.objects = None
    tendrlNS._validate_ns_obj_definitions(raw_ns, defs)
예제 #11
0
 def task_status(self, *args, **kwargs):
     if args[0] == "task_id_test":
         return maps.NamedDict(ended=True, succeeded=False)
     if args[0] == "task_test_id":
         return maps.NamedDict(ended=False, succeeded=False)
     if args[0] == "ret_none":
         return {}
     return maps.NamedDict(ended=True, succeeded=True)
def test_render(monkeypatch):
    setattr(__builtin__, "NS", maps.NamedDict())
    monkeypatch.setattr(NodeContext, 'load', load)
    NS.node_context = NodeContext()
    NS.tendrl_context = maps.NamedDict()
    NS.tendrl_context.integration_id = 1
    cluster_node_context = ClusterNodeContext()
    assert cluster_node_context.render() is not None
예제 #13
0
def test_delete(patch_client):
    setattr(__builtin__, "NS", maps.NamedDict())
    setattr(NS, "_int", maps.NamedDict())
    NS._int.wclient = "Temp_obj"
    patch_client.return_value = etcd.Client()
    NS._int.etcd_kwargs = {'port': 1, 'host': 2, 'allow_reconnect': True}
    wreconnect()
    delete()
예제 #14
0
def test_alert_job_status_fail():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.publisher_id = 2
    NS.tendrl_context = maps.NamedDict(integration_id="",
                                       cluster_name="test_name",
                                       sds_name="Test_sds")
    NS.node_context = maps.NamedDict(fqdn="test", node_id="test_node_id")
    tendrl_alert(_test_curr_value, _test_message)
def init():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.tendrl = maps.NamedDict()
    NS.tendrl.objects = maps.NamedDict()
    NS.tendrl.objects.ClusterNodeContext = MockCNC
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = "test"
    NS.publisher_id = "pytest"
def test_check_sync_done():
    init()
    test = CheckSyncDone()
    test.parameters = maps.NamedDict()
    test.parameters['TendrlContext'] = maps.NamedDict()
    test.parameters['TendrlContext.integration_id'] = \
        '94ac63ba-de73-4e7f-8dfa-9010d9554084'
    test.run()
예제 #17
0
def conn(*args):
    if args[0]:
        con = maps.NamedDict(status='LISTEN',
                             laddr=["0.0.0.0", "25"])
    else:
        con = maps.NamedDict(status='READ',
                             laddr=["0.0.0.0", "25"])
    return [con]
예제 #18
0
def init():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.publisher_id = "node_agent"
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict()
    NS.config.data['logging_socket_path'] = "test"
    NS["node_context"] = maps.NamedDict()
    NS.node_context["node_id"] = 1
예제 #19
0
def test_reconnect(patch_client):
    setattr(__builtin__, "NS", maps.NamedDict())
    setattr(NS, "_int", maps.NamedDict())
    patch_client.return_value = etcd.Client()
    NS._int.etcd_kwargs = {'port': 1, 'host': 2, 'allow_reconnect': True}
    reconnect()
    with patch.object(etcd, 'Client', client):
        with patch.object(time, 'sleep', sleep):
            reconnect()
def read(param):
    if "Volumes/3c4b48cc-1a61-4c64-90d6-eba840c00081/status" in param:
        return maps.NamedDict(value="Stopped")
    elif "alert_counters" in param:
        raise etcd.EtcdKeyNotFound
    elif "Networks" in param:
        raise etcd.EtcdKeyNotFound
    elif "Bricks" in param and "status" in param:
        return maps.NamedDict(value="Stopped")
def test_check_sync_done(run):
    run.return_value = ({"stdout": "test_volume", "rc": 0}, '')
    init()
    test = CheckSyncDone()
    test.parameters = maps.NamedDict()
    test.parameters['TendrlContext'] = maps.NamedDict()
    test.parameters['TendrlContext.integration_id'] = \
        '94ac63ba-de73-4e7f-8dfa-9010d9554084'
    test.run()
예제 #22
0
 def __init__(self, *args, **kwargs):
     if kwargs:
         self.test = kwargs["test"]
     self.value = "nodes/Test_object"
     self.updated_at = "latest"
     self._defs = maps.NamedDict(attrs=maps.NamedDict(
         value=maps.NamedDict(type="string"), updated_at=maps.NamedDict(
             type="string")))
     super(BaseObject_Child, self).__init__(*args, **kwargs)
예제 #23
0
def test_render():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.publisher_id = 1
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict(logging_socket_path="test/path")
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    os = Os()
    assert os.render() is not None
예제 #24
0
def test_render():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    disk = Disk()
    with pytest.raises(AttributeError):
        disk.render()
    disk = Disk("Test-Disk")
    disk.render()
예제 #25
0
def test_render():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    block_device = BlockDevice()
    with pytest.raises(AttributeError):
        block_device.render()
    block_device = BlockDevice("Test/Device")
    block_device.render()
예제 #26
0
def init():
    with patch.object(etcd, "Client") as patch_client:
        with patch.object(Client, "read") as patch_read:
            with patch.object(Client, "write") as patch_write:
                with patch.object(cs_utils, "write") as util_write:
                    with patch.object(cs_utils, "read") as util_read:
                        with patch.object(cmn_config,
                                          "load_config") as load_conf:
                            with patch.object(
                                    node.NodeContext,
                                    '_get_node_id') as patch_get_node_id:
                                patch_get_node_id.return_value = 1
                                patch_read.return_value = etcd.Client()
                                patch_write.return_value = etcd.Client()
                                patch_client.return_value = etcd.Client()
                                util_read.return_value = etcd.Client()
                                util_write.return_value = etcd.Client()
                                # creating monitoring NS
                                dummy_conf = {
                                    "etcd_port": "1234",
                                    "etcd_connection": "127.0.0.1"
                                }
                                load_conf.return_value = dummy_conf
                                TendrlNS("monitoring",
                                         "tendrl.monitoring_integration")
                                # overwriting conf
                                setattr(NS, "type", "monitoring")
                                setattr(NS, "publisher_id",
                                        "monitoring_integration")
                                NS._int.etcd_kwargs = {
                                    'port': 1,
                                    'host': 2,
                                    'allow_reconnect': True
                                }
                                NS["config"] = maps.NamedDict()
                                NS["conf"] = maps.NamedDict()
                                NS.config["data"] = maps.NamedDict()
                                NS.conf["dashboards"] = []
                                NS.config.data['tags'] = "test"
                                setattr(NS.config.data, "credentials",
                                        mock.MagicMock())
                                setattr(NS.config.data, "grafana_host",
                                        "127.0.0.1")
                                setattr(NS.config.data, "grafana_port", "1234")
                                setattr(NS.config.data, "datasource_host",
                                        "127.0.0.1")
                                setattr(NS.config.data, "datasource_port",
                                        "234")
                                setattr(NS.config.data, "datasource_name",
                                        "test")
                                setattr(NS.config.data, "datasource_type",
                                        "test_type")
                                setattr(NS.config.data, "access", "test")
                                setattr(NS.config.data, "basicAuth", True)
                                setattr(NS.config.data, "isDefault", True)
                                TendrlNS()
예제 #27
0
def init():
    setattr(__builtin__, "NS", maps.NamedDict())
    setattr(NS, "_int", maps.NamedDict())
    NS._int.etcd_kwargs = {
        'port': 1,
        'host': 2,
        'allow_reconnect': True}
    obj = importlib.import_module("tendrl.commons.tests.fixtures.client")
    NS._int.client = obj.Client()
    NS._int.wclient = obj.Client()
예제 #28
0
def test_getNodeMemory():
    memory = Memory()
    ret = memory._getNodeMemory()
    assert ret is not None
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.publisher_id = "node_agent"
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    with patch.object(__builtin__, "open", open):
        memory._getNodeMemory()
예제 #29
0
def test_install_pyton_gdeploy_pip():
    setattr(__builtin__, "NS", maps.NamedDict())
    setattr(NS, "_int", maps.NamedDict())
    NS["node_context"] = maps.NamedDict()
    NS.node_context["node_id"] = "Test_node_id"
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict()
    NS.config.data['package_source_type'] = 'pip'
    NS.publisher_id = "node_context"
    utils.install_python_gdeploy()
예제 #30
0
def test_find_pid():
    setattr(__builtin__, "NS", maps.NamedDict())
    NS.publisher_id = "node_agent"
    NS["config"] = maps.NamedDict()
    NS.config["data"] = maps.NamedDict(logging_socket_path="test/path")
    NS.node_context = maps.NamedDict()
    NS.node_context.node_id = 1
    cmd = cmd_utils.Command("systemctl show sshd.service")
    out, err, rc = cmd.run()
    sshd_status._find_pid(out)