From 932ce8935e0857304e803670a3527536579169d5 Mon Sep 17 00:00:00 2001
From: Bruno Goncalves <bgoncalv@redhat.com>
Date: May 22 2018 14:29:35 +0000
Subject: Fix inventory scripts, to produce output only if related TEST_SUBJECTS is specified.


---

diff --git a/inventory/standard-inventory-docker b/inventory/standard-inventory-docker
index 40006d8..4b9e3ba 100755
--- a/inventory/standard-inventory-docker
+++ b/inventory/standard-inventory-docker
@@ -36,6 +36,8 @@ import tempfile
 import time
 import distutils.util
 
+EMPTY_INVENTORY = {}
+
 
 def main(argv):
     parser = argparse.ArgumentParser(description="Inventory for a container image in a registry")
@@ -54,6 +56,7 @@ def main(argv):
         sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': ')))
     except RuntimeError as ex:
         sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex)))
+        sys.stdout.write(json.dumps(EMPTY_INVENTORY, indent=4, separators=(',', ': ')))
         return 1
 
     return 0
@@ -69,6 +72,8 @@ def inv_list(subjects, docker_extra_args):
             if host_vars:
                 hosts.append(name)
                 variables[name] = host_vars
+    if not hosts:
+        return EMPTY_INVENTORY
     return {"localhost": {"hosts": hosts, "vars": {}},
             "subjects": {"hosts": hosts, "vars": {}},
             "_meta": {"hostvars": variables}}
diff --git a/inventory/standard-inventory-local b/inventory/standard-inventory-local
index b9998a5..9200745 100755
--- a/inventory/standard-inventory-local
+++ b/inventory/standard-inventory-local
@@ -28,6 +28,8 @@ import json
 import os
 import sys
 
+EMPTY_INVENTORY = {}
+
 
 def main(argv):
     parser = argparse.ArgumentParser(description="Inventory for local")
@@ -43,6 +45,7 @@ def main(argv):
         sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': ')))
     except RuntimeError as ex:
         sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex)))
+        sys.stdout.write(json.dumps(EMPTY_INVENTORY, indent=4, separators=(',', ': ')))
         return 1
 
     return 0
@@ -56,6 +59,8 @@ def inv_list():
         if host_vars:
             hosts.append("local")
             variables["local"] = host_vars
+    if not hosts:
+        return EMPTY_INVENTORY
     return {"subjects": {"hosts": hosts, "vars": {}},
             "localhost": {"hosts": hosts, "vars": {}},
             "_meta": {"hostvars": variables}}
diff --git a/inventory/standard-inventory-qcow2 b/inventory/standard-inventory-qcow2
index 06d4bde..43af9a5 100755
--- a/inventory/standard-inventory-qcow2
+++ b/inventory/standard-inventory-qcow2
@@ -70,6 +70,9 @@ chpasswd:
 """.format(DEF_USER, DEF_PASSWD, AUTH_KEY)
 
 
+EMPTY_INVENTORY = {}
+
+
 def main(argv):
     parser = argparse.ArgumentParser(description="Inventory for a QCow2 test image")
     parser.add_argument("--list", action="store_true", help="Verbose output")
@@ -85,6 +88,7 @@ def main(argv):
         sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': ')))
     except RuntimeError as ex:
         sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex)))
+        sys.stdout.write(json.dumps(EMPTY_INVENTORY, indent=4, separators=(',', ': ')))
         return 1
 
     return 0
@@ -99,6 +103,8 @@ def inv_list(subjects):
             if host_vars:
                 hosts.append(subject)
                 variables[subject] = host_vars
+    if not hosts:
+        return EMPTY_INVENTORY
     return {"localhost": {"hosts": hosts, "vars": {}},
             "subjects": {"hosts": hosts, "vars": {}},
             "_meta": {"hostvars": variables}}
diff --git a/inventory/standard-inventory-rpm b/inventory/standard-inventory-rpm
index 7fca09f..400eda8 100755
--- a/inventory/standard-inventory-rpm
+++ b/inventory/standard-inventory-rpm
@@ -31,6 +31,8 @@ import shlex
 import subprocess
 import sys
 
+EMPTY_INVENTORY = {}
+
 
 def main(argv):
     parser = argparse.ArgumentParser(description="Inventory for local RPM installed host")
@@ -47,6 +49,7 @@ def main(argv):
         sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': ')))
     except RuntimeError as ex:
         sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex)))
+        sys.stdout.write(json.dumps(EMPTY_INVENTORY, indent=4, separators=(',', ': ')))
         return 1
 
     return 0
@@ -70,6 +73,8 @@ def getlist(subjects):
             hosts.append("rpms")
             variables["rpms"] = vars
 
+    if not hosts:
+        return EMPTY_INVENTORY
     return {"subjects": {"hosts": hosts, "vars": {}},
             "localhost": {"hosts": hosts, "vars": {}},
             "_meta": {"hostvars": variables}}
diff --git a/scripts/merge-standard-inventory b/scripts/merge-standard-inventory
index 6e3d5be..0909e67 100755
--- a/scripts/merge-standard-inventory
+++ b/scripts/merge-standard-inventory
@@ -164,15 +164,12 @@ class Inventory:
         inventory = json.loads(s)
 
         if not isinstance(inventory, dict):
-            raise ValueError(
-                "inventory JSON does not contain the expected top level dictionary")
+            return
 
         if "subjects" not in inventory or not isinstance(inventory["subjects"], dict):
-            raise ValueError(
-                "inventory JSON does not contain the expected [subjects] dictionary")
+            return
         if "hosts" not in inventory["subjects"] or not isinstance(inventory["subjects"]["hosts"], list):
-            raise ValueError(
-                "inventory JSON does not contain the expected [subjects][hosts] list")
+            return
 
         for h in inventory["subjects"]["hosts"]:
             self.hosts.append(h)
@@ -199,6 +196,9 @@ class Inventory:
                 "inventory JSON does not contain the expected [localhost][hosts] list")
 
     def dumps(self):
+        if not self.hosts:
+            empty_inventory = {}
+            return json.dumps(empty_inventory, indent=4, separators=(',', ': '))
         data = {"subjects": {"hosts": self.hosts, "vars": {}}, "localhost": {
             "hosts": self.hosts, "vars": {}}, "_meta": {"hostvars": self.variables}}
         return json.dumps(data, indent=4, separators=(',', ': '))

