def test_set(): """ Test the etcd_mod.set state function """ get_mock = MagicMock() set_mock = MagicMock() dunder_salt = { "etcd.get": get_mock, "etcd.set": set_mock, } with patch.dict(etcd_state.__salt__, dunder_salt): # Test new key creation get_mock.return_value = None set_mock.return_value = "new value" expected = { "name": "new_key", "comment": "New key created", "result": True, "changes": { "new_key": "new value" }, } assert etcd_state.set_("new_key", "new value", profile="test") == expected # Test key updating get_mock.return_value = "old value" set_mock.return_value = "new value" expected = { "name": "new_key", "comment": "Key value updated", "result": True, "changes": { "new_key": "new value" }, } assert etcd_state.set_("new_key", "new value", profile="test") == expected # Test setting the same value to a key get_mock.return_value = "value" set_mock.return_value = "value" expected = { "name": "key", "comment": "Key contains correct value", "result": True, "changes": {}, } assert etcd_state.set_("key", "value", profile="test") == 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