-
Notifications
You must be signed in to change notification settings - Fork 30
/
fakes.go
103 lines (92 loc) · 3.29 KB
/
fakes.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
package testimports
import (
"strings"
"testing"
"github.com/jenkins-x-plugins/jx-project/pkg/cmd/importcmd"
fakescm "github.com/jenkins-x/go-scm/scm/driver/fake"
v1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
fakejx "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned/fake"
"github.com/jenkins-x/jx-helpers/v3/pkg/boot"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner/fakerunner"
fakeinput "github.com/jenkins-x/jx-helpers/v3/pkg/input/fake"
"github.com/jenkins-x/jx-helpers/v3/pkg/kube/jxenv"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)
const (
// PipelineUsername the pipeline bot user name used in tests
PipelineUsername = "my-pipeline-bot-user"
)
// SetFakeClients sets the fake clients on the options
func SetFakeClients(t *testing.T, o *importcmd.ImportOptions, realJXConvert bool) (*fakescm.Data, *v1.Environment, *fakerunner.FakeRunner) {
fakeInput := &fakeinput.FakeInput{
Values: map[string]string{},
}
o.Input = fakeInput
client, fakeScmData := fakescm.NewDefault()
o.ScmFactory.ScmClient = client
// lets add a dummy token so we can create authenticated git URLs
o.ScmFactory.GitToken = "my.fake.token"
o.ScmFactory.NoWriteGitCredentialsFile = true
ns := "jx"
devEnv := jxenv.CreateDefaultDevEnvironment(ns)
devEnv.Namespace = ns
devEnv.Spec.Source.URL = "https://fake.git/jenkins-x-labs-bdd-tests/jx3-gke-gsm"
devEnv.Spec.TeamSettings.PipelineUsername = PipelineUsername
o.KubeClient = fake.NewSimpleClientset(
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: boot.SecretName,
Namespace: "jx-git-operator",
},
Data: map[string][]byte{
"url": []byte("https://github.com/myorg/myclusterrepo"),
"username": []byte(PipelineUsername),
"password": []byte("dummy-pipeline-user-token"),
},
},
)
o.JXClient = fakejx.NewSimpleClientset(devEnv)
o.Namespace = ns
runner := NewFakeRunnerWithoutGitPush(t, realJXConvert)
o.CommandRunner = runner.Run
return fakeScmData, devEnv, runner
}
// NewFakeRunnerWithoutGitPush create a fake command runner that fakes out git push
func NewFakeRunnerWithoutGitPush(t *testing.T, realJXConvert bool) *fakerunner.FakeRunner {
runner := &fakerunner.FakeRunner{}
runner.CommandRunner = func(c *cmdrunner.Command) (string, error) {
if c.Name == "git" && len(c.Args) > 0 {
switch c.Args[0] {
case "clone":
// if we are cloning a fake URL lets switch to github
gitURL := c.Args[1]
if strings.HasPrefix(gitURL, "https://fake.git") {
c.Args[1] = "https://github.com" + strings.TrimPrefix(gitURL, "https://fake.git")
} else {
dummyServer := "@fake.git"
idx := strings.Index(gitURL, dummyServer)
if idx > 0 {
c.Args[1] = "https://github.com" + gitURL[idx+len(dummyServer):]
}
}
case "push":
// lets fake out git push
t.Logf("faking command: %s\n", c.CLI())
return "", nil
}
}
if c.Name == "jx" && len(c.Args) > 0 && c.Args[0] == "pipeline" {
if !realJXConvert || len(c.Args) < 2 || c.Args[1] != "convert" {
// lets fake out starting pipelines
t.Logf("faking command: %s\n", c.CLI())
return "", nil
}
}
// otherwise lets do it for real
return cmdrunner.DefaultCommandRunner(c)
}
return runner
}