-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
168 lines (147 loc) · 5.05 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/artemnikitin/devicefarm-ci-tool/errors"
"github.com/artemnikitin/devicefarm-ci-tool/model"
"github.com/artemnikitin/devicefarm-ci-tool/service"
"github.com/artemnikitin/devicefarm-ci-tool/tools"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/devicefarm"
"github.com/aws/aws-sdk-go/service/devicefarm/devicefarmiface"
)
var (
project = flag.String("project", "", "Device Farm project name")
runName = flag.String("run", "", "Name of test run")
appPath = flag.String("app", "", "Path to an app")
testPath = flag.String("test", "", "Path to test app")
devicePool = flag.String("devices", "Top Devices", "Specify list of devices for tests")
randomDevicePool = flag.String("randomDevices", "", "List of device pools for random choice")
configJSON = flag.String("config", "", "Path to JSON config")
wait = flag.Bool("wait", false, "Wait for run end")
checkEvery = flag.Int("checkEvery", 5, "Specified time slice for checking status of run")
useRandomDevicePool = flag.Bool("useRandomDevicePool", false, "If enabled will select one of available device pools")
ignoreUnavailableDevices = flag.Bool("ignoreUnavailableDevices", false, "Consider test run where one of devices failed as green")
testType = flag.String("testType", "", "Type of tests to run")
)
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.Parse()
if *project == "" || *appPath == "" {
fmt.Println("Please specify correct parameters!")
fmt.Println("You should specify:")
fmt.Println("-project, name of a project in AWS Device Farm")
fmt.Println("-app, path to an app you want to run")
os.Exit(1)
}
failedTests, success := runJob(getAWSClient(), getConfig())
if !success {
if len(failedTests) == 0 {
log.Println(fmt.Sprintf("Test job fails, it looks like AWS/infrastructure/setup issue, check the report."))
} else {
log.Println(fmt.Sprintf("There are %d test fails, check it out!\n", len(failedTests)))
for i := 0; i < len(failedTests); i++ {
fmt.Println(failedTests[i].ToString())
}
}
os.Exit(1)
}
}
func runJob(client devicefarmiface.DeviceFarmAPI, config *model.RunConfig) ([]*model.FailedTest, bool) {
svc := &service.DeviceFarmRun{
Client: client,
Config: config,
Project: *project,
}
if svc.GetProjectArn() == "" {
log.Fatal("Application finished, because it can't retrieve project ARN")
}
if *useRandomDevicePool {
svc.SelectDevicePool(svc.ProjectArn)
}
svc.GetDevicePoolArn(svc.Config.DevicePoolName)
appArn, url := svc.CreateUpload(*appPath)
code := tools.UploadFile(*appPath, url)
if code != 200 {
log.Fatal("Can't upload an app to Device Farm")
}
svc.AppArn = appArn
svc.WaitForAppProcessed(svc.AppArn, *checkEvery)
runArn, status := svc.RunWithConfig()
statusCheck(status)
pass := true
var failedTests []*model.FailedTest
if *wait {
result := svc.WaitForRunEnds(runArn, *checkEvery)
if result != devicefarm.ExecutionResultPassed {
failedTests = svc.GetListOfFailedTests(runArn)
pass = makeBuildFailed(result, failedTests, *ignoreUnavailableDevices)
}
}
if *ignoreUnavailableDevices && *wait {
pass = svc.IsTestRunPassIgnoringUnavailableDevices(runArn)
}
printReportURL(runArn)
return failedTests, pass
}
func getConfig() *model.RunConfig {
configFile := &model.RunConfig{
Test: &devicefarm.ScheduleRunTest{},
}
if *configJSON != "" {
bytes, err := ioutil.ReadFile(*configJSON)
errors.Validate(err, "Can't read model file")
configFile = model.Transform(bytes)
}
if configFile.DevicePoolArn == "" && configFile.DevicePoolName == "" {
if *randomDevicePool != "" {
pools := strings.Split(*randomDevicePool, ",")
i := tools.Random(0, len(pools)-1)
configFile.DevicePoolName = pools[i]
} else {
configFile.DevicePoolName = *devicePool
}
}
if *runName != "" {
configFile.Name = *runName
}
if *testPath != "" {
configFile.TestPackagePath = *testPath
}
if *testType != "" {
configFile.Test.Type = aws.String(*testType)
}
return configFile
}
func getAWSClient() devicefarmiface.DeviceFarmAPI {
config := aws.NewConfig()
config.WithCredentials(credentials.NewEnvCredentials())
config.WithRegion("us-west-2")
ses, err := session.NewSession(config)
errors.Validate(err, "Can't create an AWS session")
return devicefarm.New(ses)
}
func statusCheck(status string) {
if status == devicefarm.ExecutionStatusScheduling {
log.Println("Job is scheduled!")
} else {
log.Println("Status =", status)
log.Fatal("Failed to start a job ...")
}
}
func makeBuildFailed(result string, failed []*model.FailedTest, option bool) bool {
testResult := false
if result == devicefarm.ExecutionResultErrored && len(failed) == 0 && option {
testResult = true
}
return testResult
}
func printReportURL(arn string) {
log.Println("Test report URL:", tools.GenerateReportURL(arn))
}