This repository was archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
113 lines (97 loc) · 3.89 KB
/
main_test.go
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
package main
import (
"encoding/xml"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"os"
"strings"
"testing"
)
// TestGofetch tests all of GoFetch features with dummy datasets hosted on S3.
func TestGofetch(t *testing.T) {
testGofetch = true
// Setting some environment variables.
testSettings := map[string]string{"MAX_CPUS": "1", "AWS_STORAGE_BUCKET_NAME": "example-bucket",
"LOG_LEVEL": "DEBUG", "AWS_CONFIG_FILE": "/gofetch/test_data/test_config_nominal.xml", "FETCH_ID": "1",
"FETCH_OFFSET": "0", "FETCH_LIMIT": "10"}
for env, val := range testSettings {
err := os.Setenv(env, val)
if err != nil {
panic(fmt.Errorf("could not set %s to %s", env, val))
}
log.Debug("Set envvar %s to %s.", env, val)
}
Convey("With dummy data, check that all output is nominal and reset S3 test folder", t, func() {
bucket := S3BucketFromOS()
// Expectations
expChecksumPath := []string{"/gofetch/test_data/index/sha384_checksum/a6cd5b1ffba20c8789d770c3ed80263947c4f13510fb2d63cb1c96043e25f2b4f5b0b40f8f91c21606cec409cb376dfb",
"/gofetch/test_data/index/sha384_checksum/3c0edeebdfb207f6b233368a06bfd612e00a19afa9ea0468ecad85c9f245c1b5f9c5dff62408d4374fa975aaf3a87b64"}
expContentPath := []string{"/gofetch/test_data/sha384_content/3c0edeebdfb207f6b233368a06bfd612e00a19afa9ea0468ecad85c9f245c1b5f9c5dff62408d4374fa975aaf3a87b64",
"/gofetch/test_data/sha384_content/a6cd5b1ffba20c8789d770c3ed80263947c4f13510fb2d63cb1c96043e25f2b4f5b0b40f8f91c21606cec409cb376dfb"}
expIndexLinks := []string{"http://example-bucket.s3.amazonaws.com/gofetch/test_data/feeds/apa-journals-pas.xml",
"http://example-bucket.s3.amazonaws.com/gofetch/test_data/feeds/dydan1.xml"}
logFile := logFilePath()
defer func() {
// Let's delete the test data as a defer of this test.
for pi := range expChecksumPath {
bucket.Del(expChecksumPath[pi])
}
for pi := range expContentPath {
bucket.Del(expContentPath[pi])
}
bucket.Del(logFile)
}()
main()
// Let's grab the log file.
logBody, notFoundErr := bucket.Get(logFile)
if notFoundErr != nil {
panic(notFoundErr)
}
log := Fetches{}
xmlErr := xml.Unmarshal(logBody, &log)
if xmlErr != nil {
// Oops, couldn't read the configuration file! Gotta panic now!
panic(xmlErr)
}
So(log.Meta.Report.Novel, ShouldEqual, 2)
So(log.Meta.Report.Errors, ShouldEqual, 1)
So(log.Meta.Report.Total, ShouldEqual, 4)
So(len(log.FetchError), ShouldEqual, 1)
So(log.FetchError[0].Original, ShouldEqual, "http:/some.invalid.com/link")
So(log.FetchError[0].Cleaned, ShouldEqual, "http:/some.invalid.com/link")
for fid := range log.Fetch {
fetch := log.Fetch[fid]
So(fetch.Parser, ShouldEqual, "RawArticle")
So(fetch.ChecksumIndex.Bucket, ShouldEqual, S3BucketFromOS().Name)
So(fetch.ChecksumIndex.Path, ShouldBeIn, expChecksumPath)
So(fetch.S3Content.Bucket, ShouldEqual, S3BucketFromOS().Name)
So(fetch.S3Content.Path, ShouldBeIn, expContentPath)
// Let's load the index for this item and check its validity.
idxBody, notFoundErr := bucket.Get(fetch.ChecksumIndex.Path)
if notFoundErr != nil {
panic(notFoundErr)
}
idxLines := strings.Split(string(idxBody), "\n")
// The index is always appended with an empty line.
So(len(idxLines), ShouldBeLessThanOrEqualTo, 3)
for lno := range idxLines {
if idxLines[lno] == "" {
continue // Last line of index.
}
rows := strings.Fields(idxLines[lno])
So(len(rows), ShouldEqual, 6)
So(rows[0], ShouldBeIn, expContentPath)
So(rows[1], ShouldBeIn, expIndexLinks)
So(rows[5], ShouldEqual, "RawArticle")
}
}
})
Convey("With empty config file", t, func() {
os.Setenv("AWS_CONFIG_FILE", "/gofetch/test_data/test_config_empty.xml")
So(main, ShouldPanic)
})
Convey("With an invalid throttling duration in config file", t, func() {
os.Setenv("AWS_CONFIG_FILE", "/gofetch/test_data/test_config_invalid_duration.xml")
So(main, ShouldPanic)
})
}