github.com/google/go-github/v65@v65.0.0/github/enterprise_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  	"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{"name": "MBP", "per_page": "2", "page": "2"})
   110  		fmt.Fprint(w, `{"total_count":1,"runners":[{"id":23,"name":"MBP","os":"macos","status":"online"}]}`)
   111  	})
   112  
   113  	opts := &ListRunnersOptions{
   114  		Name:        String("MBP"),
   115  		ListOptions: ListOptions{Page: 2, PerPage: 2},
   116  	}
   117  	ctx := context.Background()
   118  	runners, _, err := client.Enterprise.ListRunners(ctx, "e", opts)
   119  	if err != nil {
   120  		t.Errorf("Enterprise.ListRunners returned error: %v", err)
   121  	}
   122  
   123  	want := &Runners{
   124  		TotalCount: 1,
   125  		Runners: []*Runner{
   126  			{ID: Int64(23), Name: String("MBP"), OS: String("macos"), Status: String("online")},
   127  		},
   128  	}
   129  	if !cmp.Equal(runners, want) {
   130  		t.Errorf("Actions.ListRunners returned %+v, want %+v", runners, want)
   131  	}
   132  
   133  	const methodName = "ListRunners"
   134  	testBadOptions(t, methodName, func() (err error) {
   135  		_, _, err = client.Enterprise.ListRunners(ctx, "\n", &ListRunnersOptions{})
   136  		return err
   137  	})
   138  
   139  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   140  		got, resp, err := client.Enterprise.ListRunners(ctx, "e", nil)
   141  		if got != nil {
   142  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   143  		}
   144  		return resp, err
   145  	})
   146  }
   147  
   148  func TestEnterpriseService_GetRunner(t *testing.T) {
   149  	client, mux, _, teardown := setup()
   150  	defer teardown()
   151  
   152  	mux.HandleFunc("/enterprises/e/actions/runners/23", func(w http.ResponseWriter, r *http.Request) {
   153  		testMethod(t, r, "GET")
   154  		fmt.Fprint(w, `{"id":23,"name":"MBP","os":"macos","status":"online"}`)
   155  	})
   156  
   157  	ctx := context.Background()
   158  	runner, _, err := client.Enterprise.GetRunner(ctx, "e", 23)
   159  	if err != nil {
   160  		t.Errorf("Enterprise.GetRunner returned error: %v", err)
   161  	}
   162  
   163  	want := &Runner{
   164  		ID:     Int64(23),
   165  		Name:   String("MBP"),
   166  		OS:     String("macos"),
   167  		Status: String("online"),
   168  	}
   169  	if !cmp.Equal(runner, want) {
   170  		t.Errorf("Enterprise.GetRunner returned %+v, want %+v", runner, want)
   171  	}
   172  
   173  	const methodName = "GetRunner"
   174  	testBadOptions(t, methodName, func() (err error) {
   175  		_, _, err = client.Enterprise.GetRunner(ctx, "\n", 23)
   176  		return err
   177  	})
   178  
   179  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   180  		got, resp, err := client.Enterprise.GetRunner(ctx, "e", 23)
   181  		if got != nil {
   182  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   183  		}
   184  		return resp, err
   185  	})
   186  }
   187  
   188  func TestEnterpriseService_RemoveRunner(t *testing.T) {
   189  	client, mux, _, teardown := setup()
   190  	defer teardown()
   191  
   192  	mux.HandleFunc("/enterprises/o/actions/runners/21", func(w http.ResponseWriter, r *http.Request) {
   193  		testMethod(t, r, "DELETE")
   194  	})
   195  
   196  	ctx := context.Background()
   197  	_, err := client.Enterprise.RemoveRunner(ctx, "o", 21)
   198  	if err != nil {
   199  		t.Errorf("Actions.RemoveRunner returned error: %v", err)
   200  	}
   201  
   202  	const methodName = "RemoveRunner"
   203  	testBadOptions(t, methodName, func() (err error) {
   204  		_, err = client.Enterprise.RemoveRunner(ctx, "\n", 21)
   205  		return err
   206  	})
   207  
   208  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   209  		return client.Enterprise.RemoveRunner(ctx, "o", 21)
   210  	})
   211  }
   212  
   213  func TestEnterpriseService_ListRunnerApplicationDownloads(t *testing.T) {
   214  	client, mux, _, teardown := setup()
   215  	defer teardown()
   216  
   217  	mux.HandleFunc("/enterprises/o/actions/runners/downloads", func(w http.ResponseWriter, r *http.Request) {
   218  		testMethod(t, r, "GET")
   219  		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"}]`)
   220  	})
   221  
   222  	ctx := context.Background()
   223  	downloads, _, err := client.Enterprise.ListRunnerApplicationDownloads(ctx, "o")
   224  	if err != nil {
   225  		t.Errorf("Enterprise.ListRunnerApplicationDownloads returned error: %v", err)
   226  	}
   227  
   228  	want := []*RunnerApplicationDownload{
   229  		{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")},
   230  		{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")},
   231  		{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")},
   232  		{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")},
   233  		{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")},
   234  	}
   235  	if !cmp.Equal(downloads, want) {
   236  		t.Errorf("Enterprise.ListRunnerApplicationDownloads returned %+v, want %+v", downloads, want)
   237  	}
   238  
   239  	const methodName = "ListRunnerApplicationDownloads"
   240  	testBadOptions(t, methodName, func() (err error) {
   241  		_, _, err = client.Enterprise.ListRunnerApplicationDownloads(ctx, "\n")
   242  		return err
   243  	})
   244  
   245  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   246  		got, resp, err := client.Enterprise.ListRunnerApplicationDownloads(ctx, "o")
   247  		if got != nil {
   248  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   249  		}
   250  		return resp, err
   251  	})
   252  }