def test_output_env_fallback(self):
        TEST_BASE_OUTPUT = "summary line"
        TEST_LONG_OUTPUT = "extended information"
        target_prefixes = {
            "SERVICE": [
                "-S", "fake_service", "-s", "CRITICAL", "-k", "fake_key", "-H",
                "fake_host"
            ],
            "HOST": ["-H", "fake_host", "-s", "CRITICAL", "-k", "fake_key"]
        }
        mon_prefixes = ("ICINGA_", "NAGIOS_", "")

        for prefix, args in target_prefixes.items():
            for envkey in mon_prefixes:
                for envkey2 in mon_prefixes:
                    envsummary = ("{envkey}{prefix}OUTPUT".format(
                        envkey=envkey, prefix=prefix))
                    envlong = ("{envkey}LONG{prefix}OUTPUT".format(
                        envkey=envkey2, prefix=prefix))
                    os.environ[envsummary] = TEST_BASE_OUTPUT
                    os.environ[envlong] = TEST_LONG_OUTPUT
                    opts, _ = send_signifai.parse_opts(args)
                    expected = str.join("\n",
                                        [TEST_BASE_OUTPUT, TEST_LONG_OUTPUT])
                    self.assertEqual(opts.check_output, expected)
                    del os.environ[envsummary]
                    del os.environ[envlong]
    def test_no_host_fails(self):
        args = [
            "-S", "servicename", "-s", "CRITICAL", "-o", "fake output", "-k",
            "fake_key"
        ]

        self.assertEqual(send_signifai.parse_opts(args), (None, None))
    def _do_test_envs(self, option_name, base_args, *envs):
        test_str = "TEST_STRING"
        test_str2 = "SHOULD_NEVER_BE"

        for i in range(0, len(envs) - 1):
            os.environ[envs[i]] = test_str
            os.environ[envs[i + 1]] = test_str2
            opts, _ = send_signifai.parse_opts(base_args)
            self.assertEqual(getattr(opts, option_name), test_str)
            self.assertNotEqual(getattr(opts, option_name), test_str2)
            del os.environ[envs[i]]
            del os.environ[envs[i + 1]]

        os.environ[envs[-1]] = test_str
        opts, _ = send_signifai.parse_opts(base_args)
        del os.environ[envs[-1]]
        self.assertEqual(getattr(opts, option_name), test_str)
    def test_service_state_valid_name(self):
        # Must allow CRITICAL to work
        args = [
            "-S", "fake_service", "-s", "CRITICAL", "-o", "fake output", "-k",
            "fake_key", "-H", "fake_host"
        ]

        opts, _ = send_signifai.parse_opts(args)
        self.assertEqual(opts.target_state, "CRITICAL")
    def test_service_state_int_aliasing(self):
        # Must translate the state index to CRITICAL
        args = [
            "-S", "fake_service", "-s", "2", "-o", "fake output", "-k",
            "fake_key", "-H", "fake_host"
        ]

        opts, _ = send_signifai.parse_opts(args)
        self.assertEqual(opts.target_state, "CRITICAL")
    def test_no_output_means_empty(self):
        # Must be an empty string and NOT None
        args = [
            "-S", "fake_service", "-s", "CRITICAL", "-k", "fake_key", "-H",
            "fake_host"
        ]

        opts, _ = send_signifai.parse_opts(args)
        self.assertEqual(opts.check_output, "")
        self.assertIsNotNone(opts.check_output, "")
    def test_service_state_invalid_name(self):
        # Must totally fail
        args = [
            "-S", "fake_service", "-s", "BEEPBOOP", "-o", "fake output", "-k",
            "fake_key", "-H", "fake_host"
        ]

        # Looks like this test will actually run this function three
        # times for some reason...
        self.assertEqual(send_signifai.parse_opts(args), (None, None))
    def test_no_service_means_None(self):
        # Must set the service_name to None, which will in turn
        # handle the output as a host check result
        args = [
            "-H", "aHostname", "-o", "fake_output", "-k", "fake_key", "-s",
            "CRITICAL"
        ]

        opts, _ = send_signifai.parse_opts(args)
        self.assertIsNone(opts.service_name)
 def _generate_options(self, *args):
     opts, _ = send_signifai.parse_opts(list(args))
     self.assertNotEqual(opts, (None, None))
     return opts