Debian Codegraph @

github/enterprise_actions_runners_test.go

raw
1 // Copyright 2020 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 "encoding/json"
11 "fmt"
12 "net/http"
13 "testing"
14 "time"
15
16 "github.com/google/go-cmp/cmp"
17 )
18
19 func TestEnterpriseService_GenerateEnterpriseJITConfig(t *testing.T) {
20 client, mux, _, teardown := setup()
21 defer teardown()
22
23 input := &GenerateJITConfigRequest{Name: "test", RunnerGroupID: 1, Labels: []string{"one", "two"}}
24
25 mux.HandleFunc("/enterprises/o/actions/runners/generate-jitconfig", func(w http.ResponseWriter, r *http.Request) {
26 v := new(GenerateJITConfigRequest)
27 err := json.NewDecoder(r.Body).Decode(v)
28 if err != nil {
29 t.Errorf("Request body decode failed: %v", err)
30 }
31
32 testMethod(t, r, "POST")
33 if !cmp.Equal(v, input) {
34 t.Errorf("Request body = %+v, want %+v", v, input)
35 }
36
37 fmt.Fprint(w, `{"encoded_jit_config":"foo"}`)
38 })
39
40 ctx := context.Background()
41 jitConfig, _, err := client.Enterprise.GenerateEnterpriseJITConfig(ctx, "o", input)
42 if err != nil {
43 t.Errorf("Enterprise.GenerateEnterpriseJITConfig returned error: %v", err)
44 }
45
46 want := &JITRunnerConfig{EncodedJITConfig: String("foo")}
47 if !cmp.Equal(jitConfig, want) {
48 t.Errorf("Enterprise.GenerateEnterpriseJITConfig returned %+v, want %+v", jitConfig, want)
49 }
50
51 const methodName = "GenerateEnterpriseJITConfig"
52 testBadOptions(t, methodName, func() (err error) {
53 _, _, err = client.Enterprise.GenerateEnterpriseJITConfig(ctx, "\n", input)
54 return err
55 })
56
57 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
58 got, resp, err := client.Enterprise.GenerateEnterpriseJITConfig(ctx, "o", input)
59 if got != nil {
60 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
61 }
62 return resp, err
63 })
64 }
65
66 func TestEnterpriseService_CreateRegistrationToken(t *testing.T) {
67 client, mux, _, teardown := setup()
68 defer teardown()
69
70 mux.HandleFunc("/enterprises/e/actions/runners/registration-token", func(w http.ResponseWriter, r *http.Request) {
71 testMethod(t, r, "POST")
72 fmt.Fprint(w, `{"token":"LLBF3JGZDX3P5PMEXLND6TS6FCWO6","expires_at":"2020-01-22T12:13:35.123Z"}`)
73 })
74
75 ctx := context.Background()
76 token, _, err := client.Enterprise.CreateRegistrationToken(ctx, "e")
77 if err != nil {
78 t.Errorf("Enterprise.CreateRegistrationToken returned error: %v", err)
79 }
80
81 want := &RegistrationToken{Token: String("LLBF3JGZDX3P5PMEXLND6TS6FCWO6"),
82 ExpiresAt: &Timestamp{time.Date(2020, time.January, 22, 12, 13, 35,
83 123000000, time.UTC)}}
84 if !cmp.Equal(token, want) {
85 t.Errorf("Enterprise.CreateRegistrationToken returned %+v, want %+v", token, want)
86 }
87
88 const methodName = "CreateRegistrationToken"
89 testBadOptions(t, methodName, func() (err error) {
90 _, _, err = client.Enterprise.CreateRegistrationToken(ctx, "\n")
91 return err
92 })
93
94 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
95 got, resp, err := client.Enterprise.CreateRegistrationToken(ctx, "e")
96 if got != nil {
97 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
98 }
99 return resp, err
100 })
101 }
102
103 func TestEnterpriseService_ListRunners(t *testing.T) {
104 client, mux, _, teardown := setup()
105 defer teardown()
106
107 mux.HandleFunc("/enterprises/e/actions/runners", func(w http.ResponseWriter, r *http.Request) {
108 testMethod(t, r, "GET")
109 testFormValues(t, r, values{"per_page": "2", "page": "2"})
110 fmt.Fprint(w, `{"total_count":2,"runners":[{"id":23,"name":"MBP","os":"macos","status":"online"},{"id":24,"name":"iMac","os":"macos","status":"offline"}]}`)
111 })
112
113 opts := &ListOptions{Page: 2, PerPage: 2}
114 ctx := context.Background()
115 runners, _, err := client.Enterprise.ListRunners(ctx, "e", opts)
116 if err != nil {
117 t.Errorf("Enterprise.ListRunners returned error: %v", err)
118 }
119
120 want := &Runners{
121 TotalCount: 2,
122 Runners: []*Runner{
123 {ID: Int64(23), Name: String("MBP"), OS: String("macos"), Status: String("online")},
124 {ID: Int64(24), Name: String("iMac"), OS: String("macos"), Status: String("offline")},
125 },
126 }
127 if !cmp.Equal(runners, want) {
128 t.Errorf("Actions.ListRunners returned %+v, want %+v", runners, want)
129 }
130
131 const methodName = "ListRunners"
132 testBadOptions(t, methodName, func() (err error) {
133 _, _, err = client.Enterprise.ListRunners(ctx, "\n", &ListOptions{})
134 return err
135 })
136
137 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
138 got, resp, err := client.Enterprise.ListRunners(ctx, "e", 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 TestEnterpriseService_RemoveRunner(t *testing.T) {
147 client, mux, _, teardown := setup()
148 defer teardown()
149
150 mux.HandleFunc("/enterprises/o/actions/runners/21", func(w http.ResponseWriter, r *http.Request) {
151 testMethod(t, r, "DELETE")
152 })
153
154 ctx := context.Background()
155 _, err := client.Enterprise.RemoveRunner(ctx, "o", 21)
156 if err != nil {
157 t.Errorf("Actions.RemoveRunner returned error: %v", err)
158 }
159
160 const methodName = "RemoveRunner"
161 testBadOptions(t, methodName, func() (err error) {
162 _, err = client.Enterprise.RemoveRunner(ctx, "\n", 21)
163 return err
164 })
165
166 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
167 return client.Enterprise.RemoveRunner(ctx, "o", 21)
168 })
169 }
170
171 func TestEnterpriseService_ListRunnerApplicationDownloads(t *testing.T) {
172 client, mux, _, teardown := setup()
173 defer teardown()
174
175 mux.HandleFunc("/enterprises/o/actions/runners/downloads", func(w http.ResponseWriter, r *http.Request) {
176 testMethod(t, r, "GET")
177 fmt.Fprint(w, `[{"os":"osx","architecture":"x64","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-osx-x64-2.164.0.tar.gz","filename":"actions-runner-osx-x64-2.164.0.tar.gz"},{"os":"linux","architecture":"x64","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-x64-2.164.0.tar.gz","filename":"actions-runner-linux-x64-2.164.0.tar.gz"},{"os": "linux","architecture":"arm","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-arm-2.164.0.tar.gz","filename":"actions-runner-linux-arm-2.164.0.tar.gz"},{"os":"win","architecture":"x64","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-win-x64-2.164.0.zip","filename":"actions-runner-win-x64-2.164.0.zip"},{"os":"linux","architecture":"arm64","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-arm64-2.164.0.tar.gz","filename":"actions-runner-linux-arm64-2.164.0.tar.gz"}]`)
178 })
179
180 ctx := context.Background()
181 downloads, _, err := client.Enterprise.ListRunnerApplicationDownloads(ctx, "o")
182 if err != nil {
183 t.Errorf("Enterprise.ListRunnerApplicationDownloads returned error: %v", err)
184 }
185
186 want := []*RunnerApplicationDownload{
187 {OS: String("osx"), Architecture: String("x64"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-osx-x64-2.164.0.tar.gz"), Filename: String("actions-runner-osx-x64-2.164.0.tar.gz")},
188 {OS: String("linux"), Architecture: String("x64"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-x64-2.164.0.tar.gz"), Filename: String("actions-runner-linux-x64-2.164.0.tar.gz")},
189 {OS: String("linux"), Architecture: String("arm"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-arm-2.164.0.tar.gz"), Filename: String("actions-runner-linux-arm-2.164.0.tar.gz")},
190 {OS: String("win"), Architecture: String("x64"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-win-x64-2.164.0.zip"), Filename: String("actions-runner-win-x64-2.164.0.zip")},
191 {OS: String("linux"), Architecture: String("arm64"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-arm64-2.164.0.tar.gz"), Filename: String("actions-runner-linux-arm64-2.164.0.tar.gz")},
192 }
193 if !cmp.Equal(downloads, want) {
194 t.Errorf("Enterprise.ListRunnerApplicationDownloads returned %+v, want %+v", downloads, want)
195 }
196
197 const methodName = "ListRunnerApplicationDownloads"
198 testBadOptions(t, methodName, func() (err error) {
199 _, _, err = client.Enterprise.ListRunnerApplicationDownloads(ctx, "\n")
200 return err
201 })
202
203 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
204 got, resp, err := client.Enterprise.ListRunnerApplicationDownloads(ctx, "o")
205 if got != nil {
206 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
207 }
208 return resp, err
209 })
210 }