def setUp(self):
     self.nulecule_base = NuleculeBase(params=[],
                                       basepath=os.getcwd(),
                                       namespace="test")
     self.tmpdir = tempfile.mkdtemp(prefix="atomicapp-test", dir="/tmp")
     self.artifact_dir = os.path.dirname(
         __file__) + '/docker_artifact_test/'
class TestDockerProviderBase(unittest.TestCase):
    
    # Create a temporary directory for our setup as well as load the required providers
    def setUp(self):
        self.nulecule_base = NuleculeBase(params = [], basepath = os.getcwd(), namespace = "test")
        self.tmpdir = tempfile.mkdtemp(prefix = "atomicapp-test", dir = "/tmp")
        self.artifact_dir = os.path.dirname(__file__) + '/docker_artifact_test/'

    def tearDown(self):
        pass
    
    # Lets prepare the docker provider with pre-loaded configuration and use the DockerProvider
    def prepare_provider(self, data):
        self.nulecule_base.load_config(data)
        config = self.nulecule_base.config
        provider = DockerProvider(config, self.tmpdir, dryrun = True)
        return provider

    # Test deploying multiple artifacts within docker
    def test_multiple_artifact_load(self):
        data = {'namespace': 'test', 'provider': 'docker'}
        provider = self.prepare_provider(data)
        provider.init()
        provider.artifacts = [
                self.artifact_dir + 'hello-world-one',
                self.artifact_dir + 'hello-world-two',
                self.artifact_dir + 'hello-world-three'
                ]
        # Mock the effects of 'docker ps -a'. As if each deployment adds the container to the host
        mock_container_list = mock.Mock(side_effect = [
            ["test_fedora-httpd_e9b9a7bfe8f9"], 
            ["test_fedora-httpd_e9b9a7bfe8f9", "test_centos-httpd_e9b9a7bfe8f9"],
            ["test_fedora-httpd_e9b9a7bfe8f9", "test_centos-httpd_e9b9a7bfe8f9", "test_centos-httpd_e9b9a7bfe8f9"]
            ])
        with mock.patch("atomicapp.providers.docker.DockerProvider._get_containers", mock_container_list):
            provider.run()

   
    # Patch in a general container list and make sure it fails if there is already a container with the same name 
    @mock.patch("atomicapp.providers.docker.DockerProvider._get_containers", mock_name_get_call)
    def test_namespace_name_check(self):
        data = {'namespace': 'test', 'provider': 'docker', 'image': 'centos/httpd'}
        provider = self.prepare_provider(data)
        provider.init()
        provider.artifacts = [self.artifact_dir + 'hello-world-one']
        with pytest.raises(ProviderFailedException):
            provider.run()

    def test_docker_run_with_backslashes(self):
        data = {'namespace': 'test', 'provider': 'docker'}
        provider = self.prepare_provider(data)
        provider.init()
        provider.artifacts = [
                self.artifact_dir + 'run-with-backslashes',
                ]
        expected_output = 'docker run -d -p 80:80 --name centos7 centos7'
        with mock.patch('atomicapp.providers.docker.logger') as mock_logger:
            provider.run()
            mock_logger.info.assert_called_with('DRY-RUN: %s', expected_output)
Пример #3
0
 def test_get_provider_failure(self):
     """
     Test if get_provider method when passed an invalid key raises an
     exception.
     """
     nb = NuleculeBase(params = [], basepath = '', namespace = '')
     # purposefully give the wrong provider key
     provider_key = u'mesos'
     nb.config = {u'general': {u'provider': provider_key}}
     with self.assertRaises(NuleculeException):
         nb.get_provider() 
class TestKubernetesProviderBase(unittest.TestCase):

    # Create a temporary directory for our setup as well as load the required providers
    def setUp(self):
        self.nulecule_base = NuleculeBase(params=[],
                                          basepath=os.getcwd(),
                                          namespace="test")
        self.tmpdir = tempfile.mkdtemp(prefix="atomicapp-test", dir="/tmp")
        self.artifact_dir = os.path.dirname(
            __file__) + '/docker_artifact_test/'

    def tearDown(self):
        pass

    def create_temp_file(self):
        return tempfile.mktemp(prefix="test-config", dir=self.tmpdir)

    # Lets prepare the docker provider with pre-loaded configuration and use the KubernetesProvider
    def prepare_provider(self, data):
        self.nulecule_base.load_config(data)
        config = self.nulecule_base.config
        provider = KubernetesProvider(config, self.tmpdir, dryrun=True)
        return provider

    # Check that the provider configuration file exists
    @mock.patch.object(KubernetesProvider, '_call', mock_provider_call)
    def test_provider_config_exist(self):
        provider_config_path = self.create_temp_file()
        mock_content = "%s_%s" % (MOCK_CONTENT, "_unchanged")
        with open(provider_config_path, "w") as fp:
            fp.write(mock_content)

        data = {
            'namespace': 'testing',
            'provider': 'kubernetes',
            'provider-config': provider_config_path
        }

        provider = self.prepare_provider(data)

        self.assertEqual(provider.config_file, provider_config_path)
        provider.checkConfigFile()  # should exist since we just created it
        with open(provider_config_path, "r") as fp:
            self.assertEqual(fp.read(), mock_content)

    # If we call checkConfigFile but do not provide a configuration file: fail
    def test_provider_check_config_fail(self):
        path = self.create_temp_file()
        data = {'namespace': 'testing', 'provider': 'openshift'}
        provider = self.prepare_provider(data)
        self.assertRaises(ProviderFailedException, provider.checkConfigFile)
Пример #5
0
class TestDockerProviderBase(unittest.TestCase):
    
    # Create a temporary directory for our setup as well as load the required providers
    def setUp(self):
        self.nulecule_base = NuleculeBase(params = [], basepath = os.getcwd(), namespace = "test")
        self.tmpdir = tempfile.mkdtemp(prefix = "atomicapp-test", dir = "/tmp")
        self.artifact_dir = os.path.dirname(__file__) + '/docker_artifact_test/'

    def tearDown(self):
        pass
    
    # Lets prepare the docker provider with pre-loaded configuration and use the DockerProvider
    def prepare_provider(self, data):
        self.nulecule_base.load_config(data)
        config = self.nulecule_base.config
        provider = DockerProvider(config, self.tmpdir, dryrun = True)
        return provider

    # Test deploying multiple artifacts within docker
    def test_multiple_artifact_load(self):
        data = {'namespace': 'test', 'provider': 'docker'}
        provider = self.prepare_provider(data)
        provider.init()
        provider.artifacts = [
                self.artifact_dir + 'hello-world-one',
                self.artifact_dir + 'hello-world-two',
                self.artifact_dir + 'hello-world-three'
                ]
        # Mock the effects of 'docker ps -a'. As if each deployment adds the container to the host
        mock_container_list = mock.Mock(side_effect = [
            ["atomic_default_e9b9a7bfe8f9"], 
            ["atomic_default_e9b9a7bfe8f9", "atomic_test_e9b9a7bfe8f9"],
            ["atomic_default_e9b9a7bfe8f9", "atomic_test_e9b9a7bfe8f9", "atomic_test_e9b9a7bfe8f9"]
            ])
        with mock.patch("atomicapp.providers.docker.DockerProvider._get_containers", mock_container_list):
            provider.deploy()

   
    # Patch in a general container list and make sure it fails if there is already a container with the same name 
    @mock.patch("atomicapp.providers.docker.DockerProvider._get_containers", mock_name_get_call)
    def test_namespace_name_check(self):
        data = {'namespace': 'test', 'provider': 'docker'}
        provider = self.prepare_provider(data)
        provider.init()
        provider.artifacts = [self.artifact_dir + 'hello-world-one']
        with pytest.raises(ProviderFailedException):
            provider.deploy()
class TestKubernetesProviderBase(unittest.TestCase):

    # Create a temporary directory for our setup as well as load the required providers
    def setUp(self):
        self.nulecule_base = NuleculeBase(params = [], basepath = os.getcwd(), namespace = "test")
        self.tmpdir = tempfile.mkdtemp(prefix = "atomicapp-test", dir = "/tmp")
        self.artifact_dir = os.path.dirname(__file__) + '/docker_artifact_test/'

    def tearDown(self):
        pass

    def create_temp_file(self):
        return tempfile.mktemp(prefix = "test-config", dir = self.tmpdir)

    # Lets prepare the docker provider with pre-loaded configuration and use the KubernetesProvider
    def prepare_provider(self, data):
        self.nulecule_base.load_config(data)
        config = self.nulecule_base.config
        provider = KubernetesProvider(config, self.tmpdir, dryrun = True)
        return provider

    # Check that the provider configuration file exists
    @mock.patch.object(KubernetesProvider, '_call', mock_provider_call)
    def test_provider_config_exist(self):
        provider_config_path = self.create_temp_file()
        mock_content = "%s_%s" % (MOCK_CONTENT, "_unchanged")
        with open(provider_config_path, "w") as fp:
            fp.write(mock_content)

        data = {'namespace': 'testing', 'provider': 'kubernetes', 'providerconfig': provider_config_path}
        
        provider = self.prepare_provider(data)

        self.assertEqual(provider.config_file, provider_config_path)
        provider.checkConfigFile()  # should exist since we just created it
        with open(provider_config_path, "r") as fp:
            self.assertEqual(fp.read(), mock_content)

    # If we call checkConfigFile but do not provide a configuration file: fail
    def test_provider_check_config_fail(self):
        path = self.create_temp_file()
        data = {'namespace': 'testing', 'provider': 'openshift'}
        provider = self.prepare_provider(data)
        self.assertRaises(ProviderFailedException, provider.checkConfigFile)
Пример #7
0
    def test_get_provider_success(self):
        """
        Test if get_provider method when passed a particular valid key returns
        the corresponding class.
        """
        nb = NuleculeBase(params = [], basepath = '', namespace = '')
        provider_key = u'openshift'
        # method `get_provider` will read from this config, we give it here
        # since we have neither provided it before nor it is auto-generated
        nb.config = {u'general': {u'provider': provider_key}}

        return_provider = mock.Mock()
        # mocking return value of method plugin.getProvider,because it returns
        # provider class and that class gets called with values
        nb.plugin.getProvider = mock.Mock(return_value=return_provider)
        ret_provider_key, ret_provider = nb.get_provider()
        self.assertEqual(provider_key, ret_provider_key)
        return_provider.assert_called_with({u'provider': provider_key}, 
                                            '', False)
 def setUp(self):
     self.nulecule_base = NuleculeBase(params = [], basepath = os.getcwd(), namespace = "test")
     self.tmpdir = tempfile.mkdtemp(prefix = "atomicapp-test", dir = "/tmp")
     self.artifact_dir = os.path.dirname(__file__) + '/docker_artifact_test/'