github.com/jiasir/deis@v1.12.2/controller/registry/tests.py (about)

     1  """
     2  Unit tests for the Deis registry app.
     3  
     4  Run the tests with "./manage.py test registry"
     5  """
     6  
     7  import unittest
     8  try:
     9      from unittest import mock
    10  except ImportError:
    11      import mock
    12  
    13  from django.conf import settings
    14  from rest_framework.exceptions import PermissionDenied
    15  from registry.dockerclient import DockerClient
    16  from registry.dockerclient import strip_prefix
    17  
    18  
    19  @mock.patch('docker.Client')
    20  class DockerClientTest(unittest.TestCase):
    21      """Test that the client makes appropriate Docker engine API calls."""
    22  
    23      def setUp(self):
    24          settings.REGISTRY_HOST, settings.REGISTRY_PORT = 'localhost', 5000
    25  
    26      def test_publish_release(self, mock_client):
    27          self.client = DockerClient()
    28          self.client.publish_release('ozzy/embryo:git-f2a8020',
    29                                      {'POWERED_BY': 'Deis'}, 'ozzy/embryo:v4', True)
    30          self.assertTrue(self.client.client.pull.called)
    31          self.assertTrue(self.client.client.tag.called)
    32          self.assertTrue(self.client.client.build.called)
    33          self.assertTrue(self.client.client.push.called)
    34          # Test that a registry host prefix is replaced with deis-registry for the target
    35          self.client.publish_release('ozzy/embryo:git-f2a8020',
    36                                      {'POWERED_BY': 'Deis'}, 'quay.io/ozzy/embryo:v4', True)
    37          docker_push = self.client.client.push
    38          docker_push.assert_called_with(
    39              'localhost:5000/ozzy/embryo', tag='v4', insecure_registry=True, stream=True)
    40          # Test that blacklisted image names can't be published
    41          with self.assertRaises(PermissionDenied):
    42              self.client.publish_release(
    43                  'deis/controller:v1.11.1', {}, 'deis/controller:v1.11.1', True)
    44          with self.assertRaises(PermissionDenied):
    45              self.client.publish_release(
    46                  'localhost:5000/deis/controller:v1.11.1', {}, 'deis/controller:v1.11.1', True)
    47  
    48      def test_build(self, mock_client):
    49          # test that self.client.build was called with proper arguments
    50          self.client = DockerClient()
    51          self.client.build('ozzy/embryo:git-f3a8020', {'POWERED_BY': 'Deis'}, 'ozzy/embryo', 'v4')
    52          docker_build = self.client.client.build
    53          self.assertTrue(docker_build.called)
    54          args = {"rm": True, "tag": u'localhost:5000/ozzy/embryo:v4', "stream": True}
    55          kwargs = docker_build.call_args[1]
    56          self.assertDictContainsSubset(args, kwargs)
    57          # test that the fileobj arg to "docker build" contains a correct Dockerfile
    58          f = kwargs['fileobj']
    59          self.assertEqual(f.read(), "FROM ozzy/embryo:git-f3a8020\nENV POWERED_BY='Deis'")
    60          # Test that blacklisted image names can't be built
    61          with self.assertRaises(PermissionDenied):
    62              self.client.build('deis/controller:v1.11.1', {}, 'deis/controller', 'v1.11.1')
    63          with self.assertRaises(PermissionDenied):
    64              self.client.build(
    65                  'localhost:5000/deis/controller:v1.11.1', {}, 'deis/controller', 'v1.11.1')
    66  
    67      def test_pull(self, mock_client):
    68          self.client = DockerClient()
    69          self.client.pull('alpine', '3.2')
    70          docker_pull = self.client.client.pull
    71          docker_pull.assert_called_once_with(
    72              'alpine', tag='3.2', insecure_registry=True, stream=True)
    73          # Test that blacklisted image names can't be pulled
    74          with self.assertRaises(PermissionDenied):
    75              self.client.pull('deis/controller', 'v1.11.1')
    76          with self.assertRaises(PermissionDenied):
    77              self.client.pull('localhost:5000/deis/controller', 'v1.11.1')
    78  
    79      def test_push(self, mock_client):
    80          self.client = DockerClient()
    81          self.client.push('ozzy/embryo', 'v4')
    82          docker_push = self.client.client.push
    83          docker_push.assert_called_once_with(
    84              'ozzy/embryo', tag='v4', insecure_registry=True, stream=True)
    85  
    86      def test_tag(self, mock_client):
    87          self.client = DockerClient()
    88          self.client.tag('ozzy/embryo:git-f2a8020', 'ozzy/embryo', 'v4')
    89          docker_tag = self.client.client.tag
    90          docker_tag.assert_called_once_with(
    91              'ozzy/embryo:git-f2a8020', 'ozzy/embryo', tag='v4', force=True)
    92          # Test that blacklisted image names can't be tagged
    93          with self.assertRaises(PermissionDenied):
    94              self.client.tag('deis/controller:v1.11.1', 'deis/controller', 'v1.11.1')
    95          with self.assertRaises(PermissionDenied):
    96              self.client.tag('localhost:5000/deis/controller:v1.11.1', 'deis/controller', 'v1.11.1')
    97  
    98      def test_strip_prefix(self, mock_client):
    99          self.assertEqual(strip_prefix('quay.io/boris/riotsugar'), 'boris/riotsugar')
   100          self.assertEqual(strip_prefix('127.0.0.1:5000/boris/galaxians'), 'boris/galaxians')
   101          self.assertEqual(strip_prefix('boris/jacksonhead'), 'boris/jacksonhead')
   102          self.assertEqual(strip_prefix(':8888/boris/pink'), 'boris/pink')