salt/tests/integration/modules/test_dockermod.py

79 lines
2.2 KiB
Python
Raw Normal View History

2017-05-02 22:16:51 +00:00
# -*- coding: utf-8 -*-
'''
Integration tests for the docker_container states
'''
# Import Python Libs
from __future__ import absolute_import
import functools
import random
2017-05-02 22:31:38 +00:00
import string
2017-05-02 22:16:51 +00:00
import tempfile
# Import Salt Testing Libs
from tests.support.unit import skipIf
from tests.support.case import ModuleCase
from tests.support.paths import FILES, TMP
from tests.support.helpers import destructiveTest
from tests.support.mixins import SaltReturnAssertsMixin
2017-05-02 22:31:38 +00:00
# Import Salt Libs
import salt.utils
2017-05-02 22:16:51 +00:00
def _random_name(prefix=''):
ret = prefix
for _ in range(8):
ret += random.choice(string.ascii_lowercase)
return ret
def with_random_name(func):
'''
generate a randomized name for a container
'''
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
name = _random_name(prefix='salt_')
return func(self, _random_name(prefix='salt_test_'), *args, **kwargs)
return wrapper
@destructiveTest
@skipIf(not salt.utils.which('dockerd'), 'Docker not installed')
class DockerCallTestCase(ModuleCase, SaltReturnAssertsMixin):
'''
Test docker_container states
'''
@with_random_name
def setUp(self, name):
'''
'''
# Create temp dir
self.random_name = name
2017-05-02 22:33:36 +00:00
self.tmp_build_dir= tempfile.mkdtemp(dir=TMP)
2017-05-02 22:16:51 +00:00
self.run_state('file.managed',
2017-05-02 22:31:38 +00:00
source='salt://docker_non_root/Dockerfile',
2017-05-02 22:33:36 +00:00
name='{0}/Dockerfile'.format(self.tmp_build_dir))
2017-05-02 22:16:51 +00:00
self.run_state('docker_image.present',
2017-05-02 22:33:36 +00:00
build=self.tmp_build_dir,
2017-05-02 22:16:51 +00:00
name=self.random_name)
self.run_state('docker_container.running',
name=self.random_name,
image=self.random_name)
def tearDown(self):
2017-05-02 22:33:36 +00:00
self.run_state('file.absent',
name=self.tmp_build_dir)
self.run_state('docker_container.absent',
name=self.random_name,
force=True)
self.run_state('docker_image.absent',
name=self.random_name,
force=True)
2017-05-02 22:16:51 +00:00
def test_docker_call(self):
ret = self.run_function('docker.call', [self.random_name, 'test.ping'])
self.assertTrue(ret)