Example #1
0
    def test_get_volumes_from_container(self):
        container_id = 'aabbccddee'
        service = Service(
            'test',
            volumes_from=[mock.Mock(id=container_id, spec=Container)])

        self.assertEqual(service._get_volumes_from(), [container_id])
Example #2
0
    def test_create_container_no_build(self):
        self.mock_client.images.return_value = []
        service = Service('foo', client=self.mock_client, build='.')
        service.create_container(do_build=False)

        self.assertFalse(self.mock_client.images.called)
        self.assertFalse(self.mock_client.build.called)
Example #3
0
    def test_get_volumes_from_container(self):
        container_id = 'aabbccddee'
        service = Service(
            'test',
            volumes_from=[mock.Mock(id=container_id, spec=Container)])

        self.assertEqual(service._get_volumes_from(), [container_id])
Example #4
0
    def test_build_with_cache(self):
        mock_client = mock.create_autospec(docker.Client)
        service = Service(
            'buildtest',
            client=mock_client,
            build='/path',
            tags=['foo', 'foo:v2'])
        expected = 'abababab'

        with mock.patch('fig.service.stream_output') as mock_stream_output:
            mock_stream_output.return_value = [
                dict(stream='Successfully built %s' % expected)
            ]
            image_id = service.build()
        self.assertEqual(image_id, expected)
        mock_client.build.assert_called_once_with(
            '/path',
            tag=service.full_name,
            stream=True,
            rm=True,
            nocache=False)

        self.assertEqual(mock_client.tag.mock_calls, [
            mock.call(image_id, 'foo', tag=None),
            mock.call(image_id, 'foo', tag='v2'),
        ])
Example #5
0
 def test_split_domainname_fqdn(self):
     service = Service('foo',
             hostname='name.domain.tld',
             client=self.mock_client)
     self.mock_client.containers.return_value = []
     opts = service._get_container_create_options({'image': 'foo'})
     self.assertEqual(opts['hostname'], 'name', 'hostname')
     self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
Example #6
0
    def test_create_container_with_build(self):
        self.mock_client.images.return_value = []
        service = Service('foo', client=self.mock_client, build='.')
        service.build = mock.create_autospec(service.build)
        service.create_container(do_build=True)

        self.mock_client.images.assert_called_once_with(name=service.full_name)
        service.build.assert_called_once_with()
Example #7
0
 def test_split_domainname_fqdn(self):
     service = Service('foo',
             hostname = 'name.domain.tld',
         )
     service.next_container_name = lambda x: 'foo'
     opts = service._get_container_create_options({})
     self.assertEqual(opts['hostname'], 'name', 'hostname')
     self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
Example #8
0
 def test_start_container_uses_tagged_image_if_it_exists(self):
     self.client.build("tests/fixtures/simple-dockerfile", tag="figtest_test")
     service = Service(
         name="test", client=self.client, build="this/does/not/exist/and/will/throw/error", project="figtest"
     )
     container = service.start_container()
     container.wait()
     self.assertIn("success", container.logs())
Example #9
0
 def test_split_domainname_fqdn(self):
     service = Service('foo',
             hostname='name.domain.tld',
             client=self.mock_client)
     self.mock_client.containers.return_value = []
     opts = service._get_container_create_options({})
     self.assertEqual(opts['hostname'], 'name', 'hostname')
     self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
Example #10
0
    def test_get_container(self, mock_container_class):
        container_dict = dict(Name="default_foo_2")
        self.mock_client.containers.return_value = [container_dict]
        service = Service("foo", client=self.mock_client)

        container = service.get_container(number=2)
        self.assertEqual(container, mock_container_class.from_ps.return_value)
        mock_container_class.from_ps.assert_called_once_with(self.mock_client, container_dict)
Example #11
0
    def test_get_container(self, mock_container_class):
        container_dict = dict(Name='default_foo_2')
        self.mock_client.containers.return_value = [container_dict]
        service = Service('foo', client=self.mock_client)

        container = service.get_container(number=2)
        self.assertEqual(container, mock_container_class.from_ps.return_value)
        mock_container_class.from_ps.assert_called_once_with(
            self.mock_client, container_dict)
