Skip to content

Commit 520c324

Browse files
committed
Merge pull request #44 from iron-io/log-processing-fixes
log parsing and test result awaiting/processing rewritten
2 parents f36fc41 + 5ca8531 commit 520c324

File tree

7 files changed

+468
-214
lines changed

7 files changed

+468
-214
lines changed

test-suite/glide.lock

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-suite/iron.go

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import (
55
"bytes"
66
"encoding/json"
77
"fmt"
8-
"io"
98
"log"
109
"os"
1110
"regexp"
1211
"strings"
13-
"sync"
1412
"time"
1513

1614
"github.com/iron-io/iron_go3/worker"
@@ -37,31 +35,32 @@ func cleanIronGeneric(output []byte) []byte {
3735
return output
3836
}
3937

40-
func cleanPython27IronOutput(output string) (string, error) {
38+
func cleanIronTaskIdAndTimestamp(output string) (string, error) {
4139
var buf bytes.Buffer
42-
var requestId string = ""
40+
var taskId string = ""
4341
// expecting request id as hex of bson_id
4442
requestIdPattern, _ := regexp.Compile("[a-f0-9]{24}")
4543
scanner := bufio.NewScanner(strings.NewReader(output))
4644
for scanner.Scan() {
4745
line := scanner.Text()
4846

49-
if requestId == "" {
47+
if taskId == "" {
5048
parts := strings.Fields(line)
5149

5250
// generic logging through logger.info, logger.warning & etc
5351
if len(parts) >= 3 {
5452
requestIdCandidate := parts[2]
5553
if requestIdPattern.MatchString(requestIdCandidate) {
56-
requestId = requestIdCandidate
54+
taskId = requestIdCandidate
5755
}
5856
}
5957
}
6058

61-
line = util.RemoveTimestampAndRequestIdFromLogLine(line, requestId)
62-
63-
buf.WriteString(line)
64-
buf.WriteRune('\n')
59+
line, isOk := util.RemoveTimestampAndRequestIdFromIronLogLine(line, taskId)
60+
if isOk {
61+
buf.WriteString(line)
62+
buf.WriteRune('\n')
63+
}
6564
if err := scanner.Err(); err != nil {
6665
return "", err
6766
}
@@ -70,57 +69,77 @@ func cleanPython27IronOutput(output string) (string, error) {
7069
return buf.String(), nil
7170
}
7271

73-
func cleanIron(runtime string, output []byte) ([]byte, error) {
72+
func cleanIron(output []byte) ([]byte, error) {
7473
output = cleanIronGeneric(output)
75-
switch runtime {
76-
case "python2.7":
77-
cleaned, err := cleanPython27IronOutput(string(output))
78-
return []byte(cleaned), err
79-
default:
80-
return output, nil
81-
}
74+
cleaned, err := cleanIronTaskIdAndTimestamp(string(output))
75+
return []byte(cleaned), err
8276
}
8377

84-
func runOnIron(w *worker.Worker, wg *sync.WaitGroup, test *util.TestDescription, result chan<- io.Reader) {
85-
var imagePrefix string
86-
if imagePrefix = os.Getenv("IRON_LAMBDA_TEST_IMAGE_PREFIX"); imagePrefix == "" {
87-
log.Fatalf("IRON_LAMBDA_TEST_IMAGE_PREFIX not set")
88-
}
78+
//Returns a result and a debug channels. The channels are closed on test run finalization
79+
func runOnIron(w *worker.Worker, test *util.TestDescription) (<-chan string, <-chan string) {
80+
result := make(chan string, 1)
81+
debug := make(chan string, 1)
82+
go func() {
83+
defer close(result)
84+
defer close(debug)
85+
var imagePrefix string
86+
if imagePrefix = os.Getenv("IRON_LAMBDA_TEST_IMAGE_PREFIX"); imagePrefix == "" {
87+
log.Fatalf("IRON_LAMBDA_TEST_IMAGE_PREFIX not set")
88+
}
8989

90-
var output bytes.Buffer
91-
defer func() {
92-
result <- &output
93-
wg.Done()
94-
}()
90+
payload, _ := json.Marshal(test.Event)
91+
timeout := time.Duration(test.Timeout) * time.Second
9592

96-
payload, _ := json.Marshal(test.Event)
97-
timeout := time.Duration(test.Timeout) * time.Second
93+
debug <- "Enqueuing the task"
94+
taskids, err := w.TaskQueue(worker.Task{
95+
Cluster: "internal",
96+
CodeName: fmt.Sprintf("%s/%s", imagePrefix, test.Name),
97+
Payload: string(payload),
98+
Timeout: &timeout,
99+
})
98100

99-
taskids, err := w.TaskQueue(worker.Task{
100-
Cluster: "internal",
101-
CodeName: fmt.Sprintf("%s/%s", imagePrefix, test.Name),
102-
Payload: string(payload),
103-
Timeout: &timeout,
104-
})
101+
if err != nil {
102+
debug <- fmt.Sprintf("Error queueing task %s", err)
103+
return
104+
}
105105

106-
if err != nil {
107-
output.WriteString(fmt.Sprintf("Error queueing task %s %s", test.Name, err))
108-
return
109-
}
106+
if len(taskids) < 1 {
107+
debug <- "Something went wrong, empty taskids list"
108+
return
109+
}
110110

111-
if len(taskids) < 1 {
112-
output.WriteString(fmt.Sprintf("Something went wrong, empty taskids list", test.Name))
113-
return
114-
}
111+
end := time.After(timeout)
112+
taskid := taskids[0]
113+
debug <- fmt.Sprintf("Task Id: %s", taskid)
115114

116-
taskid := taskids[0]
115+
debug <- "Waiting for task"
116+
select {
117+
case <-w.WaitForTask(taskid):
118+
case <-end:
119+
debug <- fmt.Sprintf("Task timed out %s", taskid)
120+
return
121+
}
117122

118-
<-w.WaitForTask(taskid)
119-
iron_log := <-w.WaitForTaskLog(taskid)
120-
cleanedLog, err := cleanIron(test.Runtime, iron_log)
121-
if err != nil {
122-
output.WriteString(fmt.Sprintf("Error processing a log for task %s %s", test.Name, err))
123-
} else {
124-
output.Write(cleanedLog)
125-
}
123+
var iron_log []byte
124+
debug <- "Waiting for task log"
125+
select {
126+
case _iron_log, wait_log_ok := <-w.WaitForTaskLog(taskid):
127+
if !wait_log_ok {
128+
debug <- fmt.Sprintf("Something went wrong, no task log %s", taskid)
129+
return
130+
}
131+
iron_log = _iron_log
132+
case <-end:
133+
debug <- fmt.Sprintf("Task timed out to get task log or the log is empty %s", taskid)
134+
return
135+
}
136+
137+
cleanedLog, err := cleanIron(iron_log)
138+
if err != nil {
139+
debug <- fmt.Sprintf("Error processing a log %s", test.Name)
140+
} else {
141+
result <- string(cleanedLog)
142+
}
143+
}()
144+
return result, debug
126145
}

0 commit comments

Comments
 (0)