add lxd tests

This commit is contained in:
Daniel Wallace 2018-08-08 15:01:19 -05:00
parent 3c694af952
commit 0abd02a6f6
No known key found for this signature in database
GPG key ID: 5FA5E5544F010D48
8 changed files with 306 additions and 24 deletions

View file

@ -39,3 +39,4 @@ croniter
kazoo
ansible; sys.platform != 'win32' and sys.platform != 'darwin' and python_version >= '3.5'
ansible; sys.platform != 'win32' and sys.platform != 'darwin' and python_version == '2.7'
pylxd>=2.2.5; sys.platform != 'win32' and sys.platform != 'darwin'

View file

@ -51,16 +51,19 @@ def __virtual__():
return __virtualname__ if 'lxd.version' in __salt__ else False
def init(storage_backend='dir', trust_password=None, network_address=None,
network_port=None, storage_create_device=None,
def init(name, storage_backend='dir', trust_password=None,
network_address=None, network_port=None, storage_create_device=None,
storage_create_loop=None, storage_pool=None,
done_file='%SALT_CONFIG_DIR%/lxd_initialized', name=None):
done_file='%SALT_CONFIG_DIR%/lxd_initialized'):
'''
Initalizes the LXD Daemon, as LXD doesn't tell if its initialized
we touch the the done_file and check if it exist.
This can only be called once per host unless you remove the done_file.
name :
Ignore this. This is just here for salt.
storage_backend :
Storage backend to use (zfs or dir, default: dir)
@ -86,23 +89,6 @@ def init(storage_backend='dir', trust_password=None, network_address=None,
Path where we check that this method has been called,
as it can run only once and theres currently no way
to ask LXD if init has been called.
name :
Ignore this. This is just here for salt.
CLI Examples:
To listen on all IPv4/IPv6 Addresses:
.. code-block:: bash
salt '*' lxd.init dir PaSsW0rD [::]
To not listen on Network:
.. code-block:: bash
salt '*' lxd.init
'''
ret = {
@ -215,7 +201,7 @@ def config_managed(name, value, force_password=False):
return _success(ret, result_msg)
def authenticate(remote_addr, password, cert, key, verify_cert=True, name=''):
def authenticate(name, remote_addr, password, cert, key, verify_cert=True):
'''
Authenticate with a remote peer.
@ -252,7 +238,7 @@ def authenticate(remote_addr, password, cert, key, verify_cert=True, name=''):
but in the most cases you want to set it off as LXD
normaly uses self-signed certificates.
name :
name:
Ignore this. This is just here for salt.
'''
ret = {

View file

@ -508,7 +508,7 @@ def running(name,
if not restart:
return _success(
ret,
'The container "{0} is already running"'.format(name)
'The container "{0}" is already running'.format(name)
)
else:
if __opts__['test']:

View file

@ -196,7 +196,7 @@ def present(name,
src_remote_addr=source['remote_addr'],
src_cert=source['cert'],
src_key=source['key'],
src_verify_cert=source['verify_cert'],
src_verify_cert=source.get('verify_cert', True),
remote_addr=remote_addr,
cert=cert,
key=key,

View file

@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
'''
Integration tests for the lxd states
'''
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
# Import salt utils
import salt.utils.path
# Import Salt Testing Libs
from tests.support.unit import skipIf
from tests.support.case import ModuleCase
from tests.support.helpers import destructiveTest
from tests.support.mixins import SaltReturnAssertsMixin
try:
import pylxd # pylint: disable=import-error,unused-import
HAS_PYLXD = True
except ImportError:
HAS_PYLXD = False
@destructiveTest
@skipIf(not salt.utils.path.which('lxd'), 'LXD not installed')
@skipIf(not salt.utils.path.which('lxc'), 'LXC not installed')
@skipIf(not HAS_PYLXD, 'pylxd not installed')
class LxdTestCase(ModuleCase, SaltReturnAssertsMixin):
run_once = False
def test_01__init_lxd(self):
if LxdTestCase.run_once:
return
ret = self.run_state('lxd.init', name='foobar')
self.assertSaltTrueReturn(ret)
LxdTestCase.run_once = True
name = 'lxd_|-foobar_|-foobar_|-init'
assert name in ret
assert ret[name]['storage_backend'] == 'dir'

View file

@ -0,0 +1,143 @@
# -*- coding: utf-8 -*-
'''
Integration tests for the lxd states
'''
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Lxd Test Case
import tests.integration.states.test_lxd
class LxdContainerTestCase(tests.integration.states.test_lxd.LxdTestCase):
def setUp(self):
self.run_state(
'lxd_image.present',
name='images:centos/7',
source={
'name': 'centos/7',
'type': 'simplestreams',
'server': 'https://images.linuxcontainers.org',
},
)
def tearDown(self):
self.run_state('lxd_image.absent', name='images:centos/7',)
self.run_state('lxd_container.absent', name='test-container',)
def test_02__create_container(self):
ret = self.run_state(
'lxd_container.present',
name='test-container',
running=True,
source={'type': 'image', 'alias': 'images:centos/7'}
)
name = 'lxd_container_|-test-container_|-test-container_|-present'
self.assertSaltTrueReturn(ret)
assert name in ret
assert ret[name]['changes']['started'] == 'Started the container "test-container"'
def test_03__change_container(self):
self.run_state(
'lxd_container.present',
name='test-container',
running=True,
source={'type': 'image', 'alias': 'images:centos/7'}
)
ret = self.run_state(
'lxd_container.present',
name='test-container',
running=True,
source={'type': 'image', 'alias': 'images:centos/7'},
restart_on_change=True,
config=[
{'key': 'boot.autostart', 'value': 1},
{'key': 'security.privileged', 'value': '1'},
],
)
name = 'lxd_container_|-test-container_|-test-container_|-present'
self.assertSaltTrueReturn(ret)
assert name in ret
assert ret[name]['changes']['config'] == {
'boot.autostart': 'Added config key "boot.autostart" = "1"',
'security.privileged': 'Added config key "security.privileged" = "1"',
}
def test_08__running_container(self):
self.run_state(
'lxd_container.present',
name='test-container',
running=True,
source={'type': 'image', 'alias': 'images:centos/7'}
)
ret = self.run_state(
'lxd_container.running',
name='test-container',
)
self.assertSaltTrueReturn(ret)
name = 'lxd_container_|-test-container_|-test-container_|-running'
assert name in ret
assert not ret[name]['changes']
assert ret[name]['comment'] == 'The container "test-container" is already running'
ret = self.run_state(
'lxd_container.running',
name='test-container',
restart=True,
)
self.assertSaltTrueReturn(ret)
assert name in ret
assert ret[name]['changes'] == {'restarted': 'Restarted the container "test-container"'}
assert ret[name]['comment'] == 'Restarted the container "test-container"'
def test_09__stop_container(self):
self.run_state(
'lxd_container.present',
name='test-container',
running=True,
source={'type': 'image', 'alias': 'images:centos/7'}
)
ret = self.run_state(
'lxd_container.stopped',
name='test-container',
)
name = 'lxd_container_|-test-container_|-test-container_|-stopped'
self.assertSaltTrueReturn(ret)
assert ret[name]['changes'] == {'stopped': 'Stopped the container "test-container"'}
ret = self.run_state(
'lxd_container.stopped',
name='test-container',
)
name = 'lxd_container_|-test-container_|-test-container_|-stopped'
self.assertSaltTrueReturn(ret)
assert not ret[name]['changes']
def test_10__delete_container(self):
self.run_state(
'lxd_container.present',
name='test-container',
running=True,
source={'type': 'image', 'alias': 'images:centos/7'}
)
ret = self.run_state(
'lxd_container.absent',
name='test-container',
)
name = 'lxd_container_|-test-container_|-test-container_|-absent'
assert name in ret
assert ret[name]['result'] is False
ret = self.run_state(
'lxd_container.stopped',
name='test-container',
)
name = 'lxd_container_|-test-container_|-test-container_|-stopped'
assert name in ret
assert ret[name]['result'] is True
ret = self.run_state(
'lxd_container.absent',
name='test-container',
)
name = 'lxd_container_|-test-container_|-test-container_|-absent'
self.assertSaltTrueReturn(ret)
assert name in ret
assert ret[name]['changes'] == {'deleted': 'Container "test-container" has been deleted.'}

View file

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
'''
Integration tests for the lxd states
'''
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Lxd Test Case
import tests.integration.states.test_lxd
class LxdImageTestCase(tests.integration.states.test_lxd.LxdTestCase):
def test_02__pull_image(self):
ret = self.run_state(
'lxd_image.present',
name='images:centos/7',
source={
'name': 'centos/7',
'type': 'simplestreams',
'server': 'https://images.linuxcontainers.org',
},
)
name = 'lxd_image_|-images:centos/7_|-images:centos/7_|-present'
self.assertSaltTrueReturn(ret)
assert name in ret
assert ret[name]['changes']['aliases'] == ['Added alias "images:centos/7"']
def test_03__delete_image(self):
ret = self.run_state(
'lxd_image.absent',
name='images:centos/7',
)
name = 'lxd_image_|-images:centos/7_|-images:centos/7_|-absent'
self.assertSaltTrueReturn(ret)
assert name in ret
assert ret[name]['changes']['removed'] == 'Image "images:centos/7" has been deleted.'

View file

@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
'''
Integration tests for the lxd states
'''
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Lxd Test Case
import tests.integration.states.test_lxd
class LxdProfileTestCase(tests.integration.states.test_lxd.LxdTestCase):
def tearDown(self):
self.run_state(
'lxd_profile.absent',
name='test-profile',
)
def test_02__create_profile(self):
self.run_state(
'lxd_profile.absent',
name='test-profile',
)
ret = self.run_state(
'lxd_profile.present',
name='test-profile',
config=[
{'key': 'boot.autostart', 'value': 1},
],
)
name = 'lxd_profile_|-test-profile_|-test-profile_|-present'
self.assertSaltTrueReturn(ret)
assert name in ret
assert ret[name]['changes'] == {'created': 'Profile "test-profile" has been created'}
def test_03__change_profile(self):
self.run_state(
'lxd_profile.present',
name='test-profile',
config=[
{'key': 'boot.autostart', 'value': 1},
],
)
ret = self.run_state(
'lxd_profile.present',
name='test-profile',
config=[
{'key': 'boot.autostart', 'value': 1},
{'key': 'security.privileged', 'value': '1'},
],
)
name = 'lxd_profile_|-test-profile_|-test-profile_|-present'
self.assertSaltTrueReturn(ret)
assert name in ret
assert ret[name]['changes']['config'] == {
'security.privileged': 'Added config key "security.privileged" = "1"'
}
def test_04__delete_profile(self):
self.run_state(
'lxd_profile.present',
name='test-profile',
config=[
{'key': 'boot.autostart', 'value': 1},
],
)
ret = self.run_state(
'lxd_profile.absent',
name='test-profile',
)
name = 'lxd_profile_|-test-profile_|-test-profile_|-absent'
self.assertSaltTrueReturn(ret)
assert name in ret
assert ret[name]['changes'] == {'removed': 'Profile "test-profile" has been deleted.'}