forked from xanzy/go-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environments_test.go
192 lines (166 loc) · 6.45 KB
/
environments_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
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
//
// Copyright 2021, Sander van Harmelen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package gitlab
import (
"encoding/json"
"fmt"
"log"
"net/http"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListEnvironments(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/environments", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
testURL(t, r, "/api/v4/projects/1/environments?name=review%2Ffix-foo&page=1&per_page=10")
fmt.Fprint(w, `[{"id": 1,"name": "review/fix-foo", "slug": "review-fix-foo-dfjre3", "external_url": "https://review-fix-foo-dfjre3.example.gitlab.com"}]`)
})
envs, _, err := client.Environments.ListEnvironments(1, &ListEnvironmentsOptions{Name: String("review/fix-foo"), ListOptions: ListOptions{Page: 1, PerPage: 10}})
if err != nil {
log.Fatal(err)
}
want := []*Environment{{ID: 1, Name: "review/fix-foo", Slug: "review-fix-foo-dfjre3", ExternalURL: "https://review-fix-foo-dfjre3.example.gitlab.com"}}
if !reflect.DeepEqual(want, envs) {
t.Errorf("Environments.ListEnvironments returned %+v, want %+v", envs, want)
}
}
func TestGetEnvironment(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/environments/5949167", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"id":1,"name":"test/test"}`)
})
env, _, err := client.Environments.GetEnvironment(1, 5949167)
if err != nil {
t.Errorf("Environemtns.GetEnvironment returned error: %v", err)
}
want := &Environment{ID: 1, Name: "test/test"}
if !reflect.DeepEqual(want, env) {
t.Errorf("Environments.GetEnvironment returned %+v, want %+v", env, want)
}
}
func TestCreateEnvironment(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/environments", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
testURL(t, r, "/api/v4/projects/1/environments")
fmt.Fprint(w, `{"id": 1,"name": "deploy", "slug": "deploy", "external_url": "https://deploy.example.gitlab.com"}`)
})
envs, _, err := client.Environments.CreateEnvironment(1, &CreateEnvironmentOptions{Name: String("deploy"), ExternalURL: String("https://deploy.example.gitlab.com")})
if err != nil {
log.Fatal(err)
}
want := &Environment{ID: 1, Name: "deploy", Slug: "deploy", ExternalURL: "https://deploy.example.gitlab.com"}
if !reflect.DeepEqual(want, envs) {
t.Errorf("Environments.CreateEnvironment returned %+v, want %+v", envs, want)
}
}
func TestEditEnvironment(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/environments/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
testURL(t, r, "/api/v4/projects/1/environments/1")
fmt.Fprint(w, `{"id": 1,"name": "staging", "slug": "staging", "external_url": "https://staging.example.gitlab.com"}`)
})
envs, _, err := client.Environments.EditEnvironment(1, 1, &EditEnvironmentOptions{Name: String("staging"), ExternalURL: String("https://staging.example.gitlab.com")})
if err != nil {
log.Fatal(err)
}
want := &Environment{ID: 1, Name: "staging", Slug: "staging", ExternalURL: "https://staging.example.gitlab.com"}
if !reflect.DeepEqual(want, envs) {
t.Errorf("Environments.EditEnvironment returned %+v, want %+v", envs, want)
}
}
func TestDeleteEnvironment(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/environments/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
testURL(t, r, "/api/v4/projects/1/environments/1")
})
_, err := client.Environments.DeleteEnvironment(1, 1)
if err != nil {
log.Fatal(err)
}
}
func TestStopEnvironment(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/environments/1/stop", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
testURL(t, r, "/api/v4/projects/1/environments/1/stop")
})
_, err := client.Environments.StopEnvironment(1, 1)
if err != nil {
log.Fatal(err)
}
}
func TestUnmarshal(t *testing.T) {
jsonObject := `
{
"id": 10,
"name": "production",
"slug": "production",
"external_url": "https://example.com",
"project": {
"id": 1,
"description": "",
"name": "Awesome Project",
"name_with_namespace": "FooBar Group / Awesome Project",
"path": "awesome-project",
"path_with_namespace": "foobar-group/awesome-project",
"created_at": "2017-09-30T11:10:08.476-04:00",
"default_branch": "develop",
"tag_list": [],
"ssh_url_to_repo": "[email protected]:foobar-group/api.git",
"http_url_to_repo": "https://example.gitlab.com/foobar-group/api.git",
"web_url": "https://example.gitlab.com/foobar-group/api",
"readme_url": null,
"avatar_url": null,
"star_count": 0,
"forks_count": 1,
"last_activity_at": "2019-11-03T22:22:46.564-05:00",
"namespace": {
"id": 15,
"name": "FooBar Group",
"path": "foobar-group",
"kind": "group",
"full_path": "foobar-group",
"parent_id": null,
"avatar_url": null,
"web_url": "https://example.gitlab.com/groups/foobar-group"
}
},
"state": "available"
}`
var env Environment
err := json.Unmarshal([]byte(jsonObject), &env)
if assert.NoError(t, err) {
assert.Equal(t, 10, env.ID)
assert.Equal(t, "production", env.Name)
assert.Equal(t, "https://example.com", env.ExternalURL)
assert.Equal(t, "available", env.State)
if assert.NotNil(t, env.Project) {
assert.Equal(t, "Awesome Project", env.Project.Name)
}
}
}