def test_get_fun(): """ Test getting the latest fn run for each minion and matching to a target fn """ ret = { "id": "test-id-1", "jid": "1", "single-key": "single-value", "dict-key": { "dict-subkey-1": "subvalue-1", "dict-subkey-2": "subvalue-2", }, "return": "test-return-1", "fun": "test.ping", } etcd_return.returner(ret) ret = { "id": "test-id-2", "jid": "2", "return": "test-return-2", "fun": "test.collatz", } etcd_return.returner(ret) expected = { "test-id-2": "test.collatz", } assert etcd_return.get_fun("test.collatz") == expected
def test_get_jid(): """ Test getting the return for a given jid """ jid = "123456789" ret = { "id": "test-id-1", "jid": jid, "single-key": "single-value", "dict-key": { "dict-subkey-1": "subvalue-1", "dict-subkey-2": "subvalue-2", }, "return": "test-return-1", } etcd_return.returner(ret) ret = {"id": "test-id-2", "jid": jid, "return": "test-return-2"} etcd_return.returner(ret) expected = { "test-id-1": { "return": "test-return-1" }, "test-id-2": { "return": "test-return-2" }, } assert etcd_return.get_jid(jid) == expected
def test_returner(etcd_client_mock, instance, returner_root, profile_name, etcd_config): """ Test the returner function in etcd_return """ with patch("salt.utils.etcd_util.get_conn", etcd_client_mock): ret = { "id": "test-id", "jid": "123456789", "single-key": "single-value", "dict-key": { "dict-subkey-1": "subvalue-1", "dict-subkey-2": "subvalue-2", }, } # Test returner with ttl in etcd config config = copy.deepcopy(etcd_config) config[profile_name]["etcd.ttl"] = 5 config["etcd.returner_write_profile"] = profile_name with patch.dict(etcd_return.__opts__, config): assert etcd_return.returner(ret) is None dest = "/".join( (returner_root, "jobs", ret["jid"], ret["id"], "{}")) calls = [ call("/".join((returner_root, "minions", ret["id"])), ret["jid"], ttl=5) ] + [ call(dest.format(key), salt.utils.json.dumps(ret[key]), ttl=5) for key in ret ] instance.set.assert_has_calls(calls, any_order=True) # Test returner with ttl in top level config config = copy.deepcopy(etcd_config) config["etcd.ttl"] = 6 instance.set.reset_mock() with patch.dict(etcd_return.__opts__, config): assert etcd_return.returner(ret) is None dest = "/".join( (returner_root, "jobs", ret["jid"], ret["id"], "{}")) calls = [ call("/".join((returner_root, "minions", ret["id"])), ret["jid"], ttl=6) ] + [ call(dest.format(key), salt.utils.json.dumps(ret[key]), ttl=6) for key in ret ] instance.set.assert_has_calls(calls, any_order=True)
def test_returner(prefix, etcd_client): """ Test returning values to etcd """ ret = { "id": "test-id", "jid": "123456789", "single-key": "single-value", "dict-key": { "dict-subkey-1": "subvalue-1", "dict-subkey-2": "subvalue-2", }, } etcd_return.returner(ret) assert etcd_client.get("/".join( (prefix, "minions", ret["id"]))) == ret["jid"] expected = {key: salt.utils.json.dumps(ret[key]) for key in ret} assert (etcd_client.get("/".join((prefix, "jobs", ret["jid"], ret["id"])), recurse=True) == expected)
def test_get_minions(): """ Test getting a list of minions """ ret = { "id": "test-id-1", "jid": "1", } etcd_return.returner(ret) ret = { "id": "test-id-2", "jid": "2", } etcd_return.returner(ret) retval = etcd_return.get_minions() assert len(retval) == 2 assert "test-id-1" in retval assert "test-id-2" in retval