forked from getgauge/gauge
/
runner.go
239 lines (212 loc) · 6.11 KB
/
runner.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/getgauge/common"
"github.com/getgauge/gauge/config"
"github.com/getgauge/gauge/conn"
"github.com/getgauge/gauge/gauge_messages"
"github.com/getgauge/gauge/version"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
type testRunner struct {
cmd *exec.Cmd
connection net.Conn
errorChannel chan error
}
type runner struct {
Id string
Name string
Version string
Description string
Run struct {
Windows []string
Linux []string
Darwin []string
}
Init struct {
Windows []string
Linux []string
Darwin []string
}
Lib string
GaugeVersionSupport versionSupport
}
func executeInitHookForRunner(language string) error {
if err := config.SetProjectRoot([]string{}); err != nil {
return err
}
runnerInfo, err := getRunnerInfo(language)
if err != nil {
return err
}
command := []string{}
switch runtime.GOOS {
case "windows":
command = runnerInfo.Init.Windows
break
case "darwin":
command = runnerInfo.Init.Darwin
break
default:
command = runnerInfo.Init.Linux
break
}
languageJsonFilePath, err := common.GetLanguageJSONFilePath(language)
runnerDir := filepath.Dir(languageJsonFilePath)
cmd, err := common.ExecuteCommand(command, runnerDir, os.Stdout, os.Stderr)
if err != nil {
return err
}
return cmd.Wait()
}
func getRunnerInfo(language string) (*runner, error) {
runnerInfo := new(runner)
languageJsonFilePath, err := common.GetLanguageJSONFilePath(language)
if err != nil {
return nil, err
}
contents, err := common.ReadFileContents(languageJsonFilePath)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(contents), &runnerInfo)
if err != nil {
return nil, err
}
return runnerInfo, nil
}
func (testRunner *testRunner) kill(writer executionLogger) error {
if testRunner.isStillRunning() {
defer testRunner.connection.Close()
testRunner.sendProcessKillMessage()
exited := make(chan bool, 1)
go func() {
for {
if testRunner.isStillRunning() {
time.Sleep(100 * time.Millisecond)
} else {
exited <- true
return
}
}
}()
select {
case done := <-exited:
if done {
return nil
}
case <-time.After(config.PluginKillTimeout()):
writer.Warning("Killing runner with PID:%d forcefully\n", testRunner.cmd.Process.Pid)
return testRunner.killRunner()
}
}
return nil
}
func (testRunner *testRunner) killRunner() error {
return testRunner.cmd.Process.Kill()
}
func (testRunner *testRunner) isStillRunning() bool {
return !(testRunner == nil) && !(testRunner.cmd == nil) && (testRunner.cmd.ProcessState == nil || !testRunner.cmd.ProcessState.Exited())
}
func (testRunner *testRunner) sendProcessKillMessage() {
id := common.GetUniqueId()
message := &gauge_messages.Message{MessageId: &id, MessageType: gauge_messages.Message_KillProcessRequest.Enum(),
KillProcessRequest: &gauge_messages.KillProcessRequest{}}
conn.WriteGaugeMessage(message, testRunner.connection)
}
// Looks for a runner configuration inside the runner directory
// finds the runner configuration matching to the manifest and executes the commands for the current OS
func startRunner(manifest *manifest, port string, writer executionLogger) (*testRunner, error) {
var r runner
runnerDir, err := getLanguageJSONFilePath(manifest, &r)
if err != nil {
return nil, err
}
compatibilityErr := checkCompatibility(version.CurrentGaugeVersion, &r.GaugeVersionSupport)
if compatibilityErr != nil {
return nil, errors.New(fmt.Sprintf("Compatible runner version to %s not found. To update plugin, run `gauge --update {pluginName}`.", version.CurrentGaugeVersion))
}
command := getOsSpecificCommand(r)
env := getCleanEnv(port, os.Environ())
cmd, err := common.ExecuteCommandWithEnv(command, runnerDir, writer, writer, env)
if err != nil {
return nil, err
}
// Wait for the process to exit so we will get a detailed error message
errChannel := make(chan error)
waitAndGetErrorMessage(errChannel, cmd, writer)
return &testRunner{cmd: cmd, errorChannel: errChannel}, nil
}
func getLanguageJSONFilePath(manifest *manifest, r *runner) (string, error) {
languageJsonFilePath, err := common.GetLanguageJSONFilePath(manifest.Language)
if err != nil {
return "", err
}
contents, err := common.ReadFileContents(languageJsonFilePath)
if err != nil {
return "", err
}
err = json.Unmarshal([]byte(contents), r)
if err != nil {
return "", err
}
return filepath.Dir(languageJsonFilePath), nil
}
func waitAndGetErrorMessage(errChannel chan error, cmd *exec.Cmd, writer executionLogger) {
go func() {
err := cmd.Wait()
if err != nil {
writer.Debug("Runner exited with error: %s", err)
errChannel <- errors.New(fmt.Sprintf("Runner exited with error: %s\n", err.Error()))
}
}()
}
func getCleanEnv(port string, env []string) []string {
//clear environment variable common.GaugeInternalPortEnvName
isPresent := false
for i, k := range env {
if strings.TrimSpace(strings.Split(k, "=")[0]) == common.GaugeInternalPortEnvName {
isPresent = true
env[i] = common.GaugeInternalPortEnvName + "=" + port
}
}
if !isPresent {
env = append(env, common.GaugeInternalPortEnvName+"="+port)
}
return env
}
func getOsSpecificCommand(r runner) []string {
command := []string{}
switch runtime.GOOS {
case "windows":
command = r.Run.Windows
break
case "darwin":
command = r.Run.Darwin
break
default:
command = r.Run.Linux
break
}
return command
}