class TestMeasurementsCommand(unittest.TestCase):

    def setUp(self):
        self.cmd = Command()

    @mock.patch("ripe.atlas.tools.commands.measurements.MeasurementRequest")
    def test_with_empty_args(self, mock_request):
        mock_request.return_value = FakeGen()
        self.cmd.init_args([])
        self.cmd.run()
Esempio n. 2
0
 def test_fail_arguments(self):
     expected_failures = (
         ("--status", "not a status"),
         ("--not-an-option",),
         ("--af", "5"),
         ("--type", "not a type"),
         ("--field", "not a field"),
     )
     for failure in expected_failures:
         with capture_sys_output():
             with self.assertRaises(SystemExit):
                 Command().init_args(failure)
Esempio n. 3
0
 def test_get_colour_from_status(self):
     cmd = Command()
     self.assertEqual(cmd._get_colour_from_status(0), "blue")
     self.assertEqual(cmd._get_colour_from_status(1), "blue")
     self.assertEqual(cmd._get_colour_from_status(2), "green")
     self.assertEqual(cmd._get_colour_from_status(4), "yellow")
     self.assertEqual(cmd._get_colour_from_status(5), "red")
     self.assertEqual(cmd._get_colour_from_status(6), "red")
     self.assertEqual(cmd._get_colour_from_status(7), "red")
     self.assertEqual(cmd._get_colour_from_status("XXX"), "white")
Esempio n. 4
0
    def test_with_empty_args(self, mock_request):

        mock_request.return_value = FakeGen()

        cmd = Command()
        with capture_sys_output() as (stdout, stderr):
            cmd.init_args([])
            cmd.run()

        expected_content = (
            "\n"
            "Id      Type       Description                                               Status\n"
            "===================================================================================\n"
            "1       ping       Description 1                                            Ongoing\n"
            "2       ping       Description 2                                            Ongoing\n"
            "3       ping       Description 3                                            Ongoing\n"
            "4       ping       Description 4                                            Ongoing\n"
            "5       ping       Description 5                                            Ongoing\n"
            "===================================================================================\n"
            "                                                  Showing 5 of 5 total measurements\n"
            "\n"
        )
        self.assertEqual(
            set(stdout.getvalue().split("\n")),
            set(expected_content.split("\n"))
        )
        self.assertEqual(
            cmd.arguments.field, ("id", "type", "description", "status"))
Esempio n. 5
0
 def test_get_filters(self):
     cmd = Command()
     cmd.init_args([
         "--search", "the force is strong with this one",
         "--status", "ongoing",
         "--af", "6",
         "--type", "ping",
         "--started-before", "2015-01-01",
         "--started-after", "2014-01-01",
         "--stopped-before", "2015-01-01",
         "--stopped-after", "2014-01-01",
     ])
     self.assertEqual(cmd._get_filters(), {
         "search": "the force is strong with this one",
         "status__in": (2,),
         "af": 6,
         "type": "ping",
         "start_time__lt": datetime.datetime(2015, 1, 1),
         "start_time__gt": datetime.datetime(2014, 1, 1),
         "stop_time__lt": datetime.datetime(2015, 1, 1),
         "stop_time__gt": datetime.datetime(2014, 1, 1),
     })
 def setUp(self):
     self.cmd = Command()
