-
Notifications
You must be signed in to change notification settings - Fork 175
/
tests.sh
184 lines (104 loc) · 3.49 KB
/
tests.sh
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
#!/bin/bash
set -e
if [ -z $1 ]; then
echo "Stack name not passed, exiting"
exit 1
fi
STACK=$1
echo "$STACK"
function clean {
## Cleanup DB
for f in "${TEST_FILES[@]}"; do
FILE_NAME=$(cut -d "/" -f2- <<< "$f")
echo "Removing item from DB : $FILE_NAME"
aws dynamodb delete-item --table-name $DYNAMO_TABLE \
--key "{\"id\": {\"S\": \"s3://$BUCKET_IN/$FILE_NAME\"}}"
if [ $? -ne 0 ]; then
echo -e "Remove item failed" ; exit 1
fi
done
## Cleanup Output
for f in "${TEST_FILES[@]}"; do
FILE_NAME=$(cut -d "/" -f2- <<< "$f")
echo "Removing from output : ${FILE_NAME/md/html}"
aws s3 rm s3://"$BUCKET_OUT"/"${FILE_NAME/md/html}"
if [[ $? -ne 0 ]]; then
echo -e "File not processed" ; exit 1
fi
done
## Cleanup Input
for f in "${TEST_FILES[@]}"; do
FILE_NAME=$(cut -d "/" -f2- <<< "$f")
echo "Removing from input: $FILE_NAME"
aws s3 rm s3://"$BUCKET_IN"/"$FILE_NAME"
if [[ $? -ne 0 ]]; then
echo -e "File not processed" ; exit 1
fi
done
}
## Get Stack Resources
BUCKET_IN=$(aws cloudformation describe-stack-resource \
--stack-name "$STACK" --logical-resource-id 'InputBucket' \
--query "StackResourceDetail.PhysicalResourceId" \
--output text)
BUCKET_OUT=$(aws cloudformation describe-stack-resource \
--stack-name "$STACK" \
--logical-resource-id 'ConversionTargetBucket' \
--query "StackResourceDetail.PhysicalResourceId" \
--output text)
DYNAMO_TABLE=$(aws cloudformation describe-stack-resource \
--stack-name "$STACK" --logical-resource-id 'SentimentTable' \
--query "StackResourceDetail.PhysicalResourceId" \
--output text)
echo "Found Input Bucket: $BUCKET_IN"
echo "Found Ouput Bucket: $BUCKET_OUT"
echo "Found DynamoDB Table: $DYNAMO_TABLE"
## Get Samples
TEST_FILES=(tests/sample-*.md)
## Upload test samples
for f in "${TEST_FILES[@]}"; do
FILE_NAME=$(cut -d "/" -f2- <<< "$f")
echo "Upload sample : $FILE_NAME"
aws s3 cp $f s3://"$BUCKET_IN"/"$FILE_NAME"
if [ $? -ne 0 ]; then
echo -e "Upload Failed" ; exit 1
fi
done
# Give Lambda a chance to execute or wait for last file to get to output.
END=$((SECONDS+30))
echo "Waiting for execution"
while [ $SECONDS -lt $END ]
do
FILE_NAME=$(cut -d "/" -f2- <<< ${TEST_FILES[*]: -1})
EXISTS=$(aws s3api head-object --bucket $BUCKET_OUT --key ${FILE_NAME/md/html}) || NOT_EXIST=true
if [[ $EXISTS ]]; then
echo "File exists, continuing"
break
fi
echo "File doesn't exist yet... Waiting for timeout."
sleep 1
done
# Check for files in output bucket
for f in "${TEST_FILES[@]}"; do
FILE_NAME=$(cut -d "/" -f2- <<< "$f")
echo "Checking for Output : ${FILE_NAME/md/html}"
EXISTS=(aws s3api head-object --bucket $BUCKET_OUT --key ${FILE_NAME/md/html}) || $NOT_EXIST=true
if [[ -z $EXISTS ]]; then
echo -e "File ${FILE_NAME/md/html} not processed" ; exit 1
fi
echo "${FILE_NAME/md/html} found"
done
# Check DynamoDB for Sentiment
for f in "${TEST_FILES[@]}"; do
FILE_NAME=$(cut -d "/" -f2- <<< "$f")
echo "Checking for Sentiment : $FILE_NAME"
SENTIMENT=$(aws dynamodb get-item --table-name $DYNAMO_TABLE \
--projection-expression "overall_sentiment" \
--key "{\"id\": {\"S\": \"s3://$BUCKET_IN/$FILE_NAME\"}}")
if [[ -z $SENTIMENT ]]; then
echo -e "No Sentiment" ; exit 1
fi
echo $SENTIMENT
done
# Call Clean Up
clean