Esempio n. 1
0
 def test_load_keystore_from_cache(self):
     lib = Library(self._path)
     ks = Keystore(IniAdapter)
     ks.load(self._ini_uri)
     lib.add_keystore("my_store", "ini", self._ini_uri)
     ks1 = lib.get_keystore("my_store")
     ks2 = lib.get_keystore("my_store")
     self.assertIs(ks2, ks1)
Esempio n. 2
0
    def test_create_keystore(self):
        _u = str(uuid4())
        k = Keystore(IniAdapter)
        k.load(self._ini_uri)
        k['test_value'] = _u
        del k

        cp = SafeConfigParser()
        cp.read(self._path)
        self.assertEqual(_u, cp.get("_default", "test_value"))
Esempio n. 3
0
 def test_read_library(self):
     ks = Keystore(IniAdapter)
     ks.load(self._ini_uri)
     with open(self._path, 'w') as fid:
         fid.writelines([
             "[_DEFAULT]\n",
             "type = {}\n".format(ks.ks_type),
             "uri = {}\n".format(ks.uri),
         ])
     lib = Library(self._path)
     ks1 = lib.get_keystore("default")
     self.assertEqual(ks1.ks_type, ks.ks_type)
     self.assertEqual(ks1.uri, ks.uri)
     self.assertEqual(self._ini_uri, ks1.uri)
Esempio n. 4
0
    def test_undefined_key(self):
        _u = str(uuid4())

        cp = SafeConfigParser()
        cp.read(self._path)
        cp.add_section("_default")
        cp.set("_default", 'test_value', _u)
        with open(self._path, 'w') as fp:
            cp.write(fp)

        k = Keystore(IniAdapter)
        k.load(self._ini_uri)
        self.assertNotIn("bad_key", k)
        self.assertEqual(k['bad_key'], None)
Esempio n. 5
0
    def test_remove_key(self):
        _u = str(uuid4())
        _u2 = str(uuid4())

        cp = SafeConfigParser()
        cp.read(self._path)
        cp.add_section("_default")
        cp.set("_default", 'test_value1', _u)
        cp.set("_default", 'test_value2', _u2)
        with open(self._path, 'w') as fp:
            cp.write(fp)

        k = Keystore(IniAdapter)
        k.load(self._ini_uri)
        self.assertEqual(k['test_value1'], _u)
        del k['test_value1']

        del k

        k2 = Keystore(IniAdapter)
        k2.load(self._ini_uri)
        self.assertNotIn("test_value1", k2)
        self.assertEqual(k2['test_value2'], _u2)
Esempio n. 6
0
class TestKeystore(TestCase):
    def setUp(self):
        self._path_template = tempfile.mktemp(".ini")
        self._path_target = tempfile.mktemp(".ini")
        self._path_off_target = tempfile.mktemp(".ini")
        self._path_keystore = tempfile.mktemp(".ini")
        self._keystore = Keystore(IniAdapter)
        self._keystore.load(self._path_keystore)

        self._VALUE1 = str(uuid4())
        self._VALUE2 = str(uuid4())
        self._VALUE3 = str(uuid4())

        self._keystore["VALUE1"] = self._VALUE1
        self._keystore["VALUE2"] = self._VALUE2
        self._keystore["VALUE3"] = self._VALUE3

        with open(self._path_template, 'w') as fp:
            fp.write("\n".join([
                self._path_target,
                "[DEFAULT]",
                "value1 = {{VALUE1}}",
                "value2 = {{VALUE2}}",
                "value3 = {{VALUE3}}"
            ]))

    def test_render_fp(self):
        with open(self._path_template, 'r') as fp:
            tp = TemplateProcessor(fp)

        with open(self._path_off_target, 'w') as fp:
            tp.render_fp(self._keystore, fp)

        cp = ConfigParser()
        cp.read(self._path_off_target)
        cp.get("DEFAULT", "VALUE1", self._VALUE1)
        cp.get("DEFAULT", "VALUE2", self._VALUE2)
        cp.get("DEFAULT", "VALUE3", self._VALUE3)

    def test_render_file(self):
        with open(self._path_template, 'r') as fp:
            tp = TemplateProcessor(fp)
        tp.render_file(self._keystore, self._path_off_target)
        cp = ConfigParser()
        cp.read(self._path_off_target)
        cp.get("DEFAULT", "VALUE1", self._VALUE1)
        cp.get("DEFAULT", "VALUE2", self._VALUE2)
        cp.get("DEFAULT", "VALUE3", self._VALUE3)

    def test_render_target(self):
        with open(self._path_template, 'r') as fp:
            tp = TemplateProcessor(fp)
        tp.render_file(self._keystore)
        cp = ConfigParser()
        cp.read(self._path_target)
        cp.get("DEFAULT", "VALUE1", self._VALUE1)
        cp.get("DEFAULT", "VALUE2", self._VALUE2)
        cp.get("DEFAULT", "VALUE3", self._VALUE3)

    def test_render_string(self):
        with open(self._path_template, 'r') as fp:
            tp = TemplateProcessor(fp)
        s = tp.render(self._keystore).split('\n')
        self.assertEqual(s[0], "[DEFAULT]")
        self.assertEqual(s[1], "value1 = {}".format(self._VALUE1))
        self.assertEqual(s[2], "value2 = {}".format(self._VALUE2))
        self.assertEqual(s[3], "value3 = {}".format(self._VALUE3))