Skip to content

Commit

Permalink
Add volume integration tests, more config parsing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joffrey F <[email protected]>
  • Loading branch information
shin- committed Oct 8, 2015
1 parent 290b73a commit 2bdaabf
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 4 deletions.
80 changes: 80 additions & 0 deletions tests/integration/project_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

import random

from .testcases import DockerClientTestCase
from compose.config import config
from compose.const import LABEL_PROJECT
Expand Down Expand Up @@ -205,6 +207,84 @@ def test_project_up(self):
self.assertEqual(len(db.containers()), 1)
self.assertEqual(len(web.containers()), 0)

def test_project_up_volumes(self):
vol_name = 'composetests_{0:x}'.format(random.getrandbits(32))
config_data = config.Config(
2, [{
'name': 'web',
'image': 'busybox:latest',
'command': 'top'
}], {vol_name: {'driver': 'local'}}
)

project = Project.from_config(
name='composetest',
config_data=config_data, client=self.client
)
project.up()
self.assertEqual(len(project.containers()), 1)

volume_data = self.client.inspect_volume(vol_name)
self.assertEqual(volume_data['Name'], vol_name)
self.assertEqual(volume_data['Driver'], 'local')

def test_initialize_volumes(self):
vol_name = 'composetests_{0:x}'.format(random.getrandbits(32))
config_data = config.Config(
2, [{
'name': 'web',
'image': 'busybox:latest',
'command': 'top'
}], {vol_name: {'driver': 'local'}}
)

project = Project.from_config(
name='composetest',
config_data=config_data, client=self.client
)
project.initialize_volumes()

volume_data = self.client.inspect_volume(vol_name)
self.assertEqual(volume_data['Name'], vol_name)
self.assertEqual(volume_data['Driver'], 'local')

def test_project_up_implicit_volume_driver(self):
vol_name = 'composetests_{0:x}'.format(random.getrandbits(32))
config_data = config.Config(
2, [{
'name': 'web',
'image': 'busybox:latest',
'command': 'top'
}], {vol_name: {}}
)

project = Project.from_config(
name='composetest',
config_data=config_data, client=self.client
)
project.up()

volume_data = self.client.inspect_volume(vol_name)
self.assertEqual(volume_data['Name'], vol_name)
self.assertEqual(volume_data['Driver'], 'local')

def test_project_up_invalid_volume_driver(self):
vol_name = 'composetests_{0:x}'.format(random.getrandbits(32))
config_data = config.Config(
2, [{
'name': 'web',
'image': 'busybox:latest',
'command': 'top'
}], {vol_name: {'driver': 'foobar'}}
)

project = Project.from_config(
name='composetest',
config_data=config_data, client=self.client
)
with self.assertRaises(config.ConfigurationError):
project.initialize_volumes()

def test_project_up_starts_uncreated_services(self):
db = self.create_service('db')
web = self.create_service('web', links=[(db, 'db')])
Expand Down
85 changes: 81 additions & 4 deletions tests/unit/config/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_load(self):
)

def test_load_v2(self):
service_dicts = config.load(
config_data = config.load(
build_config_details({
'version': 2,
'services': {
Expand All @@ -78,7 +78,9 @@ def test_load_v2(self):
}
}
}, 'working_dir', 'filename.yml')
).services
)
service_dicts = config_data.services
volume_dict = config_data.volumes
self.assertEqual(
service_sort(service_dicts),
service_sort([
Expand All @@ -93,6 +95,52 @@ def test_load_v2(self):
}
])
)
self.assertEqual(volume_dict, {
'hello': {
'driver': 'default',
'driver_opts': {'beep': 'boop'}
}
})

def test_load_service_with_name_version(self):
config_data = config.load(
build_config_details({
'version': {
'image': 'busybox'
}
}, 'working_dir', 'filename.yml')
)
service_dicts = config_data.services
self.assertEqual(
service_sort(service_dicts),
service_sort([
{
'name': 'version',
'image': 'busybox',
}
])
)

def test_load_invalid_version(self):
with self.assertRaises(ConfigurationError):
config.load(
build_config_details({
'version': 18,
'services': {
'foo': {'image': 'busybox'}
}
}, 'working_dir', 'filename.yml')
)

with self.assertRaises(ConfigurationError):
config.load(
build_config_details({
'version': 'two point oh',
'services': {
'foo': {'image': 'busybox'}
}
}, 'working_dir', 'filename.yml')
)

def test_load_throws_error_when_not_dict(self):
with self.assertRaises(ConfigurationError):
Expand All @@ -104,9 +152,18 @@ def test_load_throws_error_when_not_dict(self):
)
)

def test_config_invalid_service_names(self):
with self.assertRaises(ConfigurationError):
for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']:
config.load(
build_config_details(
{'version': 2, 'services': {'web': 'busybox:latest'}},
'working_dir',
'filename.yml'
)
)

def test_config_invalid_service_names(self):
for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']:
with self.assertRaises(ConfigurationError):
config.load(
build_config_details(
{invalid_name: {'image': 'busybox'}},
Expand All @@ -115,6 +172,14 @@ def test_config_invalid_service_names(self):
)
)

with self.assertRaises(ConfigurationError):
config.load(
build_config_details({
'version': 2,
'services': {invalid_name: {'image': 'busybox'}}
}, 'working_dir', 'filename.yml')
)

def test_config_integer_service_name_raise_validation_error(self):
expected_error_msg = "Service name: 1 needs to be a string, eg '1'"
with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
Expand All @@ -126,6 +191,18 @@ def test_config_integer_service_name_raise_validation_error(self):
)
)

with self.assertRaisesRegexp(ConfigurationError, expected_error_msg):
config.load(
build_config_details(
{
'version': 2,
'services': {1: {'image': 'busybox'}}
},
'working_dir',
'filename.yml'
)
)

@pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
def test_load_with_multiple_files(self):
base_file = config.ConfigFile(
Expand Down

0 comments on commit 2bdaabf

Please sign in to comment.