<commit_before>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    * See the License for the specific language governing permissions and
#    * limitations under the License.

import os
import shutil
import uuid
import tarfile
import filecmp

from cosmo_tester.framework.testenv import TestCase


class DownloadBlueprintTest(TestCase):
    """
    CFY-196: Tests downloading of a previously uploaded blueprint.
    CFY-995" Added a large (50MB) file to the blueprint
    """

    def setUp(self):
        super(DownloadBlueprintTest, self).setUp()
        self.blueprint_id = str(uuid.uuid4())
        self.blueprint_file = '%s.tar.gz' % self.blueprint_id
        tempdir_name = str(uuid.uuid4())
        self.download_path = os.path.join(self.cfy.workdir, tempdir_name)
        self.downloaded_archive_path = os.path.join(self.cfy.workdir,
                                                    self.blueprint_file)

    def tearDown(self):
        if os.path.exists(self.download_path):
            shutil.rmtree(self.download_path)
        super(DownloadBlueprintTest, self).tearDown()

    def download_blueprint_test(self):
        blueprint_path = self.copy_blueprint('mocks')
        self._create_file("50M", "just_a_large_file.img", blueprint_path)
        blueprint_yaml = blueprint_path / 'single-node-blueprint.yaml'
        self.cfy.upload_blueprint(self.blueprint_id, blueprint_yaml)
        self.cfy.download_blueprint(self.blueprint_id)
        self.assertTrue(os.path.exists(self.downloaded_archive_path))
        self._extract_tar_file()
        downloaded_blueprint_file = os.path.join(
            self.download_path,
            'mocks/single-node-blueprint.yaml')
        self.assertTrue(os.path.exists(downloaded_blueprint_file))
        self.assertTrue(filecmp.cmp(blueprint_yaml, downloaded_blueprint_file))

    def _extract_tar_file(self):
        with tarfile.open(self.downloaded_archive_path) as tar:
            for item in tar:
                tar.extract(item, self.download_path)

    def _create_file(self, fileSize, fileName, path):
        os.system("fallocate -l " + fileSize +
                  " " + path + "/" + fileName)
<commit_msg>CFY-995 delete large file when tearing down the test<commit_after>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    * See the License for the specific language governing permissions and
#    * limitations under the License.

import os
import shutil
import uuid
import tarfile
import filecmp

from cosmo_tester.framework.testenv import TestCase


class DownloadBlueprintTest(TestCase):
    """
    CFY-196: Tests downloading of a previously uploaded blueprint.
    CFY-995: Added a large (50MB) file to the blueprint
    """

    def setUp(self):
        super(DownloadBlueprintTest, self).setUp()
        self.blueprint_id = str(uuid.uuid4())
        self.blueprint_file = '%s.tar.gz' % self.blueprint_id
        tempdir_name = str(uuid.uuid4())
        self.download_path = os.path.join(self.cfy.workdir, tempdir_name)
        self.downloaded_archive_path = os.path.join(self.cfy.workdir,
                                                    self.blueprint_file)

    def tearDown(self):
        if os.path.exists(self.download_path):
            shutil.rmtree(self.download_path)
        if os.path.isfile(self.large_file_location):
            os.remove(self.large_file_location)

        super(DownloadBlueprintTest, self).tearDown()

    def download_blueprint_test(self):
        blueprint_path = self.copy_blueprint('mocks')
        self.large_file_location = blueprint_path / "just_a_large_file.img"
        self._create_file("50M", self.large_file_location)
        blueprint_yaml = blueprint_path / 'single-node-blueprint.yaml'
        self.cfy.upload_blueprint(self.blueprint_id, blueprint_yaml)
        self.cfy.download_blueprint(self.blueprint_id)
        self.assertTrue(os.path.exists(self.downloaded_archive_path))
        self._extract_tar_file()
        downloaded_blueprint_file = os.path.join(
            self.download_path,
            'mocks/single-node-blueprint.yaml')
        self.assertTrue(os.path.exists(downloaded_blueprint_file))
        self.assertTrue(filecmp.cmp(blueprint_yaml, downloaded_blueprint_file))

    def _extract_tar_file(self):
        with tarfile.open(self.downloaded_archive_path) as tar:
            for item in tar:
                tar.extract(item, self.download_path)

    def _create_file(self, fileSize, fileLocation):
        os.system("fallocate -l " + fileSize +
                  " " + fileLocation)

{'fraction_non_alphanumeric': 0.09310783778868885, 'fraction_numerical': 0.007819603564284416, 'mean_word_length': 3.0145985401459856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1463711', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1600, 'n_words': 389}