1
2
3
4
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 TestEnterpriseService_CreateRegistrationToken(t *testing.T) {
19 client, mux, _, teardown := setup()
20 defer teardown()
21
22 mux.HandleFunc("/enterprises/e/actions/runners/registration-token", func(w http.ResponseWriter, r *http.Request) {
23 testMethod(t, r, "POST")
24 fmt.Fprint(w, `{"token":"LLBF3JGZDX3P5PMEXLND6TS6FCWO6","expires_at":"2020-01-22T12:13:35.123Z"}`)
25 })
26
27 ctx := context.Background()
28 token, _, err := client.Enterprise.CreateRegistrationToken(ctx, "e")
29 if err != nil {
30 t.Errorf("Enterprise.CreateRegistrationToken returned error: %v", err)
31 }
32
33 want := &RegistrationToken{Token: String("LLBF3JGZDX3P5PMEXLND6TS6FCWO6"),
34 ExpiresAt: &Timestamp{time.Date(2020, time.January, 22, 12, 13, 35,
35 123000000, time.UTC)}}
36 if !cmp.Equal(token, want) {
37 t.Errorf("Enterprise.CreateRegistrationToken returned %+v, want %+v", token, want)
38 }
39
40 const methodName = "CreateRegistrationToken"
41 testBadOptions(t, methodName, func() (err error) {
42 _, _, err = client.Enterprise.CreateRegistrationToken(ctx, "\n")
43 return err
44 })
45
46 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
47 got, resp, err := client.Enterprise.CreateRegistrationToken(ctx, "e")
48 if got != nil {
49 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
50 }
51 return resp, err
52 })
53 }
54
55 func TestEnterpriseService_ListRunners(t *testing.T) {
56 client, mux, _, teardown := setup()
57 defer teardown()
58
59 mux.HandleFunc("/enterprises/e/actions/runners", func(w http.ResponseWriter, r *http.Request) {
60 testMethod(t, r, "GET")
61 testFormValues(t, r, values{"per_page": "2", "page": "2"})
62 fmt.Fprint(w, `{"total_count":2,"runners":[{"id":23,"name":"MBP","os":"macos","status":"online"},{"id":24,"name":"iMac","os":"macos","status":"offline"}]}`)
63 })
64
65 opts := &ListOptions{Page: 2, PerPage: 2}
66 ctx := context.Background()
67 runners, _, err := client.Enterprise.ListRunners(ctx, "e", opts)
68 if err != nil {
69 t.Errorf("Enterprise.ListRunners returned error: %v", err)
70 }
71
72 want := &Runners{
73 TotalCount: 2,
74 Runners: []*Runner{
75 {ID: Int64(23), Name: String("MBP"), OS: String("macos"), Status: String("online")},
76 {ID: Int64(24), Name: String("iMac"), OS: String("macos"), Status: String("offline")},
77 },
78 }
79 if !cmp.Equal(runners, want) {
80 t.Errorf("Actions.ListRunners returned %+v, want %+v", runners, want)
81 }
82
83 const methodName = "ListRunners"
84 testBadOptions(t, methodName, func() (err error) {
85 _, _, err = client.Enterprise.ListRunners(ctx, "\n", &ListOptions{})
86 return err
87 })
88
89 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
90 got, resp, err := client.Enterprise.ListRunners(ctx, "e", nil)
91 if got != nil {
92 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
93 }
94 return resp, err
95 })
96 }
97
98 func TestEnterpriseService_RemoveRunner(t *testing.T) {
99 client, mux, _, teardown := setup()
100 defer teardown()
101
102 mux.HandleFunc("/enterprises/o/actions/runners/21", func(w http.ResponseWriter, r *http.Request) {
103 testMethod(t, r, "DELETE")
104 })
105
106 ctx := context.Background()
107 _, err := client.Enterprise.RemoveRunner(ctx, "o", 21)
108 if err != nil {
109 t.Errorf("Actions.RemoveRunner returned error: %v", err)
110 }
111
112 const methodName = "RemoveRunner"
113 testBadOptions(t, methodName, func() (err error) {
114 _, err = client.Enterprise.RemoveRunner(ctx, "\n", 21)
115 return err
116 })
117
118 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
119 return client.Enterprise.RemoveRunner(ctx, "o", 21)
120 })
121 }
122
123 func TestEnterpriseService_ListRunnerApplicationDownloads(t *testing.T) {
124 client, mux, _, teardown := setup()
125 defer teardown()
126
127 mux.HandleFunc("/enterprises/o/actions/runners/downloads", func(w http.ResponseWriter, r *http.Request) {
128 testMethod(t, r, "GET")
129 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"}]`)
130 })
131
132 ctx := context.Background()
133 downloads, _, err := client.Enterprise.ListRunnerApplicationDownloads(ctx, "o")
134 if err != nil {
135 t.Errorf("Enterprise.ListRunnerApplicationDownloads returned error: %v", err)
136 }
137
138 want := []*RunnerApplicationDownload{
139 {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")},
140 {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")},
141 {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")},
142 {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")},
143 {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")},
144 }
145 if !cmp.Equal(downloads, want) {
146 t.Errorf("Enterprise.ListRunnerApplicationDownloads returned %+v, want %+v", downloads, want)
147 }
148
149 const methodName = "ListRunnerApplicationDownloads"
150 testBadOptions(t, methodName, func() (err error) {
151 _, _, err = client.Enterprise.ListRunnerApplicationDownloads(ctx, "\n")
152 return err
153 })
154
155 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
156 got, resp, err := client.Enterprise.ListRunnerApplicationDownloads(ctx, "o")
157 if got != nil {
158 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
159 }
160 return resp, err
161 })
162 }
163
View as plain text