def test_rm(): """ Test the etcd_mod.set state function """ get_mock = MagicMock() rm_mock = MagicMock() dunder_salt = { "etcd.get": get_mock, "etcd.rm": rm_mock, } with patch.dict(etcd_state.__salt__, dunder_salt): # Test removing a key get_mock.return_value = "value" rm_mock.return_value = True expected = { "name": "key", "comment": "Key removed", "result": True, "changes": { "key": "Deleted" }, } assert etcd_state.rm("key") == expected # Test failing to remove an existing key get_mock.return_value = "value" rm_mock.return_value = False expected = { "name": "key", "comment": "Unable to remove key", "result": True, "changes": {}, } assert etcd_state.rm("key") == expected # Test removing a nonexistent key get_mock.return_value = False expected = { "name": "key", "comment": "Key does not exist", "result": True, "changes": {}, } assert etcd_state.rm("key") == expected
def test_with_missing_profile(subtests, prefix, etcd_version, etcd_port): """ Test the correct response when the profile is missing and we can't connect """ if etcd_version in (EtcdVersion.v2, EtcdVersion.v3_v2_mode) and etcd_port != 2379: # Only need to run this once with subtests.test("Test no profile and bad connection in set_"): ret = etcd_state.set_("{}/1".format(prefix), "one") assert not ret["result"] assert ret["comment"] == etcd_state.NO_PROFILE_MSG with subtests.test("Test no profile and bad connection in directory"): ret = etcd_state.directory("{}/2".format(prefix)) assert not ret["result"] assert ret["comment"] == etcd_state.NO_PROFILE_MSG with subtests.test("Test no profile and bad connection in rm"): ret = etcd_state.rm("{}/2/3".format(prefix)) assert not ret["result"] assert ret["comment"] == etcd_state.NO_PROFILE_MSG
def test_basic_operations(subtests, profile_name, prefix): """ Test basic CRUD operations """ with subtests.test("Removing a non-existent key should not explode"): expected = { "name": "{}/2/3".format(prefix), "comment": "Key does not exist", "result": True, "changes": {}, } assert etcd_state.rm("{}/2/3".format(prefix), profile=profile_name) == expected with subtests.test("We should be able to set a value"): expected = { "name": "{}/1".format(prefix), "comment": "New key created", "result": True, "changes": { "{}/1".format(prefix): "one" }, } assert (etcd_state.set_("{}/1".format(prefix), "one", profile=profile_name) == expected) with subtests.test( "We should be able to create an empty directory and set values in it" ): expected = { "name": "{}/2".format(prefix), "comment": "New directory created", "result": True, "changes": { "{}/2".format(prefix): "Created" }, } assert (etcd_state.directory("{}/2".format(prefix), profile=profile_name) == expected) expected = { "name": "{}/2/3".format(prefix), "comment": "New key created", "result": True, "changes": { "{}/2/3".format(prefix): "two-three" }, } assert (etcd_state.set_("{}/2/3".format(prefix), "two-three", profile=profile_name) == expected) with subtests.test("We should be able to remove an existing key"): expected = { "name": "{}/2/3".format(prefix), "comment": "Key removed", "result": True, "changes": { "{}/2/3".format(prefix): "Deleted" }, } assert etcd_state.rm("{}/2/3".format(prefix), profile=profile_name) == expected