github.com/google/go-github/v33@v33.0.0/github/actions_runners_test.go (about)

     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  	"fmt"
    11  	"net/http"
    12  	"reflect"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func TestActionsService_ListRunnerApplicationDownloads(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/repos/o/r/actions/runners/downloads", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		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"}]`)
    24  	})
    25  
    26  	downloads, _, err := client.Actions.ListRunnerApplicationDownloads(context.Background(), "o", "r")
    27  	if err != nil {
    28  		t.Errorf("Actions.ListRunnerApplicationDownloads returned error: %v", err)
    29  	}
    30  
    31  	want := []*RunnerApplicationDownload{
    32  		{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")},
    33  		{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")},
    34  		{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")},
    35  		{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")},
    36  		{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")},
    37  	}
    38  	if !reflect.DeepEqual(downloads, want) {
    39  		t.Errorf("Actions.ListRunnerApplicationDownloads returned %+v, want %+v", downloads, want)
    40  	}
    41  }
    42  
    43  func TestActionsService_CreateRegistrationToken(t *testing.T) {
    44  	client, mux, _, teardown := setup()
    45  	defer teardown()
    46  
    47  	mux.HandleFunc("/repos/o/r/actions/runners/registration-token", func(w http.ResponseWriter, r *http.Request) {
    48  		testMethod(t, r, "POST")
    49  		fmt.Fprint(w, `{"token":"LLBF3JGZDX3P5PMEXLND6TS6FCWO6","expires_at":"2020-01-22T12:13:35.123Z"}`)
    50  	})
    51  
    52  	token, _, err := client.Actions.CreateRegistrationToken(context.Background(), "o", "r")
    53  	if err != nil {
    54  		t.Errorf("Actions.CreateRegistrationToken returned error: %v", err)
    55  	}
    56  
    57  	want := &RegistrationToken{Token: String("LLBF3JGZDX3P5PMEXLND6TS6FCWO6"),
    58  		ExpiresAt: &Timestamp{time.Date(2020, time.January, 22, 12, 13, 35,
    59  			123000000, time.UTC)}}
    60  	if !reflect.DeepEqual(token, want) {
    61  		t.Errorf("Actions.CreateRegistrationToken returned %+v, want %+v", token, want)
    62  	}
    63  }
    64  
    65  func TestActionsService_ListRunners(t *testing.T) {
    66  	client, mux, _, teardown := setup()
    67  	defer teardown()
    68  
    69  	mux.HandleFunc("/repos/o/r/actions/runners", func(w http.ResponseWriter, r *http.Request) {
    70  		testMethod(t, r, "GET")
    71  		testFormValues(t, r, values{"per_page": "2", "page": "2"})
    72  		fmt.Fprint(w, `{"total_count":2,"runners":[{"id":23,"name":"MBP","os":"macos","status":"online"},{"id":24,"name":"iMac","os":"macos","status":"offline"}]}`)
    73  	})
    74  
    75  	opts := &ListOptions{Page: 2, PerPage: 2}
    76  	runners, _, err := client.Actions.ListRunners(context.Background(), "o", "r", opts)
    77  	if err != nil {
    78  		t.Errorf("Actions.ListRunners returned error: %v", err)
    79  	}
    80  
    81  	want := &Runners{
    82  		TotalCount: 2,
    83  		Runners: []*Runner{
    84  			{ID: Int64(23), Name: String("MBP"), OS: String("macos"), Status: String("online")},
    85  			{ID: Int64(24), Name: String("iMac"), OS: String("macos"), Status: String("offline")},
    86  		},
    87  	}
    88  	if !reflect.DeepEqual(runners, want) {
    89  		t.Errorf("Actions.ListRunners returned %+v, want %+v", runners, want)
    90  	}
    91  }
    92  
    93  func TestActionsService_GetRunner(t *testing.T) {
    94  	client, mux, _, teardown := setup()
    95  	defer teardown()
    96  
    97  	mux.HandleFunc("/repos/o/r/actions/runners/23", func(w http.ResponseWriter, r *http.Request) {
    98  		testMethod(t, r, "GET")
    99  		fmt.Fprint(w, `{"id":23,"name":"MBP","os":"macos","status":"online"}`)
   100  	})
   101  
   102  	runner, _, err := client.Actions.GetRunner(context.Background(), "o", "r", 23)
   103  	if err != nil {
   104  		t.Errorf("Actions.GetRunner returned error: %v", err)
   105  	}
   106  
   107  	want := &Runner{
   108  		ID:     Int64(23),
   109  		Name:   String("MBP"),
   110  		OS:     String("macos"),
   111  		Status: String("online"),
   112  	}
   113  	if !reflect.DeepEqual(runner, want) {
   114  		t.Errorf("Actions.GetRunner returned %+v, want %+v", runner, want)
   115  	}
   116  }
   117  
   118  func TestActionsService_CreateRemoveToken(t *testing.T) {
   119  	client, mux, _, teardown := setup()
   120  	defer teardown()
   121  
   122  	mux.HandleFunc("/repos/o/r/actions/runners/remove-token", func(w http.ResponseWriter, r *http.Request) {
   123  		testMethod(t, r, "POST")
   124  		fmt.Fprint(w, `{"token":"AABF3JGZDX3P5PMEXLND6TS6FCWO6","expires_at":"2020-01-29T12:13:35.123Z"}`)
   125  	})
   126  
   127  	token, _, err := client.Actions.CreateRemoveToken(context.Background(), "o", "r")
   128  	if err != nil {
   129  		t.Errorf("Actions.CreateRemoveToken returned error: %v", err)
   130  	}
   131  
   132  	want := &RemoveToken{Token: String("AABF3JGZDX3P5PMEXLND6TS6FCWO6"), ExpiresAt: &Timestamp{time.Date(2020, time.January, 29, 12, 13, 35, 123000000, time.UTC)}}
   133  	if !reflect.DeepEqual(token, want) {
   134  		t.Errorf("Actions.CreateRemoveToken returned %+v, want %+v", token, want)
   135  	}
   136  }
   137  
   138  func TestActionsService_RemoveRunner(t *testing.T) {
   139  	client, mux, _, teardown := setup()
   140  	defer teardown()
   141  
   142  	mux.HandleFunc("/repos/o/r/actions/runners/21", func(w http.ResponseWriter, r *http.Request) {
   143  		testMethod(t, r, "DELETE")
   144  	})
   145  
   146  	_, err := client.Actions.RemoveRunner(context.Background(), "o", "r", 21)
   147  	if err != nil {
   148  		t.Errorf("Actions.RemoveRunner returned error: %v", err)
   149  	}
   150  }
   151  
   152  func TestActionsService_ListOrganizationRunnerApplicationDownloads(t *testing.T) {
   153  	client, mux, _, teardown := setup()
   154  	defer teardown()
   155  
   156  	mux.HandleFunc("/orgs/o/actions/runners/downloads", func(w http.ResponseWriter, r *http.Request) {
   157  		testMethod(t, r, "GET")
   158  		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"}]`)
   159  	})
   160  
   161  	downloads, _, err := client.Actions.ListOrganizationRunnerApplicationDownloads(context.Background(), "o")
   162  	if err != nil {
   163  		t.Errorf("Actions.ListRunnerApplicationDownloads returned error: %v", err)
   164  	}
   165  
   166  	want := []*RunnerApplicationDownload{
   167  		{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")},
   168  		{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")},
   169  		{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")},
   170  		{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")},
   171  		{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")},
   172  	}
   173  	if !reflect.DeepEqual(downloads, want) {
   174  		t.Errorf("Actions.ListOrganizationRunnerApplicationDownloads returned %+v, want %+v", downloads, want)
   175  	}
   176  }
   177  
   178  func TestActionsService_CreateOrganizationRegistrationToken(t *testing.T) {
   179  	client, mux, _, teardown := setup()
   180  	defer teardown()
   181  
   182  	mux.HandleFunc("/orgs/o/actions/runners/registration-token", func(w http.ResponseWriter, r *http.Request) {
   183  		testMethod(t, r, "POST")
   184  		fmt.Fprint(w, `{"token":"LLBF3JGZDX3P5PMEXLND6TS6FCWO6","expires_at":"2020-01-22T12:13:35.123Z"}`)
   185  	})
   186  
   187  	token, _, err := client.Actions.CreateOrganizationRegistrationToken(context.Background(), "o")
   188  	if err != nil {
   189  		t.Errorf("Actions.CreateRegistrationToken returned error: %v", err)
   190  	}
   191  
   192  	want := &RegistrationToken{Token: String("LLBF3JGZDX3P5PMEXLND6TS6FCWO6"),
   193  		ExpiresAt: &Timestamp{time.Date(2020, time.January, 22, 12, 13, 35,
   194  			123000000, time.UTC)}}
   195  	if !reflect.DeepEqual(token, want) {
   196  		t.Errorf("Actions.CreateRegistrationToken returned %+v, want %+v", token, want)
   197  	}
   198  }
   199  
   200  func TestActionsService_ListOrganizationRunners(t *testing.T) {
   201  	client, mux, _, teardown := setup()
   202  	defer teardown()
   203  
   204  	mux.HandleFunc("/orgs/o/actions/runners", func(w http.ResponseWriter, r *http.Request) {
   205  		testMethod(t, r, "GET")
   206  		testFormValues(t, r, values{"per_page": "2", "page": "2"})
   207  		fmt.Fprint(w, `{"total_count":2,"runners":[{"id":23,"name":"MBP","os":"macos","status":"online"},{"id":24,"name":"iMac","os":"macos","status":"offline"}]}`)
   208  	})
   209  
   210  	opts := &ListOptions{Page: 2, PerPage: 2}
   211  	runners, _, err := client.Actions.ListOrganizationRunners(context.Background(), "o", opts)
   212  	if err != nil {
   213  		t.Errorf("Actions.ListRunners returned error: %v", err)
   214  	}
   215  
   216  	want := &Runners{
   217  		TotalCount: 2,
   218  		Runners: []*Runner{
   219  			{ID: Int64(23), Name: String("MBP"), OS: String("macos"), Status: String("online")},
   220  			{ID: Int64(24), Name: String("iMac"), OS: String("macos"), Status: String("offline")},
   221  		},
   222  	}
   223  	if !reflect.DeepEqual(runners, want) {
   224  		t.Errorf("Actions.ListRunners returned %+v, want %+v", runners, want)
   225  	}
   226  }
   227  
   228  func TestActionsService_ListEnabledReposInOrg(t *testing.T) {
   229  	client, mux, _, teardown := setup()
   230  	defer teardown()
   231  
   232  	mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) {
   233  		testMethod(t, r, "GET")
   234  		testFormValues(t, r, values{
   235  			"page": "1",
   236  		})
   237  		fmt.Fprint(w, `{"total_count":2,"repositories":[{"id":2}, {"id": 3}]}`)
   238  	})
   239  
   240  	ctx := context.Background()
   241  	opt := &ListOptions{
   242  		Page: 1,
   243  	}
   244  	got, _, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt)
   245  	if err != nil {
   246  		t.Errorf("Actions.ListEnabledReposInOrg returned error: %v", err)
   247  	}
   248  
   249  	want := &ActionsEnabledOnOrgRepos{TotalCount: int(2), Repositories: []*Repository{
   250  		{ID: Int64(2)},
   251  		{ID: Int64(3)},
   252  	}}
   253  	if !reflect.DeepEqual(got, want) {
   254  		t.Errorf("Actions.ListEnabledReposInOrg returned %+v, want %+v", got, want)
   255  	}
   256  
   257  	// Test addOptions failure
   258  	_, _, err = client.Actions.ListEnabledReposInOrg(ctx, "\n", opt)
   259  	if err == nil {
   260  		t.Error("bad options ListEnabledReposInOrg err = nil, want error")
   261  	}
   262  
   263  	// Test s.client.Do failure
   264  	client.BaseURL.Path = "/api-v3/"
   265  	client.rateLimits[0].Reset.Time = time.Now().Add(10 * time.Minute)
   266  	got, resp, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt)
   267  	if got != nil {
   268  		t.Errorf("rate.Reset.Time > now ListEnabledReposInOrg = %#v, want nil", got)
   269  	}
   270  	if want := http.StatusForbidden; resp == nil || resp.Response.StatusCode != want {
   271  		t.Errorf("rate.Reset.Time > now ListEnabledReposInOrg resp = %#v, want StatusCode=%v", resp.Response, want)
   272  	}
   273  	if err == nil {
   274  		t.Error("rate.Reset.Time > now ListEnabledReposInOrg err = nil, want error")
   275  	}
   276  }
   277  
   278  func TestActionsService_GetOrganizationRunner(t *testing.T) {
   279  	client, mux, _, teardown := setup()
   280  	defer teardown()
   281  
   282  	mux.HandleFunc("/orgs/o/actions/runners/23", func(w http.ResponseWriter, r *http.Request) {
   283  		testMethod(t, r, "GET")
   284  		fmt.Fprint(w, `{"id":23,"name":"MBP","os":"macos","status":"online"}`)
   285  	})
   286  
   287  	runner, _, err := client.Actions.GetOrganizationRunner(context.Background(), "o", 23)
   288  	if err != nil {
   289  		t.Errorf("Actions.GetRunner returned error: %v", err)
   290  	}
   291  
   292  	want := &Runner{
   293  		ID:     Int64(23),
   294  		Name:   String("MBP"),
   295  		OS:     String("macos"),
   296  		Status: String("online"),
   297  	}
   298  	if !reflect.DeepEqual(runner, want) {
   299  		t.Errorf("Actions.GetRunner returned %+v, want %+v", runner, want)
   300  	}
   301  }
   302  
   303  func TestActionsService_CreateOrganizationRemoveToken(t *testing.T) {
   304  	client, mux, _, teardown := setup()
   305  	defer teardown()
   306  
   307  	mux.HandleFunc("/orgs/o/actions/runners/remove-token", func(w http.ResponseWriter, r *http.Request) {
   308  		testMethod(t, r, "POST")
   309  		fmt.Fprint(w, `{"token":"AABF3JGZDX3P5PMEXLND6TS6FCWO6","expires_at":"2020-01-29T12:13:35.123Z"}`)
   310  	})
   311  
   312  	token, _, err := client.Actions.CreateOrganizationRemoveToken(context.Background(), "o")
   313  	if err != nil {
   314  		t.Errorf("Actions.CreateRemoveToken returned error: %v", err)
   315  	}
   316  
   317  	want := &RemoveToken{Token: String("AABF3JGZDX3P5PMEXLND6TS6FCWO6"), ExpiresAt: &Timestamp{time.Date(2020, time.January, 29, 12, 13, 35, 123000000, time.UTC)}}
   318  	if !reflect.DeepEqual(token, want) {
   319  		t.Errorf("Actions.CreateRemoveToken returned %+v, want %+v", token, want)
   320  	}
   321  }
   322  
   323  func TestActionsService_RemoveOrganizationRunner(t *testing.T) {
   324  	client, mux, _, teardown := setup()
   325  	defer teardown()
   326  
   327  	mux.HandleFunc("/orgs/o/actions/runners/21", func(w http.ResponseWriter, r *http.Request) {
   328  		testMethod(t, r, "DELETE")
   329  	})
   330  
   331  	_, err := client.Actions.RemoveOrganizationRunner(context.Background(), "o", 21)
   332  	if err != nil {
   333  		t.Errorf("Actions.RemoveOganizationRunner returned error: %v", err)
   334  	}
   335  }