Exemplo n.º 1
0
    def _testMainArgsAndGetResponseStdOutArray(self, args):

        saved_stdout = sys.stdout
        finaloutput = None

        try:
            out = StringIO()
            sys.stdout = out

            self.assertEqual(
                main(args), 0,
                "command args {} should return successcode, but returned:\n+++++\n{}\n+++++\n"
                .format(args, out.getvalue()))

            output = list(out.getvalue().split("\n"))
            finaloutput = list(filter(lambda line: line != '', output))

            self.assertGreater(
                len(finaloutput), 0,
                "command args {} and its output should be greater than zero".
                format(args))

            sys.stdout = saved_stdout

        finally:
            pass
            sys.stdout = saved_stdout

        return (output, out)
Exemplo n.º 2
0
    def testJsonResponse(self, mockSessionObj):

        response = MockResponse()
        response.status_code = 200

        fetch = LdsFetch()
        response.jsonObj = fetch.adjustResponseJSON(
            self.getJSONFromFile(
                "{}/bin/tests/json/_lds-api_v3_log-sources_cpcode-products.json"
                .format(os.getcwd())))

        session = mockSessionObj()
        session.get.return_value = response

        edgeRc = "{}/bin/tests/other/.dummy_edgerc".format(os.getcwd())

        args = [
            "ldslist", "--section", "default", "--edgerc", edgeRc,
            "--show-json"
        ]

        saved_stdout = sys.stdout
        finaloutput = None

        try:
            out = StringIO()
            sys.stdout = out

            mainresult = main(args)

            outputStr = out.getvalue()
            output = list(outputStr.split("\n"))
            finaloutput = list(filter(lambda line: line != '', output))
            outputJson = json.loads(outputStr)

            self.assertEquals("200957",
                              outputJson[0]["logSource"]["cpCodeNumber"])
            self.assertEquals("104523",
                              outputJson[1]["logSource"]["cpCodeNumber"])

            self.assertGreater(
                len(finaloutput), 0,
                "command args {} and its output should be greater than zero".
                format(args))

            self.assertEqual(
                mainresult, 0,
                "command args {} should return successcode".format(args))

            self.assertEqual(304, len(finaloutput))

        finally:
            pass
            sys.stdout = saved_stdout
Exemplo n.º 3
0
    def _testParseLDSCommandCombo(self, args, expectedLines=2):

        saved_stdout = sys.stdout
        saved_sterr = sys.stdout

        finaloutput = None

        try:
            out = StringIO()
            sys.stdout = out

            outerr = StringIO()
            sys.stderr = outerr

            returnVal = main(args)
            stdErrStr = outerr.getvalue()

            self.assertEqual(
                returnVal, 0,
                "command args {} should return successcode. Error msg: {}".
                format(args, stdErrStr))

            output = list(out.getvalue().split("\n"))
            finaloutput = list(filter(lambda line: line != '', output))

            self.assertGreater(
                len(finaloutput), 0,
                "command args {} and its output should be greater than zero".
                format(args))

            line = finaloutput[0]
            self.assertIn("CPCODE", line)
            self.assertIn("Aggregation_Frequency", line)
            self.assertIn("Status", line)
            self.assertIn("Encoding", line)

            line = finaloutput[1]

            sys.stdout = saved_stdout

            self.assertIn("200957", line)
            self.assertIn("Every 24 hours", line)
            self.assertIn("active", line)
            self.assertIn("GZIP", line)

            self.assertEqual(expectedLines, len(finaloutput))

        finally:
            pass
            sys.stdout = saved_stdout
            sys.stderr = saved_sterr

        return finaloutput
Exemplo n.º 4
0
    def _testMainArgsAndGetResponseStdOutArray(self, args, expectedStatusMsgs = 0):

        saved_stdout = sys.stdout
        saved_stderr = sys.stderr

        finaloutput = None

        try:
            out = StringIO()
            sys.stdout = out

            errout = StringIO()
            sys.stderr = errout
            
            mainresult = main(args)
            
            output = list(out.getvalue().split("\n"))
            finaloutput = list(filter(lambda line: line != '', output))

            erroutput = list(errout.getvalue().split("\n"))
            finalerroutput = list(filter(lambda line: line != '', erroutput))

           
            self.assertGreater(len(finaloutput), 0, "command args {} and its output should be greater than zero".format(args) )
            self.assertEquals(len(finalerroutput), expectedStatusMsgs, "for args {} the std err message line count should be {} but got:\n {}".format(args,expectedStatusMsgs,erroutput) )
            self.assertEqual(mainresult, 0, "command args {} should return successcode, but returned:\n+++++\n{}\n+++++\n".format(args,out.getvalue()) )


        except Exception as e:
            sys.stdout = saved_stdout
            sys.stderr = saved_stderr

            print(out.getvalue, sys.stdout)
            print(errout.getvalue, sys.stderr)
            raise e

        finally:
            
            sys.stdout = saved_stdout
            sys.stderr = saved_stderr

        return (output, finalerroutput, out, errout)   