Example #12
0
    def test_get_volumes_from_service_no_container(self):
        container_id = "abababab"
        from_service = mock.create_autospec(Service)
        from_service.containers.return_value = []
        from_service.create_container.return_value = mock.Mock(id=container_id, spec=Container)
        service = Service("test", volumes_from=[from_service])

        self.assertEqual(service._get_volumes_from(), [container_id])
        from_service.create_container.assert_called_once_with()
Example #13
0
 def test_pull_image(self, mock_log):
     service = Service('foo',
                       client=self.mock_client,
                       image='someimage:sometag')
     service.pull(insecure_registry=True)
     self.mock_client.pull.assert_called_once_with('someimage:sometag',
                                                   insecure_registry=True)
     mock_log.info.assert_called_once_with(
         'Pulling foo (someimage:sometag)...')
Example #14
0
 def test_parse_environment(self):
     service = Service(
         "foo",
         environment=["NORMAL=F1", "CONTAINS_EQUALS=F=2", "TRAILING_EQUALS="],
         client=self.mock_client,
         image="image_name",
     )
     options = service._get_container_create_options({})
     self.assertEqual(options["environment"], {"NORMAL": "F1", "CONTAINS_EQUALS": "F=2", "TRAILING_EQUALS": ""})
Example #15
0
 def test_env_from_multiple_files(self):
     service = Service(
         "foo",
         env_file=["tests/fixtures/env/one.env", "tests/fixtures/env/two.env"],
         client=self.mock_client,
         image="image_name",
     )
     options = service._get_container_create_options({})
     self.assertEqual(options["environment"], {"ONE": "2", "TWO": "1", "THREE": "3", "FOO": "baz", "DOO": "dah"})
Example #16
0
    def test_get_volumes_from_service_container_exists(self):
        container_ids = ["aabbccddee", "12345"]
        from_service = mock.create_autospec(Service)
        from_service.containers.return_value = [
            mock.Mock(id=container_id, spec=Container) for container_id in container_ids
        ]
        service = Service("test", volumes_from=[from_service])

        self.assertEqual(service._get_volumes_from(), container_ids)
Example #17
0
 def test_start_container_builds_images(self):
     service = Service(
         name='test',
         client=self.client,
         build='tests/fixtures/simple-dockerfile',
     )
     container = service.start_container()
     container.wait()
     self.assertIn('success', container.logs())
     self.assertEqual(len(self.client.images(name='default_test')), 1)
Example #18
0
    def test_get_volumes_from_service_container_exists(self):
        container_ids = ['aabbccddee', '12345']
        from_service = mock.create_autospec(Service)
        from_service.containers.return_value = [
            mock.Mock(id=container_id, spec=Container)
            for container_id in container_ids
        ]
        service = Service('test', volumes_from=[from_service])

        self.assertEqual(service._get_volumes_from(), container_ids)
Example #19
0
    def test_get_container(self, mock_container_class):
        mock_client = mock.create_autospec(docker.Client)
        container_dict = dict(Name='default_foo_2')
        mock_client.containers.return_value = [container_dict]
        service = Service('foo', client=mock_client)

        container = service.get_container(number=2)
        self.assertEqual(container, mock_container_class.from_ps.return_value)
        mock_container_class.from_ps.assert_called_once_with(
            mock_client, container_dict)
Example #20
0
 def test_parse_environment(self):
     service = Service('foo',
             environment=['NORMAL=F1', 'CONTAINS_EQUALS=F=2', 'TRAILING_EQUALS='],
             client=self.mock_client,
         )
     options = service._get_container_create_options({})
     self.assertEqual(
         options['environment'],
         {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''}
         )
Example #21
0
 def test_start_container_uses_tagged_image_if_it_exists(self):
     self.client.build('tests/fixtures/simple-dockerfile', tag='default_test')
     service = Service(
         name='test',
         client=self.client,
         build='this/does/not/exist/and/will/throw/error',
     )
     container = service.start_container()
     container.wait()
     self.assertIn('success', container.logs())
Example #22
0
    def test_get_volumes_from_service_no_container(self):
        container_id = 'abababab'
        from_service = mock.create_autospec(Service)
        from_service.containers.return_value = []
        from_service.create_container.return_value = mock.Mock(id=container_id,
                                                               spec=Container)
        service = Service('test', volumes_from=[from_service])

        self.assertEqual(service._get_volumes_from(), [container_id])
        from_service.create_container.assert_called_once_with()
