Beispiel #1
0
 def setUp(self):
     with open(os.path.join(os.path.dirname(__file__),
                            'data/http_mocks/ncsos_describesensor.xml')) as f:
         self.resp = f.read()
     # need to monkey patch checkers prior to running tests, or no checker
     # classes will show up
     CheckSuite().load_all_available_checkers()
 def test_64bit(self):
     dataset = self.load_dataset(STATIC_FILES['ints64'])
     suite = CheckSuite()
     suite.checkers = {
         'cf'        : CFBaseCheck
     }
     suite.run(dataset, 'cf')
Beispiel #3
0
 def run_checker(self, checker, dataset_location):
     cs = CheckSuite()
     cs.load_all_available_checkers()
     ds = cs.load_dataset(dataset_location)
     score_groups = cs.run(ds, [], checker)
     results, self.errors = score_groups[checker]
     self.results = cs.build_structure(checker, results, dataset_location)
    def test_cdl_file(self):
        # Testing whether you can run compliance checker on a .cdl file
        cs = CheckSuite()
        cs.load_all_available_checkers()

        # Load the cdl file
        ds = cs.load_dataset(static_files['test_cdl'])
        vals = cs.run(ds, 'cf')

        limit = 2
        for checker, rpair in vals.items():
            groups, errors = rpair
            score_list, cdl_points, cdl_out_of = cs.standard_output(limit, checker, groups)
            # This asserts that print is able to generate all of the unicode output
            cs.non_verbose_output_generation(score_list, groups, limit, cdl_points, cdl_out_of)
        ds.close()

        # Ok now load the nc file that it came from
        ds = cs.load_dataset(static_files['test_cdl_nc'])
        vals = cs.run(ds, 'cf')

        limit = 2
        for checker, rpair in vals.items():
            groups, errors = rpair
            score_list, nc_points, nc_out_of = cs.standard_output(limit, checker, groups)
            # This asserts that print is able to generate all of the unicode output
            cs.non_verbose_output_generation(score_list, groups, limit, nc_points, nc_out_of)
        ds.close()

        nc_file_path = static_files['test_cdl'].replace('.cdl', '.nc')
        self.addCleanup(os.remove, nc_file_path)

        # Ok the scores should be equal!
        self.assertEqual(nc_points, cdl_points)
        self.assertEqual(nc_out_of, cdl_out_of)
Beispiel #5
0
 def test_suite(self):
     # BWA: what's the purpose of this test?  Just to see if the suite
     # runs without errors?
     cs = CheckSuite()
     cs.load_all_available_checkers()
     ds = cs.load_dataset(static_files['2dim'])
     cs.run(ds, 'acdd')
Beispiel #6
0
 def setUp(self):
     """
     Initialize the dataset
     """
     self.cs = CheckSuite()
     self.cs.load_all_available_checkers()
     # get current std names table version (it changes)
     self._std_names = util.StandardNameTable()
 def test_erddap(self):
     """
     Tests that a connection can be made to ERDDAP's GridDAP
     """
     url = "http://coastwatch.pfeg.noaa.gov/erddap/griddap/osuChlaAnom"
     cs = CheckSuite()
     ds = cs.load_dataset(url)
     assert ds is not None
 def test_hyrax(self):
     """
     Tests that a connection can be made to Hyrax
     """
     url = "http://ingria.coas.oregonstate.edu/opendap/hyrax/aggregated/ocean_time_aggregation.ncml"
     cs = CheckSuite()
     ds = cs.load_dataset(url)
     assert ds is not None
 def test_thredds(self):
     '''
     Tests that a connection can be made to a remote THREDDS endpoint
     '''
     url = 'http://data.ioos.us/thredds/dodsC/deployments/rutgers/ru24-20150105T1441/ru24-20150105T1441.nc3.nc'
     cs = CheckSuite()
     ds = cs.load_dataset(url)
     assert ds is not None
 def test_sos(self):
     """
     Tests that a connection can be made to an SOS endpoint
     """
     url = "https://data.oceansmap.com/thredds/sos/caricoos_ag/VIA/VIA.ncml"
     cs = CheckSuite()
     ds = cs.load_dataset(url)
     assert ds is not None
    def test_thredds(self):
        '''
        Tests that a connection can be made to a remote THREDDS endpoint
        '''
        url = "http://thredds.ucar.edu/thredds/dodsC/grib/NCEP/GFS/Global_0p25deg_ana/TP"

        cs = CheckSuite()
        ds = cs.load_dataset(url)
        assert ds is not None
 def test_netcdf_content_type(self):
     """
     Check that urls with Content-Type header of "application/x-netcdf" can
     successfully be read into memory for checks.
     """
     url = "https://gliders.ioos.us/erddap/tabledap/amelia-20180501T0000.ncCF?&time%3E=max(time)-1%20hour"
     cs = CheckSuite()
     ds = cs.load_dataset(url)
     assert ds is not None
