forked from joshzarrabi/concourse-smoke-tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_test.go
137 lines (113 loc) · 3.21 KB
/
init_test.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
// Heavily inspired by:
// https://github.com/concourse/concourse/blob/master/testflight/suite_test.go
package smoke_tests
import (
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
yaml "gopkg.in/yaml.v2"
)
const flyTarget = "st"
var source ConcourseSource
func TestSmoke(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "concourse-smoke-tests")
}
type LocalUser struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type ConcourseSource struct {
ExternalURL string `yaml:"external_url"`
LocalUser LocalUser `yaml:"local_user"`
SkipSSLValidation bool `yaml:"skip_ssl_validation"`
FlyPath string `yaml:"fly_path"`
}
var _ = BeforeSuite(func() {
sourceFile := os.Getenv("CONCOURSE_SOURCE_FILE")
if _, err := os.Stat(sourceFile); !os.IsNotExist(err) {
source = ConcourseSource{}
sourceYml, err := ioutil.ReadFile(sourceFile)
Expect(err).ToNot(HaveOccurred())
err = yaml.Unmarshal(sourceYml, &source)
Expect(err).ToNot(HaveOccurred())
} else {
source = ConcourseSource{
ExternalURL: os.Getenv("CONCOURSE_URL"),
LocalUser: LocalUser{
Username: os.Getenv("CONCOURSE_USERNAME"),
Password: os.Getenv("CONCOURSE_PASSWORD"),
},
FlyPath: os.Getenv("FLY_PATH"),
}
}
skipSSLValidation, err := strconv.ParseBool(os.Getenv("FLY_SKIP_SSL"))
if err != nil {
skipSSLValidation = false
}
source.SkipSSLValidation = skipSSLValidation
if source.ExternalURL == "" {
Fail("CONCOURSE_URL is a required paramter")
}
if source.FlyPath == "" {
source.FlyPath = "fly"
}
})
var _ = SynchronizedAfterSuite(func() {
}, func() {
gexec.CleanupBuildArtifacts()
})
func spawn(argc string, argv ...string) *gexec.Session {
By("running: " + argc + " " + strings.Join(argv, " "))
cmd := exec.Command(argc, argv...)
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
return session
}
func spawnFly(argv ...string) *gexec.Session {
return spawn(source.FlyPath, append([]string{"-t", flyTarget}, argv...)...)
}
func fly(argv ...string) *gexec.Session {
sess := spawnFly(argv...)
wait(sess)
return sess
}
func wait(session *gexec.Session) {
<-session.Exited
Expect(session.ExitCode()).To(Equal(0))
}
func spawnFlyLogin(args ...string) *gexec.Session {
extraArgs := []string{"login", "-c", source.ExternalURL, "-u", source.LocalUser.Username, "-p", source.LocalUser.Password}
if source.SkipSSLValidation {
extraArgs = append(extraArgs, "-k")
}
return spawnFly(append(extraArgs, args...)...)
}
func waitForBuildAndWatch(jobName string, buildName ...string) *gexec.Session {
args := []string{"watch", "-j", jobName}
if len(buildName) > 0 {
args = append(args, "-b", buildName[0])
}
keepPollingCheck := regexp.MustCompile("job has no builds|build not found|failed to get build")
for {
session := spawnFly(args...)
<-session.Exited
if session.ExitCode() == 1 {
output := strings.TrimSpace(string(session.Err.Contents()))
if keepPollingCheck.MatchString(output) {
// build hasn't started yet; keep polling
time.Sleep(time.Second)
continue
}
}
return session
}
}