Exemplo n.º 1
0
 def UpdateFile(self, request, context):
     container_id = request.container_id
     path = request.path
     file_name = request.file_name
     old_version = request.old_version
     exit_code, old_file_stat = ContainerUtils.ExecCommand(\
         container_id=container_id,
         exec_cmd = ["stat", "-c", "%Y", path+"/"+file_name]
     )
     if exit_code == 0:
         if old_version == old_file_stat or request.force:
             # generate_data = GenerateData(request)
             if not ContainerUtils.PutArchive(container_id, path,
                                              request.data):
                 return Manager.UpdateFile_Response(
                     exit_code=Manager.UpdateFile_Response.ExitCode.
                     UNKNOWN_ERROR)
             else:
                 exit_code, new_version = ContainerUtils.ExecCommand(\
                     container_id=container_id,
                     exec_cmd = ["stat", "-c", "%Y", path]
                 )
                 return Manager.UpdateFile_Response(\
                         exit_code = Manager.UpdateFile_Response.ExitCode.SUCCESS,
                         new_version = new_version
                     )
         else:
             return Manager.UpdateFile_Response(\
                 exit_code=Manager.UpdateFile_Response.ExitCode.MTIME_SYNC_ERROR)
     elif exit_code == 1:
         return Manager.UpdateFile_Response(\
             exit_code=Manager.UpdateFile_Response.ExitCode.FILE_IS_NOT_EXIST)
Exemplo n.º 2
0
 def ListFile(self, request, context):
     cmd = ["ls", "-Al", request.path]
     exit_code, output = ContainerUtils.ExecCommand(request.container_id,
                                                    cmd)
     response = Manager.ListFile_Response()
     if exit_code == 0:
         response.exit_code = Manager.ListFile_Response.ExitCode.SUCCESS
         List = output.strip().split('\n')[1:]
         for file in List:
             temp = file.split(' ')
             stat = temp[0]
             filename = temp[-1]
             temp = response.files.add()
             temp.file_name = filename
             if stat[0] == '-':
                 temp.file_type = Manager.FileStat.FileType.FILE
             elif stat[0] == 'd':
                 temp.file_type = Manager.FileStat.FileType.FOLDER
             elif stat[0] == 'i':
                 temp.file_type = Manager.FileStat.FileType.LINK
     elif exit_code == 1:
         response.exit_code = Manager.ListFile_Response.ExitCode.MINOR_PROBLEMS
     elif exit_code == 2:
         response.exit_code = Manager.ListFile_Response.ExitCode.SERIOUS_TROUBLE
     return response
Exemplo n.º 3
0
 def CreateContainer(self, request, context):
     container_id = ContainerUtils.CreateContainer(
         image_id=request.image_id,
         mount_path="/workplace/{username}/mount".format(
             username=request.username),
         hostname=request.username,
         container_name=request.container_name)
     return Manager.CreateContainer_Response(container_id=container_id)
Exemplo n.º 4
0
 def ListContainers(self, request, context):
     containers = ContainerUtils.ListContainers()
     response = Manager.ListContainers_Response()
     for container in containers:
         attr = response.containers.add()
         attr.id = container['Id']
         attr.created = container['Created']
         attr.status = GetContainerStatus(container['Status'])
         attr.image = container['Image']
         attr.name = container['Name']
     return response
Exemplo n.º 5
0
    def __init__(self, **kwargs):
        self.is_docker_daemon_running()
        self.test_dir = self._create_temp_dir()
        self.imageutils = ImageUtils()
        self.containerutils = ContainerUtils()

        self.selinux_checks = SELinuxTests()
        self.selinux_denials_test = SELinuxDenials()
        self.image_inspection_test = InspectImage()
        self.container_inspection_test = InspectContainer()
        self.metadata = Metadata()
        self._process_kwargs(**kwargs)
Exemplo n.º 6
0
 def GetContainer(self, request, context):
     response = Manager.GetContainer_Response()
     try:
         container = ContainerUtils.GetContainer(request.container_id)
     except docker.errors.APIError:
         response.exit_code = Manager.GetContainer_Response.ExitCode.NOTFOUND
         return response
     response.exit_code = Manager.GetContainer_Response.ExitCode.SUCCESS
     response.container_attr.id = container['Id']
     response.container_attr.created = container['Created']
     response.container_attr.status = GetContainerStatus(container['State'])
     response.container_attr.image = container['Image']
     response.container_attr.name = container['Name']
     return response