Beispiel #13
0
    def run_checker(cls, ds_loc, checker_names, verbose, criteria,
                    skip_checks=None, output_filename='-',
                    output_format='text'):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names    List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  skip_checks     Names of checks to skip
        @param  output_format   Format of the output

        @returns                If the tests failed (based on the criteria)
        """
        cs = CheckSuite()
        ds = cs.load_dataset(ds_loc)

        score_groups = cs.run(ds, [] if skip_checks is None else skip_checks,
                              *checker_names)

        if not score_groups:
            raise ValueError("No checks found, please check the name of the checker(s) and that they are installed")

        if criteria == 'normal':
            limit = 2
        elif criteria == 'strict':
            limit = 1
        elif criteria == 'lenient':
            limit = 3

        if output_format == 'text':
            if output_filename == '-':
                groups = cls.stdout_output(cs, score_groups, verbose, limit)
            # need to redirect output from stdout since print functions are
            # presently used to generate the standard report output
            else:
                with io.open(output_filename, 'w', encoding='utf-8') as f:
                    with stdout_redirector(f):
                        groups = cls.stdout_output(cs, score_groups, verbose,
                                                   limit)

        elif output_format == 'html':
            groups = cls.html_output(cs, score_groups, output_filename, ds_loc,
                                     limit)

        elif output_format == 'json':
            groups = cls.json_output(cs, score_groups, output_filename, ds_loc,
                                     limit)

        else:
            raise TypeError('Invalid format %s' % output_format)

        errors_occurred = cls.check_errors(score_groups, verbose)

        return cs.passtree(groups, limit), errors_occurred
 def test_skip_checks(self):
     """Tests that checks are properly skipped when specified"""
     cs = CheckSuite()
     cs.load_all_available_checkers()
     ds = cs.load_dataset(static_files['2dim'])
     # exclude title from the check attributes
     score_groups = cs.run(ds, ['check_high'], 'acdd')
     assert all(sg.name not in {'Conventions', 'title', 'keywords',
                                'summary'} for sg in score_groups['acdd'][0])
Beispiel #15
0
    def test_unicode_formatting(self):
        cs = CheckSuite()
        cs.load_all_available_checkers()
        ds = cs.load_dataset(static_files['bad_region'])
        score_groups = cs.run(ds, 'cf')

        limit = 2
        for checker, rpair in score_groups.iteritems():
            groups, errors = rpair
            score_list, points, out_of = cs.standard_output(limit, checker, groups)
            # This asserts that print is able to generate all of the unicode output
            cs.non_verbose_output_generation(score_list, groups, limit, points, out_of)
Beispiel #16
0
def test_standardise_cf(standardised):
    suite = CheckSuite()
    suite.load_all_available_checkers()

    ds = suite.load_dataset(standardised)
    results = suite.run(ds, [], 'cf')

    check_failures = 0
    for r in results['cf'][0]:
        if r.value[1] - r.value[0] > 0:
            print(r, file=sys.stderr)
            check_failures += 1

    assert check_failures == 0
Beispiel #17
0
    def test_group_func(self):
        # This is checking for issue #183, where group_func results in
        # IndexError: list index out of range
        cs = CheckSuite()
        cs.load_all_available_checkers()
        ds = cs.load_dataset(static_files['bad_data_type'])
        score_groups = cs.run(ds, 'cf')

        limit = 2
        for checker, rpair in score_groups.iteritems():
            groups, errors = rpair
            score_list, points, out_of = cs.standard_output(limit, checker, groups)
            # This asserts that print is able to generate all of the unicode output
            cs.non_verbose_output_generation(score_list, groups, limit, points, out_of)
Beispiel #18
0
    def run_checker(cls,
                    ds_loc,
                    checker_names,
                    verbose,
                    criteria,
                    output_filename='stdout',
                    output_format='stdout'):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names    List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  output_format   Format of the output

        @returns                If the tests failed (based on the criteria)
        """
        retval = True

        cs = CheckSuite()
        ds = cs.load_dataset(ds_loc)
        score_groups = cs.run(ds, *checker_names)

        if criteria == 'normal':
            limit = 2
        elif criteria == 'strict':
            limit = 1
        elif criteria == 'lenient':
            limit = 3

        if output_filename == '-' and output_format == 'text':
            groups = cls.stdout_output(cs, score_groups, verbose, limit)

        elif output_format == 'html':
            groups = cls.html_output(cs, score_groups, output_filename, ds_loc,
                                     limit)

        elif output_format == 'json':
            groups = cls.json_output(cs, score_groups, output_filename, ds_loc,
                                     limit)

        else:
            raise TypeError('Invalid format %s' % output_format)

        errors_occurred = cls.check_errors(score_groups, verbose)

        return cs.passtree(groups, limit), errors_occurred
 def test_score_grouping(self):
     # Testing the grouping of results for output, which can fail
     # if some assumptions are not met, e.g. if a Result object has
     # a value attribute of unexpected type
     cs = CheckSuite()
     res = [
         Result(BaseCheck.MEDIUM, True, 'one'),
         Result(BaseCheck.MEDIUM, (1, 3), 'one'),
         Result(BaseCheck.MEDIUM, None, 'one'),
         Result(BaseCheck.MEDIUM, True, 'two'),
         Result(BaseCheck.MEDIUM, np.isnan(1), 'two')  # value is type numpy.bool_
     ]
     score = cs.scores(res)
     self.assertEqual(score[0].name, 'one')
     self.assertEqual(score[0].value, (2, 4))
     self.assertEqual(score[1].name, 'two')
     self.assertEqual(score[1].value, (1, 2))
 def setUp(self):
     self.cs = CheckSuite()
     self.cs.load_all_available_checkers()
