def _generate_tags(self, testname):
        """
        Generate tags for the current test run

        @param testname the name of the current test
        @return an array of tags
        """
        # get chrome version
        version_string = utils.system_output(constants.CHROME_VERSION_COMMAND,
                                             ignore_status=True)
        version_string = utils.parse_chrome_version(version_string)[0]

        return {
            'testname': testname,
            'chromeos_version': utils.get_chromeos_release_version(),
            'chrome_version': version_string,
            'board': utils.get_board(),
            'date': datetime.date.today().strftime("%m/%d/%y"),
        }
    def run_once(self, kiosk_app_attributes=None):
        if kiosk_app_attributes:
            app_name, app_id, ext_page = (
                kiosk_app_attributes.rstrip().split(':'))
        self.subtest_name = app_name
        self.board_name = utils.get_board()
        self.hw_id = self._get_hwid()
        self.chrome_version = self._get_chrome_version()[0]
        self.chromeos_version = '0.' + utils.get_chromeos_release_version()
        self.epoch_minutes = str(int(time.time() / 60))  # Minutes since 1970.
        self.point_id = self._get_point_id(self.chromeos_version,
                                           self.epoch_minutes)

        kiosk_info = self._get_kiosk_app_info(app_id)
        self.kiosk_app_name = kiosk_info['name']
        self.kiosk_app_version = kiosk_info['version']
        self.test_suite_name = self.tagged_testname
        if self.subtest_name:
            self.test_suite_name += '.' + self.subtest_name

        # Delete exit flag file at start of test run.
        if os.path.isfile(EXIT_FLAG_FILE):
            os.remove(EXIT_FLAG_FILE)

        # Run a single test cycle.
        self.perf_results = {'cpu': '0', 'mem': '0', 'temp': '0'}
        self.perf_results = self._run_test_cycle()

        # Write results for AutoTest to pick up at end of test.
        self._write_perf_keyvals(self.perf_results)
        self._write_perf_results(self.perf_results)

        # Post perf results directly to performance dashboard. You may view
        # uploaded data at https://chromeperf.appspot.com/new_points,
        # with test path pattern=ChromeOS_Enterprise/cros-*/longevity*/*
        chart_data = self._read_perf_results()
        data_obj = self._format_data_for_upload(chart_data)
        self._send_to_dashboard(data_obj)
 def get_chromeos_release_version(self):
     """Returns chromeos version in device under test as string. None on
     fail.
     """
     return utils.get_chromeos_release_version()
    def run_screenshot_comparison_test(self):
        """
        Template method to run screenshot comparison tests for ui pieces.

        1. Set up test dirs.
        2. Create folder name
        3. Download golden image.
        4. Capture test image.
        5. Compare images locally, if FAIL upload to remote for analysis later.
        6. Clean up test dirs.

        """

        img_comp_conf_path = os.path.join(ui_TestBase.AUTOTEST_CROS_UI_DIR,
                                          ui_TestBase.IMG_COMP_CONF_FILE)

        img_comp_factory = image_comparison_factory.ImageComparisonFactory(
            img_comp_conf_path)

        golden_image_local_dir = os.path.join(ui_TestBase.WORKING_DIR,
                                              'golden_images')

        file_utils.make_leaf_dir(golden_image_local_dir)

        filename = '%s.png' % self.tagged_testname

        golden_image_remote_path = os.path.join(
            ui_TestBase.REMOTE_DIR, 'ui',
            lsbrelease_utils.get_chrome_milestone(), self.folder_name,
            filename)

        golden_image_local_path = os.path.join(golden_image_local_dir,
                                               filename)

        test_image_filepath = os.path.join(ui_TestBase.WORKING_DIR, filename)

        try:
            file_utils.download_file(golden_image_remote_path,
                                     golden_image_local_path)
        except urllib2.HTTPError as e:
            warn = "No screenshot found for {0} on milestone {1}. ".format(
                self.tagged_testname, lsbrelease_utils.get_chrome_milestone())
            warn += e.msg
            raise error.TestWarn(warn)

        self.capture_screenshot(test_image_filepath)

        comparer = img_comp_factory.make_pdiff_comparer()
        comp_res = comparer.compare(golden_image_local_path,
                                    test_image_filepath)

        if comp_res.diff_pixel_count > img_comp_factory.pixel_thres:
            publisher = img_comp_factory.make_imagediff_publisher(
                self.resultsdir)

            # get chrome version
            version_string = utils.system_output(
                constants.CHROME_VERSION_COMMAND, ignore_status=True)
            version_string = utils.parse_chrome_version(version_string)[0]

            # tags for publishing
            tags = {
                'testname': self.tagged_testname,
                'chromeos_version': utils.get_chromeos_release_version(),
                'chrome_version': version_string,
                'board': utils.get_board(),
                'date': datetime.date.today().strftime("%m/%d/%y"),
                'diff_pixels': comp_res.diff_pixel_count
            }

            publisher.publish(golden_image_local_path, test_image_filepath,
                              comp_res.pdiff_image_path, tags)

            raise error.TestFail('Test Failed. Please see image comparison '
                                 'result by opening index.html from the '
                                 'results directory.')

        file_utils.rm_dir_if_exists(ui_TestBase.WORKING_DIR)