From fb9efd3c7eafa8c649a06557eae387c8e98a400c Mon Sep 17 00:00:00 2001
From: Matt Clay <matt@mystile.com>
Date: Thu, 11 May 2017 21:21:11 +0800
Subject: [PATCH] Add `--remote-terminate` option to ansible-test.

Set `--remote-terminate success` for osx tests.
---
 test/runner/lib/delegation.py | 5 ++++-
 test/runner/lib/util.py       | 1 +
 test/runner/test.py           | 7 +++++++
 test/utils/shippable/osx.sh   | 2 +-
 4 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/test/runner/lib/delegation.py b/test/runner/lib/delegation.py
index d991fd6c035..40721d70cf4 100644
--- a/test/runner/lib/delegation.py
+++ b/test/runner/lib/delegation.py
@@ -245,6 +245,7 @@ def delegate_remote(args, exclude, require):
     version = parts[1]
 
     core_ci = AnsibleCoreCI(args, platform, version, stage=args.remote_stage)
+    success = False
 
     try:
         core_ci.start()
@@ -277,11 +278,13 @@ def delegate_remote(args, exclude, require):
 
         try:
             manage.ssh(cmd, ssh_options)
+            success = True
         finally:
             manage.ssh('rm -rf /tmp/results && cp -a ansible/test/results /tmp/results')
             manage.download('/tmp/results', 'test')
     finally:
-        pass
+        if args.remote_terminate == 'always' or (args.remote_terminate == 'success' and success):
+            core_ci.stop()
 
 
 def generate_command(args, path, options, exclude, require):
diff --git a/test/runner/lib/util.py b/test/runner/lib/util.py
index e075f92458c..4880bf6136c 100644
--- a/test/runner/lib/util.py
+++ b/test/runner/lib/util.py
@@ -467,6 +467,7 @@ class EnvironmentConfig(CommonConfig):
 
         self.remote_stage = args.remote_stage  # type: str
         self.remote_aws_region = args.remote_aws_region  # type: str
+        self.remote_terminate = args.remote_terminate  # type: str
 
         self.requirements = args.requirements  # type: bool
 
diff --git a/test/runner/test.py b/test/runner/test.py
index 30b242ef541..873c5054047 100755
--- a/test/runner/test.py
+++ b/test/runner/test.py
@@ -456,6 +456,7 @@ def add_environments(parser, tox_version=False, tox_only=False):
             remote=None,
             remote_stage=None,
             remote_aws_region=None,
+            remote_terminate=None,
         )
 
         return
@@ -486,6 +487,12 @@ def add_environments(parser, tox_version=False, tox_only=False):
                         choices=sorted(AWS_ENDPOINTS),
                         default=None)
 
+    remote.add_argument('--remote-terminate',
+                        metavar='WHEN',
+                        help='terminate remote instance: %(choices)s (default: %(default)s)',
+                        choices=['never', 'always', 'success'],
+                        default='never')
+
 
 def add_extra_docker_options(parser, integration=True):
     """
diff --git a/test/utils/shippable/osx.sh b/test/utils/shippable/osx.sh
index 5af7eab052b..5e6c18c91f7 100755
--- a/test/utils/shippable/osx.sh
+++ b/test/utils/shippable/osx.sh
@@ -9,4 +9,4 @@ platform="${args[0]}"
 version="${args[1]}"
 target="posix/ci/"
 
-ansible-test integration --color -v --retry-on-error "${target}" --remote "${platform}/${version}" --exclude "posix/ci/cloud/"
+ansible-test integration --color -v --retry-on-error "${target}" --remote "${platform}/${version}" --remote-terminate success --exclude "posix/ci/cloud/"