def test_set(self):
        '''
        Test if it set a key in etcd, by direct path
        '''
        with patch.dict(etcd_mod.__utils__,
                        {'etcd_util.get_conn': self.EtcdClientMock}):
            self.instance.set.return_value = 'stack'
            self.assertEqual(etcd_mod.set_('salt', 'stack'), 'stack')
            self.instance.set.assert_called_with('salt',
                                                 'stack',
                                                 directory=False,
                                                 ttl=None)

            self.instance.set.return_value = True
            self.assertEqual(etcd_mod.set_('salt', '', directory=True), True)
            self.instance.set.assert_called_with('salt',
                                                 '',
                                                 directory=True,
                                                 ttl=None)

            self.assertEqual(etcd_mod.set_('salt', '', directory=True, ttl=5),
                             True)
            self.instance.set.assert_called_with('salt',
                                                 '',
                                                 directory=True,
                                                 ttl=5)

            self.assertEqual(etcd_mod.set_('salt', '', None, 10, True), True)
            self.instance.set.assert_called_with('salt',
                                                 '',
                                                 directory=True,
                                                 ttl=10)

            self.instance.set.side_effect = Exception
            self.assertRaises(Exception, etcd_mod.set_, 'err', 'stack')
Esempio n. 2
0
    def test_set(self):
        """
        Test if it set a key in etcd, by direct path
        """
        with patch.dict(etcd_mod.__utils__,
                        {"etcd_util.get_conn": self.EtcdClientMock}):
            self.instance.set.return_value = "stack"
            self.assertEqual(etcd_mod.set_("salt", "stack"), "stack")
            self.instance.set.assert_called_with("salt",
                                                 "stack",
                                                 directory=False,
                                                 ttl=None)

            self.instance.set.return_value = True
            self.assertEqual(etcd_mod.set_("salt", "", directory=True), True)
            self.instance.set.assert_called_with("salt",
                                                 "",
                                                 directory=True,
                                                 ttl=None)

            self.assertEqual(etcd_mod.set_("salt", "", directory=True, ttl=5),
                             True)
            self.instance.set.assert_called_with("salt",
                                                 "",
                                                 directory=True,
                                                 ttl=5)

            self.assertEqual(etcd_mod.set_("salt", "", None, 10, True), True)
            self.instance.set.assert_called_with("salt",
                                                 "",
                                                 directory=True,
                                                 ttl=10)

            self.instance.set.side_effect = Exception
            self.assertRaises(Exception, etcd_mod.set_, "err", "stack")
Esempio n. 3
0
    def test_set(self):
        '''
        Test if it set a value in etcd, by direct path
        '''
        class MockEtcd(object):
            """
            Mock of etcd
            """
            value = 'salt'

            def __init__(self):
                self.key = None
                self.value = None

            def write(self, key, value):
                """
                Mock of write method
                """
                self.key = key
                self.value = value
                if key == '':
                    raise KeyError
                elif key == 'err':
                    raise
                return MockEtcd

        with patch.object(etcd_util, 'get_conn',
                          MagicMock(return_value=MockEtcd())):
            self.assertEqual(etcd_mod.set_('salt', 'stack'), 'salt')

            self.assertEqual(etcd_mod.set_('', 'stack'), '')

            self.assertRaises(Exception, etcd_mod.set_, 'err', 'stack')
Esempio n. 4
0
def test_set(etcd_client_mock, instance):
    """
    Test if it set a key in etcd, by direct path
    """
    with patch.dict(etcd_mod.__utils__,
                    {"etcd_util.get_conn": etcd_client_mock}):
        instance.set.return_value = "stack"
        assert etcd_mod.set_("salt", "stack") == "stack"
        instance.set.assert_called_with("salt",
                                        "stack",
                                        directory=False,
                                        ttl=None)

        instance.set.return_value = True
        assert etcd_mod.set_("salt", "", directory=True) is True
        instance.set.assert_called_with("salt", "", directory=True, ttl=None)

        assert etcd_mod.set_("salt", "", directory=True, ttl=5) is True
        instance.set.assert_called_with("salt", "", directory=True, ttl=5)

        assert etcd_mod.set_("salt", "", None, 10, True) is True
        instance.set.assert_called_with("salt", "", directory=True, ttl=10)

        instance.set.side_effect = Exception
        pytest.raises(Exception, etcd_mod.set_, "err", "stack")