Beispiel #21
0
    def run_checker(
        cls,
        ds_loc,
        checker_names,
        verbose,
        criteria,
        skip_checks=None,
        output_filename="-",
        output_format=["text"],
        options=None,
    ):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names   List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  skip_checks     Names of checks to skip
        @param  output_format   Format of the output(s)

        @returns                If the tests failed (based on the criteria)
        """
        all_groups = []
        cs = CheckSuite(options=options or {})
        # using OrderedDict is important here to preserve the order
        # of multiple datasets which may be passed in
        score_dict = OrderedDict()
        if not isinstance(ds_loc, str):
            locs = ds_loc
        # if single dataset, put in list
        else:
            locs = [ds_loc]

        # Make sure output format is a list
        if isinstance(output_format, str):
            output_format = [output_format]

        for loc in locs:  # loop through each dataset and run specified checks
            ds = cs.load_dataset(loc)

            score_groups = cs.run(ds, skip_checks, *checker_names)
            for group in score_groups.values():
                all_groups.append(group[0])
            # TODO: consider wrapping in a proper context manager instead
            if hasattr(ds, "close"):
                ds.close()

            if not score_groups:
                raise ValueError(
                    "No checks found, please check the name of the checker(s) and that they are installed"
                )
            else:
                score_dict[loc] = score_groups

        # define a score limit to truncate the ouput to the strictness level
        # specified by the user
        if criteria == "normal":
            limit = 2
        elif criteria == "strict":
            limit = 1
        elif criteria == "lenient":
            limit = 3

        for out_fmt in output_format:
            if out_fmt == "text":
                if output_filename == "-":
                    cls.stdout_output(cs, score_dict, verbose, limit)
                # need to redirect output from stdout since print functions are
                # presently used to generate the standard report output
                else:
                    if len(output_format) > 1:
                        # Update file name if needed
                        output_filename = "{}.txt".format(
                            os.path.splitext(output_filename)[0])
                    with io.open(output_filename, "w", encoding="utf-8") as f:
                        with stdout_redirector(f):
                            cls.stdout_output(cs, score_dict, verbose, limit)

            elif out_fmt == "html":
                # Update file name if needed
                if len(output_format) > 1 and output_filename != "-":
                    output_filename = "{}.html".format(
                        os.path.splitext(output_filename)[0])
                cls.html_output(cs, score_dict, output_filename, ds_loc, limit)

            elif out_fmt in {"json", "json_new"}:
                # Update file name if needed
                if len(output_format) > 1 and output_filename != "-":
                    output_filename = "{}.json".format(
                        os.path.splitext(output_filename)[0])
                cls.json_output(cs, score_dict, output_filename, ds_loc, limit,
                                out_fmt)

            else:
                raise TypeError("Invalid format %s" % out_fmt)

            errors_occurred = cls.check_errors(score_groups, verbose)

        return (
            all(cs.passtree(groups, limit) for groups in all_groups),
            errors_occurred,
        )
Beispiel #22
0
def glider_deployment_check(data_type=None,
                            completed=True,
                            force=False,
                            deployment_dir=None,
                            username=None):
    """
    """
    cs = CheckSuite()
    cs.load_all_available_checkers()
    with app.app_context():
        if data_type is not None:
            is_delayed_mode = data_type == 'delayed'
            if is_delayed_mode:
                q_dict = {"delayed_mode": True, "completed": completed}
            else:
                q_dict = {
                    "$or": [{
                        "delayed_mode": False
                    }, {
                        "delayed_mode": {
                            "$exists": False
                        }
                    }],
                    "completed":
                    completed
                }

            if not force:
                q_dict["compliance_check_passed"] = {"$ne": True}

        # TODO: combine username/deployment cases?
        if username:
            q_dict = {"username": username}
        # a particular deployment has been specified
        elif deployment_dir:
            q_dict = {"deployment_dir": deployment_dir}
        else:
            q_dict = {}

        agg_pipeline = [{
            "$match": q_dict
        }, {
            "$group": {
                "_id": "$user_id",
                "deployments": {
                    "$push": {
                        "_id": "$_id",
                        "name": "$name",
                        "deployment_dir": "$deployment_dir"
                    }
                }
            }
        }]
        # if force is enabled, re-check the datasets no matter what

        # is this syntax still used?  if the first fn call fails, use the
        # second set of results
        try:
            agg_result_set = db.deployments.aggregate(agg_pipeline)['result']
        except:
            agg_result_set = db.deployments.aggregate(agg_pipeline, cursor={})
        for res in agg_result_set:
            user = db.users.find_one(res["_id"])
            all_messages = []
            failing_deployments = []
            for dep in res['deployments']:
                root_logger.info("Running compliance check on glider "
                                 "deployment: {}".format(dep))
                try:
                    dep_passed, dep_messages = process_deployment(dep)
                    all_messages.append(dep_messages)
                    if not dep_passed:
                        failing_deployments.append(dep)
                except Exception as e:
                    root_logger.exception(
                        "Exception occurred while processing deployment {}".
                        format(dep['name']))
                    text_body = ''
            send_deployment_cchecker_email(user, failing_deployments,
                                           "\n".join(all_messages))
Beispiel #23
0
 def test_suite(self):
     cs = CheckSuite()
     cs.load_all_available_checkers()
     ds = cs.load_dataset(static_files['2dim'])
     vals = cs.run(ds, 'acdd')
Beispiel #24
0
 def test_64bit(self):
     dataset = self.get_pair(static_files['ints64'])
     suite = CheckSuite()
     suite.checkers = {'cf': CFBaseCheck}
     suite.run(dataset, 'cf')
 def setUp(self):
     '''
     Initialize the dataset
     '''
     self.cs = CheckSuite()
     self.cs.load_all_available_checkers()
    def run_checker(cls, ds_loc, checker_names, verbose, criteria,
                    skip_checks=None, output_filename='-',
                    output_format=['text']):
        """
        Static check runner.

        @param  ds_loc          Dataset location (url or file)
        @param  checker_names   List of string names to run, should match keys of checkers dict (empty list means run all)
        @param  verbose         Verbosity of the output (0, 1, 2)
        @param  criteria        Determines failure (lenient, normal, strict)
        @param  output_filename Path to the file for output
        @param  skip_checks     Names of checks to skip
        @param  output_format   Format of the output(s)

        @returns                If the tests failed (based on the criteria)
        """
        cs = CheckSuite()
        # using OrderedDict is important here to preserve the order
        # of multiple datasets which may be passed in
        score_dict = OrderedDict()
        if not isinstance(ds_loc, six.string_types):
            locs = ds_loc
        # if single dataset, put in list
        else:
            locs = [ds_loc]

        # Make sure output format is a list
        if isinstance(output_format, six.string_types):
            output_format = [output_format]

        for loc in locs:
            ds = cs.load_dataset(loc)

            score_groups = cs.run(ds, [] if skip_checks is None else skip_checks,
                                *checker_names)

            if not score_groups:
                raise ValueError("No checks found, please check the name of the checker(s) and that they are installed")
            else:
                score_dict[loc] = score_groups

        if criteria == 'normal':
            limit = 2
        elif criteria == 'strict':
            limit = 1
        elif criteria == 'lenient':
            limit = 3

        for out_fmt in output_format:
            if out_fmt == 'text':
                if output_filename == '-':
                    groups = cls.stdout_output(cs, score_dict, verbose, limit)
                # need to redirect output from stdout since print functions are
                # presently used to generate the standard report output
                else:
                    if len(output_format) > 1:
                        # Update file name if needed
                        output_filename = '{}.txt'.format(os.path.splitext(output_filename)[0])
                    with io.open(output_filename, 'w', encoding='utf-8') as f:
                        with stdout_redirector(f):
                            groups = cls.stdout_output(cs, score_dict, verbose,
                                                       limit)

            elif out_fmt == 'html':
                # Update file name if needed
                if len(output_format) > 1 and output_filename != '-':
                    output_filename = '{}.html'.format(os.path.splitext(output_filename)[0])
                groups = cls.html_output(cs, score_dict, output_filename, ds_loc,
                                         limit)

            elif out_fmt == 'json' or 'json_new':
                # Update file name if needed
                if len(output_format) > 1 and output_filename != '-':
                    output_filename = '{}.json'.format(os.path.splitext(output_filename)[0])
                groups = cls.json_output(cs, score_dict, output_filename, ds_loc,
                                         limit, out_fmt)

            else:
                raise TypeError('Invalid format %s' % out_fmt)

        errors_occurred = cls.check_errors(score_groups, verbose)

        return cs.passtree(groups, limit), errors_occurred
 def test_load_local_dataset_GenericFile(self):
     cs = CheckSuite()
     resp = cs.load_local_dataset(static_files['empty'])
     assert isinstance(resp, GenericFile) ==  True