-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCLI_test.py
More file actions
222 lines (167 loc) · 6.59 KB
/
CLI_test.py
File metadata and controls
222 lines (167 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import sys
# look in ../ BEFORE trying to import Algorithmia. If you append to the
# you will load the version installed on the computer.
sys.path = ['../'] + sys.path
import unittest
import os
import json
import Algorithmia
from Algorithmia.CLI import CLI
import argparse
import shutil
class CLITest(unittest.TestCase):
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 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_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_rmdir(self):
parentDir = "data://.my/"
newDir = "testRmdir"
CLI().mkdir(parentDir+newDir, self.client)
result = CLI().ls(parentDir, self.client)
self.assertTrue(newDir in result)
CLI().rmdir(parentDir+newDir, self.client)
result = CLI().ls(parentDir, self.client)
self.assertTrue(newDir not 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_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)
#local to remote
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)
#remote to remote
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)
#remote to local
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_run(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.client)
self.assertEqual(result, inputs)
def test_auth(self):
#key for test account
key = os.getenv('ALGORITHMIA_API_KEY')
address = 'apiAddress'
profile = 'default'
CLI().auth(key,address,profile=profile)
resultK = CLI().getAPIkey(profile)
resultA = CLI().getAPIaddress(profile)
self.assertEqual(resultK, key)
self.assertEqual(resultA, address)
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 = 'apiAddress'
cacert = localfile
profile = 'test'
CLI().auth(key,address,cacert,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 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 "Python 2.7" in result)
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 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)
if __name__ == '__main__':
unittest.main()