Exemplo n.º 5
0
    def testTemplateNames(self):

        args = ["filtertemplate", "--type", "ldslist"]

        saved_stdout = sys.stdout
        saved_stderr = sys.stderr

        finaloutput = None

        try:
            out = StringIO()
            sys.stdout = out

            outerr = StringIO()
            sys.stderr = outerr
            result = main(args)

            output = list(out.getvalue().split("\n"))
            finaloutput = list(filter(lambda line: line != '', output))

            self.assertEqual(
                result, 0,
                "command args {} should return successcode".format(args))

            self.assertGreater(
                len(finaloutput), 0,
                "command args {} and its output should be greater than zero".
                format(args))

            jsonStr = out.getvalue()
            jsondict = json.loads(jsonStr)
            self.assertGreaterEqual(len(jsondict), 3)
            self.assertIsInstance(jsondict, list)

            self.assertIn("default.json", jsondict)
            self.assertIn("active-gpg.json", jsondict)
            self.assertIn("all-default.json", jsondict)

        finally:
            pass
            sys.stdout = saved_stdout
            sys.stderr = saved_stderr
Exemplo n.º 6
0
    def testTemplateTypes_filterfile(self):

        filterfile = "{}/bin/queries/ldslist/default.json".format(os.getcwd())

        args = ["filtertemplate", "--filterfile", filterfile]

        saved_stdout = sys.stdout
        saved_stderr = sys.stderr

        finaloutput = None

        try:
            out = StringIO()
            sys.stdout = out

            outerr = StringIO()
            sys.stderr = outerr

            self.assertEqual(
                main(args), 0,
                "command args {} should return successcode".format(args))

            output = list(out.getvalue().split("\n"))
            finaloutput = list(filter(lambda line: line != '', output))

            self.assertGreater(
                len(finaloutput), 0,
                "command args {} and its output should be greater than zero".
                format(args))

            jsonStr = out.getvalue()
            jsondict = json.loads(jsonStr)
            self.assertIsInstance(jsondict, dict)

            self.assertIn("CPCODE", jsondict)
            self.assertIn("Aggregation_Frequency", jsondict)
            self.assertIn("Directory", jsondict)

        finally:
            sys.stdout = saved_stdout
            sys.stderr = saved_stderr
Exemplo n.º 7
0
    def testTemplateTypes(self):
        #filterfile = "{}/bin/tests/json/_storage_v1_storage-groups.json".format(os.getcwd())

        args = ["filtertemplate"]

        saved_stdout = sys.stdout
        saved_stderr = sys.stderr

        finaloutput = None

        try:
            out = StringIO()
            sys.stdout = out

            outerr = StringIO()
            sys.stderr = outerr

            self.assertEqual(
                main(args), 0,
                "command args {} should return successcode".format(args))

            output = list(out.getvalue().split("\n"))
            finaloutput = list(filter(lambda line: line != '', output))

            self.assertGreater(
                len(finaloutput), 0,
                "command args {} and its output should be greater than zero".
                format(args))

            jsonStr = out.getvalue()
            jsondict = json.loads(jsonStr)
            self.assertGreaterEqual(len(jsondict), 3)
            self.assertIsInstance(jsondict, list)

            self.assertIn("ldslist", jsondict)
            self.assertIn("netstoragelist", jsondict)

        finally:
            sys.stdout = saved_stdout
            sys.stderr = saved_stderr
Exemplo n.º 8
0
    def test_main(self):

        args = ["help"]
        #print("\n###testing args: {}".format(args))
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.assertTrue(len(result) > 0)

        args = ["help", "filtertemplate"]
        #print("\n###testing args: {}".format(args))
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.assertTrue(len(result) > 0)
        self.assertIn("filtertemplate", result[0])

        args = ["help", "ldslist"]
        #print("\n###testing args: {}".format(args))
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.assertTrue(len(result) > 0)
        self.assertIn("ldslist", result[0])

        args = ["help", "not_a_command"]
        #print("\n###testing args: {}".format(args))
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               True)
        self.assertTrue(len(result) == 0)

        args = ["help", "not_a_command"]
        #print("\n###testing args: {}".format(args))
        result, _ = self.redirectOutputToArray(lambda args: main(None), args,
                                               True)
        self.assertTrue(len(result) == 0)

        args = ["help", "not_a_command"]
        #print("\n###testing args: {}".format(args))

        args = list([])
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               True)
        self.assertTrue(len(result) == 0)
