Ejemplo n.º 1
0
    def test_running_with_predifined_volume(self):
        '''
        Test dockerng.running function with an image
        that already have VOLUME defined.

        The ``binds`` argument, shouldn't have side effects on
        container creation.
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_history = MagicMock(return_value=['VOLUME /container-0'])
        __salt__ = {'dockerng.list_containers': MagicMock(),
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(),
                    'dockerng.state': MagicMock(),
                    'dockerng.inspect_image': MagicMock(),
                    'dockerng.create': dockerng_create,
                    'dockerng.start': dockerng_start,
                    'dockerng.history': dockerng_history,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            dockerng_state.running(
                'cont',
                image='image:latest',
                binds=['/host-0:/container-0:ro'])
        dockerng_create.assert_called_with(
            'image:latest',
            validate_input=False,
            binds={'/host-0': {'bind': '/container-0', 'ro': True}},
            validate_ip_addrs=False,
            name='cont',
            client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 2
0
 def test_running_with_labels(self):
     '''
     Test dockerng.running with labels parameter.
     '''
     dockerng_create = Mock()
     __salt__ = {'dockerng.list_containers': MagicMock(),
                 'dockerng.list_tags': MagicMock(),
                 'dockerng.pull': MagicMock(),
                 'dockerng.state': MagicMock(),
                 'dockerng.inspect_image': MagicMock(),
                 'dockerng.create': dockerng_create,
                 }
     with patch.dict(dockerng_state.__dict__,
                     {'__salt__': __salt__}):
         dockerng_state.running(
             'cont',
             image='image:latest',
             labels=['LABEL1', 'LABEL2'],
             )
     dockerng_create.assert_called_with(
         'image:latest',
         validate_input=False,
         validate_ip_addrs=False,
         name='cont',
         labels=['LABEL1', 'LABEL2'],
         client_timeout=60)
Ejemplo n.º 3
0
 def test_running_with_labels(self):
     '''
     Test dockerng.running with labels parameter.
     '''
     dockerng_create = Mock()
     __salt__ = {'dockerng.list_containers': MagicMock(),
                 'dockerng.list_tags': MagicMock(),
                 'dockerng.pull': MagicMock(),
                 'dockerng.state': MagicMock(),
                 'dockerng.inspect_image': MagicMock(),
                 'dockerng.create': dockerng_create,
                 }
     with patch.dict(dockerng_state.__dict__,
                     {'__salt__': __salt__}):
         dockerng_state.running(
             'cont',
             image='image:latest',
             labels=['LABEL1', 'LABEL2'],
             )
     dockerng_create.assert_called_with(
         'image:latest',
         validate_input=False,
         validate_ip_addrs=False,
         name='cont',
         labels=['LABEL1', 'LABEL2'],
         client_timeout=60)
Ejemplo n.º 4
0
    def test_running_with_predifined_ports(self):
        '''
        Test dockerng.running function with an image
        that contains EXPOSE statements.

        The ``port_bindings`` argument, shouldn't have side effect on container
        creation.
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_history = MagicMock(return_value=['EXPOSE 9797/tcp'])
        __salt__ = {'dockerng.list_containers': MagicMock(),
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(),
                    'dockerng.state': MagicMock(),
                    'dockerng.inspect_image': MagicMock(),
                    'dockerng.create': dockerng_create,
                    'dockerng.start': dockerng_start,
                    'dockerng.history': dockerng_history,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            dockerng_state.running(
                'cont',
                image='image:latest',
                port_bindings=['9090:9797/tcp'])
        dockerng_create.assert_called_with(
            'image:latest',
            validate_input=False,
            name='cont',
            port_bindings={9797: [9090]},
            validate_ip_addrs=False,
            client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 5
0
    def test_running_with_predifined_ports(self):
        '''
        Test dockerng.running function with an image
        that contains EXPOSE statements.

        The ``port_bindings`` argument, shouldn't have side effect on container
        creation.
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_history = MagicMock(return_value=['EXPOSE 9797/tcp'])
        __salt__ = {
            'dockerng.list_containers': MagicMock(),
            'dockerng.list_tags': MagicMock(),
            'dockerng.pull': MagicMock(),
            'dockerng.state': MagicMock(),
            'dockerng.inspect_image': MagicMock(),
            'dockerng.create': dockerng_create,
            'dockerng.start': dockerng_start,
            'dockerng.history': dockerng_history,
        }
        with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
            dockerng_state.running('cont',
                                   image='image:latest',
                                   port_bindings=['9090:9797/tcp'])
        dockerng_create.assert_called_with('image:latest',
                                           validate_input=False,
                                           name='cont',
                                           client_timeout=60)
        dockerng_start.assert_called_with('cont',
                                          port_bindings={9797: [9090]},
                                          validate_ip_addrs=False,
                                          validate_input=False)
Ejemplo n.º 6
0
    def test_running_with_no_predifined_volume(self):
        '''
        Test dockerng.running function with an image
        that doens't have VOLUME defined.

        The ``binds`` argument, should create a container
        with respective volumes extracted from ``binds``.
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        __salt__ = {'dockerng.list_containers': MagicMock(),
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(),
                    'dockerng.state': MagicMock(),
                    'dockerng.inspect_image': MagicMock(),
                    'dockerng.create': dockerng_create,
                    'dockerng.start': dockerng_start,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            dockerng_state.running(
                'cont',
                image='image:latest',
                binds=['/host-0:/container-0:ro'])
        dockerng_create.assert_called_with(
            'image:latest',
            validate_input=False,
            name='cont',
            binds={'/host-0': {'bind': '/container-0', 'ro': True}},
            volumes=['/container-0'],
            validate_ip_addrs=False,
            client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 7
0
    def test_running_with_no_predifined_ports(self):
        '''
        Test dockerng.running function with an image
        that doens't have EXPOSE defined.

        The ``port_bindings`` argument, should create a container
        with respective ``ports`` extracted from ``port_bindings``.
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_history = MagicMock(return_value=[])
        __salt__ = {
            'dockerng.list_containers': MagicMock(),
            'dockerng.list_tags': MagicMock(),
            'dockerng.pull': MagicMock(),
            'dockerng.state': MagicMock(),
            'dockerng.create': dockerng_create,
            'dockerng.start': dockerng_start,
            'dockerng.history': dockerng_history,
        }
        with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
            dockerng_state.running('cont',
                                   image='image:latest',
                                   port_bindings=['9090:9797/tcp'])
        dockerng_create.assert_called_with('image:latest',
                                           validate_input=False,
                                           name='cont',
                                           ports=[9797],
                                           client_timeout=60)
        dockerng_start.assert_called_with('cont',
                                          port_bindings={9797: [9090]},
                                          validate_ip_addrs=False,
                                          validate_input=False)
Ejemplo n.º 8
0
    def test_running_with_no_predifined_volume(self):
        '''
        Test dockerng.running function with an image
        that doens't have VOLUME defined.

        The ``binds`` argument, should create a container
        with respective volumes extracted from ``binds``.
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        __salt__ = {'dockerng.list_containers': MagicMock(),
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(),
                    'dockerng.state': MagicMock(),
                    'dockerng.inspect_image': MagicMock(),
                    'dockerng.create': dockerng_create,
                    'dockerng.start': dockerng_start,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            dockerng_state.running(
                'cont',
                image='image:latest',
                binds=['/host-0:/container-0:ro'])
        dockerng_create.assert_called_with(
            'image:latest',
            validate_input=False,
            name='cont',
            binds={'/host-0': {'bind': '/container-0', 'ro': True}},
            volumes=['/container-0'],
            validate_ip_addrs=False,
            client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 9
0
    def test_running_with_udp_bindings(self):
        '''
        Check that `ports` contains ports defined from `port_bindings` with
        protocol declaration passed as tuple. As stated by docker-py
        documentation

        https://docker-py.readthedocs.io/en/latest/port-bindings/

        In sls:

        .. code-block:: yaml

            container:
                dockerng.running:
                    - port_bindings:
                        - '9090:9797/udp'

        is equivalent of:

        .. code-block:: yaml

            container:
                dockerng.running:
                    - ports:
                        - 9797/udp
                    - port_bindings:
                        - '9090:9797/udp'
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_inspect_image = Mock(return_value={
            'Id': 'abcd',
            'Config': {
                'ExposedPorts': {}
            }
        })
        __salt__ = {
            'dockerng.list_containers': MagicMock(),
            'dockerng.list_tags': MagicMock(),
            'dockerng.pull': MagicMock(),
            'dockerng.state': MagicMock(),
            'dockerng.inspect_image': dockerng_inspect_image,
            'dockerng.create': dockerng_create,
            'dockerng.start': dockerng_start,
        }
        with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
            with patch.dict(dockerng_mod.__salt__,
                            {'dockerng.version': MagicMock(return_value={})}):
                dockerng_state.running('cont',
                                       image='image:latest',
                                       port_bindings=['9090:9797/udp'])
        dockerng_create.assert_called_with('image:latest',
                                           validate_input=False,
                                           name='cont',
                                           ports=[(9797, 'udp')],
                                           port_bindings={'9797/udp': [9090]},
                                           validate_ip_addrs=False,
                                           client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 10
0
    def test_running_with_predifined_ports(self):
        '''
        Test dockerng.running function with an image
        that expose ports (via Dockerfile EXPOSE statement).

        Check that `ports` contains ports defined on Image and by
        `port_bindings` argument.

        Inside Dockerfile:

        .. code-block::

            EXPOSE 9898

        In sls:

        .. code-block:: yaml

            container:
                dockerng.running:
                    - port_bindings:
                        - '9090:9797/tcp'

        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_inspect_image = Mock(return_value={
            'Id': 'abcd',
            'Config': {
                'ExposedPorts': {
                    '9898/tcp': {}
                }
            }
        })
        __salt__ = {
            'dockerng.list_containers': MagicMock(),
            'dockerng.list_tags': MagicMock(),
            'dockerng.pull': MagicMock(),
            'dockerng.state': MagicMock(),
            'dockerng.inspect_image': dockerng_inspect_image,
            'dockerng.create': dockerng_create,
            'dockerng.start': dockerng_start,
        }
        with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
            with patch.dict(dockerng_mod.__salt__,
                            {'dockerng.version': MagicMock(return_value={})}):
                dockerng_state.running('cont',
                                       image='image:latest',
                                       port_bindings=['9090:9797/tcp'])
        dockerng_create.assert_called_with('image:latest',
                                           validate_input=False,
                                           name='cont',
                                           ports=[9797],
                                           port_bindings={9797: [9090]},
                                           validate_ip_addrs=False,
                                           client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 11
0
    def test_running_with_udp_bindings(self):
        '''
        Check that `ports` contains ports defined from `port_bindings` with
        protocol declaration passed as tuple. As stated by docker-py
        documentation

        https://docker-py.readthedocs.io/en/latest/port-bindings/

        In sls:

        .. code-block:: yaml

            container:
                dockerng.running:
                    - port_bindings:
                        - '9090:9797/udp'

        is equivalent of:

        .. code-block:: yaml

            container:
                dockerng.running:
                    - ports:
                        - 9797/udp
                    - port_bindings:
                        - '9090:9797/udp'
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_inspect_image = Mock(return_value={
            'Id': 'abcd',
            'Config': {'ExposedPorts': {}}
        })
        __salt__ = {'dockerng.list_containers': MagicMock(),
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(),
                    'dockerng.state': MagicMock(),
                    'dockerng.inspect_image': dockerng_inspect_image,
                    'dockerng.create': dockerng_create,
                    'dockerng.start': dockerng_start,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            dockerng_state.running(
                'cont',
                image='image:latest',
                port_bindings=['9090:9797/udp'])
        dockerng_create.assert_called_with(
            'image:latest',
            validate_input=False,
            name='cont',
            ports=[(9797, 'udp')],
            port_bindings={'9797/udp': [9090]},
            validate_ip_addrs=False,
            client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 12
0
    def test_running_with_labels_from_image(self):
        '''
        Test dockerng.running with labels parameter supports also
        labels carried by the image.
        '''
        dockerng_create = Mock()

        image_id = 'a' * 128
        dockerng_inspect_image = MagicMock(
            return_value={
                'Id': image_id,
                'Config': {
                    'Hostname': 'saltstack-container',
                    'WorkingDir': '/',
                    'Cmd': ['bash'],
                    'Volumes': {
                        '/path': {}
                    },
                    'Entrypoint': None,
                    'ExposedPorts': {},
                    'Labels': {
                        'IMAGE_LABEL': 'image_foo',
                        'LABEL1': 'label1'
                    },
                },
            })
        __salt__ = {
            'dockerng.list_containers': MagicMock(),
            'dockerng.list_tags': MagicMock(),
            'dockerng.pull': MagicMock(),
            'dockerng.state': MagicMock(),
            'dockerng.inspect_image': dockerng_inspect_image,
            'dockerng.create': dockerng_create,
        }
        with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
            dockerng_state.running(
                'cont',
                image='image:latest',
                labels=[{
                    'LABEL1': 'foo1'
                }, {
                    'LABEL2': 'foo2'
                }],
            )
        dockerng_create.assert_called_with('image:latest',
                                           validate_input=False,
                                           validate_ip_addrs=False,
                                           name='cont',
                                           labels={
                                               'LABEL1': 'foo1',
                                               'LABEL2': 'foo2'
                                           },
                                           client_timeout=60)
Ejemplo n.º 13
0
    def test_running_with_predifined_ports(self):
        '''
        Test dockerng.running function with an image
        that expose ports (via Dockerfile EXPOSE statement).

        Check that `ports` contains ports defined on Image and by
        `port_bindings` argument.

        Inside Dockerfile:

        .. code-block::

            EXPOSE 9898

        In sls:

        .. code-block:: yaml

            container:
                dockerng.running:
                    - port_bindings:
                        - '9090:9797/tcp'

        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_inspect_image = Mock(return_value={
            'Id': 'abcd',
            'Config': {'ExposedPorts': {'9898/tcp': {}}}
        })
        __salt__ = {'dockerng.list_containers': MagicMock(),
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(),
                    'dockerng.state': MagicMock(),
                    'dockerng.inspect_image': dockerng_inspect_image,
                    'dockerng.create': dockerng_create,
                    'dockerng.start': dockerng_start,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            dockerng_state.running(
                'cont',
                image='image:latest',
                port_bindings=['9090:9797/tcp'])
        dockerng_create.assert_called_with(
            'image:latest',
            validate_input=False,
            name='cont',
            ports=[9797],
            port_bindings={9797: [9090]},
            validate_ip_addrs=False,
            client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 14
0
    def test_running_compare_images_by_id(self):
        '''
        Make sure the container is running
        against expected image.

        Here the local image is named 'image:latest' and the container
        is also running against an image called 'image:latest'.
        Therefore the image ids are diverging because the tag 'image:latest'
        moved to a fresher image.
        Thus this test make sure the old container is droped and recreated.
        '''
        new_fake_image_id = 'abcdefgh'
        old_fake_image_id = '123456789'
        dockerng_inspect_image = Mock(return_value={'Id': new_fake_image_id})
        dockerng_inspect_container = Mock(return_value={
            'Image': old_fake_image_id,
            'Config': {
                'Image': 'image:latest'
            }
        })
        dockerng_list_containers = Mock(return_value=['cont'])
        dockerng__state = Mock(return_value='running')
        dockerng_stop = Mock(return_value={'result': True})
        dockerng_rm = Mock(return_value=['container-id'])
        __salt__ = {
            'dockerng.list_containers': dockerng_list_containers,
            'dockerng.inspect_container': dockerng_inspect_container,
            'dockerng.inspect_image': dockerng_inspect_image,
            'dockerng.list_tags': MagicMock(),
            'dockerng.state': dockerng__state,
            'dockerng.pull': MagicMock(return_value=new_fake_image_id),
            'dockerng.create': MagicMock(return_value='new_container'),
            'dockerng.start': MagicMock(),
            'dockerng.stop': dockerng_stop,
            'dockerng.rm': dockerng_rm,
        }
        with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
            with patch.dict(dockerng_mod.__salt__,
                            {'dockerng.version': MagicMock(return_value={})}):
                ret = dockerng_state.running(
                    'cont',
                    image='image:latest',
                )
                dockerng_stop.assert_called_with('cont',
                                                 timeout=10,
                                                 unpause=True)
                dockerng_rm.assert_called_with('cont')
        self.assertEqual(
            ret, {
                'name': 'cont',
                'comment': "Container 'cont' was replaced",
                'result': True,
                'changes': {
                    'added': 'new_container',
                    'image': new_fake_image_id,
                    'removed': ['container-id']
                }
            })
Ejemplo n.º 15
0
    def test_running_with_labels_from_image(self):
        '''
        Test dockerng.running with labels parameter supports also
        labels carried by the image.
        '''
        dockerng_create = Mock()

        image_id = 'a' * 128
        dockerng_inspect_image = MagicMock(
            return_value={
                'Id': image_id,
                'Config': {
                    'Hostname': 'saltstack-container',
                    'WorkingDir': '/',
                    'Cmd': ['bash'],
                    'Volumes': {'/path': {}},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                    'Labels': {'IMAGE_LABEL': 'image_foo',
                               'LABEL1': 'label1'},
                },
                })
        __salt__ = {'dockerng.list_containers': MagicMock(),
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(),
                    'dockerng.state': MagicMock(),
                    'dockerng.inspect_image': dockerng_inspect_image,
                    'dockerng.create': dockerng_create,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            dockerng_state.running(
                'cont',
                image='image:latest',
                labels=[{'LABEL1': 'foo1'}, {'LABEL2': 'foo2'}],
                )
        dockerng_create.assert_called_with(
            'image:latest',
            validate_input=False,
            validate_ip_addrs=False,
            name='cont',
            labels={'LABEL1': 'foo1', 'LABEL2': 'foo2'},
            client_timeout=60)
Ejemplo n.º 16
0
    def test_running_with_no_predifined_ports(self):
        '''
        Test dockerng.running function with an image
        that doens't have EXPOSE defined.

        The ``port_bindings`` argument, should create a container
        with ``ports`` extracted from ``port_bindings``.
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_inspect_image = Mock(return_value={
            'Id': 'abcd',
            'Config': {
                'Config': {
                    'ExposedPorts': {}
                }
            },
        })
        __salt__ = {
            'dockerng.list_containers': MagicMock(),
            'dockerng.list_tags': MagicMock(),
            'dockerng.pull': MagicMock(),
            'dockerng.state': MagicMock(),
            'dockerng.inspect_image': dockerng_inspect_image,
            'dockerng.create': dockerng_create,
            'dockerng.start': dockerng_start,
        }
        with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
            with patch.dict(dockerng_mod.__salt__,
                            {'dockerng.version': MagicMock(return_value={})}):
                dockerng_state.running('cont',
                                       image='image:latest',
                                       port_bindings=['9090:9797/tcp'])
        dockerng_create.assert_called_with('image:latest',
                                           validate_input=False,
                                           name='cont',
                                           ports=[9797],
                                           port_bindings={9797: [9090]},
                                           validate_ip_addrs=False,
                                           client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 17
0
    def test_running_discard_wrong_environment_values(self):
        '''
        environment values should be string.
        It is easy to write wrong sls this way

        .. code-block:: yaml

            container:
                dockerng.running:
                    - environment:
                        - KEY: 1

        instead of:

        .. code-block:: yaml

            container:
                dockerng.running:
                    - environment:
                        - KEY: "1"
        '''
        __salt__ = {
            'dockerng.list_containers': MagicMock(),
            'dockerng.list_tags': MagicMock(return_value=['image:latest']),
            'dockerng.inspect_image': MagicMock(),
            'dockerng.pull': MagicMock(),
            'dockerng.state': MagicMock(),
            'dockerng.create': MagicMock(),
            'dockerng.start': MagicMock(),
        }
        with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
            with patch.dict(dockerng_mod.__salt__,
                            {'dockerng.version': MagicMock(return_value={})}):
                for wrong_value in (1, .2, (), [], {}):
                    ret = dockerng_state.running('cont',
                                                 image='image:latest',
                                                 environment=[{
                                                     'KEY':
                                                     wrong_value
                                                 }])
                    self.assertEqual(
                        ret, {
                            'changes': {},
                            'comment':
                            'Environment values must'
                            ' be strings KEY=\'{0}\''.format(wrong_value),
                            'name':
                            'cont',
                            'result':
                            False
                        })
Ejemplo n.º 18
0
    def test_running_with_no_predifined_ports(self):
        '''
        Test dockerng.running function with an image
        that doens't have EXPOSE defined.

        The ``port_bindings`` argument, should create a container
        with ``ports`` extracted from ``port_bindings``.
        '''
        dockerng_create = Mock()
        dockerng_start = Mock()
        dockerng_inspect_image = Mock(return_value={
            'Id': 'abcd',
            'Config': {'Config': {'ExposedPorts': {}}},
        })
        __salt__ = {'dockerng.list_containers': MagicMock(),
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(),
                    'dockerng.state': MagicMock(),
                    'dockerng.inspect_image': dockerng_inspect_image,
                    'dockerng.create': dockerng_create,
                    'dockerng.start': dockerng_start,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            dockerng_state.running(
                'cont',
                image='image:latest',
                port_bindings=['9090:9797/tcp'])
        dockerng_create.assert_called_with(
            'image:latest',
            validate_input=False,
            name='cont',
            ports=[9797],
            port_bindings={9797: [9090]},
            validate_ip_addrs=False,
            client_timeout=60)
        dockerng_start.assert_called_with('cont')
Ejemplo n.º 19
0
    def test_running_compare_images_by_id(self):
        '''
        Make sure the container is running
        against expected image.

        Here the local image is named 'image:latest' and the container
        is also running against an image called 'image:latest'.
        Therefore the image ids are diverging because the tag 'image:latest'
        moved to a fresher image.
        Thus this test make sure the old container is droped and recreated.
        '''
        new_fake_image_id = 'abcdefgh'
        old_fake_image_id = '123456789'
        dockerng_inspect_image = Mock(return_value={'Id': new_fake_image_id})
        dockerng_inspect_container = Mock(
            return_value={'Image': old_fake_image_id,
                          'Config': {'Image': 'image:latest'}})
        dockerng_list_containers = Mock(return_value=['cont'])
        dockerng__state = Mock(return_value='running')
        dockerng_stop = Mock(return_value={'result': True})
        dockerng_rm = Mock(return_value=['container-id'])
        __salt__ = {'dockerng.list_containers': dockerng_list_containers,
                    'dockerng.inspect_container': dockerng_inspect_container,
                    'dockerng.inspect_image': dockerng_inspect_image,
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.state': dockerng__state,
                    'dockerng.pull': MagicMock(return_value=new_fake_image_id),
                    'dockerng.create': MagicMock(return_value='new_container'),
                    'dockerng.start': MagicMock(),
                    'dockerng.stop': dockerng_stop,
                    'dockerng.rm': dockerng_rm,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            ret = dockerng_state.running(
                'cont',
                image='image:latest',
                )
            dockerng_stop.assert_called_with('cont', timeout=10, unpause=True)
            dockerng_rm.assert_called_with('cont')
        self.assertEqual(ret, {'name': 'cont',
                               'comment': "Container 'cont' was replaced",
                               'result': True,
                               'changes': {'added': 'new_container',
                                           'image': new_fake_image_id,
                                           'removed': ['container-id']}
                               })
Ejemplo n.º 20
0
    def test_running_discard_wrong_environemnt_values(self):
        '''
        environment values should be string.
        It is easy to write wrong sls this way

        .. code-block:: yaml

            container:
                dockerng.running:
                    - environment:
                        - KEY: 1

        instead of:

        .. code-block:: yaml

            container:
                dockerng.running:
                    - environment:
                        - KEY: "1"
        '''
        __salt__ = {'dockerng.list_containers': MagicMock(),
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.inspect_image': MagicMock(),
                    'dockerng.pull': MagicMock(),
                    'dockerng.state': MagicMock(),
                    'dockerng.create': MagicMock(),
                    'dockerng.start': MagicMock(),
                    'dockerng.history': MagicMock(),
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            for wrong_value in (1, .2, (), [], {}):
                ret = dockerng_state.running(
                    'cont',
                    image='image:latest',
                    environment=[{'KEY': wrong_value}])
                self.assertEqual(ret,
                                 {'changes': {},
                                  'comment': 'Environment values must'
                                  ' be strings KEY=\'{0}\''.format(wrong_value),
                                  'name': 'cont',
                                  'result': False})
Ejemplo n.º 21
0
 def test_check_start_true(self):
     '''
     If start is True, then dockerng.running will try
     to start a container that is stopped.
     '''
     image_id = 'abcdefg'
     dockerng_create = Mock()
     dockerng_start = Mock()
     dockerng_list_containers = Mock(return_value=['cont'])
     dockerng_inspect_container = Mock(return_value={
         'Config': {
             'Image': 'image:latest'
         },
         'Image': image_id
     })
     __salt__ = {
         'dockerng.list_containers': dockerng_list_containers,
         'dockerng.inspect_container': dockerng_inspect_container,
         'dockerng.inspect_image': MagicMock(return_value={'Id': image_id}),
         'dockerng.list_tags': MagicMock(),
         'dockerng.pull': MagicMock(),
         'dockerng.state': MagicMock(side_effect=['stopped', 'running']),
         'dockerng.create': dockerng_create,
         'dockerng.start': dockerng_start,
     }
     with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
         ret = dockerng_state.running(
             'cont',
             image='image:latest',
             start=True,
         )
     self.assertEqual(
         ret, {
             'name': 'cont',
             'comment': "Container 'cont' changed state.",
             'changes': {
                 'state': {
                     'new': 'running',
                     'old': 'stopped'
                 }
             },
             'result': True,
         })
Ejemplo n.º 22
0
 def test_check_start_true(self):
     '''
     If start is True, then dockerng.running will try
     to start a container that is stopped.
     '''
     image_id = 'abcdefg'
     dockerng_create = Mock()
     dockerng_start = Mock()
     dockerng_list_containers = Mock(return_value=['cont'])
     dockerng_inspect_container = Mock(
         return_value={'Config': {'Image': 'image:latest'},
                       'Image': image_id})
     __salt__ = {'dockerng.list_containers': dockerng_list_containers,
                 'dockerng.inspect_container': dockerng_inspect_container,
                 'dockerng.inspect_image': MagicMock(
                     return_value={'Id': image_id}),
                 'dockerng.list_tags': MagicMock(),
                 'dockerng.pull': MagicMock(),
                 'dockerng.state': MagicMock(side_effect=['stopped',
                                                          'running']),
                 'dockerng.create': dockerng_create,
                 'dockerng.start': dockerng_start,
                 }
     with patch.dict(dockerng_state.__dict__,
                     {'__salt__': __salt__}):
         ret = dockerng_state.running(
             'cont',
             image='image:latest',
             start=True,
             )
     self.assertEqual(ret, {'name': 'cont',
                            'comment': "Container 'cont' changed state.",
                            'changes': {'state': {'new': 'running',
                                                  'old': 'stopped'}},
                            'result': True,
                            })
Ejemplo n.º 23
0
    def test_command_defined_on_image_layer_dont_diff_if_attempted_to_blank(self):
        '''
        Assuming the docker image has a command defined, like ``sh``.
        Erasing this value on sls level will not delete the command
        in the container. And such the diff shouldn't be reported.
        Assuming also the container is already running.

        1. define your sls

        .. code-block:: yaml

            cont:
                dockerng.running:
                    - image: image:latest


        2. run state.highstate

        No diff should be reported
        '''
        image_id = 'abcdefg'
        dockerng_create = Mock(return_value=True)
        dockerng_start = Mock()
        dockerng_list_containers = Mock(return_value=['cont'])
        dockerng_inspect_container = Mock(
            side_effect=[{
                'Config': {
                    'Image': 'image:latest',
                    'Tty': False,
                    'Labels': {},
                    'Domainname': '',
                    'User': '',
                    'AttachStderr': True,
                    'AttachStdout': True,
                    'Hostname': 'saltstack-container',
                    'Env': [],
                    'WorkingDir': '/',
                    'Cmd': None,
                    'Volumes': {},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                    'OpenStdin': False,
                },
                'HostConfig': {
                    'PublishAllPorts': False,
                    'Dns': [],
                    'Links': None,
                    'CpusetCpus': '',
                    'RestartPolicy': {'MaximumRetryCount': 0, 'Name': ''},
                    'CapAdd': None,
                    'NetworkMode': 'default',
                    'PidMode': '',
                    'MemorySwap': 0,
                    'ExtraHosts': None,
                    'PortBindings': None,
                    'LxcConf': None,
                    'DnsSearch': [],
                    'Privileged': False,
                    'Binds': [],
                    'Memory': 0,
                    'VolumesFrom': None,
                    'CpuShares': 0,
                    'CapDrop': None,
                },
                'NetworkSettings': {
                    'MacAddress': '00:00:00:00:00:01',
                },
                'Image': image_id}]
        )
        dockerng_inspect_image = MagicMock(
            return_value={
                'Id': image_id,
                'Config': {
                    'Hostname': 'saltstack-container',
                    'WorkingDir': '/',
                    'Cmd': ['bash'],  # !!! Cmd defined on Image
                    'Volumes': {},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                },
                })
        __salt__ = {'dockerng.list_containers': dockerng_list_containers,
                    'dockerng.inspect_container': dockerng_inspect_container,
                    'dockerng.inspect_image': dockerng_inspect_image,
                    'dockerng.list_tags': MagicMock(side_effect=[['image:latest']]),
                    'dockerng.state': MagicMock(side_effect=['running']),
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            ret = dockerng_state.running(
                'cont',
                image='image:latest',
                )
        self.assertEqual(ret, {'name': 'cont',
                               'comment': "Container 'cont' is already"
                               " configured as specified",
                               'changes': {},
                               'result': True,
                               })
Ejemplo n.º 24
0
 def test_check_start_true(self):
     '''
     If start is True, then dockerng.running will try
     to start a container that is stopped.
     '''
     image_id = 'abcdefg'
     dockerng_create = Mock()
     dockerng_start = Mock()
     dockerng_list_containers = Mock(return_value=['cont'])
     dockerng_inspect_container = Mock(
         return_value={
             'Config': {
                 'Image': 'image:latest',
                 'Tty': False,
                 'Labels': {},
                 'Domainname': '',
                 'User': '',
                 'AttachStderr': True,
                 'AttachStdout': True,
                 'Hostname': 'saltstack-container',
                 'Env': [],
                 'WorkingDir': '/',
                 'Cmd': ['bash'],
                 'Volumes': {},
                 'Entrypoint': None,
                 'ExposedPorts': {},
                 'OpenStdin': False,
             },
             'HostConfig': {
                 'PublishAllPorts': False,
                 'Dns': [],
                 'Links': None,
                 'CpusetCpus': '',
                 'RestartPolicy': {'MaximumRetryCount': 0, 'Name': ''},
                 'CapAdd': None,
                 'NetworkMode': 'default',
                 'PidMode': '',
                 'MemorySwap': 0,
                 'ExtraHosts': None,
                 'PortBindings': None,
                 'LxcConf': None,
                 'DnsSearch': [],
                 'Privileged': False,
                 'Binds': None,
                 'Memory': 0,
                 'VolumesFrom': None,
                 'CpuShares': 0,
                 'CapDrop': None,
             },
             'NetworkSettings': {
                 'MacAddress': '00:00:00:00:00:01',
             },
             'Image': image_id})
     dockerng_inspect_image = MagicMock(
         return_value={
             'Id': image_id,
             'Config': {
                 'Hostname': 'saltstack-container',
                 'WorkingDir': '/',
                 'Cmd': ['bash'],
                 'Volumes': {},
                 'Entrypoint': None,
                 'ExposedPorts': {},
             },
             })
     __salt__ = {'dockerng.list_containers': dockerng_list_containers,
                 'dockerng.inspect_container': dockerng_inspect_container,
                 'dockerng.inspect_image': dockerng_inspect_image,
                 'dockerng.list_tags': MagicMock(),
                 'dockerng.pull': MagicMock(return_value=True),
                 'dockerng.state': MagicMock(side_effect=['stopped',
                                                          'running']),
                 'dockerng.create': dockerng_create,
                 'dockerng.start': dockerng_start,
                 }
     with patch.dict(dockerng_state.__dict__,
                     {'__salt__': __salt__}):
         ret = dockerng_state.running(
             'cont',
             image='image:latest',
             start=True,
             )
     self.assertEqual(ret, {'name': 'cont',
                            'comment': "Container 'cont' changed state.",
                            'changes': {'state': {'new': 'running',
                                                  'old': 'stopped'},
                                        'image': True},
                            'result': True,
                            })
Ejemplo n.º 25
0
    def test_removal_of_parameter_is_detected(self):
        '''
        Test dockerng.running with deleted parameter.

        1. define your sls

        .. code-block:: yaml

            container:
                dockerng.running:
                    - name: super-container
                    - binds:
                        - /path:/path:ro

        2. run state.highstate

        3. modify your sls by removing `- binds:`

        .. code-block:: yaml

            container:
                dockerng.running:
                    - name: super-container

        4. enjoy your new created container without mounted volumes.
        '''
        image_id = 'abcdefg'
        dockerng_create = Mock(return_value=True)
        dockerng_start = Mock()
        dockerng_list_containers = Mock(return_value=['cont'])
        dockerng_inspect_container = Mock(
            side_effect=[{
                'Config': {
                    'Image': 'image:latest',
                    'Tty': False,
                    'Labels': {},
                    'Domainname': '',
                    'User': '',
                    'AttachStderr': True,
                    'AttachStdout': True,
                    'Hostname': 'saltstack-container',
                    'Env': [],
                    'WorkingDir': '/',
                    'Cmd': ['bash'],
                    'Volumes': {'/path': {}},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                    'OpenStdin': False,
                },
                'HostConfig': {
                    'PublishAllPorts': False,
                    'Dns': [],
                    'Links': None,
                    'CpusetCpus': '',
                    'RestartPolicy': {'MaximumRetryCount': 0, 'Name': ''},
                    'CapAdd': None,
                    'NetworkMode': 'default',
                    'PidMode': '',
                    'MemorySwap': 0,
                    'ExtraHosts': None,
                    'PortBindings': None,
                    'LxcConf': None,
                    'DnsSearch': [],
                    'Privileged': False,
                    'Binds': ['/path:/path:ro'],
                    'Memory': 0,
                    'VolumesFrom': None,
                    'CpuShares': 0,
                    'CapDrop': None,
                },
                'NetworkSettings': {
                    'MacAddress': '00:00:00:00:00:01',
                },
                'Image': image_id},
                {'Config': {
                    'Image': 'image:latest',
                    'Tty': False,
                    'Labels': {},
                    'Domainname': '',
                    'User': '',
                    'AttachStderr': True,
                    'AttachStdout': True,
                    'Hostname': 'saltstack-container',
                    'Env': [],
                    'WorkingDir': '/',
                    'Cmd': ['bash'],
                    'Volumes': {'/path': {}},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                    'OpenStdin': False,
                },
                'HostConfig': {
                    'PublishAllPorts': False,
                    'Dns': [],
                    'Links': None,
                    'CpusetCpus': '',
                    'RestartPolicy': {'MaximumRetryCount': 0, 'Name': ''},
                    'CapAdd': None,
                    'NetworkMode': 'default',
                    'PidMode': '',
                    'MemorySwap': 0,
                    'ExtraHosts': None,
                    'PortBindings': None,
                    'LxcConf': None,
                    'DnsSearch': [],
                    'Privileged': False,
                    'Binds': None,
                    'Memory': 0,
                    'VolumesFrom': None,
                    'CpuShares': 0,
                    'CapDrop': None,
                },
                'NetworkSettings': {
                    'MacAddress': '00:00:00:00:00:01',
                },
                'Image': image_id}]
        )
        dockerng_inspect_image = MagicMock(
            return_value={
                'Id': image_id,
                'Config': {
                    'Hostname': 'saltstack-container',
                    'WorkingDir': '/',
                    'Cmd': ['bash'],
                    'Volumes': {'/path': {}},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                },
                })
        __salt__ = {'dockerng.list_containers': dockerng_list_containers,
                    'dockerng.inspect_container': dockerng_inspect_container,
                    'dockerng.inspect_image': dockerng_inspect_image,
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(return_value=True),
                    'dockerng.state': MagicMock(side_effect=['stopped',
                                                             'running']),
                    'dockerng.rm': MagicMock(return_value='cont'),
                    'dockerng.create': dockerng_create,
                    'dockerng.start': dockerng_start,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            ret = dockerng_state.running(
                'cont',
                image='image:latest',
                )
        self.assertEqual(ret, {'name': 'cont',
                               'comment': "Container 'cont' changed state.."
                               " Container 'cont' was replaced.",
                               'changes': {
                                   'diff': {'binds':
                                            {'new': [],
                                             'old': ['/path:/path:ro']}},
                                   'image': True,
                                   'removed': 'cont',
                                   'state': {'new': 'running',
                                             'old': 'stopped'},
                                   'added': True,
                               },
                               'result': True,
                               })
        dockerng_create.assert_called_with('image:latest',
                                           validate_ip_addrs=False,
                                           validate_input=False,
                                           name='cont',
                                           client_timeout=60)
Ejemplo n.º 26
0
    def test_removal_of_parameter_is_detected(self):
        '''
        Test dockerng.running with deleted parameter.

        1. define your sls

        .. code-block:: yaml

            container:
                dockerng.running:
                    - name: super-container
                    - binds:
                        - /path:/path:ro

        2. run state.highstate

        3. modify your sls by removing `- binds:`

        .. code-block:: yaml

            container:
                dockerng.running:
                    - name: super-container

        4. enjoy your new created container without mounted volumes.
        '''
        image_id = 'abcdefg'
        dockerng_create = Mock(return_value=True)
        dockerng_start = Mock()
        dockerng_list_containers = Mock(return_value=['cont'])
        dockerng_inspect_container = Mock(
            side_effect=[{
                'Config': {
                    'Image': 'image:latest',
                    'Tty': False,
                    'Labels': {},
                    'Domainname': '',
                    'User': '',
                    'AttachStderr': True,
                    'AttachStdout': True,
                    'Hostname': 'saltstack-container',
                    'Env': [],
                    'WorkingDir': '/',
                    'Cmd': ['bash'],
                    'Volumes': {'/path': {}},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                    'OpenStdin': False,
                },
                'HostConfig': {
                    'PublishAllPorts': False,
                    'Dns': [],
                    'Links': None,
                    'CpusetCpus': '',
                    'RestartPolicy': {'MaximumRetryCount': 0, 'Name': ''},
                    'CapAdd': None,
                    'NetworkMode': 'default',
                    'PidMode': '',
                    'MemorySwap': 0,
                    'ExtraHosts': None,
                    'PortBindings': None,
                    'LxcConf': None,
                    'DnsSearch': [],
                    'Privileged': False,
                    'Binds': ['/path:/path:ro'],
                    'Memory': 0,
                    'VolumesFrom': None,
                    'CpuShares': 0,
                    'CapDrop': None,
                },
                'NetworkSettings': {
                    'MacAddress': '00:00:00:00:00:01',
                },
                'Image': image_id},
                {'Config': {
                    'Image': 'image:latest',
                    'Tty': False,
                    'Labels': {},
                    'Domainname': '',
                    'User': '',
                    'AttachStderr': True,
                    'AttachStdout': True,
                    'Hostname': 'saltstack-container',
                    'Env': [],
                    'WorkingDir': '/',
                    'Cmd': ['bash'],
                    'Volumes': {'/path': {}},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                    'OpenStdin': False,
                },
                'HostConfig': {
                    'PublishAllPorts': False,
                    'Dns': [],
                    'Links': None,
                    'CpusetCpus': '',
                    'RestartPolicy': {'MaximumRetryCount': 0, 'Name': ''},
                    'CapAdd': None,
                    'NetworkMode': 'default',
                    'PidMode': '',
                    'MemorySwap': 0,
                    'ExtraHosts': None,
                    'PortBindings': None,
                    'LxcConf': None,
                    'DnsSearch': [],
                    'Privileged': False,
                    'Binds': None,
                    'Memory': 0,
                    'VolumesFrom': None,
                    'CpuShares': 0,
                    'CapDrop': None,
                },
                'NetworkSettings': {
                    'MacAddress': '00:00:00:00:00:01',
                },
                'Image': image_id}]
        )
        dockerng_inspect_image = MagicMock(
            return_value={
                'Id': image_id,
                'Config': {
                    'Hostname': 'saltstack-container',
                    'WorkingDir': '/',
                    'Cmd': ['bash'],
                    'Volumes': {'/path': {}},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                },
                })
        __salt__ = {'dockerng.list_containers': dockerng_list_containers,
                    'dockerng.inspect_container': dockerng_inspect_container,
                    'dockerng.inspect_image': dockerng_inspect_image,
                    'dockerng.list_tags': MagicMock(),
                    'dockerng.pull': MagicMock(return_value=True),
                    'dockerng.state': MagicMock(side_effect=['stopped',
                                                             'running']),
                    'dockerng.rm': MagicMock(return_value='cont'),
                    'dockerng.create': dockerng_create,
                    'dockerng.start': dockerng_start,
                    }
        with patch.dict(dockerng_state.__dict__,
                        {'__salt__': __salt__}):
            ret = dockerng_state.running(
                'cont',
                image='image:latest',
                )
        self.assertEqual(ret, {'name': 'cont',
                               'comment': "Container 'cont' changed state.."
                               " Container 'cont' was replaced.",
                               'changes': {
                                   'diff': {'binds':
                                            {'new': [],
                                             'old': ['/path:/path:ro']}},
                                   'image': True,
                                   'removed': 'cont',
                                   'state': {'new': 'running',
                                             'old': 'stopped'},
                                   'added': True,
                               },
                               'result': True,
                               })
        dockerng_create.assert_called_with('image:latest',
                                           validate_ip_addrs=False,
                                           validate_input=False,
                                           name='cont',
                                           client_timeout=60)
Ejemplo n.º 27
0
 def test_check_start_true(self):
     '''
     If start is True, then dockerng.running will try
     to start a container that is stopped.
     '''
     image_id = 'abcdefg'
     dockerng_create = Mock()
     dockerng_start = Mock()
     dockerng_list_containers = Mock(return_value=['cont'])
     dockerng_inspect_container = Mock(
         return_value={
             'Config': {
                 'Image': 'image:latest',
                 'Tty': False,
                 'Labels': {},
                 'Domainname': '',
                 'User': '',
                 'AttachStderr': True,
                 'AttachStdout': True,
                 'Hostname': 'saltstack-container',
                 'Env': [],
                 'WorkingDir': '/',
                 'Cmd': ['bash'],
                 'Volumes': {},
                 'Entrypoint': None,
                 'ExposedPorts': {},
                 'OpenStdin': False,
             },
             'HostConfig': {
                 'PublishAllPorts': False,
                 'Dns': [],
                 'Links': None,
                 'CpusetCpus': '',
                 'RestartPolicy': {'MaximumRetryCount': 0, 'Name': ''},
                 'CapAdd': None,
                 'NetworkMode': 'default',
                 'PidMode': '',
                 'MemorySwap': 0,
                 'ExtraHosts': None,
                 'PortBindings': None,
                 'LxcConf': None,
                 'DnsSearch': [],
                 'Privileged': False,
                 'Binds': None,
                 'Memory': 0,
                 'VolumesFrom': None,
                 'CpuShares': 0,
                 'CapDrop': None,
             },
             'NetworkSettings': {
                 'MacAddress': '00:00:00:00:00:01',
             },
             'Image': image_id})
     dockerng_inspect_image = MagicMock(
         return_value={
             'Id': image_id,
             'Config': {
                 'Hostname': 'saltstack-container',
                 'WorkingDir': '/',
                 'Cmd': ['bash'],
                 'Volumes': {},
                 'Entrypoint': None,
                 'ExposedPorts': {},
             },
             })
     __salt__ = {'dockerng.list_containers': dockerng_list_containers,
                 'dockerng.inspect_container': dockerng_inspect_container,
                 'dockerng.inspect_image': dockerng_inspect_image,
                 'dockerng.list_tags': MagicMock(),
                 'dockerng.pull': MagicMock(return_value=True),
                 'dockerng.state': MagicMock(side_effect=['stopped',
                                                          'running']),
                 'dockerng.create': dockerng_create,
                 'dockerng.start': dockerng_start,
                 }
     with patch.dict(dockerng_state.__dict__,
                     {'__salt__': __salt__}):
         ret = dockerng_state.running(
             'cont',
             image='image:latest',
             start=True,
             )
     self.assertEqual(ret, {'name': 'cont',
                            'comment': "Container 'cont' changed state.",
                            'changes': {'state': {'new': 'running',
                                                  'old': 'stopped'},
                                        'image': True},
                            'result': True,
                            })
Ejemplo n.º 28
0
    def test_command_defined_on_image_layer_dont_diff_if_attempted_to_blank(
            self):
        '''
        Assuming the docker image has a command defined, like ``sh``.
        Erasing this value on sls level will not delete the command
        in the container. And such the diff shouldn't be reported.
        Assuming also the container is already running.

        1. define your sls

        .. code-block:: yaml

            cont:
                dockerng.running:
                    - image: image:latest


        2. run state.highstate

        No diff should be reported
        '''
        image_id = 'abcdefg'
        dockerng_create = Mock(return_value=True)
        dockerng_start = Mock()
        dockerng_list_containers = Mock(return_value=['cont'])
        dockerng_inspect_container = Mock(side_effect=[{
            'Config': {
                'Image': 'image:latest',
                'Tty': False,
                'Labels': {},
                'Domainname': '',
                'User': '',
                'AttachStderr': True,
                'AttachStdout': True,
                'Hostname': 'saltstack-container',
                'Env': [],
                'WorkingDir': '/',
                'Cmd': None,
                'Volumes': {},
                'Entrypoint': None,
                'ExposedPorts': {},
                'OpenStdin': False,
            },
            'HostConfig': {
                'PublishAllPorts': False,
                'Dns': [],
                'Links': None,
                'CpusetCpus': '',
                'RestartPolicy': {
                    'MaximumRetryCount': 0,
                    'Name': ''
                },
                'CapAdd': None,
                'NetworkMode': 'default',
                'PidMode': '',
                'MemorySwap': 0,
                'ExtraHosts': None,
                'PortBindings': None,
                'LxcConf': None,
                'DnsSearch': [],
                'Privileged': False,
                'Binds': [],
                'Memory': 0,
                'VolumesFrom': None,
                'CpuShares': 0,
                'CapDrop': None,
            },
            'NetworkSettings': {
                'MacAddress': '00:00:00:00:00:01',
            },
            'Image': image_id
        }])
        dockerng_inspect_image = MagicMock(
            return_value={
                'Id': image_id,
                'Config': {
                    'Hostname': 'saltstack-container',
                    'WorkingDir': '/',
                    'Cmd': ['bash'],  # !!! Cmd defined on Image
                    'Volumes': {},
                    'Entrypoint': None,
                    'ExposedPorts': {},
                },
            })
        __salt__ = {
            'dockerng.list_containers': dockerng_list_containers,
            'dockerng.inspect_container': dockerng_inspect_container,
            'dockerng.inspect_image': dockerng_inspect_image,
            'dockerng.list_tags': MagicMock(side_effect=[['image:latest']]),
            'dockerng.state': MagicMock(side_effect=['running']),
        }
        with patch.dict(dockerng_state.__dict__, {'__salt__': __salt__}):
            ret = dockerng_state.running(
                'cont',
                image='image:latest',
            )
        self.assertEqual(
            ret, {
                'name': 'cont',
                'comment': "Container 'cont' is already"
                " configured as specified",
                'changes': {},
                'result': True,
            })