|
@@ -1,16 +1,24 @@
|
|
-import os
|
|
|
|
import mock
|
|
import mock
|
|
|
|
+import os
|
|
|
|
+import shutil
|
|
|
|
+import tempfile
|
|
from .. import unittest
|
|
from .. import unittest
|
|
|
|
|
|
from compose import config
|
|
from compose import config
|
|
|
|
|
|
|
|
|
|
class ConfigTest(unittest.TestCase):
|
|
class ConfigTest(unittest.TestCase):
|
|
- def test_from_dictionary(self):
|
|
|
|
- service_dicts = config.from_dictionary({
|
|
|
|
- 'foo': {'image': 'busybox'},
|
|
|
|
- 'bar': {'environment': ['FOO=1']},
|
|
|
|
- })
|
|
|
|
|
|
+ def test_load(self):
|
|
|
|
+ service_dicts = config.load(
|
|
|
|
+ config.ConfigDetails(
|
|
|
|
+ {
|
|
|
|
+ 'foo': {'image': 'busybox'},
|
|
|
|
+ 'bar': {'environment': ['FOO=1']},
|
|
|
|
+ },
|
|
|
|
+ 'working_dir',
|
|
|
|
+ 'filename.yml'
|
|
|
|
+ )
|
|
|
|
+ )
|
|
|
|
|
|
self.assertEqual(
|
|
self.assertEqual(
|
|
sorted(service_dicts, key=lambda d: d['name']),
|
|
sorted(service_dicts, key=lambda d: d['name']),
|
|
@@ -26,11 +34,15 @@ class ConfigTest(unittest.TestCase):
|
|
])
|
|
])
|
|
)
|
|
)
|
|
|
|
|
|
- def test_from_dictionary_throws_error_when_not_dict(self):
|
|
|
|
|
|
+ def test_load_throws_error_when_not_dict(self):
|
|
with self.assertRaises(config.ConfigurationError):
|
|
with self.assertRaises(config.ConfigurationError):
|
|
- config.from_dictionary({
|
|
|
|
- 'web': 'busybox:latest',
|
|
|
|
- })
|
|
|
|
|
|
+ config.load(
|
|
|
|
+ config.ConfigDetails(
|
|
|
|
+ {'web': 'busybox:latest'},
|
|
|
|
+ 'working_dir',
|
|
|
|
+ 'filename.yml'
|
|
|
|
+ )
|
|
|
|
+ )
|
|
|
|
|
|
def test_config_validation(self):
|
|
def test_config_validation(self):
|
|
self.assertRaises(
|
|
self.assertRaises(
|
|
@@ -335,9 +347,13 @@ class EnvTest(unittest.TestCase):
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
+def load_from_filename(filename):
|
|
|
|
+ return config.load(config.find('.', filename))
|
|
|
|
+
|
|
|
|
+
|
|
class ExtendsTest(unittest.TestCase):
|
|
class ExtendsTest(unittest.TestCase):
|
|
def test_extends(self):
|
|
def test_extends(self):
|
|
- service_dicts = config.load('tests/fixtures/extends/docker-compose.yml')
|
|
|
|
|
|
+ service_dicts = load_from_filename('tests/fixtures/extends/docker-compose.yml')
|
|
|
|
|
|
service_dicts = sorted(
|
|
service_dicts = sorted(
|
|
service_dicts,
|
|
service_dicts,
|
|
@@ -364,7 +380,7 @@ class ExtendsTest(unittest.TestCase):
|
|
])
|
|
])
|
|
|
|
|
|
def test_nested(self):
|
|
def test_nested(self):
|
|
- service_dicts = config.load('tests/fixtures/extends/nested.yml')
|
|
|
|
|
|
+ service_dicts = load_from_filename('tests/fixtures/extends/nested.yml')
|
|
|
|
|
|
self.assertEqual(service_dicts, [
|
|
self.assertEqual(service_dicts, [
|
|
{
|
|
{
|
|
@@ -380,7 +396,7 @@ class ExtendsTest(unittest.TestCase):
|
|
|
|
|
|
def test_circular(self):
|
|
def test_circular(self):
|
|
try:
|
|
try:
|
|
- config.load('tests/fixtures/extends/circle-1.yml')
|
|
|
|
|
|
+ load_from_filename('tests/fixtures/extends/circle-1.yml')
|
|
raise Exception("Expected config.CircularReference to be raised")
|
|
raise Exception("Expected config.CircularReference to be raised")
|
|
except config.CircularReference as e:
|
|
except config.CircularReference as e:
|
|
self.assertEqual(
|
|
self.assertEqual(
|
|
@@ -445,7 +461,7 @@ class ExtendsTest(unittest.TestCase):
|
|
print load_config()
|
|
print load_config()
|
|
|
|
|
|
def test_volume_path(self):
|
|
def test_volume_path(self):
|
|
- dicts = config.load('tests/fixtures/volume-path/docker-compose.yml')
|
|
|
|
|
|
+ dicts = load_from_filename('tests/fixtures/volume-path/docker-compose.yml')
|
|
|
|
|
|
paths = [
|
|
paths = [
|
|
'%s:/foo' % os.path.abspath('tests/fixtures/volume-path/common/foo'),
|
|
'%s:/foo' % os.path.abspath('tests/fixtures/volume-path/common/foo'),
|
|
@@ -455,7 +471,7 @@ class ExtendsTest(unittest.TestCase):
|
|
self.assertEqual(set(dicts[0]['volumes']), set(paths))
|
|
self.assertEqual(set(dicts[0]['volumes']), set(paths))
|
|
|
|
|
|
def test_parent_build_path_dne(self):
|
|
def test_parent_build_path_dne(self):
|
|
- child = config.load('tests/fixtures/extends/nonexistent-path-child.yml')
|
|
|
|
|
|
+ child = load_from_filename('tests/fixtures/extends/nonexistent-path-child.yml')
|
|
|
|
|
|
self.assertEqual(child, [
|
|
self.assertEqual(child, [
|
|
{
|
|
{
|
|
@@ -475,14 +491,16 @@ class BuildPathTest(unittest.TestCase):
|
|
self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
|
|
self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
|
|
|
|
|
|
def test_nonexistent_path(self):
|
|
def test_nonexistent_path(self):
|
|
- options = {'build': 'nonexistent.path'}
|
|
|
|
- self.assertRaises(
|
|
|
|
- config.ConfigurationError,
|
|
|
|
- lambda: config.from_dictionary({
|
|
|
|
- 'foo': options,
|
|
|
|
- 'working_dir': 'tests/fixtures/build-path'
|
|
|
|
- })
|
|
|
|
- )
|
|
|
|
|
|
+ with self.assertRaises(config.ConfigurationError):
|
|
|
|
+ config.load(
|
|
|
|
+ config.ConfigDetails(
|
|
|
|
+ {
|
|
|
|
+ 'foo': {'build': 'nonexistent.path'},
|
|
|
|
+ },
|
|
|
|
+ 'working_dir',
|
|
|
|
+ 'filename.yml'
|
|
|
|
+ )
|
|
|
|
+ )
|
|
|
|
|
|
def test_relative_path(self):
|
|
def test_relative_path(self):
|
|
relative_build_path = '../build-ctx/'
|
|
relative_build_path = '../build-ctx/'
|
|
@@ -502,5 +520,56 @@ class BuildPathTest(unittest.TestCase):
|
|
self.assertEquals(service_dict['build'], self.abs_context_path)
|
|
self.assertEquals(service_dict['build'], self.abs_context_path)
|
|
|
|
|
|
def test_from_file(self):
|
|
def test_from_file(self):
|
|
- service_dict = config.load('tests/fixtures/build-path/docker-compose.yml')
|
|
|
|
|
|
+ service_dict = load_from_filename('tests/fixtures/build-path/docker-compose.yml')
|
|
self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])
|
|
self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class GetConfigPathTestCase(unittest.TestCase):
|
|
|
|
+
|
|
|
|
+ files = [
|
|
|
|
+ 'docker-compose.yml',
|
|
|
|
+ 'docker-compose.yaml',
|
|
|
|
+ 'fig.yml',
|
|
|
|
+ 'fig.yaml',
|
|
|
|
+ ]
|
|
|
|
+
|
|
|
|
+ def test_get_config_path_default_file_in_basedir(self):
|
|
|
|
+ files = self.files
|
|
|
|
+ self.assertEqual('docker-compose.yml', get_config_filename_for_files(files[0:]))
|
|
|
|
+ self.assertEqual('docker-compose.yaml', get_config_filename_for_files(files[1:]))
|
|
|
|
+ self.assertEqual('fig.yml', get_config_filename_for_files(files[2:]))
|
|
|
|
+ self.assertEqual('fig.yaml', get_config_filename_for_files(files[3:]))
|
|
|
|
+ with self.assertRaises(config.ComposeFileNotFound):
|
|
|
|
+ get_config_filename_for_files([])
|
|
|
|
+
|
|
|
|
+ def test_get_config_path_default_file_in_parent_dir(self):
|
|
|
|
+ """Test with files placed in the subdir"""
|
|
|
|
+ files = self.files
|
|
|
|
+
|
|
|
|
+ def get_config_in_subdir(files):
|
|
|
|
+ return get_config_filename_for_files(files, subdir=True)
|
|
|
|
+
|
|
|
|
+ self.assertEqual('docker-compose.yml', get_config_in_subdir(files[0:]))
|
|
|
|
+ self.assertEqual('docker-compose.yaml', get_config_in_subdir(files[1:]))
|
|
|
|
+ self.assertEqual('fig.yml', get_config_in_subdir(files[2:]))
|
|
|
|
+ self.assertEqual('fig.yaml', get_config_in_subdir(files[3:]))
|
|
|
|
+ with self.assertRaises(config.ComposeFileNotFound):
|
|
|
|
+ get_config_in_subdir([])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def get_config_filename_for_files(filenames, subdir=None):
|
|
|
|
+ def make_files(dirname, filenames):
|
|
|
|
+ for fname in filenames:
|
|
|
|
+ with open(os.path.join(dirname, fname), 'w') as f:
|
|
|
|
+ f.write('')
|
|
|
|
+
|
|
|
|
+ project_dir = tempfile.mkdtemp()
|
|
|
|
+ try:
|
|
|
|
+ make_files(project_dir, filenames)
|
|
|
|
+ if subdir:
|
|
|
|
+ base_dir = tempfile.mkdtemp(dir=project_dir)
|
|
|
|
+ else:
|
|
|
|
+ base_dir = project_dir
|
|
|
|
+ return os.path.basename(config.get_config_path(base_dir))
|
|
|
|
+ finally:
|
|
|
|
+ shutil.rmtree(project_dir)
|