Esempio n. 5
0
def test_basic_operations(subtests, profile_name, prefix):
    """
    Make sure we can do the basics
    """
    with subtests.test("There should be no entries at the start with our prefix."):
        assert etcd_mod.get_(prefix, recurse=True, profile=profile_name) is None

    with subtests.test("We should be able to set and retrieve simple values"):
        etcd_mod.set_("{}/1".format(prefix), "one", profile=profile_name)
        assert (
            etcd_mod.get_("{}/1".format(prefix), recurse=False, profile=profile_name)
            == "one"
        )

    with subtests.test("We should be able to update and retrieve those values"):
        updated = {
            "1": "not one",
            "2": {
                "3": "two-three",
                "4": "two-four",
            },
        }
        etcd_mod.update(updated, path=prefix, profile=profile_name)
        assert etcd_mod.get_(prefix, recurse=True, profile=profile_name) == updated

    with subtests.test("We should be list all top level values at a directory"):
        expected = {
            prefix: {
                "{}/1".format(prefix): "not one",
                "{}/2/".format(prefix): {},
            },
        }
        assert etcd_mod.ls_(path=prefix, profile=profile_name) == expected

    with subtests.test("We should be able to remove values and get a tree hierarchy"):
        updated = {
            "2": {
                "3": "two-three",
                "4": "two-four",
            },
        }
        etcd_mod.rm_("{}/1".format(prefix), profile=profile_name)
        assert etcd_mod.tree(path=prefix, profile=profile_name) == updated

    with subtests.test("updates should be able to be caught by waiting in read"):
        return_list = []

        def wait_func(return_list):
            return_list.append(
                etcd_mod.watch("{}/1".format(prefix), timeout=30, profile=profile_name)
            )

        wait_thread = threading.Thread(target=wait_func, args=(return_list,))
        wait_thread.start()
        time.sleep(1)
        etcd_mod.set_("{}/1".format(prefix), "one", profile=profile_name)
        wait_thread.join()
        modified = return_list.pop()
        assert modified["key"] == "{}/1".format(prefix)
        assert modified["value"] == "one"
Esempio n. 6
0
    def test_set(self):
        '''
        Test if it set a value in etcd, by direct path
        '''
        class MockEtcd(object):
            """
            Mock of etcd
            """
            value = 'salt'

            def __init__(self):
                self.key = None
                self.value = None

            def write(self, key, value):
                """
                Mock of write method
                """
                self.key = key
                self.value = value
                if key == '':
                    raise KeyError
                elif key == 'err':
                    raise
                return MockEtcd

        with patch.object(etcd_util, 'get_conn',
                          MagicMock(return_value=MockEtcd())):
            self.assertEqual(etcd_mod.set_('salt', 'stack'), 'salt')

            self.assertEqual(etcd_mod.set_('', 'stack'), '')

            self.assertRaises(Exception, etcd_mod.set_, 'err', 'stack')
Esempio n. 7
0
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 get_"):
            assert etcd_mod.get_("{}/1".format(prefix)) is None

        with subtests.test("Test no profile and bad connection in set_"):
            assert etcd_mod.set_("{}/1".format(prefix), "lol") is None

        with subtests.test("Test no profile and bad connection in update"):
            assert etcd_mod.update({"{}/1".format(prefix): "SIUUU"}) is None

        with subtests.test("Test no profile and bad connection in watch"):
            assert etcd_mod.watch("{}/1".format(prefix)) is None

        with subtests.test("Test no profile and bad connection in ls_"):
            assert etcd_mod.ls_() is None

        with subtests.test("Test no profile and bad connection in rm"):
            assert etcd_mod.rm_("{}/1".format(prefix)) is None

        with subtests.test("Test no profile and bad connection in tree"):
            assert etcd_mod.tree() is None
Esempio n. 8
0
    def test_set(self):
        '''
        Test if it set a key in etcd, by direct path
        '''
        with patch.dict(etcd_mod.__utils__, {'etcd_util.get_conn': self.EtcdClientMock}):
            self.instance.set.return_value = 'stack'
            self.assertEqual(etcd_mod.set_('salt', 'stack'), 'stack')
            self.instance.set.assert_called_with('salt', 'stack', directory=False, ttl=None)

            self.instance.set.return_value = True
            self.assertEqual(etcd_mod.set_('salt', '', directory=True), True)
            self.instance.set.assert_called_with('salt', '', directory=True, ttl=None)

            self.assertEqual(etcd_mod.set_('salt', '', directory=True, ttl=5), True)
            self.instance.set.assert_called_with('salt', '', directory=True, ttl=5)

            self.assertEqual(etcd_mod.set_('salt', '', None, 10, True), True)
            self.instance.set.assert_called_with('salt', '', directory=True, ttl=10)

            self.instance.set.side_effect = Exception
            self.assertRaises(Exception, etcd_mod.set_, 'err', 'stack')