def test_init_all_sections(self, tmp_config_file):
        """Test initialization of GeographicGatherer with all defined sections."""
        from pytroll_collectors.geographic_gatherer import GeographicGatherer

        opts = arg_parse([
            "-n", "nameserver_a", "-n", "nameserver_b", "-p", "9999",
            str(tmp_config_file)
        ])

        gatherer = GeographicGatherer(opts)

        num_sections = len(self.config.sections())
        num_regions = len(self.config.get("DEFAULT", "regions").split())

        # All the sections should've created a trigger
        assert len(gatherer.triggers) == num_sections
        assert isinstance(gatherer.triggers[0], FakePostTrollTrigger)
        assert isinstance(gatherer.triggers[1], FakePostTrollTrigger)
        assert isinstance(gatherer.triggers[2], FakeWatchDogTrigger)
        assert isinstance(gatherer.triggers[3], FakeWatchDogTrigger)

        # N regions for each section
        assert all(
            len(trigger.collectors) == num_regions
            for trigger in gatherer.triggers)

        expected = {
            'name': 'gatherer',
            'port': 9999,
            'nameservers': ['nameserver_a', 'nameserver_b'],
        }
        fake_create_publisher_from_dict_config.assert_called_once_with(
            expected)
        fake_create_publisher_from_dict_config.return_value.start.assert_called_once(
        )
    def test_args_accepts_inbound_info(self, tmp_config_file):
        """Test passing the inbound_connection arg."""
        section = 'posttroll_section'
        opts = arg_parse(
            ["-c", section, "-i", "myhost:9999",
             str(tmp_config_file)])

        assert opts.inbound_connection == ["myhost:9999"]
    def test_init_observer(self, tmp_config_file):
        """Test initialization of GeographicGatherer for watchdog trigger as 'Observer'."""
        from pytroll_collectors.geographic_gatherer import GeographicGatherer

        section = 'observer_section'
        opts = arg_parse(["-c", section, str(tmp_config_file)])
        gatherer = GeographicGatherer(opts)
        self._watchdog_test(section, gatherer)
    def test_init_satpy_config_path(self, tmp_config_file):
        """Test that SATPY_CONFIG_PATH environment variable is used as default value if defined."""
        import os
        from pytroll_collectors.geographic_gatherer import GeographicGatherer
        self.config.remove_option("DEFAULT", "area_definition_file")
        section = 'minimal_config'
        opts = arg_parse(["-c", section, str(tmp_config_file)])

        os.environ["SATPY_CONFIG_PATH"] = AREA_CONFIG_PATH

        # This shouldn't raise anything
        _ = GeographicGatherer(opts)
    def test_init_minimal_no_nameservers(self, tmp_config_file):
        """Test initialization of GeographicGatherer with minimal config."""
        from pytroll_collectors.geographic_gatherer import GeographicGatherer

        section = 'minimal_config'
        opts = arg_parse([
            "-c", section, "-n", "false", "-p", "12345",
            str(tmp_config_file)
        ])

        _ = GeographicGatherer(opts)
        # A publisher is created with composed name and started
        assert_create_publisher_from_dict_config([section], 12345, False)
