-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
109 lines (87 loc) · 2.37 KB
/
plugin.go
File metadata and controls
109 lines (87 loc) · 2.37 KB
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
package main
import (
"fmt"
"time"
"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"
)
// Plugin defines the Device farm plugin parameters.
type Plugin struct {
Key string
Secret string
Region string
YamlVerified bool
TestProject string
RunName string
}
// Exec runs the plugin
func (p *Plugin) Exec() error {
// create the configuration
conf := &aws.Config{
Region: aws.String(p.Region),
}
// Use key and secret if provided otherwise fall back to ec2 instance profile
if p.Key != "" && p.Secret != "" {
conf.Credentials = credentials.NewStaticCredentials(p.Key, p.Secret, "")
}
//create Device Farm service
svc := devicefarm.New(session.New(), conf)
//Get AWS device farm Test project
project, err := getTestProject(p.TestProject, svc)
if err != nil {
return err
}
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for range ticker.C {
//Get the run to see the status
run, err := getRun(p.RunName, project, svc)
if err != nil {
return err
}
if *run.Status == "COMPLETED" && (*run.Result == "ERRORED" || *run.Result == "FAILED") {
return fmt.Errorf("The test run has failed")
}
if *run.Status == "COMPLETED" && *run.Result == "PASSED" {
break
}
}
return nil
}
func getRun(runName string, project *devicefarm.Project, svc *devicefarm.DeviceFarm) (*devicefarm.Run, error) {
var result *devicefarm.ListRunsOutput
var listRunInput devicefarm.ListRunsInput
var err error
for {
listRunInput.Arn = aws.String(*project.Arn)
if result != nil && result.NextToken != nil {
listRunInput.NextToken = aws.String(*result.NextToken)
}
result, err = svc.ListRuns(&listRunInput)
if err != nil {
return nil, err
}
for _, run := range result.Runs {
if *run.Name == runName {
return run, nil
}
}
if result.NextToken == nil {
return nil, fmt.Errorf("There was no Run with the name %s", runName)
}
}
}
func getTestProject(testProjectName string, svc *devicefarm.DeviceFarm) (*devicefarm.Project, error) {
result, err := svc.ListProjects(nil)
if err != nil {
return nil, err
}
for _, project := range result.Projects {
if *project.Name == testProjectName {
return project, nil
}
}
return nil, fmt.Errorf("There was no project with the name %s", testProjectName)
}