Exemplo n.º 9
0
    def main(self):

        os.environ['AKAMAI_CLI'] = "True"
        akamaiCMD = "akamai query"

        print(self.readFile(os.path.join(SCRIPT_DIR, "header.md")))

        print("## Commandline Help Text")
        args = ["help"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("## Querying Log Delivery Service (LDS)")
        args = ["help", "ldslist"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("## Querying Datastream Raw Logs")
        print("Basic support without result pagination")
        args = ["help", "datastream_raw"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("## Querying Datastream Aggregate Data")
        print("Basic support without result pagination")
        args = ["help", "datastream_agg"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("## Querying Netstorage NS4")
        args = ["help", "netstoragelist"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("## Querying Netstorage Users")
        args = ["help", "netstorageuser"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("## Querying Control Center Group CPCodes")
        args = ["help", "groupcpcodelist"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("## Generic Filter Template Utility")
        args = ["help", "filtertemplate"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("## Querying Property Mangager Configurations")
        args = ["help", "bulksearch"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("## Bulk Search Template Utility")
        args = ["help", "bulksearchtemplate"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        self.printHelpTest(result.getvalue())

        print("### Bulk Search Built-In Templates")
        print()
        print("Ready to go bulk searches. See them when running the command:")
        args = ["bulksearchtemplate"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()} "
        self.printHelpTest(result)

        print()
        print(
            "default.json bulk search template finds all configuration's and their cpcode values:"
        )
        args = ["bulksearchtemplate", "--get", "default.json"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()} "
        self.printHelpTest(result)

        print()
        print(
            "origins.json template finds all configuration and their origins:")
        args = ["bulksearchtemplate", "--get", "origins.json"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()} "
        self.printHelpTest(result)

        print()
        print(
            "gtm-origins.json template finds all configurations with gtm origins:"
        )
        args = ["bulksearchtemplate", "--get", "gtm-origins.json"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()} "
        self.printHelpTest(result)

        print("### Bulk Search Built-In Filters")

        print()
        print(
            "Filters modify the presentation and fields displayed as part of a bulk search result."
        )
        print("The built-in filters are listed when running this command:")
        args = ["filtertemplate", "--type", "bulksearch"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()} "
        self.printHelpTest(result)

        print()
        print(
            "default.json filter returns each configuration and the value found from the bulk search"
        )
        args = [
            "filtertemplate", "--type", "bulksearch", "--get", "default.json"
        ]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()}"
        self.printHelpTest(result)

        print()
        print(
            "result.json filter returns only the value found from the bulk search. This is handy to pipe in values into your own custom scripts for further processing"
        )
        args = [
            "filtertemplate", "--type", "bulksearch", "--get", "result.json"
        ]

        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()}"
        self.printHelpTest(result)

        print()
        print(
            "property.json filter returns only the property names that found the values from the bulk search. This is handy to pipe in property names into your own custom scripts for further processing"
        )
        args = [
            "filtertemplate", "--type", "bulksearch", "--get", "property.json"
        ]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()} "
        self.printHelpTest(result)

        print(
            self.readFile(os.path.join(SCRIPT_DIR, "bulk-search-examples.md")))

        print("### DNS Validation with Bulk Search")
        print()

        print(
            "JSON Array parsing tool to extract hostnames and filter output by DNS status"
        )
        args = ["help", "checkjsondns"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()}"
        self.printHelpTest(result)

        print("Access underlying DNS-over-HTTP API used by checkjsondns")
        args = ["help", "checkhostdns"]
        result, _ = self.redirectOutputToArray(lambda args: main(args), args,
                                               False)
        result = f"{akamaiCMD} {' '.join(args)} \n\n{result.getvalue()}"
        self.printHelpTest(result)

        print(
            self.readFile(
                os.path.join(SCRIPT_DIR, "bulk-search-dns-examples.md")))
Exemplo n.º 10
0
    def testMainBootStrap(self):

        args = ["filtertemplate", "--type", "ldslist", "--get", "active.json"]

        saved_stdout = sys.stdout
        saved_stderr = sys.stderr

        finaloutput = None

        try:
            out = StringIO()
            sys.stdout = out

            outerr = StringIO()
            sys.stderr = outerr

            self.assertEqual(
                main(args), 0,
                "command args {} should return successcode".format(args))

            output = list(out.getvalue().split("\n"))
            finaloutput = list(filter(lambda line: line != '', output))

            self.assertGreater(
                len(finaloutput), 0,
                "command args {} and its output should be greater than zero".
                format(args))

            jsondict = json.loads(out.getvalue())

            for k in jsondict:
                filename = "{}/bin/queries/ldslist/{}".format(os.getcwd(), k)
                self.assertTrue(
                    os.path.exists(filename) and os.path.isfile(filename))

            args = [
                "filtertemplate", "--type", "wrong_name", "--get",
                "default.json"
            ]

            out = StringIO()
            sys.stdout = out

            outerr = StringIO()
            sys.stderr = outerr

            self.assertEqual(
                main(args), 1,
                "command args {} should NOT return successcode".format(args))

            output = outerr.getvalue()
            self.assertAlmostEqual("template type: wrong_name not found. \n",
                                   output)

            jsonStr = out.getvalue()
            jsondict = json.loads(jsonStr)
            self.assertGreaterEqual(len(jsondict), 3)
            self.assertIsInstance(jsondict, list)

            self.assertIn("ldslist", jsondict)
            self.assertIn("netstoragelist", jsondict)

        finally:
            pass
            sys.stdout = saved_stdout
            sys.stderr = saved_stderr