Exemplo n.º 7
0
class ProbeRunner(object):

    """
    Probe runner utility
    """

    def __init__(self, **kwargs):
        self.is_docker_daemon_running()
        self.test_dir = self._create_temp_dir()
        self.imageutils = ImageUtils()
        self.containerutils = ContainerUtils()

        self.selinux_checks = SELinuxTests()
        self.selinux_denials_test = SELinuxDenials()
        self.image_inspection_test = InspectImage()
        self.container_inspection_test = InspectContainer()
        self.metadata = Metadata()
        self._process_kwargs(**kwargs)

    def setup(self):
        """
        Setup before test run
        """
        self.getimage = GetImage(self.offline,
                                 self.test_dir)
        # This method processes the image repository and return image name with
        # tag
        self.cert_container = self.cert_container_name()

    def _process_kwargs(self, **kwargs):
        """
        Process the kwargs given to __init__ method
        """
        self.image = kwargs["image"]
        self.dockeruser = kwargs.get("user", None)
        self.output_dir = kwargs.get("output_dir", None)
        self.offline = kwargs.get("offline", None)

    def is_docker_daemon_running(self):
        """
        Raise if docker daemon is not running
        """
        if not is_docker_running():
            raise ctsexceptions.CTSDockerServiceNotRunning

    def cert_container_name(self):
        """
        Returns a container name
        """
        random_name = "".join(choice(ascii_lowercase) for _ in range(6))
        return "cert_%s" % random_name


    def is_image_tests(self):
        """
        If image tests to be ran.
        """
        return True

    def test_scripts(self):
        """
        Returns names of tests scripts
        """
        return constants.TEST_SCRIPTS_NAME

    def test_scripts_source_path(self):
        """
        Source path of tests scripts
        """
        return constants.TEST_SCRIPTS_PATH

    def test_scripts_dir_in_container(self):
        """
        Destination path of tests scripts in container
        """
        return constants.TEST_SCRIPTS_DIR_IN_CONT

    def cert_temp_parent_dir(self):
        """
        Parent directory of test dir at host
        """
        return constants.CERT_TEMP_PARENT_DIR

    def _create_temp_dir(self):
        """
        Create a temporary directory at host to be shared as volume
        """
        return tempfile.mkdtemp(dir=self.cert_temp_parent_dir())

    def cert_shared_dir_at_host(self):
        """
        Shared directory path at host to be used as volume
        """
        return self.test_dir

    def copy_scripts_in_test_dir(self):
        """
        Copy tests scripts in cert dir at host
        """
        [copy(script, self.cert_shared_dir_at_host())
         for script in self.test_scripts_source_path()]

    def change_perm_for_test_dir(self, test_dir, perm):
        """
        Change permission to test_dir
        """
        os.chmod(test_dir, perm)

    def pkg_report_path(self):
        """
        Package report path generated in live container test mode
        """
        return os.path.join(self.cert_shared_dir_at_host(),
                            constants.PACKAGE_REPORT)

    def _add_user_in_params(self, user, params):
        """
        Add user in parameters
        """
        # assumes params start with "run"
        params.insert(1, "--user")
        params.insert(2, user)
        return params

    # ------------------Image-test-utilities------------------

    def _get_params_for_image_tests(self, volumes, entrypoint):
        """
        Generate parameters for image tests
        """
        # method self._add_user_in_params() asssumes params starts
        # "run", hence params should start with "run"
        params = ["run", "-v", volumes, "--entrypoint", entrypoint,
                  "--name", self.cert_container, self.image]
        # if container needs to be run as user root
        if self.dockeruser:
            self._add_user_in_params(self.dockeruser, params)
        return params

    def _test_kickstart_path_in_container(self):
        """
        Returns entry point
        """
        return os.path.join(self.test_scripts_dir_in_container(),
                            constants.TEST_KICKSTART_SCRIPT)

    def _get_volumes_mapping(self):
        """
        Returns volumes mapping from host to container
        """
        volumes = "%s:%s" % (self.cert_shared_dir_at_host(),
                             self.test_scripts_dir_in_container())
        return volumes

    def run_image_tests(self):
        """
        Run image tests
        """
        volumes = self._get_volumes_mapping()
        entrypoint = self._test_kickstart_path_in_container()
        params = self._get_params_for_image_tests(volumes, entrypoint)

        try:
            self.containerutils.create_container(params)
        except:
            raise
        else:
            msg = "Successfully ran image tests."
            return self.pkg_report_path()

    # -------------------Test-run-utilities----------------------

    def pre_test_run_setup(self):
        """
        Run pre test run setup
        """
        self.copy_scripts_in_test_dir()
        self.change_perm_for_test_dir(self.cert_shared_dir_at_host(), 0777)

    def _run(self):
        """
        Run all tests with clean up utility
        """
        self.run_image_tests()

        # image inspection test
        msg = "Inspection image under test.."
        print msg
        inspect_image_report_path = os.path.join(
            self.cert_shared_dir_at_host(),
            "%s.json" % self.image_inspection_test.__class__.__name__
        )
        self.image_inspection_test.run(
            # image=self.cert_image,
            image=self.image,
            export_file=inspect_image_report_path)

        # metadata tests
        metadata_report_path = os.path.join(
            self.cert_shared_dir_at_host(),
            "%s.json" % self.metadata.__class__.__name__
        )
        msg = "Collecting metadata of image under test.."
        print msg
        self.metadata.run(
            # image=self.cert_image,
            image=self.image,
            export_file=metadata_report_path)

    def clean_up(self, post_run=True, during_setup=False):
        """
        Clean up after test run
        """
        msg = "Cleaning.."
        try:
            if during_setup:
                self.clean.remove_test_dir(self.test_dir)
                return
            if self.cert_container:
                self.clean.clean_container(self.cert_container)
            if post_run:
                if self.test_dir:
                    self.clean.remove_test_dir(self.test_dir)
                if self.image:
                    self.clean.clean_image(self.image, all_tags=True)
        except Exception as e:
          raise

    def create_testdata_tarball(self):
        """
        Create tarball of test data
        """
        source_dir = self.cert_shared_dir_at_host()
        tempdir = tempfile.mkdtemp(dir=source_dir)

        files = [os.path.join(source_dir, item)
                 for item in os.listdir(source_dir)
                 if os.path.isfile(os.path.join(source_dir, item))]

        [copy(f, tempdir) for f in files]

        return create_tarball(tempdir, "container_cert", "/tmp/")

    def move_result_to_output_dir(self, result, output):
        """
        Move resultant test data from `result` to `output` directory
        data present in `result` is deleted after movement to
        `output` dir
        """
        if not os.path.isdir(output):
            raise ctsexceptions.CTSOutputDirectoryDoesNotExist(output)
        if os.path.isdir(result):
            files = [os.path.join(result, item) for item in os.listdir(result)
                     if os.path.isfile(os.path.join(result, item))]
        # if -t option is given for archiving the output, it will generate a
        # tarfile as output and thus it will be a file
        elif os.path.isfile(result):
            files = [result]
        try:
            [copy(src, output) for src in files]
        except IOError as e:
            raise
        else:
            if os.path.isfile(result):
                return os.path.join(output, os.path.basename(result))
            else:
                return output

    def remove_test_scripts_from_result(self):
        """
        Remove test scripts from result directory if any
        """
        for item in os.listdir(self.cert_shared_dir_at_host()):
            if item in self.test_scripts():
                os.unlink(os.path.join(self.cert_shared_dir_at_host(), item))

    def _post_run(self):
        """
        Operations to be performed post test run
        """
        self.remove_test_scripts_from_result()
        result = self.cert_shared_dir_at_host()
        print result
        # if testrun data (dir/tarfile) needs to be exported in particular dir
        if self.output_dir:
            result = self.move_result_to_output_dir(result, self.output_dir)
        return result

    def run(self):
        """
        Run all tests
        """
        self.setup()
        self.pre_test_run_setup()
        self._run()
        self._post_run()
Exemplo n.º 8
0
 def GetFile(self, request, context):
     bits, stat = ContainerUtils.GetArchive(request.container_id,
                                            request.path)
     for data in bits:
         yield Manager.GetFile_Response(data=data)
Exemplo n.º 9
0
 def RestartContainer(self, request, context):
     ContainerUtils.RestartContainer(request.container_id)
     return Manager.RestartContainer_Response()
Exemplo n.º 10
0
 def RemoveContainer(self, request, context):
     ContainerUtils.RemoveContainer(request.container_id, request.force)
     return Manager.RemoveContainer_Response()
Exemplo n.º 11
0
 def StopContainer(self, request, context):
     ContainerUtils.StopContainer(request.container_id)
     return Manager.StopContainer_Response()
Exemplo n.º 12
0
 def PruneContainers(self, request, context):
     ContainerUtils.PruneContainers()
     return Manager.PruneContainers_Response()