Example #23
0
 def test_env_from_multiple_files(self):
     service = Service('foo',
             env_file=['tests/fixtures/env/one.env', 'tests/fixtures/env/two.env'],
             client=self.mock_client,
         )
     options = service._get_container_create_options({})
     self.assertEqual(
         options['environment'],
         {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'}
         )
Example #24
0
 def test_start_container_uses_tagged_image_if_it_exists(self):
     self.client.build('tests/fixtures/simple-dockerfile', tag='figtest_test')
     service = Service(
         name='test',
         client=self.client,
         build='this/does/not/exist/and/will/throw/error',
         project='figtest',
     )
     container = service.start_container()
     container.wait()
     self.assertIn('success', container.logs())
Example #25
0
 def test_env_from_file(self):
     service = Service('foo',
             env_file='tests/fixtures/env/one.env',
             client=self.mock_client,
             image='image_name',
         )
     options = service._get_container_create_options({})
     self.assertEqual(
         options['environment'],
         {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'}
         )
Example #26
0
 def test_resolve_environment_from_file(self):
     os.environ["FILE_DEF"] = "E1"
     os.environ["FILE_DEF_EMPTY"] = "E2"
     os.environ["ENV_DEF"] = "E3"
     service = Service(
         "foo", env_file=["tests/fixtures/env/resolve.env"], client=self.mock_client, image="image_name"
     )
     options = service._get_container_create_options({})
     self.assertEqual(
         options["environment"], {"FILE_DEF": "F1", "FILE_DEF_EMPTY": "", "ENV_DEF": "E3", "NO_DEF": ""}
     )
Example #27
0
 def test_start_container_builds_images(self):
     service = Service(
         name='test',
         client=self.client,
         build='tests/fixtures/simple-dockerfile',
         project='figtest',
     )
     container = service.start_container()
     container.wait()
     self.assertIn('success', container.logs())
     self.assertEqual(len(self.client.images(name='figtest_test')), 1)
Example #28
0
 def test_create_container_from_insecure_registry(self, mock_log):
     service = Service("foo", client=self.mock_client, image="someimage:sometag")
     mock_response = mock.Mock(Response)
     mock_response.status_code = 404
     mock_response.reason = "Not Found"
     Container.create = mock.Mock()
     Container.create.side_effect = APIError("Mock error", mock_response, "No such image")
     try:
         service.create_container(insecure_registry=True)
     except APIError:  # We expect the APIError because our service requires a non-existent image.
         pass
     self.mock_client.pull.assert_called_once_with("someimage:sometag", insecure_registry=True, stream=True)
     mock_log.info.assert_called_once_with("Pulling image someimage:sometag...")
Example #29
0
 def test_create_container_from_insecure_registry(self, mock_log):
     service = Service('foo', client=self.mock_client, image='someimage:sometag')
     mock_response = mock.Mock(Response)
     mock_response.status_code = 404
     mock_response.reason = "Not Found"
     Container.create = mock.Mock()
     Container.create.side_effect = APIError('Mock error', mock_response, "No such image")
     try:
         service.create_container(insecure_registry=True)
     except APIError:  # We expect the APIError because our service requires a non-existent image.
         pass
     self.mock_client.pull.assert_called_once_with('someimage:sometag', insecure_registry=True, stream=True)
     mock_log.info.assert_called_once_with('Pulling image someimage:sometag...')
Example #30
0
 def test_resolve_environment_from_file(self):
     os.environ['FILE_DEF'] = 'E1'
     os.environ['FILE_DEF_EMPTY'] = 'E2'
     os.environ['ENV_DEF'] = 'E3'
     service = Service('foo',
             env_file=['tests/fixtures/env/resolve.env'],
             client=self.mock_client,
         )
     options = service._get_container_create_options({})
     self.assertEqual(
         options['environment'],
         {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}
         )
Example #31
0
 def test_resolve_environment(self):
     os.environ['FILE_DEF'] = 'E1'
     os.environ['FILE_DEF_EMPTY'] = 'E2'
     os.environ['ENV_DEF'] = 'E3'
     service = Service('foo',
             environment={'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': None, 'NO_DEF': None},
             client=self.mock_client,
         )
     options = service._get_container_create_options({})
     self.assertEqual(
         options['environment'],
         {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}
         )
Example #32
0
 def test_env_from_file(self):
     service = Service(
         'foo',
         env_file='tests/fixtures/env/one.env',
         client=self.mock_client,
         image='image_name',
     )
     options = service._get_container_create_options({})
     self.assertEqual(options['environment'], {
         'ONE': '2',
         'TWO': '1',
         'THREE': '3',
         'FOO': 'bar'
     })
Example #33
0
 def test_resolve_environment(self):
     os.environ["FILE_DEF"] = "E1"
     os.environ["FILE_DEF_EMPTY"] = "E2"
     os.environ["ENV_DEF"] = "E3"
     service = Service(
         "foo",
         environment={"FILE_DEF": "F1", "FILE_DEF_EMPTY": "", "ENV_DEF": None, "NO_DEF": None},
         client=self.mock_client,
         image="image_name",
     )
     options = service._get_container_create_options({})
     self.assertEqual(
         options["environment"], {"FILE_DEF": "F1", "FILE_DEF_EMPTY": "", "ENV_DEF": "E3", "NO_DEF": ""}
     )
Example #34
0
 def test_parse_environment(self):
     service = Service(
         'foo',
         environment=[
             'NORMAL=F1', 'CONTAINS_EQUALS=F=2', 'TRAILING_EQUALS='
         ],
         client=self.mock_client,
         image='image_name',
     )
     options = service._get_container_create_options({})
     self.assertEqual(options['environment'], {
         'NORMAL': 'F1',
         'CONTAINS_EQUALS': 'F=2',
         'TRAILING_EQUALS': ''
     })
Example #35
0
 def test_resolve_environment_from_file(self):
     os.environ['FILE_DEF'] = 'E1'
     os.environ['FILE_DEF_EMPTY'] = 'E2'
     os.environ['ENV_DEF'] = 'E3'
     service = Service(
         'foo',
         env_file=['tests/fixtures/env/resolve.env'],
         client=self.mock_client,
         image='image_name',
     )
     options = service._get_container_create_options({})
     self.assertEqual(options['environment'], {
         'FILE_DEF': 'F1',
         'FILE_DEF_EMPTY': '',
         'ENV_DEF': 'E3',
         'NO_DEF': ''
     })
Example #36
0
    def test_create_container_from_insecure_registry(
            self,
            mock_log,
            mock_container):
        service = Service('foo', client=self.mock_client, image='someimage:sometag')
        mock_response = mock.Mock(Response)
        mock_response.status_code = 404
        mock_response.reason = "Not Found"
        mock_container.create.side_effect = APIError(
            'Mock error', mock_response, "No such image")

        # We expect the APIError because our service requires a
        # non-existent image.
        with self.assertRaises(APIError):
            service.create_container(insecure_registry=True)

        self.mock_client.pull.assert_called_once_with(
            'someimage:sometag',
            insecure_registry=True,
            stream=True)
        mock_log.info.assert_called_once_with(
            'Pulling image someimage:sometag...')
Example #37
0
    def test_build_with_cache(self):
        mock_client = mock.create_autospec(docker.Client)
        service = Service('buildtest',
                          client=mock_client,
                          build='/path',
                          tags=['foo', 'foo:v2'])
        expected = 'abababab'

        with mock.patch('fig.service.stream_output') as mock_stream_output:
            mock_stream_output.return_value = [
                dict(stream='Successfully built %s' % expected)
            ]
            image_id = service.build()
        self.assertEqual(image_id, expected)
        mock_client.build.assert_called_once_with('/path',
                                                  tag=service.full_name,
                                                  stream=True,
                                                  rm=True,
                                                  nocache=False)

        self.assertEqual(mock_client.tag.mock_calls, [
            mock.call(image_id, 'foo', tag=None),
            mock.call(image_id, 'foo', tag='v2'),
        ])
Example #38
0
 def test_bad_tags_from_config(self):
     with self.assertRaises(ConfigError) as exc_context:
         Service('something', tags='my_tag_is_a_string')
     self.assertEqual(str(exc_context.exception),
                      'Service something tags must be a list.')
Example #39
0
 def test_split_domainname_none(self):
     service = Service('foo', hostname='name', client=self.mock_client)
     self.mock_client.containers.return_value = []
     opts = service._get_container_create_options({'image': 'foo'})
     self.assertEqual(opts['hostname'], 'name', 'hostname')
     self.assertFalse('domainname' in opts, 'domainname')
Example #40
0
 def test_pull_image(self, mock_log):
     service = Service('foo', client=self.mock_client, image='someimage:sometag')
     service.pull(insecure_registry=True)
     self.mock_client.pull.assert_called_once_with('someimage:sometag', insecure_registry=True)
     mock_log.info.assert_called_once_with('Pulling foo (someimage:sometag)...')
Example #41
0
    def test_get_container_not_found(self):
        self.mock_client.containers.return_value = []
        service = Service('foo', client=self.mock_client)

        self.assertRaises(ValueError, service.get_container)
Example #42
0
    def test_get_volumes_from_intermediate_container(self):
        container_id = 'aabbccddee'
        service = Service('test')
        container = mock.Mock(id=container_id, spec=Container)

        self.assertEqual(service._get_volumes_from(container), [container_id])
Example #43
0
 def test_latest_is_used_when_tag_is_not_specified(self):
     service = Service("foo", client=self.mock_client, image="someimage")
     Container.create = mock.Mock()
     service.create_container()
     self.assertEqual(Container.create.call_args[1]["image"], "someimage:latest")
Example #44
0
 def test_config_validation(self):
     self.assertRaises(ConfigError,
                       lambda: Service(name='foo', port=['8000']))
     Service(name='foo', ports=['8000'])
Example #45
0
 def test_project_validation(self):
     self.assertRaises(ConfigError,
                       lambda: Service(name='foo', project='_'))
     Service(name='foo', project='bar')
Example #46
0
 def test_latest_is_used_when_tag_is_not_specified(self):
     service = Service('foo', client=self.mock_client, image='someimage')
     Container.create = mock.Mock()
     service.create_container()
     self.assertEqual(Container.create.call_args[1]['image'],
                      'someimage:latest')
Example #47
0
 def test_split_domainname_weird(self):
     service = Service("foo", hostname="name.sub", domainname="domain.tld", client=self.mock_client)
     self.mock_client.containers.return_value = []
     opts = service._get_container_create_options({"image": "foo"})
     self.assertEqual(opts["hostname"], "name.sub", "hostname")
     self.assertEqual(opts["domainname"], "domain.tld", "domainname")
Example #48
0
 def test_pull_image(self, mock_log):
     service = Service("foo", client=self.mock_client, image="someimage:sometag")
     service.pull(insecure_registry=True)
     self.mock_client.pull.assert_called_once_with("someimage:sometag", insecure_registry=True)
     mock_log.info.assert_called_once_with("Pulling foo (someimage:sometag)...")
Example #49
0
 def test_latest_is_used_when_tag_is_not_specified(self):
     service = Service('foo', client=self.mock_client, image='someimage')
     Container.create = mock.Mock()
     service.create_container()
     self.assertEqual(Container.create.call_args[1]['image'], 'someimage:latest')
Example #50
0
    def test_get_container_not_found(self):
        mock_client = mock.create_autospec(docker.Client)
        mock_client.containers.return_value = []
        service = Service('foo', client=mock_client)

        self.assertRaises(ValueError, service.get_container)
Example #51
0
    def test_name_validations(self):
        self.assertRaises(ConfigError, lambda: Service(name=''))

        self.assertRaises(ConfigError, lambda: Service(name=' '))
        self.assertRaises(ConfigError, lambda: Service(name='/'))
        self.assertRaises(ConfigError, lambda: Service(name='!'))
        self.assertRaises(ConfigError, lambda: Service(name='\xe2'))
        self.assertRaises(ConfigError, lambda: Service(name='_'))
        self.assertRaises(ConfigError, lambda: Service(name='____'))
        self.assertRaises(ConfigError, lambda: Service(name='foo_bar'))
        self.assertRaises(ConfigError, lambda: Service(name='__foo_bar__'))

        Service('a')
        Service('foo')
Example #52
0
    def test_get_volumes_from_intermediate_container(self):
        container_id = 'aabbccddee'
        service = Service('test')
        container = mock.Mock(id=container_id, spec=Container)

        self.assertEqual(service._get_volumes_from(container), [container_id])
Example #53
0
 def test_build_with_build_error(self):
     mock_client = mock.create_autospec(docker.Client)
     service = Service('buildtest', client=mock_client, build='/path')
     with self.assertRaises(BuildError):
         service.build()