nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: google/turbinia
Pull request #474 (2019-12-06 23:29:32)

Allow optional config vars to be non-existent


Patch:
diff --git a/turbinia/config/__init__.py b/turbinia/config/__init__.py
index 93f9792..8bc2745 100644
--- a/turbinia/config/__init__.py
+++ b/turbinia/config/__init__.py
@@ -163,13 +163,24 @@ def ValidateAndSetConfig(_config):
 
   CONFIGVARS = REQUIRED_VARS + OPTIONAL_VARS
   for var in CONFIGVARS:
+    empty_value = False
     if not hasattr(_config, var):
-      raise TurbiniaException(
-          'No config attribute {0:s}:{1:s}'.format(_config.configSource, var))
+      if var in OPTIONAL_VARS:
+        log.debug(
+            'Setting non-existent but optional config variable {0:s} to '
+            'None'.format(var))
+        empty_value = True
+      else:
+        raise TurbiniaException(
+            'Required config attribute {0:s}:{1:s} not in config'.format(
+                _config.configSource, var))
     if var in REQUIRED_VARS and getattr(_config, var) is None:
       raise TurbiniaException(
           'Config attribute {0:s}:{1:s} is not set'.format(
               _config.configSource, var))
 
     # Set the attribute in the current module
-    setattr(sys.modules[__name__], var, getattr(_config, var))
+    if empty_value:
+      setattr(sys.modules[__name__], var, None)
+    else:
+      setattr(sys.modules[__name__], var, getattr(_config, var))
diff --git a/turbinia/workers/__init__.py b/turbinia/workers/__init__.py
index 9516f62..657d28c 100644
--- a/turbinia/workers/__init__.py
+++ b/turbinia/workers/__init__.py
@@ -613,7 +613,7 @@ class TurbiniaTask(object):
                   self.turbinia_version, turbinia.__version__))
           self.result.log(message, level=logging.ERROR)
           self.result.status = message
-          return self.result
+          return self.result.serialize()
 
         self._evidence_config = evidence.config
         self.result = self.run(evidence, self.result)


Test patch:
diff --git a/turbinia/config/turbinia_config_test.py b/turbinia/config/turbinia_config_test.py
index 2a63456..51e1079 100644
--- a/turbinia/config/turbinia_config_test.py
+++ b/turbinia/config/turbinia_config_test.py
@@ -110,6 +110,15 @@ class TestTurbiniaConfig(unittest.TestCase):
     self.assertEqual(config.UNSETKEY, None)
     self.assertEqual(config.SETKEY, 'bar')
 
+  def testNonexistentOptionalKeyConfig(self):
+    """Test that optional vars don't need to exist."""
+    self.WriteConfig('SETKEY = "bar"\n')
+    config.REQUIRED_VARS = ['SETKEY']
+    config.OPTIONAL_VARS = ['UNSETKEY']
+    config.LoadConfig()
+    self.assertEqual(config.UNSETKEY, None)
+    self.assertEqual(config.SETKEY, 'bar')
+
   def testMissingConfigPath(self):
     """Test non-existent config path."""
     config.CONFIGPATH = ['DOESNOTEXIST']
Repository: google/turbinia
Pull request #501 (2020-02-10 02:23:47)

Add evidence path to metadata file
This is so we don't have to infer the path from the metadata file location and name, and the Timesketch GCS importer can read it from the json file.

aarontp: @berggren: This should be good to review now; I fixed the tests.

Patch:
diff --git a/turbinia/output_manager.py b/turbinia/output_manager.py
index 32fb89a..de498db 100644
--- a/turbinia/output_manager.py
+++ b/turbinia/output_manager.py
@@ -143,6 +143,7 @@ class OutputManager(object):
 
     if evidence_.save_metadata:
       metadata = evidence_.config.copy()
+      metadata['evidence_path'] = path
       metadata_path = '{0:s}.metadata.json'.format(local_path)
       try:
         json_str = json.dumps(metadata)


Test patch:
diff --git a/turbinia/output_manager_test.py b/turbinia/output_manager_test.py
index 73613b6..779b1f3 100644
--- a/turbinia/output_manager_test.py
+++ b/turbinia/output_manager_test.py
@@ -16,6 +16,7 @@
 
 from __future__ import unicode_literals
 
+import json
 import unittest
 import os
 import shutil
@@ -142,7 +143,8 @@ class TestLocalOutputManager(unittest.TestCase):
     dst_file = os.path.join(local_dir, test_file)
     test_evidence = evidence.Evidence()
     test_evidence.save_metadata = True
-    test_evidence.config = {'foo': 'bar'}
+    config_input = {'foo': 'bar'}
+    test_evidence.config = config_input
 
     with open(src_file, 'w') as fh:
       fh.write(test_contents)
@@ -155,9 +157,10 @@ class TestLocalOutputManager(unittest.TestCase):
     self.assertIsInstance(return_evidence, evidence.Evidence)
     self.assertIn(dst_file, return_evidence.saved_path)
     metadata_file = '{0:s}.metadata.json'.format(dst_file)
-    metadata_contents = b'{"foo": "bar"}'
     self.assertTrue(os.path.exists(metadata_file))
-    self.assertEqual(open(metadata_file, 'rb').read(), metadata_contents)
+    config_input['evidence_path'] = dst_file
+    metadata_contents = json.loads(open(metadata_file, 'rb').read())
+    self.assertDictEqual(config_input, metadata_contents)
 
 
 class TestLocalOutputWriter(unittest.TestCase):