/
step_verify_behavior.go
288 lines (253 loc) · 8.73 KB
/
step_verify_behavior.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package verify
import (
"strings"
"time"
"github.com/jenkins-x/jx/v2/pkg/tekton"
"github.com/jenkins-x/jx/v2/pkg/tekton/metapipeline"
"github.com/sirupsen/logrus"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/builds"
"github.com/jenkins-x/jx/v2/pkg/cmd/get"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/importcmd"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/start"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/spf13/cobra"
)
// BehaviorOptions contains the command line options
type BehaviorOptions struct {
*opts.CommonOptions
SourceGitURL string
Branch string
NoImport bool
CredentialsSecret string
GitOrganisation string
UseGoProxy bool
TestSuite string
}
var (
verifyBehaviorLong = templates.LongDesc(`
Verifies the cluster behaves correctly by running the BDD tests to verify we can create quickstarts, previews and promote applications.
`)
verifyBehaviorExample = templates.Examples(`
# runs the BDD tests on the current cluster to verify it behaves nicely
jx step verify behavior
`)
)
// NewCmdStepVerifyBehavior creates the command
func NewCmdStepVerifyBehavior(commonOpts *opts.CommonOptions) *cobra.Command {
options := &BehaviorOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "behavior [flags]",
Short: "Verifies the cluster behaves correctly by running the BDD tests to verify we can create quickstarts, previews and promote applications",
Long: verifyBehaviorLong,
Example: verifyBehaviorExample,
Aliases: []string{"tck", "bdd", "behavior", "behave"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.SourceGitURL, "git-url", "u", "https://github.com/jenkins-x/bdd-jx.git", "The git URL of the BDD tests pipeline")
cmd.Flags().StringVarP(&options.Branch, "branch", "", "master", "The git branch to use to run the BDD tests")
cmd.Flags().BoolVarP(&options.NoImport, "no-import", "", false, "Create the pipeline directly, don't import the repository")
cmd.Flags().StringVarP(&options.CredentialsSecret, "credentials-secret", "", "", "The name of the secret to generate the bdd credentials from, if not specified, the default git auth will be used")
cmd.Flags().StringVarP(&options.GitOrganisation, "git-organisation", "", "", "Override the git org for the tests rather than reading from teamSettings")
cmd.Flags().BoolVarP(&options.UseGoProxy, "use-go-proxy", "", false, "Enable the GoProxy for the bdd tests")
cmd.Flags().StringVarP(&options.TestSuite, "test-suite", "", "", "Override the default test suite ")
return cmd
}
// Run implements this command
func (o *BehaviorOptions) Run() error {
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
if o.NoImport {
owner := "jenkins-x"
repo := "bdd-jx"
err = o.runPipelineDirectly(owner, repo, o.SourceGitURL)
if err != nil {
return errors.Wrapf(err, "unable to run job directly %s/%s", owner, repo)
}
// let sleep a little bit to give things a head start
time.Sleep(time.Second * 3)
return o.followLogs(owner, repo)
}
list, err := jxClient.JenkinsV1().SourceRepositories(ns).List(metav1.ListOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to load SourceRepositories in namespace '%s'", ns)
}
gitInfo, err := gits.ParseGitURL(o.SourceGitURL)
if err != nil {
return errors.Wrapf(err, "failed to parse git URL '%s'", o.SourceGitURL)
}
sr, err := o.findSourceRepository(list.Items, o.SourceGitURL, gitInfo)
if err != nil {
return errors.Wrapf(err, "failed to find SourceRepository for URL '%s'", o.SourceGitURL)
}
owner := ""
repo := ""
trigger := true
if sr == nil {
err = o.importSourceRepository(gitInfo)
if err != nil {
return errors.Wrapf(err, "failed to import SourceRepository for URL '%s'", o.SourceGitURL)
}
trigger = false
} else {
owner = sr.Spec.Org
repo = sr.Spec.Repo
}
if owner == "" {
owner = gitInfo.Organisation
}
if owner == "" {
owner = gitInfo.Organisation
}
if trigger {
err = o.triggerPipeline(owner, repo)
if err != nil {
return errors.Wrapf(err, "failed to trigger Pipeline for URL '%s/%s'", owner, repo)
}
}
// let sleep a little bit to give things a head start
time.Sleep(time.Second * 3)
return o.followLogs(owner, repo)
}
func (o *BehaviorOptions) followLogs(owner string, repo string) error {
commonOptions := *o.CommonOptions
commonOptions.BatchMode = true
lo := &get.GetBuildLogsOptions{
Options: get.Options{
CommonOptions: &commonOptions,
},
Tail: true,
Wait: true,
FailIfPodFails: true,
BuildFilter: builds.BuildPodInfoFilter{
Owner: owner,
Repository: repo,
Branch: o.Branch,
},
}
return lo.Run()
}
func (o *BehaviorOptions) findSourceRepository(repositories []v1.SourceRepository, url string, gitInfo *gits.GitRepository) (*v1.SourceRepository, error) {
for i := range repositories {
repo := &repositories[i]
u2, _ := kube.GetRepositoryGitURL(repo)
if url == u2 || strings.TrimSuffix(url, ".git") == strings.TrimSuffix(u2, ".git") {
return repo, nil
}
}
for i := range repositories {
repo := &repositories[i]
if repo.Spec.Org == gitInfo.Organisation && repo.Spec.Repo == gitInfo.Name {
return repo, nil
}
}
return nil, nil
}
func (o *BehaviorOptions) importSourceRepository(gitInfo *gits.GitRepository) error {
log.Logger().Infof("importing project %s", util.ColorInfo(o.SourceGitURL))
io := &importcmd.ImportOptions{
CommonOptions: o.CommonOptions,
RepoURL: o.SourceGitURL,
DisableDraft: true,
DisableJenkinsfileCheck: true,
DisableMaven: true,
DisableWebhooks: true,
Organisation: gitInfo.Organisation,
Repository: gitInfo.Name,
AppName: gitInfo.Name,
}
err := io.Run()
if err != nil {
return errors.Wrapf(err, "failed to import project %s", o.SourceGitURL)
}
return nil
}
func (o *BehaviorOptions) triggerPipeline(owner string, repo string) error {
pipeline := owner + "/" + repo + "/" + o.Branch
log.Logger().Infof("triggering pipeline %s", util.ColorInfo(pipeline))
so := &start.StartPipelineOptions{
CommonOptions: o.CommonOptions,
Filter: pipeline,
Branch: o.Branch,
}
err := so.Run()
if err != nil {
return errors.Wrapf(err, "failed to start pipeline %s", pipeline)
}
return nil
}
func (o *BehaviorOptions) runPipelineDirectly(owner string, repo string, sourceURL string) error {
pullRefs := ""
branch := "master"
pullRefs = branch + ":"
kind := metapipeline.ReleasePipeline
sa := tekton.DefaultPipelineSA
l := logrus.WithFields(logrus.Fields(map[string]interface{}{
"Owner": owner,
"Name": repo,
"SourceURL": sourceURL,
"Branch": branch,
"PullRefs": pullRefs,
//"Job": job,
}))
l.Info("about to start Jenkinx X meta pipeline")
pullRefData := metapipeline.NewPullRef(sourceURL, branch, "")
envVars := map[string]string{}
if o.CredentialsSecret != "" {
envVars["JX_CREDENTIALS_FROM_SECRET"] = o.CredentialsSecret
}
if o.GitOrganisation != "" {
envVars["GIT_ORGANISATION"] = o.GitOrganisation
}
if o.UseGoProxy {
envVars["GOPROXY"] = "https://proxy.golang.org"
}
if o.TestSuite != "" {
envVars["SUITE"] = o.TestSuite
}
pipelineCreateParam := metapipeline.PipelineCreateParam{
PullRef: pullRefData,
PipelineKind: kind,
Context: "",
// No equivalent to https://github.com/jenkins-x/jx/blob/bb59278c2707e0e99b3c24be926745c324824388/pkg/cmd/controller/pipeline/pipelinerunner_controller.go#L236
// for getting environment variables from the prow job here, so far as I can tell (abayer)
// Also not finding an equivalent to labels from the PipelineRunRequest
ServiceAccount: sa,
// I believe we can use an empty string default image?
DefaultImage: "",
EnvVariables: envVars,
UseBranchAsRevision: true,
NoReleasePrepare: true,
}
c, err := metapipeline.NewMetaPipelineClient()
if err != nil {
return errors.Wrap(err, "unable to create metapipeline client")
}
pipelineActivity, tektonCRDs, err := c.Create(pipelineCreateParam)
if err != nil {
return errors.Wrap(err, "unable to create Tekton CRDs")
}
err = c.Apply(pipelineActivity, tektonCRDs)
if err != nil {
return errors.Wrap(err, "unable to apply Tekton CRDs")
}
return nil
}