forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariable_test.go
154 lines (125 loc) · 3.52 KB
/
variable_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
148
149
150
151
152
153
154
package tests
import (
"fmt"
"os"
"testing"
"time"
"github.com/diggerhq/go-bitbucket"
_ "github.com/k0kubun/pp"
)
func TestEndToEndDeploymentVariables(t *testing.T) {
user := os.Getenv("BITBUCKET_TEST_USERNAME")
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
owner := os.Getenv("BITBUCKET_TEST_OWNER")
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
if user == "" {
t.Error("BITBUCKET_TEST_USERNAME is empty.")
}
if pass == "" {
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
}
if owner == "" {
t.Error("BITBUCKET_TEST_OWNER is empty.")
}
if repo == "" {
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
}
c := bitbucket.NewBasicAuth(user, pass)
environmentOpt := &bitbucket.RepositoryEnvironmentsOptions{
Owner: owner,
RepoSlug: repo,
}
environments, err := c.Repositories.Repository.ListEnvironments(environmentOpt)
if err != nil {
t.Error(err)
}
if environments == nil {
t.Error("list didn't return any environments")
}
environment, err := findEnvironmentByName("Test", environments)
if err != nil {
t.Error(err)
}
opt := &bitbucket.RepositoryDeploymentVariableOptions{
Owner: owner,
RepoSlug: repo,
Environment: environment,
Key: "foo",
Value: "value",
}
variable, err := c.Repositories.Repository.AddDeploymentVariable(opt)
if err != nil {
t.Error(err)
}
opt.Key = "bar"
opt.Value = "other value"
opt.Uuid = variable.Uuid
updatedVariable, err := c.Repositories.Repository.UpdateDeploymentVariable(opt)
if err != nil {
t.Error(err)
}
listOpt := &bitbucket.RepositoryDeploymentVariablesOptions{
Owner: owner,
RepoSlug: repo,
Environment: environment,
MaxDepth: 10,
PageNum: 1,
Pagelen: 10,
}
err = waitForVariables(updatedVariable.Uuid, "bar", "other value", c, listOpt)
if err != nil {
t.Error(err)
}
deleteOpt := &bitbucket.RepositoryDeploymentVariableDeleteOptions{
Owner: owner,
RepoSlug: repo,
Environment: environment,
Uuid: variable.Uuid,
}
_, err = c.Repositories.Repository.DeleteDeploymentVariable(deleteOpt)
if err != nil {
t.Error(err)
}
err = waitForDeletion(updatedVariable.Uuid, c, listOpt)
if err == nil {
t.Error("updated variable was not deleted")
}
}
func findEnvironmentByName(name string, e *bitbucket.Environments) (*bitbucket.Environment, error) {
for _, environment := range e.Environments {
if environment.Name == name {
return &environment, nil
}
}
return nil, fmt.Errorf("no environment named %s", name)
}
func waitForDeletion(uuid string, c *bitbucket.Client, opt *bitbucket.RepositoryDeploymentVariablesOptions) error {
for i := 0; i < 3; i++ {
deploymentVariables, err := c.Repositories.Repository.ListDeploymentVariables(opt)
if err != nil {
return err
}
for _, deploymentVariable := range deploymentVariables.Variables {
if deploymentVariable.Uuid == uuid {
time.Sleep(3 * time.Second)
break
}
}
}
return fmt.Errorf("update variable not found in list")
}
func waitForVariables(uuid string, key string, value string, c *bitbucket.Client, opt *bitbucket.RepositoryDeploymentVariablesOptions) error {
for i := 0; i < 3; i++ {
deploymentVariables, err := c.Repositories.Repository.ListDeploymentVariables(opt)
if err != nil {
return err
}
for _, deploymentVariable := range deploymentVariables.Variables {
if deploymentVariable.Uuid == uuid && deploymentVariable.Key == key && deploymentVariable.Value == value {
return nil
}
}
time.Sleep(3 * time.Second)
}
return fmt.Errorf("update variable not found in list")
}