IMPALA-9100: Handle duplicate occurrences of flags for tests/run-tests.py

If someone passes --skip-stress multiple times to tests/run-tests.py,
it currently only removes one of the occurrences from the arguments
and allows the other one to pass through to pytest. This causes pytest
to immediately error out. This behavior is seen on the docker-based
tests, because test-with-docker.py specifies --skip-stress and
bin/run-all-tests.sh adds another --skip-stress for core runs.

This changes tests/run-tests.py to handle multiple occurrences of
--skip-stress, --skip-parallel, and --skip-serial.

Testing:
 - Tested manually with duplicate skip flags.

Change-Id: I60dc9a898f69804e2a53c05b5dfab2f948a22097
Reviewed-on: http://gerrit.cloudera.org:8080/14629
Reviewed-by: Joe McDonnell <joemcdonnell@cloudera.com>
Tested-by: Impala Public Jenkins <impala-public-jenkins@cloudera.com>
diff --git a/tests/run-tests.py b/tests/run-tests.py
index 36d915f..55b002a 100755
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -238,19 +238,24 @@
     print "<" * 80
 
 
+def detect_and_remove_flag(flag):
+  """Find any usage of 'flag' in sys.argv and remove them. Return true if the
+     flag is found. Return false otherwise."""
+  flag_exists = False
+  # Handle multiple occurrences of the same flag
+  while flag in sys.argv:
+    flag_exists = True
+    sys.argv.remove(flag)
+  return flag_exists
+
+
 if __name__ == "__main__":
   # Ensure that logging is configured for the 'run-test.py' wrapper itself.
   configure_logging()
   exit_on_error = '-x' in sys.argv or '--exitfirst' in sys.argv
-  skip_serial = '--skip-serial' in sys.argv
-  if skip_serial:
-    sys.argv.remove("--skip-serial")
-  skip_stress = '--skip-stress' in sys.argv
-  if skip_stress:
-    sys.argv.remove("--skip-stress")
-  skip_parallel = '--skip-parallel' in sys.argv
-  if skip_parallel:
-    sys.argv.remove("--skip-parallel")
+  skip_serial = detect_and_remove_flag('--skip-serial')
+  skip_stress = detect_and_remove_flag('--skip-stress')
+  skip_parallel = detect_and_remove_flag('--skip-parallel')
   test_executor = TestExecutor(exit_on_error=exit_on_error)
 
   # If the user is just asking for --help, just print the help test and then exit.