-
Notifications
You must be signed in to change notification settings - Fork 1
/
execution.go
124 lines (103 loc) · 2.97 KB
/
execution.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
114
115
116
117
118
119
120
121
122
123
124
// This file is part of twist
package main
import (
"code.google.com/p/goprotobuf/proto"
"fmt"
"io/ioutil"
"net"
"os"
)
type execution struct {
tokens []*token
manifest *manifest
connection net.Conn
}
func newExecution(manifest *manifest, tokens []*token, conn net.Conn) *execution {
e := execution{manifest: manifest, tokens: tokens, connection: conn}
return &e
}
func (e *execution) startScenarioExecution() error {
message := &Message{MessageType: Message_ExecutionStarting.Enum(),
ExecutionStartingRequest: &ExecutionStartingRequest{ScenarioFile: proto.String("sample.sc")}}
_, err := getResponse(e.connection, message)
if err != nil {
return err
}
return nil
}
func (e *execution) startStepExecution(token *token) (bool, error) {
message := &Message{MessageType: Message_ExecuteStep.Enum(),
ExecuteStepRequest: &ExecuteStepRequest{StepText: proto.String(token.value), Args: token.args}}
fmt.Printf("=> %s\n", token.line)
response, err := getResponse(e.connection, message)
if err != nil {
return false, err
}
if response.GetMessageType() == Message_ExecuteStepResponse {
stepResponse := response.GetExecuteStepResponse()
if stepResponse.GetPassed() != true {
ioutil.WriteFile("/tmp/twist-screenshot.png", stepResponse.GetScreenShot(), 0644)
fmt.Printf("=> \x1b[31;1m%s\n\x1b[0m", token.line)
fmt.Printf("\x1b[31;1m%s\n\x1b[0m", stepResponse.GetErrorMessage())
fmt.Printf("\x1b[31;1m%s\n\x1b[0m", stepResponse.GetStackTrace())
return false, nil
} else {
fmt.Printf("=> \x1b[32;1m%s\n\x1b[0m", token.line)
}
}
return true, nil
}
func (e *execution) validateStep(token *token) (bool, error) {
message := &Message{MessageType: Message_StepValidateRequest.Enum(),
StepValidateRequest: &StepValidateRequest{StepText: proto.String(token.value)}}
response, err := getResponse(e.connection, message)
if err != nil {
return false, err
}
if response.GetMessageType() == Message_StepValidateResponse {
validateResponse := response.GetStepValidateResponse()
return validateResponse.GetIsValid(), nil
} else {
panic("Expected a validate step response")
}
}
func (e *execution) stopScenarioExecution() error {
message := &Message{MessageType: Message_ExecutionEnding.Enum(),
ExecutionEndingRequest: &ExecutionEndingRequest{}}
_, err := getResponse(e.connection, message)
if err != nil {
return err
}
return nil
}
func (e *execution) start() error {
for _, token := range e.tokens {
var err error
quit := false
switch token.kind {
case typeScenario:
err = e.startScenarioExecution()
break
case typeWorkflowStep:
valid, err := e.validateStep(token)
if !valid {
fmt.Printf("Error. Unimplemented step: %s\n", token.line)
quit = true
err = err
} else {
passed, err := e.startStepExecution(token)
quit = !passed
err = err
}
break
}
if err != nil {
fmt.Printf("Failed to execute step. %s\n", err.Error())
os.Exit(1)
}
if quit {
break
}
}
return e.stopScenarioExecution()
}