Esempio n. 7
0
class TestMeasurementsCommand(unittest.TestCase):

    def setUp(self):
        self.cmd = Command()

    @mock.patch("ripe.atlas.tools.commands.measurements.MeasurementRequest")
    def test_with_empty_args(self, mock_request):

        mock_request.return_value = FakeGen()

        with capture_sys_output() as (stdout, stderr):
            self.cmd.init_args([])
            self.cmd.run()

        expected_content = (
            "\n"
            "Id      Type       Description                                           Status\n"
            "===============================================================================\n"
            "1       ping       Description 1                                        Ongoing\n"
            "2       ping       Description 2                                        Ongoing\n"
            "3       ping       Description 3                                        Ongoing\n"
            "4       ping       Description 4                                        Ongoing\n"
            "5       ping       Description 5                                        Ongoing\n"
            "===============================================================================\n"
            "                                              Showing 5 of 5 total measurements\n"
            "\n"
        )
        self.assertEqual(
            set(stdout.getvalue().split("\n")),
            set(expected_content.split("\n"))
        )
        self.assertEqual(
            self.cmd.arguments.field, ("id", "type", "description", "status"))

    @mock.patch("ripe.atlas.tools.commands.measurements.MeasurementRequest")
    def test_get_line_items(self, mock_request):

        self.cmd.init_args([])
        self.cmd.run()  # Forces the defaults
        self.assertEqual(
            self.cmd._get_line_items(FakeGen.Measurement(
                id=1, type="ping", status="Ongoing", status_id=2,
                meta_data={"status": {"name": "Ongoing", "id": 2}},
                destination_name="Name 1", description="Description 1",
            )),
            [1, "ping", "Description 1", "Ongoing"]
        )

        self.cmd.init_args([
            "--field", "id",
            "--field", "status"
        ])
        self.assertEqual(
            self.cmd._get_line_items(FakeGen.Measurement(
                id=1, type="ping", status="Ongoing", status_id=2,
                meta_data={"status": {"name": "Ongoing", "id": 2}},
                destination_name="Name 1", description="Description 1",
            )),
            [1, "Ongoing"]
        )

        self.cmd.init_args([
            "--field", "url",
        ])
        self.assertEqual(
            self.cmd._get_line_items(FakeGen.Measurement(
                id=1, type="ping", status="Ongoing", status_id=2,
                meta_data={"status": {"name": "Ongoing", "id": 2}},
                destination_name="Name 1", description="Description 1",
            )),
            ["https://atlas.ripe.net/measurements/1/"]
        )

    def test_get_filters(self):
        self.cmd.init_args([
            "--search", "the force is strong with this one",
            "--status", "ongoing",
            "--af", "6",
            "--type", "ping",
            "--started-before", "2015-01-01",
            "--started-after", "2014-01-01",
            "--stopped-before", "2015-01-01",
            "--stopped-after", "2014-01-01",
        ])
        self.assertEqual(self.cmd._get_filters(), {
            "search": "the force is strong with this one",
            "status__in": (2,),
            "af": 6,
            "type": "ping",
            "start_time__lt": datetime.datetime(2015, 1, 1),
            "start_time__gt": datetime.datetime(2014, 1, 1),
            "stop_time__lt": datetime.datetime(2015, 1, 1),
            "stop_time__gt": datetime.datetime(2014, 1, 1),
        })

    def test_get_colour_from_status(self):
        self.assertEqual(self.cmd._get_colour_from_status(0), "blue")
        self.assertEqual(self.cmd._get_colour_from_status(1), "blue")
        self.assertEqual(self.cmd._get_colour_from_status(2), "green")
        self.assertEqual(self.cmd._get_colour_from_status(4), "yellow")
        self.assertEqual(self.cmd._get_colour_from_status(5), "red")
        self.assertEqual(self.cmd._get_colour_from_status(6), "red")
        self.assertEqual(self.cmd._get_colour_from_status(7), "red")
        self.assertEqual(self.cmd._get_colour_from_status("XXX"), "white")

    def test_fail_arguments(self):
        expected_failures = (
            ("--status", "not a status"),
            ("--not-an-option",),
            ("--af", "5"),
            ("--type", "not a type"),
            ("--field", "not a field"),
        )
        for failure in expected_failures:
            with capture_sys_output() as (stdout, stderr):
                with self.assertRaises(SystemExit):
                    self.cmd.init_args(failure)
Esempio n. 8
0
 def setUp(self):
     self.cmd = Command()
