def testInterpolation(self):
    kb = rdf_client.KnowledgeBase()

    # No users yet, this should raise
    self.assertRaises(artifact_utils.KnowledgeBaseInterpolationError, list,
                      artifact_utils.InterpolateKbAttributes(
                          "test%%users.username%%test", kb))

    # Now we have two users
    kb.users.Append(rdf_client.User(username="joe", uid=1))
    kb.users.Append(rdf_client.User(username="jim", uid=2))
    kb.Set("environ_allusersprofile", "c:\\programdata")

    paths = artifact_utils.InterpolateKbAttributes("test%%users.username%%test",
                                                   kb)
    paths = list(paths)
    self.assertEqual(len(paths), 2)
    self.assertItemsEqual(paths, ["testjoetest", "testjimtest"])

    paths = artifact_utils.InterpolateKbAttributes(
        "%%environ_allusersprofile%%\\a", kb)
    self.assertEqual(list(paths), ["c:\\programdata\\a"])

    # Check a bad attribute raises
    self.assertRaises(
        artifact_utils.KnowledgeBaseInterpolationError, list,
        artifact_utils.InterpolateKbAttributes("%%nonexistent%%\\a", kb))

    # Empty values should also raise
    kb.Set("environ_allusersprofile", "")
    self.assertRaises(
        artifact_utils.KnowledgeBaseInterpolationError, list,
        artifact_utils.InterpolateKbAttributes(
            "%%environ_allusersprofile%%\\a", kb))

    # No users have temp defined, so this should raise
    self.assertRaises(artifact_utils.KnowledgeBaseInterpolationError, list,
                      artifact_utils.InterpolateKbAttributes(
                          "%%users.temp%%\\a", kb))

    # One user has users.temp defined, the others do not.  This is common on
    # windows where users have been created but have never logged in. We should
    # get just one value back.
    kb.users.Append(rdf_client.User(
        username="jason", uid=1, temp="C:\\Users\\jason\\AppData\\Local\\Temp"))
    paths = artifact_utils.InterpolateKbAttributes(
        r"%%users.temp%%\abcd", kb)
    self.assertItemsEqual(paths,
                          ["C:\\Users\\jason\\AppData\\Local\\Temp\\abcd"])
Check we can interpolate values from the knowledge base.
dataset/ETHPy150Open google/grr/grr/lib/artifact_utils_test.py/ArtifactKBTest.testInterpolation