-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
git_test.go
133 lines (123 loc) · 3.36 KB
/
git_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
package git
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
var d = uint64(1)
func TestGitArtifactDriver_Load(t *testing.T) {
_ = os.Remove("git-ask-pass.sh")
driver := &ArtifactDriver{}
path := "/tmp/git-found"
assert.NoError(t, os.RemoveAll(path))
assert.NoError(t, os.MkdirAll(path, 0o777))
err := driver.Load(&wfv1.Artifact{
ArtifactLocation: wfv1.ArtifactLocation{
Git: &wfv1.GitArtifact{
Repo: "https://github.com/argoproj/argoproj.git",
Fetch: []string{"+refs/heads/*:refs/remotes/origin/*"},
Revision: "HEAD",
Depth: &d,
},
},
}, path)
if assert.NoError(t, err) {
_, err := os.Stat(path)
assert.NoError(t, err)
}
}
func TestGitArtifactDriver_Save(t *testing.T) {
driver := &ArtifactDriver{}
err := driver.Save("", nil)
assert.Error(t, err)
}
func TestGitArtifactDriverLoad_HTTPS(t *testing.T) {
if os.Getenv("GITHUB_TOKEN") == "" {
t.Skip("not running an GITHUB_TOKEN not set")
}
_ = os.Remove("git-ask-pass.sh")
tmp, err := ioutil.TempDir("", "")
assert.NoError(t, err)
driver := &ArtifactDriver{Username: os.Getenv("GITHUB_TOKEN")}
assert.NotEmpty(t, driver.Username)
err = driver.Load(&wfv1.Artifact{
ArtifactLocation: wfv1.ArtifactLocation{
Git: &wfv1.GitArtifact{
Repo: "https://github.com/argoproj/argo-workflows.git",
Fetch: []string{"+refs/heads/*:refs/remotes/origin/*"},
Revision: "HEAD",
Depth: &d,
},
},
}, tmp)
assert.NoError(t, err)
println(tmp)
}
func TestGitArtifactDriverLoad_SSL(t *testing.T) {
for _, tt := range []struct {
name string
insecure bool
}{
{"Insecure", true},
{"Secure", false},
} {
t.Run(tt.name, func(t *testing.T) {
_ = os.Remove("git-ask-pass.sh")
key := os.Getenv("HOME") + "/.ssh/id_rsa"
data, err := ioutil.ReadFile(key)
if err != nil && os.IsNotExist(err) {
t.Skip(key + " does not exist")
}
assert.NoError(t, err)
tmp, err := ioutil.TempDir("", "")
assert.NoError(t, err)
println(tmp)
driver := &ArtifactDriver{SSHPrivateKey: string(data)}
err = driver.Load(&wfv1.Artifact{
ArtifactLocation: wfv1.ArtifactLocation{
Git: &wfv1.GitArtifact{
Repo: "[email protected]:argoproj/argo-workflows.git",
Fetch: []string{"+refs/heads/*:refs/remotes/origin/*"},
Revision: "HEAD",
InsecureIgnoreHostKey: tt.insecure,
Depth: &d,
},
},
}, tmp)
assert.NoError(t, err)
})
}
}
func TestGetCheckoutRevision(t *testing.T) {
for _, tt := range []struct {
in string
expected string
}{
{"my-branch", "my-branch"},
{"refs/heads/my-branch", "my-branch"},
{"refs/tags/1.0.0", "refs/tags/1.0.0"},
{"ae7b5432cfa15577d4740fb047762254be3652db", "ae7b5432cfa15577d4740fb047762254be3652db"},
} {
t.Run(tt.in, func(t *testing.T) {
result := getRevisionForCheckout(tt.in)
assert.Equal(t, result, tt.expected)
})
}
}
func TestGetUser(t *testing.T) {
for _, tt := range []struct {
name string
url string
user string
}{
{"Username in SSH url", "[email protected]:argoproj/argo-workflows.git", "gitaly"},
{"Default username", "https://github.com/argoproj/argo-workflows.git", "git"},
} {
t.Run(tt.name, func(t *testing.T) {
sshUser := GetUser(tt.url)
assert.Equal(t, sshUser, tt.user)
})
}
}