def test_cp_R2R(self): src = ["data://.my/moredata/test.txt"] dest = "data://.my/moredata/test2.txt" CLI().cp(src, dest, self.client) result = CLI().ls("data://.my/moredata/", self.client) self.assertTrue("test2.txt" in result)
def test_ls(self): parentDir = "data://.my/" newDir = "test" CLI().mkdir(parentDir + newDir, self.client) result = CLI().ls(parentDir, self.client) self.assertTrue(result is not None and "moredata" in result and newDir in result) CLI().rmdir(parentDir + newDir, self.client)
def test_auth_token(self): address = 'https://api.algorithmia.com' bearer = 'testtokenabcd' profile = 'test' CLI().auth(apiaddress=address, bearer=bearer, profile=profile) resultA = CLI().getAPIaddress(profile) resultT = CLI().getBearerToken(profile) self.assertEqual(resultA, address) self.assertEqual(resultT, bearer)
def test_auth(self): #key for test account key = "simdylfCeXZ8/MgaQzokUHlalWm1" address = 'apiAddress' profile = 'defualt' CLI().auth(key, address, profile) resultK = CLI().getAPIkey(profile) resultA = CLI().getAPIaddress(profile) self.assertEqual(resultK, key) self.assertEqual(resultA, address)
def test_api_address_auth(self): api_key = os.getenv('ALGORITHMIA_API_KEY') api_address = "https://api.algorithmia.com" CLI().auth(api_address, api_key) profile = "default" client = Algorithmia.client(CLI().getAPIkey(profile), CLI().getAPIaddress(profile), CLI().getCert(profile)) result2 = CLI().ls("data://.my", client) print(result2) self.assertTrue(result2 != "")
def test_mkdir(self): parentDir = "data://.my/" newDir = "test" CLI().mkdir(parentDir + newDir, self.client) result = CLI().ls(parentDir, self.client) self.assertTrue(newDir in result) CLI().rmdir(parentDir + newDir, self.client)
def test_auth(self): # key for test account key = os.getenv('ALGORITHMIA_API_KEY') api_address = "https://api.algorithmia.com" profile = 'default' CLI().auth(api_address, key, profile=profile) resultK = CLI().getAPIkey(profile) resultA = CLI().getAPIaddress(profile) self.assertEqual(resultK, key) self.assertEqual(resultA, api_address)
def test_cp_L2R(self): localfile = "./../TestFiles/test.txt" testfile = open(localfile, "w") testfile.write("some text") testfile.close() src = [localfile] dest = "data://.my/moredata/test.txt" CLI().cp(src, dest, self.client) result = CLI().ls("data://.my/moredata/", self.client) self.assertTrue("test.txt" in result)
def test_cat(self): file = "data://.my/moredata/test.txt" localfile = "./../TestFiles/test.txt" fileContents = "some text in test file" CLI().rm(file, self.client) testfile = open(localfile, "w") testfile.write(fileContents) testfile.close() CLI().cp([localfile], file, self.client) result = CLI().cat([file], self.client) self.assertEqual(result, fileContents)
def test_run_token(self): name = "util/Echo" inputs = "test" parser = argparse.ArgumentParser('CLI for interacting with Algorithmia') subparsers = parser.add_subparsers(help='sub cmd', dest='subparser_name') parser_run = subparsers.add_parser('run', help='algo run <algo> [input options] <args..> [output options]') parser_run.add_argument('algo') parser_run.add_argument('-d', '--data', action='store', help='detect input type', default=None) parser_run.add_argument('-t', '--text', action='store', help='treat input as text', default=None) parser_run.add_argument('-j', '--json', action='store', help='treat input as json data', default=None) parser_run.add_argument('-b', '--binary', action='store', help='treat input as binary data', default=None) parser_run.add_argument('-D', '--data-file', action='store', help='specify a path to an input file', default=None) parser_run.add_argument('-T', '--text-file', action='store', help='specify a path to a text file', default=None) parser_run.add_argument('-J', '--json-file', action='store', help='specify a path to a json file', default=None) parser_run.add_argument('-B', '--binary-file', action='store', help='specify a path to a binary file', default=None) parser_run.add_argument('--timeout', action='store', type=int, default=300, help='specify a timeout (seconds)') parser_run.add_argument('--debug', action='store_true', help='print the stdout from the algo <this only works for the owner>') parser_run.add_argument('--profile', action='store', type=str, default='default') parser_run.add_argument('-o', '--output', action='store', default=None, type=str) args = parser.parse_args(['run', name, '-d', inputs]) result = CLI().runalgo(args, self.bearerClient) self.assertEqual(result, inputs)
def test_get_build_logs(self): user = os.environ.get('ALGO_USER_NAME') algo = "Echo" result = json.loads(CLI().getBuildLogs(user, algo, self.client)) if "error" in result: print(result) self.assertTrue("error" not in result)
def test_rm(self): localfile = "./../TestFiles/testRM.txt" testfile = open(localfile, "w") testfile.write("some text") testfile.close() src = [localfile] dest = "data://.my/moredata/" CLI().cp(src, dest, self.client) result1 = CLI().ls(dest, self.client) CLI().rm("data://.my/moredata/testRM.txt", self.client) result2 = CLI().ls(dest, self.client) self.assertTrue("testRM.txt" in result1 and "testRM.txt" not in result2)
def test_get_template(self): filename = "./../temptest" envid = "36fd467e-fbfe-4ea6-aa66-df3f403b7132" response = CLI().get_template(envid, filename, self.client) print(response) self.assertTrue(response.ok) try: shutil.rmtree(filename) except OSError as e: print(e)
def test_auth_cert(self): localfile = "./../TestFiles/fakecert.pem" testfile = open(localfile, "w") testfile.write("") testfile.close() # key for test account key = os.getenv('ALGORITHMIA_API_KEY') address = 'https://api.algorithmia.com' cacert = localfile profile = 'test' CLI().auth(address, key, cacert=cacert, profile=profile) resultK = CLI().getAPIkey(profile) resultA = CLI().getAPIaddress(profile) resultC = CLI().getCert(profile) self.assertEqual(resultK, key) self.assertEqual(resultA, address) self.assertEqual(resultC, cacert)
def setUp(self): # create a directory to use in testing the cp command self.client = Algorithmia.client() CLI().mkdir("data://.my/moredata", self.client) if not os.path.exists("../TestFiles/"): os.mkdir("../TestFiles/")
def main(): parser = argparse.ArgumentParser( 'algo', description="algo [<cmd>] [options] [<args>...] [--help] [--profile]") subparsers = parser.add_subparsers(help='sub cmd', dest='cmd') parser_auth = subparsers.add_parser( 'auth', help='save api key and api address for profile') parser_auth.add_argument('--profile', action='store', type=str, default='default') parser_clone = subparsers.add_parser( 'clone', help='clone <algo> clone the algorithm repository') parser_clone.add_argument('algo') parser_clone.add_argument('--profile', action='store', type=str, default='default') #parse options for the run command parser_run = subparsers.add_parser( 'run', help= 'algo run <algo> [input options] <args..> [output options] run an algorithm' ) parser_run.add_argument('algo') parser_run.add_argument('-d', '--data', action='store', help='detect input type', default=None) parser_run.add_argument('-t', '--text', action='store', help='treat input as text', default=None) parser_run.add_argument('-j', '--json', action='store', help='treat input as json data', default=None) parser_run.add_argument('-b', '--binary', action='store', help='treat input as binary data', default=None) parser_run.add_argument('-D', '--data-file', action='store', help='specify a path to an input file', default=None) parser_run.add_argument('-T', '--text-file', action='store', help='specify a path to a text file', default=None) parser_run.add_argument('-J', '--json-file', action='store', help='specify a path to a json file', default=None) parser_run.add_argument('-B', '--binary-file', action='store', help='specify a path to a binary file', default=None) parser_run.add_argument('--timeout', action='store', type=int, default=300, help='specify a timeout (seconds)') parser_run.add_argument( '--debug', action='store_true', help='print the stdout from the algo <this only works for the owner>') parser_run.add_argument('--profile', action='store', type=str, default='default') parser_run.add_argument('-o', '--output', action='store', default=None, type=str) #subparser for ls parser_ls = subparsers.add_parser( 'ls', help='ls [-l] [directory] list the contents of a directory', ) parser_ls.add_argument('-l', '--long', action='store_true') parser_ls.add_argument('path', nargs='?', default=None) parser_ls.add_argument('--profile', action='store', type=str, default='default') #subparser for rm parser_rm = subparsers.add_parser( 'rm', help='rm <path> remove a file', ) parser_rm.add_argument('path', nargs='?', default=None) parser_rm.add_argument('--profile', action='store', type=str, default='default') #subparser for mkdir parser_mkdir = subparsers.add_parser( 'mkdir', help='mkdir <directory> create a directory') parser_mkdir.add_argument('path', help='directory to create') parser_mkdir.add_argument('--profile', action='store', type=str, default='default') #subparser for rmdir parser_rmdir = subparsers.add_parser( 'rmdir', help='rmdir [-f] <directory> remove a directory') parser_rmdir.add_argument( '-f', '--force', action='store_true', help='force directory removal if it is not empty') parser_rmdir.add_argument('path', help='directory to remove') parser_rmdir.add_argument('--profile', action='store', type=str, default='default') #subparser for cp parser_cp = subparsers.add_parser( 'cp', help='cp <src,...> <dest> copy file(s) to the destination', ) parser_cp.add_argument('src', nargs='*', type=str, help='file(s) to be copied') parser_cp.add_argument('dest', help='destination for file(s) to be copied to') parser_cp.add_argument('--profile', action='store', type=str, default='default') #sub parser for cat parser_cat = subparsers.add_parser( 'cat', help='cat <path,...> concatenate and print file(s)') parser_cat.add_argument('path', nargs='*', help='file(s) to concatenate and print') parser_cat.add_argument('--profile', action='store', type=str, default='default') #sub parser for getting environment template parser_template = subparsers.add_parser( 'template', help= 'template <envid> <dest> downloads an environment template to the destination' ) parser_template.add_argument('envid', help='environment specification id') parser_template.add_argument('dest', help='destination for template download') #sub parser for getting environment by language name parser_env = subparsers.add_parser( 'environment', help='environment <language> gets environment info by language') parser_env.add_argument('language', help='supported language name') #sub parser for listing languages subparsers.add_parser('languages', help='lists supported languages') #sub parser for builds parser_builds = subparsers.add_parser( 'builds', help='builds <user> <algo> gets build logs for algorithm') parser_builds.add_argument('user') parser_builds.add_argument('algo', help='algorithm name') #sub parser for help subparsers.add_parser('help') parser.add_argument('--profile', action='store', type=str, default='default') #sub parser for freeze subparsers.add_parser( 'freeze', help= "freezes a model_manifest.json file into a model_manifest.json.freeze") args = parser.parse_args() #run auth before trying to create a client if args.cmd == 'auth': print("Configuring authentication for profile: " + args.profile) APIaddress = input("enter API address [https://api.algorithmia.com]: ") APIkey = input("enter API key: ") CACert = input('(optional) enter path to custom CA certificate: ') if APIaddress == "" or not APIaddress.startswith("https://api."): print("invalid API address") else: if len(APIkey) == 28 and APIkey.startswith("sim"): CLI().auth(apikey=APIkey, apiaddress=APIaddress, cacert=CACert, profile=args.profile) else: jwt = re.compile( r"^([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-\+\/=]*)" ) Bearer = input("enter JWT token: ") if jwt.match(Bearer): CLI().auth(apikey=APIkey, bearer=Bearer, apiaddress=APIaddress, cacert=CACert, profile=args.profile) else: print("invalid authentication") if args.cmd == 'help': parser.parse_args(['-h']) #create a client with the appropreate api address and key client = CLI().getClient(args.profile) if args.cmd == 'run': print(CLI().runalgo(args, client)) elif args.cmd == 'clone': algo_name = args.algo print("cloning src for " + algo_name) if CLI().getAPIaddress(args.profile) == None: exitcode = os.system("git clone https://git.algorithmia.com/git/" + algo_name + ".git") else: #replace https://api.<domain> with https://git.<domain> exitcode = os.system("git clone " + (CLI().getAPIaddress( args.profile).replace("//api.", "//git.")) + "/git/" + algo_name + ".git") if exitcode != 0: print("failed to clone\nis git installed?") elif args.cmd == 'ls': print(CLI().ls(args.path, client, args.long)) elif args.cmd == 'mkdir': CLI().mkdir(args.path, client) elif args.cmd == 'rmdir': CLI().rmdir(args.path, client, args.force) elif args.cmd == 'rm': CLI().rm(args.path, client) elif args.cmd == 'cp': CLI().cp(args.src, args.dest, client) elif args.cmd == 'cat': print(CLI().cat(args.path, client)) elif args.cmd == 'languages': response = CLI().list_languages(client) for line in response: print(line) elif args.cmd == 'template': CLI().get_template(args.envid, args.dest, client) elif args.cmd == 'environment': response = CLI().get_environment_by_language(args.language, client) print(response) elif args.cmd == 'builds': print(CLI().getBuildLogs(args.user, args.algo, client)) elif args.cmd == "freeze": print(CLI().freezeAlgo(client)) else: parser.parse_args(['-h'])
def test_get_environment(self): result = CLI().get_environment_by_language("python2", self.client) print(result) if ("error" in result): print(result) self.assertTrue(result is not None and "display_name" in result)
def test_cp_R2L(self): src = ["data://.my/moredata/test.txt"] dest = "./../test.txt" CLI().cp(src, dest, self.client) self.assertTrue(os.path.isfile(dest))
def test_list_languages(self): result = CLI().list_languages(self.client) if ("error" in result[0]): print(result) self.assertTrue(result is not None and "anaconda3" in result[1])
def main(): parser = argparse.ArgumentParser( 'algo', description="algo [<cmd>] [options] [<args>...] [--help] [--profile]") subparsers = parser.add_subparsers(help='sub cmd', dest='cmd') parser_auth = subparsers.add_parser( 'auth', help='save api key and api address for profile') parser_auth.add_argument('--profile', action='store', type=str, default='default') parser_clone = subparsers.add_parser( 'clone', help='clone <algo> clone the algorithm repository') parser_clone.add_argument('algo') parser_clone.add_argument('--profile', action='store', type=str, default='default') #parse options for the run command parser_run = subparsers.add_parser( 'run', help= 'algo run <algo> [input options] <args..> [output options] run an algorithm' ) parser_run.add_argument('algo') parser_run.add_argument('-d', '--data', action='store', help='detect input type', default=None) parser_run.add_argument('-t', '--text', action='store', help='treat input as text', default=None) parser_run.add_argument('-j', '--json', action='store', help='treat input as json data', default=None) parser_run.add_argument('-b', '--binary', action='store', help='treat input as binary data', default=None) parser_run.add_argument('-D', '--data-file', action='store', help='specify a path to an input file', default=None) parser_run.add_argument('-T', '--text-file', action='store', help='specify a path to a text file', default=None) parser_run.add_argument('-J', '--json-file', action='store', help='specify a path to a json file', default=None) parser_run.add_argument('-B', '--binary-file', action='store', help='specify a path to a binary file', default=None) parser_run.add_argument('--timeout', action='store', type=int, default=300, help='specify a timeout (seconds)') parser_run.add_argument( '--debug', action='store_true', help='print the stdout from the algo <this only works for the owner>') parser_run.add_argument('--profile', action='store', type=str, default='default') parser_run.add_argument('-o', '--output', action='store', default=None, type=str) #subparser for ls parser_ls = subparsers.add_parser( 'ls', help='ls [-l] [directory] list the contents of a directory', ) parser_ls.add_argument('-l', '--long', action='store_true') parser_ls.add_argument('path', nargs='?', default=None) parser_ls.add_argument('--profile', action='store', type=str, default='default') #subparser for rm parser_rm = subparsers.add_parser( 'rm', help='rm <path> remove a file', ) parser_rm.add_argument('path', nargs='?', default=None) parser_rm.add_argument('--profile', action='store', type=str, default='default') #subparser for mkdir parser_mkdir = subparsers.add_parser( 'mkdir', help='mkdir <directory> create a directory') parser_mkdir.add_argument('path', help='directory to create') parser_mkdir.add_argument('--profile', action='store', type=str, default='default') #subparser for rmdir parser_rmdir = subparsers.add_parser( 'rmdir', help='rmdir [-f] <directory> remove a directory') parser_rmdir.add_argument( '-f', '--force', action='store_true', help='force directory removal if it is not empty') parser_rmdir.add_argument('path', help='directory to remove') parser_rmdir.add_argument('--profile', action='store', type=str, default='default') #subparser for cp parser_cp = subparsers.add_parser( 'cp', help='cp <src,...> <dest> copy file(s) to the destination', ) parser_cp.add_argument('src', nargs='*', type=str, help='file(s) to be copied') parser_cp.add_argument('dest', help='destination for file(s) to be copied to') parser_cp.add_argument('--profile', action='store', type=str, default='default') #sub parser for cat parser_cat = subparsers.add_parser( 'cat', help='cat <path,...> concatenate and print file(s)') parser_cat.add_argument('path', nargs='*', help='file(s) to concatenate and print') parser_cat.add_argument('--profile', action='store', type=str, default='default') subparsers.add_parser('help') parser.add_argument('--profile', action='store', type=str, default='default') args = parser.parse_args() #run auth before trying to create a client if args.cmd == 'auth': print("Configuring authentication for profile: " + args.profile) APIaddress = input("enter API address [https://api.algorithmia.com]: ") APIkey = input("enter API key: ") if len(APIkey) == 28 and APIkey.startswith("sim"): if APIaddress == "" or not APIaddress.startswith("https://api."): APIaddress = "https://api.algorithmia.com" CLI().auth(APIkey, APIaddress, args.profile) else: print("invalid api key") if args.cmd == 'help': parser.parse_args(['-h']) #create a client with the appropreate api address and key client = Algorithmia.client() if len(CLI().getAPIaddress(args.profile)) > 1: client = Algorithmia.client(CLI().getAPIkey(args.profile), CLI().getAPIaddress(args.profile)) else: client = Algorithmia.client(CLI().getAPIkey(args.profile)) if args.cmd == 'run': print(CLI().runalgo(args, client)) elif args.cmd == 'clone': algo_name = args.algo print("cloning src for " + algo_name) if CLI().getAPIaddress(args.profile) == None: exitcode = os.system("git clone https://git.algorithmia.com/git/" + algo_name + ".git") else: #replace https://api.<domain> with https://git.<domain> exitcode = os.system("git clone " + (CLI().getAPIaddress( args.profile).replace("//api.", "//git.")) + "/git/" + algo_name + ".git") if exitcode != 0: print("failed to clone\nis git installed?") elif args.cmd == 'ls': print(CLI().ls(args.path, client, args.long)) elif args.cmd == 'mkdir': CLI().mkdir(args.path, client) elif args.cmd == 'rmdir': CLI().rmdir(args.path, client, args.force) elif args.cmd == 'rm': CLI().rm(args.path, client) elif args.cmd == 'cp': CLI().cp(args.src, args.dest, client) elif args.cmd == 'cat': print(CLI().cat(args.path, client)) else: parser.parse_args(['-h'])
def setUp(self): # create a directory to use in testing the cp command self.client = Algorithmia.client('simdylfCeXZ8/MgaQzokUHlalWm1') CLI().mkdir("data://.my/moredata", self.client) if (not os.path.exists("./TestFiles/")): os.mkdir("./TestFiles/")