<s> from __future__ import absolute_import <EOL> from __future__ import unicode_literals <EOL> import datetime <EOL> import docker <EOL> from docker . errors import NotFound <EOL> from . . import mock <EOL> from . . import unittest <EOL> from compose . config . config import Config <EOL> from compose . config . types import VolumeFromSpec <EOL> from compose . const import LABEL_SERVICE <EOL> from compose . container import Container <EOL> from compose . project import Project <EOL> from compose . service import ImageType <EOL> from compose . service import Service <EOL> class ProjectTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_client = mock . create_autospec ( docker . Client ) <EOL> def test_from_config ( self ) : <EOL> config = Config ( <EOL> version = None , <EOL> services = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } , <EOL> ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT>' , <EOL> config_data = config , <EOL> client = None , <EOL> ) <EOL> self . assertEqual ( len ( project . services ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( project . get_service ( '<STR_LIT>' ) . name , '<STR_LIT>' ) <EOL> self . assertEqual ( project . get_service ( '<STR_LIT>' ) . options [ '<STR_LIT:image>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( project . get_service ( '<STR_LIT>' ) . name , '<STR_LIT>' ) <EOL> self . assertEqual ( project . get_service ( '<STR_LIT>' ) . options [ '<STR_LIT:image>' ] , '<STR_LIT>' ) <EOL> self . assertFalse ( project . networks . use_networking ) <EOL> def test_from_config_v2 ( self ) : <EOL> config = Config ( <EOL> version = <NUM_LIT:2> , <EOL> services = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } , <EOL> ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) <EOL> project = Project . from_config ( '<STR_LIT>' , config , None ) <EOL> self . assertEqual ( len ( project . services ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( project . networks . use_networking ) <EOL> def test_get_service ( self ) : <EOL> web = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> client = None , <EOL> image = "<STR_LIT>" , <EOL> ) <EOL> project = Project ( '<STR_LIT:test>' , [ web ] , None ) <EOL> self . assertEqual ( project . get_service ( '<STR_LIT>' ) , web ) <EOL> def test_get_services_returns_all_services_without_args ( self ) : <EOL> web = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' , <EOL> ) <EOL> console = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' , <EOL> ) <EOL> project = Project ( '<STR_LIT:test>' , [ web , console ] , None ) <EOL> self . assertEqual ( project . get_services ( ) , [ web , console ] ) <EOL> def test_get_services_returns_listed_services_with_args ( self ) : <EOL> web = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' , <EOL> ) <EOL> console = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' , <EOL> ) <EOL> project = Project ( '<STR_LIT:test>' , [ web , console ] , None ) <EOL> self . assertEqual ( project . get_services ( [ '<STR_LIT>' ] ) , [ console ] ) <EOL> def test_get_services_with_include_links ( self ) : <EOL> db = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' , <EOL> ) <EOL> web = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' , <EOL> links = [ ( db , '<STR_LIT>' ) ] <EOL> ) <EOL> cache = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' <EOL> ) <EOL> console = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' , <EOL> links = [ ( web , '<STR_LIT>' ) ] <EOL> ) <EOL> project = Project ( '<STR_LIT:test>' , [ web , db , cache , console ] , None ) <EOL> self . assertEqual ( <EOL> project . get_services ( [ '<STR_LIT>' ] , include_deps = True ) , <EOL> [ db , web , console ] <EOL> ) <EOL> def test_get_services_removes_duplicates_following_links ( self ) : <EOL> db = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' , <EOL> ) <EOL> web = Service ( <EOL> project = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> image = '<STR_LIT:foo>' , <EOL> links = [ ( db , '<STR_LIT>' ) ] <EOL> ) <EOL> project = Project ( '<STR_LIT:test>' , [ web , db ] , None ) <EOL> self . assertEqual ( <EOL> project . get_services ( [ '<STR_LIT>' , '<STR_LIT>' ] , include_deps = True ) , <EOL> [ db , web ] <EOL> ) <EOL> def test_use_volumes_from_container ( self ) : <EOL> container_id = '<STR_LIT>' <EOL> container_dict = dict ( Name = '<STR_LIT>' , Id = container_id ) <EOL> self . mock_client . inspect_container . return_value = container_dict <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = self . mock_client , <EOL> config_data = Config ( <EOL> version = None , <EOL> services = [ { <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ VolumeFromSpec ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> } ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) , <EOL> ) <EOL> assert project . get_service ( '<STR_LIT:test>' ) . _get_volumes_from ( ) == [ container_id + "<STR_LIT>" ] <EOL> def test_use_volumes_from_service_no_container ( self ) : <EOL> container_name = '<STR_LIT>' <EOL> self . mock_client . containers . return_value = [ <EOL> { <EOL> "<STR_LIT:Name>" : container_name , <EOL> "<STR_LIT>" : [ container_name ] , <EOL> "<STR_LIT>" : container_name , <EOL> "<STR_LIT>" : '<STR_LIT>' <EOL> } <EOL> ] <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = self . mock_client , <EOL> config_data = Config ( <EOL> version = None , <EOL> services = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ VolumeFromSpec ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> } <EOL> ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) , <EOL> ) <EOL> assert project . get_service ( '<STR_LIT:test>' ) . _get_volumes_from ( ) == [ container_name + "<STR_LIT>" ] <EOL> def test_use_volumes_from_service_container ( self ) : <EOL> container_ids = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = None , <EOL> config_data = Config ( <EOL> version = None , <EOL> services = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ VolumeFromSpec ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> } <EOL> ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) , <EOL> ) <EOL> with mock . patch . object ( Service , '<STR_LIT>' ) as mock_return : <EOL> mock_return . return_value = [ <EOL> mock . Mock ( id = container_id , spec = Container ) <EOL> for container_id in container_ids ] <EOL> assert ( <EOL> project . get_service ( '<STR_LIT:test>' ) . _get_volumes_from ( ) == <EOL> [ container_ids [ <NUM_LIT:0> ] + '<STR_LIT>' ] <EOL> ) <EOL> def test_events ( self ) : <EOL> services = [ Service ( name = '<STR_LIT>' ) , Service ( name = '<STR_LIT>' ) ] <EOL> project = Project ( '<STR_LIT:test>' , services , self . mock_client ) <EOL> self . mock_client . events . return_value = iter ( [ <EOL> { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } , <EOL> { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } , <EOL> { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } , <EOL> { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } , <EOL> { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } , <EOL> ] ) <EOL> def dt_with_microseconds ( dt , us ) : <EOL> return datetime . datetime . fromtimestamp ( dt ) . replace ( microsecond = us ) <EOL> def get_container ( cid ) : <EOL> if cid == '<STR_LIT>' : <EOL> raise NotFound ( None , None , "<STR_LIT>" ) <EOL> if cid == '<STR_LIT>' : <EOL> name = '<STR_LIT>' <EOL> labels = { LABEL_SERVICE : name } <EOL> elif cid == '<STR_LIT>' : <EOL> name = '<STR_LIT>' <EOL> labels = { LABEL_SERVICE : name } <EOL> else : <EOL> labels = { } <EOL> name = '<STR_LIT>' <EOL> return { <EOL> '<STR_LIT>' : cid , <EOL> '<STR_LIT>' : { '<STR_LIT>' : labels } , <EOL> '<STR_LIT:Name>' : '<STR_LIT>' % name , <EOL> } <EOL> self . mock_client . inspect_container . side_effect = get_container <EOL> events = project . events ( ) <EOL> events_list = list ( events ) <EOL> assert not list ( events ) <EOL> assert events_list == [ <EOL> { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:time>' : dt_with_microseconds ( <NUM_LIT> , <NUM_LIT:2> ) , <EOL> '<STR_LIT>' : Container ( None , { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> } , <EOL> { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:time>' : dt_with_microseconds ( <NUM_LIT> , <NUM_LIT:3> ) , <EOL> '<STR_LIT>' : Container ( None , { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> } , <EOL> { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:time>' : dt_with_microseconds ( <NUM_LIT> , <NUM_LIT:4> ) , <EOL> '<STR_LIT>' : Container ( None , { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> } , <EOL> ] <EOL> def test_net_unset ( self ) : <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = self . mock_client , <EOL> config_data = Config ( <EOL> version = None , <EOL> services = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } <EOL> ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) , <EOL> ) <EOL> service = project . get_service ( '<STR_LIT:test>' ) <EOL> self . assertEqual ( service . network_mode . id , None ) <EOL> self . assertNotIn ( '<STR_LIT>' , service . _get_container_host_config ( { } ) ) <EOL> def test_use_net_from_container ( self ) : <EOL> container_id = '<STR_LIT>' <EOL> container_dict = dict ( Name = '<STR_LIT>' , Id = container_id ) <EOL> self . mock_client . inspect_container . return_value = container_dict <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = self . mock_client , <EOL> config_data = Config ( <EOL> version = None , <EOL> services = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) , <EOL> ) <EOL> service = project . get_service ( '<STR_LIT:test>' ) <EOL> self . assertEqual ( service . network_mode . mode , '<STR_LIT>' + container_id ) <EOL> def test_use_net_from_service ( self ) : <EOL> container_name = '<STR_LIT>' <EOL> self . mock_client . containers . return_value = [ <EOL> { <EOL> "<STR_LIT:Name>" : container_name , <EOL> "<STR_LIT>" : [ container_name ] , <EOL> "<STR_LIT>" : container_name , <EOL> "<STR_LIT>" : '<STR_LIT>' <EOL> } <EOL> ] <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = self . mock_client , <EOL> config_data = Config ( <EOL> version = None , <EOL> services = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) , <EOL> ) <EOL> service = project . get_service ( '<STR_LIT:test>' ) <EOL> self . assertEqual ( service . network_mode . mode , '<STR_LIT>' + container_name ) <EOL> def test_uses_default_network_true ( self ) : <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = self . mock_client , <EOL> config_data = Config ( <EOL> version = <NUM_LIT:2> , <EOL> services = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' <EOL> } , <EOL> ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) , <EOL> ) <EOL> assert '<STR_LIT:default>' in project . networks . networks <EOL> def test_uses_default_network_false ( self ) : <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = self . mock_client , <EOL> config_data = Config ( <EOL> version = <NUM_LIT:2> , <EOL> services = [ <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : None } <EOL> } , <EOL> ] , <EOL> networks = { '<STR_LIT>' : { } } , <EOL> volumes = None , <EOL> ) , <EOL> ) <EOL> assert '<STR_LIT:default>' not in project . networks . networks <EOL> def test_container_without_name ( self ) : <EOL> self . mock_client . containers . return_value = [ <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:1>' , '<STR_LIT:Name>' : '<STR_LIT:1>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:2>' , '<STR_LIT:Name>' : None } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:3>' } , <EOL> ] <EOL> self . mock_client . inspect_container . return_value = { <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> LABEL_SERVICE : '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> } <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = self . mock_client , <EOL> config_data = Config ( <EOL> version = None , <EOL> services = [ { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } ] , <EOL> networks = None , <EOL> volumes = None , <EOL> ) , <EOL> ) <EOL> self . assertEqual ( [ c . id for c in project . containers ( ) ] , [ '<STR_LIT:1>' ] ) <EOL> def test_down_with_no_resources ( self ) : <EOL> project = Project . from_config ( <EOL> name = '<STR_LIT:test>' , <EOL> client = self . mock_client , <EOL> config_data = Config ( <EOL> version = '<STR_LIT:2>' , <EOL> services = [ { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:image>' : '<STR_LIT>' , <EOL> } ] , <EOL> networks = { '<STR_LIT:default>' : { } } , <EOL> volumes = { '<STR_LIT:data>' : { } } , <EOL> ) , <EOL> ) <EOL> self . mock_client . remove_network . side_effect = NotFound ( None , None , '<STR_LIT>' ) <EOL> self . mock_client . remove_volume . side_effect = NotFound ( None , None , '<STR_LIT>' ) <EOL> project . down ( ImageType . all , True ) <EOL> self . mock_client . remove_image . assert_called_once_with ( "<STR_LIT>" ) </s>