def main():
    """Run the gatherer."""
    opts = arg_parse()

    logger = setup_logging(opts, "geographic_gatherer")

    print("Setting timezone to UTC")
    os.environ["TZ"] = "UTC"
    time.tzset()

    granule_triggers = GeographicGatherer(opts)
    status = granule_triggers.run()

    logger.info("GeographicGatherer has stopped.")

    return status
    def test_posttroll_trigger_passes_multiple_inbound_info(
            self, posttroll_trigger_class, tmp_config_file):
        """Test that the multiple host info is passed on to the posttroll trigger."""
        section = 'posttroll_section'
        host_info = ["myhost:9999", "myotherhost:8888", "somenameserver"]
        opts = arg_parse([
            "-c", section, "-i", host_info[0], "-i", host_info[1], "-i",
            host_info[2],
            str(tmp_config_file)
        ])

        from pytroll_collectors.geographic_gatherer import GeographicGatherer
        GeographicGatherer(opts)

        assert ((host_info in posttroll_trigger_class.mock_calls[0].args)
                or (host_info
                    in posttroll_trigger_class.mock_calls[0].kwargs.values()))
    def test_posttroll_trigger_passes_inbound_info(self,
                                                   posttroll_trigger_class,
                                                   tmp_config_file):
        """Test that the host info is passed on to the posttroll trigger."""
        section = 'posttroll_section'
        host_info = "myhost:9999"
        opts = arg_parse(
            ["-c", section, "-i", host_info,
             str(tmp_config_file)])

        from pytroll_collectors.geographic_gatherer import GeographicGatherer
        gatherer = GeographicGatherer(opts)
        _check_one_trigger(gatherer, section)

        assert (([host_info] in posttroll_trigger_class.mock_calls[0].args)
                or ([host_info]
                    in posttroll_trigger_class.mock_calls[0].kwargs.values()))
    def test_init_no_area_def_file(self, tmp_config_file):
        """Test that GeographicGatherer gives a meaningful error message if area_definition_file is not defined."""
        import pytest
        from pytroll_collectors.geographic_gatherer import GeographicGatherer
        config = ConfigParser(interpolation=None)
        config.read(tmp_config_file)
        config.remove_option("DEFAULT", "area_definition_file")
        with open(tmp_config_file, mode="w") as fp:
            config.write(fp)
        # Make sure to work also when the environment has SATPY_CONFIG_PATH defined
        os.environ.pop("SATPY_CONFIG_PATH", None)
        section = 'minimal_config'
        opts = arg_parse(["-c", section, str(tmp_config_file)])

        with pytest.raises(KeyError) as err:
            _ = GeographicGatherer(opts)
        assert "'area_definition_file'" in str(err.value)
    def test_init_minimal(self, tmp_config_file):
        """Test initialization of GeographicGatherer with minimal config."""
        from pytroll_collectors.geographic_gatherer import GeographicGatherer

        section = 'minimal_config'
        opts = arg_parse(["-c", section, str(tmp_config_file)])
        gatherer = GeographicGatherer(opts)

        trigger = _check_one_trigger(gatherer, section)
        assert isinstance(trigger, FakePostTrollTrigger)

        # RegionCollector is called with two areas, the configured timeout and no duration
        timeliness = dt.timedelta(seconds=1800)
        duration = None
        self._check_region_collectors(trigger, section, timeliness, duration)

        self._check_publisher_no_args([section])
    def test_init_posttroll(self, tmp_config_file):
        """Test initialization of GeographicGatherer for posttroll trigger."""
        from pytroll_collectors.geographic_gatherer import GeographicGatherer

        section = 'posttroll_section'
        opts = arg_parse(["-c", section, str(tmp_config_file)])

        gatherer = GeographicGatherer(opts)

        trigger = _check_one_trigger(gatherer, section)

        # The PostTrollTrigger is configured, so only that should've been called
        assert isinstance(trigger, FakePostTrollTrigger)

        services = self.config.get(section, 'service').split(',')
        topics = self.config.get(section, 'topics').split(',')
        duration = self.config.getfloat(section, 'duration')
        inbound_connection = self.config.get(section, "inbound_connection")
        nameserver, addresses = inbound_connection.split(",")
        addresses = ["tcp://" + addresses.strip()]

        sub_config = dict(services=services,
                          topics=topics,
                          nameserver=nameserver,
                          addr_listener=True,
                          addresses=addresses)
        fake_sub_factory.assert_called_once_with(sub_config)

        assert trigger.duration == duration

        self._check_trigger_publishing_info(trigger, section)

        publish_message_after_each_reception = self.config.get(
            section, 'publish_message_after_each_reception')
        assert trigger.publish_message_after_each_reception == publish_message_after_each_reception
        assert trigger.start_called

        # RegionCollector is called with two areas, the configured timeout and a duration
        timeliness = dt.timedelta(
            minutes=self.config.getint(section, "timeliness"))
        duration = dt.timedelta(seconds=12, microseconds=300000)
        self._check_region_collectors(trigger, section, timeliness, duration)