Blame SOURCES/bz1022533-invalid_use_of_options-1.patch

d67cb5
commit 1db6440eb38789bcb59451cb8adb39f2084f9443
d67cb5
Author: Marek 'marx' Grac <mgrac@redhat.com>
d67cb5
Date:   Mon Oct 21 12:44:39 2013 +0200
d67cb5
d67cb5
    testing: Check if fence agent uses only valid keys in options["--???"]
d67cb5
    
d67cb5
    Valid keys are defined in fencing library or fence agent itself. This check
d67cb5
    will be run at every build, so we should be protected against case when
d67cb5
    developer unintentionally use new key (e.g. typo). These type of bugs are
d67cb5
    usually not detected by static analyzers.
d67cb5
d67cb5
diff --git a/fence/agents/lib/check_used_options.py b/fence/agents/lib/check_used_options.py
d67cb5
new file mode 100755
d67cb5
index 0000000..2d75756
d67cb5
--- /dev/null
d67cb5
+++ b/fence/agents/lib/check_used_options.py
d67cb5
@@ -0,0 +1,65 @@
d67cb5
+#!/usr/bin/python
d67cb5
+
d67cb5
+## Check if fence agent uses only options["--??"] which are defined in fencing library or 
d67cb5
+## fence agent itself
d67cb5
+##
d67cb5
+## Usage: ./check_used_options.py fence-agent (e.g. lpar/fence_lpar.py)
d67cb5
+##
d67cb5
+
d67cb5
+import sys, re
d67cb5
+sys.path.append("@FENCEAGENTSLIBDIR@")
d67cb5
+from fencing import all_opt
d67cb5
+
d67cb5
+def main():
d67cb5
+	agent = sys.argv[1]
d67cb5
+
d67cb5
+	available = { }
d67cb5
+
d67cb5
+	## all_opt from fencing library are imported
d67cb5
+	for k in all_opt.keys():
d67cb5
+		if all_opt[k].has_key("longopt"):
d67cb5
+			available["--" + all_opt[k]["longopt"]] = True
d67cb5
+
d67cb5
+	## add UUID which is derived automatically from --plug if possible
d67cb5
+	available["--uuid"] = True
d67cb5
+
d67cb5
+	## all_opt defined in fence agent are found
d67cb5
+	agent_file = open(agent)
d67cb5
+	opt_re = re.compile("\s*all_opt\[\"([^\"]*)\"\] = {")
d67cb5
+	opt_longopt_re = re.compile("\s*\"longopt\" : \"([^\"]*)\"")
d67cb5
+
d67cb5
+	in_opt = False
d67cb5
+	for line in agent_file:
d67cb5
+		if opt_re.search(line) != None:
d67cb5
+			in_opt = True
d67cb5
+		if in_opt and opt_longopt_re.search(line) != None:
d67cb5
+			available["--" + opt_longopt_re.search(line).group(1)] = True
d67cb5
+			in_opt = False
d67cb5
+
d67cb5
+	## check if all options are defined
d67cb5
+	agent_file = open(agent)
d67cb5
+	option_use_re = re.compile("options\[\"(--[^\"]*)\"\]")
d67cb5
+	option_has_re = re.compile("options.has_key\(\"(--[^\"]*)\"\)")
d67cb5
+
d67cb5
+	counter = 0
d67cb5
+	without_errors = True
d67cb5
+	for line in agent_file:
d67cb5
+		counter += 1
d67cb5
+
d67cb5
+		for x in option_use_re.findall(line):
d67cb5
+			if not available.has_key(x):
d67cb5
+				print "ERROR on line %d in %s: option %s is not defined" % (counter, agent, option_use_re.search(line).group(1))
d67cb5
+				without_errors = False
d67cb5
+
d67cb5
+		for x in option_has_re.findall(line):
d67cb5
+			if not available.has_key(x):
d67cb5
+				print "ERROR on line %d in %s: option %s is not defined" % (counter, agent, option_has_re.search(line).group(1))
d67cb5
+				without_errors = False
d67cb5
+
d67cb5
+	if without_errors:
d67cb5
+		sys.exit(0)
d67cb5
+	else:
d67cb5
+		sys.exit(1)
d67cb5
+
d67cb5
+if __name__ == "__main__":
d67cb5
+	main()
d67cb5
diff --git a/make/fencebuild.mk b/make/fencebuild.mk
d67cb5
index e86d03c..d775e92 100644
d67cb5
--- a/make/fencebuild.mk
d67cb5
+++ b/make/fencebuild.mk
d67cb5
@@ -1,4 +1,8 @@
d67cb5
 $(TARGET): $(SRC)
d67cb5
+	if [ 0 -eq `echo "$(SRC)" | grep fence_ &> /dev/null; echo $$?` ]; then \
d67cb5
+		PYTHONPATH=$(abs_srcdir)/../lib:$(abs_builddir)/../lib $(top_srcdir)/fence/agents/lib/check_used_options.py $(SRC); \
d67cb5
+	else true ; fi
d67cb5
+
d67cb5
 	bash $(top_srcdir)/scripts/fenceparse \
d67cb5
 		$(top_srcdir)/make/copyright.cf REDHAT_COPYRIGHT \
d67cb5
 		$(VERSION) \