Merge pull request #2727 from dbonev/2703-strict-validation-of-networks

Network fields schema validation
This commit is contained in:
Aanand Prasad 2016-01-25 14:39:54 +00:00
commit 3fff286e1a
3 changed files with 70 additions and 2 deletions

View File

@ -41,7 +41,34 @@
"definitions": { "definitions": {
"network": { "network": {
"id": "#/definitions/network", "id": "#/definitions/network",
"type": "object" "type": "object",
"properties": {
"driver": {"type": "string"},
"driver_opts": {
"type": "object",
"patternProperties": {
"^.+$": {"type": ["string", "number"]}
}
},
"ipam": {
"type": "object",
"properties": {
"driver": {"type": "string"},
"config": {
"type": "array"
}
},
"additionalProperties": false
},
"external": {
"type": ["boolean", "object"],
"properties": {
"name": {"type": "string"}
},
"additionalProperties": false
}
},
"additionalProperties": false
}, },
"volume": { "volume": {
"id": "#/definitions/volume", "id": "#/definitions/volume",

View File

@ -106,7 +106,7 @@ Here's an example Compose file defining two custom networks. The `proxy` service
back: back:
# Use a custom driver which takes special options # Use a custom driver which takes special options
driver: my-custom-driver driver: my-custom-driver
options: driver_opts:
foo: "1" foo: "1"
bar: "2" bar: "2"

View File

@ -88,11 +88,26 @@ class ConfigTest(unittest.TestCase):
'driver': 'default', 'driver': 'default',
'driver_opts': {'beep': 'boop'} 'driver_opts': {'beep': 'boop'}
} }
},
'networks': {
'default': {
'driver': 'bridge',
'driver_opts': {'beep': 'boop'}
},
'with_ipam': {
'ipam': {
'driver': 'default',
'config': [
{'subnet': '172.28.0.0/16'}
]
}
}
} }
}, 'working_dir', 'filename.yml') }, 'working_dir', 'filename.yml')
) )
service_dicts = config_data.services service_dicts = config_data.services
volume_dict = config_data.volumes volume_dict = config_data.volumes
networks_dict = config_data.networks
self.assertEqual( self.assertEqual(
service_sort(service_dicts), service_sort(service_dicts),
service_sort([ service_sort([
@ -113,6 +128,20 @@ class ConfigTest(unittest.TestCase):
'driver_opts': {'beep': 'boop'} 'driver_opts': {'beep': 'boop'}
} }
}) })
self.assertEqual(networks_dict, {
'default': {
'driver': 'bridge',
'driver_opts': {'beep': 'boop'}
},
'with_ipam': {
'ipam': {
'driver': 'default',
'config': [
{'subnet': '172.28.0.0/16'}
]
}
}
})
def test_named_volume_config_empty(self): def test_named_volume_config_empty(self):
config_details = build_config_details({ config_details = build_config_details({
@ -191,6 +220,18 @@ class ConfigTest(unittest.TestCase):
) )
) )
def test_load_throws_error_with_invalid_network_fields(self):
with self.assertRaises(ConfigurationError):
config.load(
build_config_details({
'version': 2,
'services': {'web': 'busybox:latest'},
'networks': {
'invalid': {'foo', 'bar'}
}
}, 'working_dir', 'filename.yml')
)
def test_load_config_invalid_service_names(self): def test_load_config_invalid_service_names(self):
for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']: for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']:
with pytest.raises(ConfigurationError) as exc: with pytest.raises(ConfigurationError) as exc: