From ffc702c8f4cd9d4e57f4824fe4f5ff21548ad4d3 Mon Sep 17 00:00:00 2001 From: "Gareth J. Greenaway" Date: Wed, 17 Oct 2018 15:32:01 -0700 Subject: [PATCH] Copying changes from integration.sdb.test_vault over to integration.module.test_vault. --- tests/integration/modules/test_vault.py | 37 +++++++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/tests/integration/modules/test_vault.py b/tests/integration/modules/test_vault.py index 2539fd574da1..5c697b4553bb 100644 --- a/tests/integration/modules/test_vault.py +++ b/tests/integration/modules/test_vault.py @@ -34,7 +34,7 @@ def setUp(self): ''' SetUp vault container ''' - if self.count == 0: + if VaultTestCase.count == 0: config = '{"backend": {"file": {"path": "/vault/file"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h"}' self.run_state('docker_image.present', name='vault', tag='0.9.6') self.run_state( @@ -54,8 +54,31 @@ def setUp(self): cmd='/usr/local/bin/vault login token=testsecret', env={'VAULT_ADDR': 'http://127.0.0.1:8200'}, ) - if ret != 0: - self.skipTest('unable to login to vault') + login_attempts = 1 + # If the login failed, container might have stopped + # attempt again, maximum of three times before + # skipping. + while ret != 0: + self.run_state( + 'docker_container.running', + name='vault', + image='vault:0.9.6', + port_bindings='8200:8200', + environment={ + 'VAULT_DEV_ROOT_TOKEN_ID': 'testsecret', + 'VAULT_LOCAL_CONFIG': config, + }, + cap_add='IPC_LOCK', + ) + time.sleep(5) + ret = self.run_function( + 'cmd.retcode', + cmd='/usr/local/bin/vault login token=testsecret', + env={'VAULT_ADDR': 'http://127.0.0.1:8200'}, + ) + login_attempts += 1 + if login_attempts >= 3: + self.skipTest('unable to login to vault') ret = self.run_function( 'cmd.retcode', cmd='/usr/local/bin/vault policy write testpolicy {0}/vault.hcl'.format(FILES), @@ -63,16 +86,18 @@ def setUp(self): ) if ret != 0: self.skipTest('unable to assign policy to vault') - self.count += 1 + VaultTestCase.count += 1 def tearDown(self): ''' TearDown vault container ''' def count_tests(funcobj): - return inspect.ismethod(funcobj) and funcobj.__name__.startswith('test_') + return (inspect.ismethod(funcobj) or + inspect.isfunction(funcobj)) and \ + funcobj.__name__.startswith('test_') numtests = len(inspect.getmembers(VaultTestCase, predicate=count_tests)) - if self.count >= numtests: + if VaultTestCase.count >= numtests: self.run_state('docker_container.stopped', name='vault') self.run_state('docker_container.absent', name='vault') self.run_state('docker_image.absent', name='vault', force=True)