Esempio n. 9
0
class TestMeasurementsCommand(unittest.TestCase):
    def setUp(self):
        self.cmd = Command()

    @mock.patch("ripe.atlas.tools.commands.measurements.MeasurementRequest")
    def test_with_empty_args(self, mock_request):

        mock_request.return_value = FakeGen()

        with capture_sys_output() as (stdout, stderr):
            self.cmd.init_args([])
            self.cmd.run()

        expected_content = (
            "\n"
            "Id      Type       Description                                           Status\n"
            "===============================================================================\n"
            "1       ping       Description 1                                        Ongoing\n"
            "2       ping       Description 2                                        Ongoing\n"
            "3       ping       Description 3                                        Ongoing\n"
            "4       ping       Description 4                                        Ongoing\n"
            "5       ping       Description 5                                        Ongoing\n"
            "===============================================================================\n"
            "                                              Showing 5 of 5 total measurements\n"
            "\n")
        self.assertEqual(set(stdout.getvalue().split("\n")),
                         set(expected_content.split("\n")))
        self.assertEqual(self.cmd.arguments.field,
                         ("id", "type", "description", "status"))

    @mock.patch("ripe.atlas.tools.commands.measurements.MeasurementRequest")
    def test_get_line_items(self, mock_request):

        self.cmd.init_args([])
        self.cmd.run()  # Forces the defaults
        self.assertEqual(
            self.cmd._get_line_items(
                FakeGen.Measurement(
                    id=1,
                    type="ping",
                    status="Ongoing",
                    status_id=2,
                    meta_data={"status": {
                        "name": "Ongoing",
                        "id": 2
                    }},
                    destination_name="Name 1",
                    description="Description 1",
                )), [1, "ping", "Description 1", "Ongoing"])

        self.cmd.init_args(["--field", "id", "--field", "status"])
        self.assertEqual(
            self.cmd._get_line_items(
                FakeGen.Measurement(
                    id=1,
                    type="ping",
                    status="Ongoing",
                    status_id=2,
                    meta_data={"status": {
                        "name": "Ongoing",
                        "id": 2
                    }},
                    destination_name="Name 1",
                    description="Description 1",
                )), [1, "Ongoing"])

        self.cmd.init_args([
            "--field",
            "url",
        ])
        self.assertEqual(
            self.cmd._get_line_items(
                FakeGen.Measurement(
                    id=1,
                    type="ping",
                    status="Ongoing",
                    status_id=2,
                    meta_data={"status": {
                        "name": "Ongoing",
                        "id": 2
                    }},
                    destination_name="Name 1",
                    description="Description 1",
                )), ["https://atlas.ripe.net/measurements/1/"])

    def test_get_filters(self):
        self.cmd.init_args([
            "--search",
            "the force is strong with this one",
            "--status",
            "ongoing",
            "--af",
            "6",
            "--type",
            "ping",
            "--started-before",
            "2015-01-01",
            "--started-after",
            "2014-01-01",
            "--stopped-before",
            "2015-01-01",
            "--stopped-after",
            "2014-01-01",
        ])
        self.assertEqual(
            self.cmd._get_filters(), {
                "search": "the force is strong with this one",
                "status__in": (2, ),
                "af": 6,
                "type": "ping",
                "start_time__lt": datetime.datetime(2015, 1, 1),
                "start_time__gt": datetime.datetime(2014, 1, 1),
                "stop_time__lt": datetime.datetime(2015, 1, 1),
                "stop_time__gt": datetime.datetime(2014, 1, 1),
            })

    def test_get_colour_from_status(self):
        self.assertEqual(self.cmd._get_colour_from_status(0), "blue")
        self.assertEqual(self.cmd._get_colour_from_status(1), "blue")
        self.assertEqual(self.cmd._get_colour_from_status(2), "green")
        self.assertEqual(self.cmd._get_colour_from_status(4), "yellow")
        self.assertEqual(self.cmd._get_colour_from_status(5), "red")
        self.assertEqual(self.cmd._get_colour_from_status(6), "red")
        self.assertEqual(self.cmd._get_colour_from_status(7), "red")
        self.assertEqual(self.cmd._get_colour_from_status("XXX"), "white")

    def test_fail_arguments(self):
        expected_failures = (
            ("--status", "not a status"),
            ("--not-an-option", ),
            ("--af", "5"),
            ("--type", "not a type"),
            ("--field", "not a field"),
        )
        for failure in expected_failures:
            with capture_sys_output() as (stdout, stderr):
                with self.assertRaises(SystemExit):
                    self.cmd.init_args(failure)
Esempio n. 10
0
    def test_get_line_items(self, mock_request):

        mock_request.return_value = FakeGen()
        cmd = Command()
        cmd.init_args([])
        cmd.run()
        self.assertEqual(
            cmd._get_line_items(FakeGen.Measurement(
                id=1, type="ping", status="Ongoing", status_id=2,
                meta_data={"status": {"name": "Ongoing", "id": 2}},
                destination_name="Name 1", description="Description 1",
            )),
            [1, "ping", "Description 1", "Ongoing"]
        )

        cmd = Command()
        cmd.init_args([
            "--field", "id",
            "--field", "status"
        ])
        self.assertEqual(
            cmd._get_line_items(FakeGen.Measurement(
                id=1, type="ping", status="Ongoing", status_id=2,
                meta_data={"status": {"name": "Ongoing", "id": 2}},
                destination_name="Name 1", description="Description 1",
            )),
            [1, "Ongoing"]
        )

        cmd = Command()
        cmd.init_args([
            "--field", "url",
        ])
        self.assertEqual(
            cmd._get_line_items(FakeGen.Measurement(
                id=1, type="ping", status="Ongoing", status_id=2,
                meta_data={"status": {"name": "Ongoing", "id": 2}},
                destination_name="Name 1", description="Description 1",
            )),
            ["https://atlas.ripe.net/measurements/1/"]
        )