| 1 |
// Copyright 2023 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
"net/http" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
) |
| 17 |
|
| 18 |
func TestCodespacesService_ListInRepo(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/repos/owner/repo/codespaces", func(w http.ResponseWriter, r *http.Request) { |
| 23 |
testMethod(t, r, "GET") |
| 24 |
testFormValues(t, r, values{ |
| 25 |
"page": "1", |
| 26 |
"per_page": "2", |
| 27 |
}) |
| 28 |
fmt.Fprint(w, `{"total_count":2,"codespaces":[{"id":1,"name":"monalisa-octocat-hello-world-g4wpq6h95q","environment_id":"26a7c758-7299-4a73-b978-5a92a7ae98a0","owner":{"login":"octocat"},"billable_owner":{"login":"octocat"},"repository":{"id":1296269},"machine":{"name":"standardLinux","display_name":"4 cores, 8 GB RAM, 64 GB storage","operating_system":"linux","storage_in_bytes":68719476736,"memory_in_bytes":8589934592,"cpus":4},"prebuild":false,"devcontainer_path":".devcontainer/devcontainer.json","created_at":"2021-10-14T00:53:30-06:00","updated_at":"2021-10-14T00:53:32-06:00","last_used_at":"2021-10-14T00:53:30-06:00","state":"Available","url":"https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q","git_status":{"ahead":0,"behind":0,"has_unpushed_changes":false,"has_uncommitted_changes":false,"ref":"main"},"location":"WestUs2","idle_timeout_minutes":60,"web_url":"https://monalisa-octocat-hello-world-g4wpq6h95q.github.dev","machines_url":"https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/machines","start_url":"https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/start","stop_url":"https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/stop","recent_folders":["testfolder1","testfolder2"]},{"id":2}]}`) |
| 29 |
}) |
| 30 |
|
| 31 |
opt := &ListOptions{Page: 1, PerPage: 2} |
| 32 |
ctx := context.Background() |
| 33 |
codespaces, _, err := client.Codespaces.ListInRepo(ctx, "owner", "repo", opt) |
| 34 |
if err != nil { |
| 35 |
t.Errorf("Codespaces.ListInRepo returned error: %v", err) |
| 36 |
} |
| 37 |
|
| 38 |
want := &ListCodespaces{TotalCount: Int(2), Codespaces: []*Codespace{ |
| 39 |
{ |
| 40 |
ID: Int64(1), |
| 41 |
Name: String("monalisa-octocat-hello-world-g4wpq6h95q"), |
| 42 |
EnvironmentID: String("26a7c758-7299-4a73-b978-5a92a7ae98a0"), |
| 43 |
Owner: &User{ |
| 44 |
Login: String("octocat"), |
| 45 |
}, |
| 46 |
BillableOwner: &User{ |
| 47 |
Login: String("octocat"), |
| 48 |
}, |
| 49 |
Repository: &Repository{ |
| 50 |
ID: Int64(1296269), |
| 51 |
}, |
| 52 |
Machine: &CodespacesMachine{ |
| 53 |
Name: String("standardLinux"), |
| 54 |
DisplayName: String("4 cores, 8 GB RAM, 64 GB storage"), |
| 55 |
OperatingSystem: String("linux"), |
| 56 |
StorageInBytes: Int64(68719476736), |
| 57 |
MemoryInBytes: Int64(8589934592), |
| 58 |
CPUs: Int(4), |
| 59 |
}, |
| 60 |
Prebuild: Bool(false), |
| 61 |
DevcontainerPath: String(".devcontainer/devcontainer.json"), |
| 62 |
CreatedAt: &Timestamp{time.Date(2021, 10, 14, 0, 53, 30, 0, time.FixedZone("", -6*60*60))}, |
| 63 |
UpdatedAt: &Timestamp{time.Date(2021, 10, 14, 0, 53, 32, 0, time.FixedZone("", -6*60*60))}, |
| 64 |
LastUsedAt: &Timestamp{time.Date(2021, 10, 14, 0, 53, 30, 0, time.FixedZone("", -6*60*60))}, |
| 65 |
State: String("Available"), |
| 66 |
URL: String("https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q"), |
| 67 |
GitStatus: &CodespacesGitStatus{ |
| 68 |
Ahead: Int(0), |
| 69 |
Behind: Int(0), |
| 70 |
HasUnpushedChanges: Bool(false), |
| 71 |
HasUncommittedChanges: Bool(false), |
| 72 |
Ref: String("main"), |
| 73 |
}, |
| 74 |
Location: String("WestUs2"), |
| 75 |
IdleTimeoutMinutes: Int(60), |
| 76 |
WebURL: String("https://monalisa-octocat-hello-world-g4wpq6h95q.github.dev"), |
| 77 |
MachinesURL: String("https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/machines"), |
| 78 |
StartURL: String("https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/start"), |
| 79 |
StopURL: String("https://api.github.com/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/stop"), |
| 80 |
RecentFolders: []string{ |
| 81 |
"testfolder1", |
| 82 |
"testfolder2", |
| 83 |
}, |
| 84 |
}, |
| 85 |
{ |
| 86 |
ID: Int64(2), |
| 87 |
}, |
| 88 |
}} |
| 89 |
if !cmp.Equal(codespaces, want) { |
| 90 |
t.Errorf("Codespaces.ListInRepo returned %+v, want %+v", codespaces, want) |
| 91 |
} |
| 92 |
|
| 93 |
const methodName = "ListInRepo" |
| 94 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 95 |
got, resp, err := client.Codespaces.ListInRepo(ctx, "", "", nil) |
| 96 |
if got != nil { |
| 97 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 98 |
} |
| 99 |
return resp, err |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
func TestCodespacesService_List(t *testing.T) { |
| 104 |
client, mux, _, teardown := setup() |
| 105 |
defer teardown() |
| 106 |
|
| 107 |
mux.HandleFunc("/user/codespaces", func(w http.ResponseWriter, r *http.Request) { |
| 108 |
testMethod(t, r, "GET") |
| 109 |
testFormValues(t, r, values{ |
| 110 |
"page": "1", |
| 111 |
"per_page": "2", |
| 112 |
"repository_id": "1296269", |
| 113 |
}) |
| 114 |
fmt.Fprint(w, `{"total_count":1,"codespaces":[{"id":1, "repository": {"id": 1296269}}]}`) |
| 115 |
}) |
| 116 |
|
| 117 |
opt := &ListCodespacesOptions{ListOptions: ListOptions{Page: 1, PerPage: 2}, RepositoryID: 1296269} |
| 118 |
ctx := context.Background() |
| 119 |
codespaces, _, err := client.Codespaces.List(ctx, opt) |
| 120 |
if err != nil { |
| 121 |
t.Errorf("Codespaces.List returned error: %v", err) |
| 122 |
} |
| 123 |
|
| 124 |
want := &ListCodespaces{TotalCount: Int(1), Codespaces: []*Codespace{ |
| 125 |
{ |
| 126 |
ID: Int64(1), |
| 127 |
Repository: &Repository{ |
| 128 |
ID: Int64(1296269), |
| 129 |
}, |
| 130 |
}, |
| 131 |
}} |
| 132 |
if !cmp.Equal(codespaces, want) { |
| 133 |
t.Errorf("Codespaces.ListInRepo returned %+v, want %+v", codespaces, want) |
| 134 |
} |
| 135 |
|
| 136 |
const methodName = "List" |
| 137 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 138 |
got, resp, err := client.Codespaces.List(ctx, nil) |
| 139 |
if got != nil { |
| 140 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 141 |
} |
| 142 |
return resp, err |
| 143 |
}) |
| 144 |
} |
| 145 |
|
| 146 |
func TestCodespacesService_CreateInRepo(t *testing.T) { |
| 147 |
client, mux, _, teardown := setup() |
| 148 |
defer teardown() |
| 149 |
mux.HandleFunc("/repos/owner/repo/codespaces", func(w http.ResponseWriter, r *http.Request) { |
| 150 |
testMethod(t, r, "POST") |
| 151 |
testHeader(t, r, "Content-Type", "application/json") |
| 152 |
testBody(t, r, `{"ref":"main","geo":"WestUs2","machine":"standardLinux","idle_timeout_minutes":60}`+"\n") |
| 153 |
fmt.Fprint(w, `{"id":1, "repository": {"id": 1296269}}`) |
| 154 |
}) |
| 155 |
input := &CreateCodespaceOptions{ |
| 156 |
Ref: String("main"), |
| 157 |
Geo: String("WestUs2"), |
| 158 |
Machine: String("standardLinux"), |
| 159 |
IdleTimeoutMinutes: Int(60), |
| 160 |
} |
| 161 |
ctx := context.Background() |
| 162 |
codespace, _, err := client.Codespaces.CreateInRepo(ctx, "owner", "repo", input) |
| 163 |
if err != nil { |
| 164 |
t.Errorf("Codespaces.CreateInRepo returned error: %v", err) |
| 165 |
} |
| 166 |
want := &Codespace{ |
| 167 |
ID: Int64(1), |
| 168 |
Repository: &Repository{ |
| 169 |
ID: Int64(1296269), |
| 170 |
}, |
| 171 |
} |
| 172 |
|
| 173 |
if !cmp.Equal(codespace, want) { |
| 174 |
t.Errorf("Codespaces.CreateInRepo returned %+v, want %+v", codespace, want) |
| 175 |
} |
| 176 |
|
| 177 |
const methodName = "CreateInRepo" |
| 178 |
testBadOptions(t, methodName, func() (err error) { |
| 179 |
_, _, err = client.Codespaces.CreateInRepo(ctx, "\n", "", input) |
| 180 |
return err |
| 181 |
}) |
| 182 |
|
| 183 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 184 |
got, resp, err := client.Codespaces.CreateInRepo(ctx, "o", "r", input) |
| 185 |
if got != nil { |
| 186 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 187 |
} |
| 188 |
return resp, err |
| 189 |
}) |
| 190 |
} |
| 191 |
|
| 192 |
func TestCodespacesService_Start(t *testing.T) { |
| 193 |
client, mux, _, teardown := setup() |
| 194 |
defer teardown() |
| 195 |
mux.HandleFunc("/user/codespaces/codespace_1/start", func(w http.ResponseWriter, r *http.Request) { |
| 196 |
testMethod(t, r, "POST") |
| 197 |
fmt.Fprint(w, `{"id":1, "repository": {"id": 1296269}}`) |
| 198 |
}) |
| 199 |
ctx := context.Background() |
| 200 |
codespace, _, err := client.Codespaces.Start(ctx, "codespace_1") |
| 201 |
if err != nil { |
| 202 |
t.Errorf("Codespaces.Start returned error: %v", err) |
| 203 |
} |
| 204 |
want := &Codespace{ |
| 205 |
ID: Int64(1), |
| 206 |
Repository: &Repository{ |
| 207 |
ID: Int64(1296269), |
| 208 |
}, |
| 209 |
} |
| 210 |
|
| 211 |
if !cmp.Equal(codespace, want) { |
| 212 |
t.Errorf("Codespaces.Start returned %+v, want %+v", codespace, want) |
| 213 |
} |
| 214 |
|
| 215 |
const methodName = "Start" |
| 216 |
testBadOptions(t, methodName, func() (err error) { |
| 217 |
_, _, err = client.Codespaces.Start(ctx, "\n") |
| 218 |
return err |
| 219 |
}) |
| 220 |
|
| 221 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 222 |
got, resp, err := client.Codespaces.Start(ctx, "o") |
| 223 |
if got != nil { |
| 224 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 225 |
} |
| 226 |
return resp, err |
| 227 |
}) |
| 228 |
} |
| 229 |
|
| 230 |
func TestCodespacesService_Stop(t *testing.T) { |
| 231 |
client, mux, _, teardown := setup() |
| 232 |
defer teardown() |
| 233 |
mux.HandleFunc("/user/codespaces/codespace_1/stop", func(w http.ResponseWriter, r *http.Request) { |
| 234 |
testMethod(t, r, "POST") |
| 235 |
fmt.Fprint(w, `{"id":1, "repository": {"id": 1296269}}`) |
| 236 |
}) |
| 237 |
ctx := context.Background() |
| 238 |
codespace, _, err := client.Codespaces.Stop(ctx, "codespace_1") |
| 239 |
if err != nil { |
| 240 |
t.Errorf("Codespaces.Stop returned error: %v", err) |
| 241 |
} |
| 242 |
want := &Codespace{ |
| 243 |
ID: Int64(1), |
| 244 |
Repository: &Repository{ |
| 245 |
ID: Int64(1296269), |
| 246 |
}, |
| 247 |
} |
| 248 |
|
| 249 |
if !cmp.Equal(codespace, want) { |
| 250 |
t.Errorf("Codespaces.Stop returned %+v, want %+v", codespace, want) |
| 251 |
} |
| 252 |
|
| 253 |
const methodName = "Stop" |
| 254 |
testBadOptions(t, methodName, func() (err error) { |
| 255 |
_, _, err = client.Codespaces.Stop(ctx, "\n") |
| 256 |
return err |
| 257 |
}) |
| 258 |
|
| 259 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 260 |
got, resp, err := client.Codespaces.Stop(ctx, "o") |
| 261 |
if got != nil { |
| 262 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 263 |
} |
| 264 |
return resp, err |
| 265 |
}) |
| 266 |
} |
| 267 |
|
| 268 |
func TestCodespacesService_Delete(t *testing.T) { |
| 269 |
client, mux, _, teardown := setup() |
| 270 |
defer teardown() |
| 271 |
|
| 272 |
mux.HandleFunc("/user/codespaces/codespace_1", func(w http.ResponseWriter, r *http.Request) { |
| 273 |
testMethod(t, r, "DELETE") |
| 274 |
}) |
| 275 |
|
| 276 |
ctx := context.Background() |
| 277 |
_, err := client.Codespaces.Delete(ctx, "codespace_1") |
| 278 |
if err != nil { |
| 279 |
t.Errorf("Codespaces.Delete return error: %v", err) |
| 280 |
} |
| 281 |
|
| 282 |
const methodName = "Delete" |
| 283 |
testBadOptions(t, methodName, func() (err error) { |
| 284 |
_, err = client.Codespaces.Delete(ctx, "\n") |
| 285 |
return err |
| 286 |
}) |
| 287 |
|
| 288 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 289 |
return client.Codespaces.Delete(ctx, "codespace_1") |
| 290 |
}) |
| 291 |
} |