-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_script.py
130 lines (106 loc) · 4.11 KB
/
test_script.py
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
from subprocess import PIPE, Popen , list2cmdline
import os
def run_cmd(command, should_print=False):
print("Running ", list2cmdline(command))
with Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False) as process:
(stdout, stderr) = process.communicate()
if should_print:
print("stderr:\n%s\nstdout:\n%s\n" % (stderr, stdout))
return process.wait()
def check_if_string_in_file(file_name, string_to_search):
""" Check if any line in the file contains given string """
# Open the file in read only mode
with open(file_name, 'r') as read_obj:
# Read all lines in the file one by one
for line in read_obj:
# For each line, check if line contains the string
if string_to_search in line:
return True
return False
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("#Test create.sh")
print("#Should fail, no parameters")
if run_cmd(["./create.sh"]) == 0:
print("#create.sh should have failed because no parameter")
exit(1)
print("#Should succeed, no spaces")
run_cmd(["./create.sh", "myUser"])
if os.path.isfile("./myUser"):
print("#Created the folder ./myUser correctly")
else:
print("#Did not create the user folder")
exit(1)
if os.path.isfile("./myUser/wall"):
print("#Created the wall file correctly")
else:
print("#Did not create the wall file")
exit(1)
if os.path.isfile("./myUser/friends"):
print("#Created the friends file correctly")
else:
print("#Did not create the friends file")
exit(1)
print("#Should succeed, spaces")
run_cmd(["./create.sh", "my User"])
if os.path.isfile("./my User"):
print("#Created the folder './my User' correctly")
else:
print("#Did not create the user folder")
exit(1)
if os.path.isfile("./my User/wall"):
print("#Created the wall file correctly")
else:
print("#Did not create the wall file")
exit(1)
if os.path.isfile("./my User/friends"):
print("#Created the friends file correctly")
else:
print("#Did not create the friends file")
exit(1)
print("#Should fail because user already exists")
if run_cmd(["./create.sh", "my User"]) == 0:
print("#create.sh should have failed as the user already exists")
exit(1)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("#Test add.sh")
print("#Should fail because not enough parameters")
if run_cmd(["./add.sh", "newUser"]) == 0:
print("add.sh should have failed because not enough parameters")
exit(1)
print("#Should fail because too many parameters")
if run_cmd(["./add.sh", "myUser", "my User", "shouldNotBeHere"]) == 0:
print("#add.sh should have failed because too many parameters")
exit(1)
print("#Should fail because user does not exist")
if run_cmd(["./add.sh", "userNotExists", "myUser"]) == 0:
print("#add.sh should have failed because user does not exist")
exit(1)
print("#Should fail because friend does not exist")
if run_cmd(["./add.sh", "myUser", "userNotExists"]) == 0:
print("#add.sh should have failed because friend does not exist")
exit(1)
print("#Should succeed")
run_cmd(["./add.sh", "myUser", "my User"])
if not check_if_string_in_file("my User", "./myUser/friends"):
print("#Did not add the friend to the friend list")
exit(1)
print("#Should fail because users are already friends")
if run_cmd(["./add.sh", "myUser", "my User"]) == 0:
print("#add.sh should have failed because users are already friends")
exit(1)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("#Test post.sh")
print("#Should succeed")
run_cmd("./post.sh", "myUser", "my User", "A test message")
if not check_if_string_in_file("my User: A test message", "./myUser/wall"):
print("#Did not add the message to the wall")
exit(1)
print("#Should fail because sender is not friend of receiver")
if run_cmd("./post.sh", "my User", "myUser", "A test message") == 0:
print("#post.sh should have failed because sender is not a friend of receiver")
exit(1)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("#Test show.sh")
if run_cmd("./show.sh", "myUser", True) != 0:
print("show.sh failed")
exit(1)