-
Notifications
You must be signed in to change notification settings - Fork 446
/
credentials_test.go
147 lines (121 loc) · 3.96 KB
/
credentials_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
138
139
140
141
142
143
144
145
146
147
package gojenkins
import (
"context"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
const integration_test string = "INTEGRATION"
var (
cm *CredentialsManager
domain = "_"
dockerID = "dockerIDCred"
sshID = "sshIdCred"
usernameID = "usernameIDcred"
fileID = "fileIDcred"
scope = "GLOBAL"
)
func TestCreateUsernameCredentials(t *testing.T) {
if _, ok := os.LookupEnv(integration_test); !ok {
return
}
cred := UsernameCredentials{
ID: usernameID,
Scope: scope,
Username: "usernameTest",
Password: "pass",
}
ctx := context.Background()
err := cm.Add(ctx, domain, cred)
assert.Nil(t, err, "Could not create credential")
getCred := UsernameCredentials{}
err = cm.GetSingle(ctx, domain, cred.ID, &getCred)
assert.Nil(t, err, "Could not get credential")
assert.Equal(t, cred.Scope, getCred.Scope, "Scope is not equal")
assert.Equal(t, cred.ID, cred.ID, "ID is not equal")
assert.Equal(t, cred.Username, cred.Username, "Username is not equal")
}
func TestCreateFileCredentials(t *testing.T) {
if _, ok := os.LookupEnv(integration_test); !ok {
return
}
cred := FileCredentials{
ID: fileID,
Scope: scope,
Filename: "testFile.json",
SecretBytes: "VGhpcyBpcyBhIHRlc3Qu\n",
}
ctx := context.Background()
err := cm.Add(ctx, domain, cred)
assert.Nil(t, err, "Could not create credential")
getCred := FileCredentials{}
err = cm.GetSingle(ctx, domain, cred.ID, &getCred)
assert.Nil(t, err, "Could not get credential")
assert.Equal(t, cred.Scope, getCred.Scope, "Scope is not equal")
assert.Equal(t, cred.ID, cred.ID, "ID is not equal")
assert.Equal(t, cred.Filename, cred.Filename, "Filename is not equal")
}
func TestCreateDockerCredentials(t *testing.T) {
if _, ok := os.LookupEnv(integration_test); !ok {
return
}
cred := DockerServerCredentials{
Scope: scope,
ID: dockerID,
Username: "docker-name",
ClientCertificate: "some secret value",
ClientKey: "client key",
}
ctx := context.Background()
err := cm.Add(ctx, domain, cred)
assert.Nil(t, err, "Could not create credential")
getCred := DockerServerCredentials{}
err = cm.GetSingle(ctx, domain, cred.ID, &getCred)
assert.Nil(t, err, "Could not get credential")
assert.Equal(t, cred.Scope, getCred.Scope, "Scope is not equal")
assert.Equal(t, cred.ID, cred.ID, "ID is not equal")
assert.Equal(t, cred.Username, cred.Username, "Username is not equal")
assert.Equal(t, cred.ClientCertificate, cred.ClientCertificate, "ClientCertificate is not equal")
assert.Equal(t, cred.ClientKey, cred.ClientKey, "Username is not equal")
}
func TestCreateSSHCredentialsFullFlow(t *testing.T) {
if _, ok := os.LookupEnv(integration_test); !ok {
return
}
sshCred := SSHCredentials{
Scope: scope,
ID: sshID,
Username: "RANDONMANE",
Passphrase: "password",
PrivateKeySource: &PrivateKeyFile{
Value: "testValueofkey",
Class: KeySourceOnMasterType,
},
}
ctx := context.Background()
err := cm.Add(ctx, domain, sshCred)
assert.Nil(t, err, "Could not create credential")
sshCred.Username = "new_username"
err = cm.Update(ctx, domain, sshCred.ID, sshCred)
assert.Nil(t, err, "Could not update credential")
getSSH := SSHCredentials{}
err = cm.GetSingle(ctx, domain, sshCred.ID, &getSSH)
assert.Nil(t, err, "Could not get ssh credential")
assert.Equal(t, sshCred.Scope, getSSH.Scope, "Scope is not equal")
assert.Equal(t, sshCred.ID, getSSH.ID, "ID is not equal")
assert.Equal(t, sshCred.Username, getSSH.Username, "Username is not equal")
assert.Equal(t, sshCred.Scope, getSSH.Scope, "Scope is not equal")
err = cm.Delete(ctx, domain, getSSH.ID)
assert.Nil(t, err, "Could not delete credentials")
}
func TestMain(m *testing.M) {
//setup
ctx := context.Background()
jenkins := CreateJenkins(nil, "http://localhost:8080", "admin", "admin")
jenkins.Init(ctx)
cm = &CredentialsManager{J: jenkins}
fmt.Printf("Debug, from TestMain\n")
//execute tests
os.Exit(m.Run())
}