github.com/google/go-github/v49@v49.1.0/github/actions_cache_test.go (about)

     1  // Copyright 2022 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  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestActionsService_ListCaches(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/repos/o/r/actions/caches", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{"page": "2"})
    24  		fmt.Fprint(w,
    25  			`{
    26  				"total_count":1,
    27  				"actions_caches":[{"id":1}]
    28  			}`,
    29  		)
    30  	})
    31  
    32  	opts := &ActionsCacheListOptions{ListOptions: ListOptions{Page: 2}}
    33  	ctx := context.Background()
    34  	cacheList, _, err := client.Actions.ListCaches(ctx, "o", "r", opts)
    35  	if err != nil {
    36  		t.Errorf("Actions.ListCaches returned error: %v", err)
    37  	}
    38  
    39  	want := &ActionsCacheList{TotalCount: 1, ActionsCaches: []*ActionsCache{{ID: Int64(1)}}}
    40  	if !cmp.Equal(cacheList, want) {
    41  		t.Errorf("Actions.ListCaches returned %+v, want %+v", cacheList, want)
    42  	}
    43  
    44  	const methodName = "ListCaches"
    45  	testBadOptions(t, methodName, func() (err error) {
    46  		_, _, err = client.Actions.ListCaches(ctx, "\n", "\n", opts)
    47  		return err
    48  	})
    49  
    50  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    51  		got, resp, err := client.Actions.ListCaches(ctx, "o", "r", opts)
    52  		if got != nil {
    53  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    54  		}
    55  		return resp, err
    56  	})
    57  }
    58  
    59  func TestActionsService_ListCaches_invalidOwner(t *testing.T) {
    60  	client, _, _, teardown := setup()
    61  	defer teardown()
    62  
    63  	ctx := context.Background()
    64  	_, _, err := client.Actions.ListCaches(ctx, "%", "r", nil)
    65  	testURLParseError(t, err)
    66  }
    67  
    68  func TestActionsService_ListCaches_invalidRepo(t *testing.T) {
    69  	client, _, _, teardown := setup()
    70  	defer teardown()
    71  
    72  	ctx := context.Background()
    73  	_, _, err := client.Actions.ListCaches(ctx, "o", "%", nil)
    74  	testURLParseError(t, err)
    75  }
    76  
    77  func TestActionsService_ListCaches_notFound(t *testing.T) {
    78  	client, mux, _, teardown := setup()
    79  	defer teardown()
    80  
    81  	mux.HandleFunc("/repos/o/r/actions/caches", func(w http.ResponseWriter, r *http.Request) {
    82  		testMethod(t, r, "GET")
    83  		w.WriteHeader(http.StatusNotFound)
    84  	})
    85  
    86  	ctx := context.Background()
    87  	caches, resp, err := client.Actions.ListCaches(ctx, "o", "r", nil)
    88  	if err == nil {
    89  		t.Errorf("Expected HTTP 404 response")
    90  	}
    91  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
    92  		t.Errorf("Actions.ListCaches return status %d, want %d", got, want)
    93  	}
    94  	if caches != nil {
    95  		t.Errorf("Actions.ListCaches return %+v, want nil", caches)
    96  	}
    97  }
    98  
    99  func TestActionsService_DeleteCachesByKey(t *testing.T) {
   100  	client, mux, _, teardown := setup()
   101  	defer teardown()
   102  
   103  	mux.HandleFunc("/repos/o/r/actions/caches", func(w http.ResponseWriter, r *http.Request) {
   104  		testMethod(t, r, "DELETE")
   105  		testFormValues(t, r, values{"key": "1", "ref": "main"})
   106  	})
   107  
   108  	ctx := context.Background()
   109  	_, err := client.Actions.DeleteCachesByKey(ctx, "o", "r", "1", String("main"))
   110  	if err != nil {
   111  		t.Errorf("Actions.DeleteCachesByKey return error: %v", err)
   112  	}
   113  
   114  	const methodName = "DeleteCachesByKey"
   115  	testBadOptions(t, methodName, func() (err error) {
   116  		_, err = client.Actions.DeleteCachesByKey(ctx, "\n", "\n", "\n", String("\n"))
   117  		return err
   118  	})
   119  
   120  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   121  		return client.Actions.DeleteCachesByKey(ctx, "o", "r", "1", String("main"))
   122  	})
   123  }
   124  
   125  func TestActionsService_DeleteCachesByKey_invalidOwner(t *testing.T) {
   126  	client, _, _, teardown := setup()
   127  	defer teardown()
   128  
   129  	ctx := context.Background()
   130  	_, err := client.Actions.DeleteCachesByKey(ctx, "%", "r", "1", String("main"))
   131  	testURLParseError(t, err)
   132  }
   133  
   134  func TestActionsService_DeleteCachesByKey_invalidRepo(t *testing.T) {
   135  	client, _, _, teardown := setup()
   136  	defer teardown()
   137  
   138  	ctx := context.Background()
   139  	_, err := client.Actions.DeleteCachesByKey(ctx, "o", "%", "1", String("main"))
   140  	testURLParseError(t, err)
   141  }
   142  func TestActionsService_DeleteCachesByKey_notFound(t *testing.T) {
   143  	client, mux, _, teardown := setup()
   144  	defer teardown()
   145  
   146  	mux.HandleFunc("/repos/o/r/actions/artifacts/1", func(w http.ResponseWriter, r *http.Request) {
   147  		testMethod(t, r, "DELETE")
   148  		w.WriteHeader(http.StatusNotFound)
   149  	})
   150  
   151  	ctx := context.Background()
   152  	resp, err := client.Actions.DeleteCachesByKey(ctx, "o", "r", "1", String("main"))
   153  	if err == nil {
   154  		t.Errorf("Expected HTTP 404 response")
   155  	}
   156  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
   157  		t.Errorf("Actions.DeleteCachesByKey return status %d, want %d", got, want)
   158  	}
   159  }
   160  
   161  func TestActionsService_DeleteCachesByID(t *testing.T) {
   162  	client, mux, _, teardown := setup()
   163  	defer teardown()
   164  
   165  	mux.HandleFunc("/repos/o/r/actions/caches/1", func(w http.ResponseWriter, r *http.Request) {
   166  		testMethod(t, r, "DELETE")
   167  	})
   168  
   169  	ctx := context.Background()
   170  	_, err := client.Actions.DeleteCachesByID(ctx, "o", "r", 1)
   171  	if err != nil {
   172  		t.Errorf("Actions.DeleteCachesByID return error: %v", err)
   173  	}
   174  
   175  	const methodName = "DeleteCachesByID"
   176  	testBadOptions(t, methodName, func() (err error) {
   177  		_, err = client.Actions.DeleteCachesByID(ctx, "\n", "\n", 0)
   178  		return err
   179  	})
   180  
   181  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   182  		return client.Actions.DeleteCachesByID(ctx, "o", "r", 1)
   183  	})
   184  }
   185  
   186  func TestActionsService_DeleteCachesByID_invalidOwner(t *testing.T) {
   187  	client, _, _, teardown := setup()
   188  	defer teardown()
   189  
   190  	ctx := context.Background()
   191  	_, err := client.Actions.DeleteCachesByID(ctx, "%", "r", 1)
   192  	testURLParseError(t, err)
   193  }
   194  
   195  func TestActionsService_DeleteCachesByID_invalidRepo(t *testing.T) {
   196  	client, _, _, teardown := setup()
   197  	defer teardown()
   198  
   199  	ctx := context.Background()
   200  	_, err := client.Actions.DeleteCachesByID(ctx, "o", "%", 1)
   201  	testURLParseError(t, err)
   202  }
   203  
   204  func TestActionsService_DeleteCachesByID_notFound(t *testing.T) {
   205  	client, mux, _, teardown := setup()
   206  	defer teardown()
   207  
   208  	mux.HandleFunc("repos/o/r/actions/caches/1", func(w http.ResponseWriter, r *http.Request) {
   209  		testMethod(t, r, "DELETE")
   210  		w.WriteHeader(http.StatusNotFound)
   211  	})
   212  
   213  	ctx := context.Background()
   214  	resp, err := client.Actions.DeleteCachesByID(ctx, "o", "r", 1)
   215  	if err == nil {
   216  		t.Errorf("Expected HTTP 404 response")
   217  	}
   218  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
   219  		t.Errorf("Actions.DeleteCachesByID return status %d, want %d", got, want)
   220  	}
   221  }
   222  
   223  func TestActionsService_GetCacheUsageForRepo(t *testing.T) {
   224  	client, mux, _, teardown := setup()
   225  	defer teardown()
   226  
   227  	mux.HandleFunc("/repos/o/r/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) {
   228  		testMethod(t, r, "GET")
   229  		fmt.Fprint(w,
   230  			`{
   231  				"full_name":"test-cache",
   232  				"active_caches_size_in_bytes":1000,
   233  				"active_caches_count":1
   234  			}`,
   235  		)
   236  	})
   237  
   238  	ctx := context.Background()
   239  	cacheUse, _, err := client.Actions.GetCacheUsageForRepo(ctx, "o", "r")
   240  	if err != nil {
   241  		t.Errorf("Actions.GetCacheUsageForRepo returned error: %v", err)
   242  	}
   243  
   244  	want := &ActionsCacheUsage{FullName: "test-cache", ActiveCachesSizeInBytes: 1000, ActiveCachesCount: 1}
   245  	if !cmp.Equal(cacheUse, want) {
   246  		t.Errorf("Actions.GetCacheUsageForRepo returned %+v, want %+v", cacheUse, want)
   247  	}
   248  
   249  	const methodName = "GetCacheUsageForRepo"
   250  	testBadOptions(t, methodName, func() (err error) {
   251  		_, _, err = client.Actions.GetCacheUsageForRepo(ctx, "\n", "\n")
   252  		return err
   253  	})
   254  
   255  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   256  		got, resp, err := client.Actions.GetCacheUsageForRepo(ctx, "o", "r")
   257  		if got != nil {
   258  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   259  		}
   260  		return resp, err
   261  	})
   262  }
   263  
   264  func TestActionsService_GetCacheUsageForRepo_invalidOwner(t *testing.T) {
   265  	client, _, _, teardown := setup()
   266  	defer teardown()
   267  
   268  	ctx := context.Background()
   269  	_, _, err := client.Actions.GetCacheUsageForRepo(ctx, "%", "r")
   270  	testURLParseError(t, err)
   271  }
   272  
   273  func TestActionsService_GetCacheUsageForRepo_invalidRepo(t *testing.T) {
   274  	client, _, _, teardown := setup()
   275  	defer teardown()
   276  
   277  	ctx := context.Background()
   278  	_, _, err := client.Actions.GetCacheUsageForRepo(ctx, "o", "%")
   279  	testURLParseError(t, err)
   280  }
   281  
   282  func TestActionsService_GetCacheUsageForRepo_notFound(t *testing.T) {
   283  	client, mux, _, teardown := setup()
   284  	defer teardown()
   285  
   286  	mux.HandleFunc("/repos/o/r/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) {
   287  		testMethod(t, r, "GET")
   288  		w.WriteHeader(http.StatusNotFound)
   289  	})
   290  
   291  	ctx := context.Background()
   292  	caches, resp, err := client.Actions.GetCacheUsageForRepo(ctx, "o", "r")
   293  	if err == nil {
   294  		t.Errorf("Expected HTTP 404 response")
   295  	}
   296  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
   297  		t.Errorf("Actions.GetCacheUsageForRepo return status %d, want %d", got, want)
   298  	}
   299  	if caches != nil {
   300  		t.Errorf("Actions.GetCacheUsageForRepo return %+v, want nil", caches)
   301  	}
   302  }
   303  
   304  func TestActionsService_ListCacheUsageByRepoForOrg(t *testing.T) {
   305  	client, mux, _, teardown := setup()
   306  	defer teardown()
   307  
   308  	mux.HandleFunc("/orgs/o/actions/cache/usage-by-repository", func(w http.ResponseWriter, r *http.Request) {
   309  		testMethod(t, r, "GET")
   310  		testFormValues(t, r, values{"page": "2", "per_page": "1"})
   311  		fmt.Fprint(w,
   312  			`{
   313  				"total_count":1,
   314  				"repository_cache_usages":[{"full_name":"test-cache","active_caches_size_in_bytes":1000,"active_caches_count":1}]
   315  			}`,
   316  		)
   317  	})
   318  
   319  	opts := &ListOptions{PerPage: 1, Page: 2}
   320  	ctx := context.Background()
   321  	cacheList, _, err := client.Actions.ListCacheUsageByRepoForOrg(ctx, "o", opts)
   322  	if err != nil {
   323  		t.Errorf("Actions.ListCacheUsageByRepoForOrg returned error: %v", err)
   324  	}
   325  
   326  	want := &ActionsCacheUsageList{TotalCount: 1, RepoCacheUsage: []*ActionsCacheUsage{{FullName: "test-cache", ActiveCachesSizeInBytes: 1000, ActiveCachesCount: 1}}}
   327  	if !cmp.Equal(cacheList, want) {
   328  		t.Errorf("Actions.ListCacheUsageByRepoForOrg returned %+v, want %+v", cacheList, want)
   329  	}
   330  
   331  	const methodName = "ListCacheUsageByRepoForOrg"
   332  	testBadOptions(t, methodName, func() (err error) {
   333  		_, _, err = client.Actions.ListCacheUsageByRepoForOrg(ctx, "\n", opts)
   334  		return err
   335  	})
   336  
   337  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   338  		got, resp, err := client.Actions.ListCacheUsageByRepoForOrg(ctx, "o", opts)
   339  		if got != nil {
   340  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   341  		}
   342  		return resp, err
   343  	})
   344  }
   345  
   346  func TestActionsService_ListCacheUsageByRepoForOrg_invalidOrganization(t *testing.T) {
   347  	client, _, _, teardown := setup()
   348  	defer teardown()
   349  
   350  	ctx := context.Background()
   351  	_, _, err := client.Actions.ListCacheUsageByRepoForOrg(ctx, "%", nil)
   352  	testURLParseError(t, err)
   353  }
   354  
   355  func TestActionsService_ListCacheUsageByRepoForOrg_notFound(t *testing.T) {
   356  	client, mux, _, teardown := setup()
   357  	defer teardown()
   358  
   359  	mux.HandleFunc("/orgs/o/actions/cache/usage-by-repository", func(w http.ResponseWriter, r *http.Request) {
   360  		testMethod(t, r, "GET")
   361  		w.WriteHeader(http.StatusNotFound)
   362  	})
   363  
   364  	ctx := context.Background()
   365  	caches, resp, err := client.Actions.ListCacheUsageByRepoForOrg(ctx, "o", nil)
   366  	if err == nil {
   367  		t.Errorf("Expected HTTP 404 response")
   368  	}
   369  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
   370  		t.Errorf("Actions.ListCacheUsageByRepoForOrg return status %d, want %d", got, want)
   371  	}
   372  	if caches != nil {
   373  		t.Errorf("Actions.ListCacheUsageByRepoForOrg return %+v, want nil", caches)
   374  	}
   375  }
   376  
   377  func TestActionsService_GetCacheUsageForOrg(t *testing.T) {
   378  	client, mux, _, teardown := setup()
   379  	defer teardown()
   380  
   381  	mux.HandleFunc("/orgs/o/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) {
   382  		testMethod(t, r, "GET")
   383  		fmt.Fprint(w,
   384  			`{
   385  				"total_active_caches_size_in_bytes":1000,
   386  				"total_active_caches_count":1
   387  			}`,
   388  		)
   389  	})
   390  
   391  	ctx := context.Background()
   392  	cache, _, err := client.Actions.GetTotalCacheUsageForOrg(ctx, "o")
   393  	if err != nil {
   394  		t.Errorf("Actions.GetTotalCacheUsageForOrg returned error: %v", err)
   395  	}
   396  
   397  	want := &TotalCacheUsage{TotalActiveCachesUsageSizeInBytes: 1000, TotalActiveCachesCount: 1}
   398  	if !cmp.Equal(cache, want) {
   399  		t.Errorf("Actions.GetTotalCacheUsageForOrg returned %+v, want %+v", cache, want)
   400  	}
   401  
   402  	const methodName = "GetTotalCacheUsageForOrg"
   403  	testBadOptions(t, methodName, func() (err error) {
   404  		_, _, err = client.Actions.GetTotalCacheUsageForOrg(ctx, "\n")
   405  		return err
   406  	})
   407  
   408  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   409  		got, resp, err := client.Actions.GetTotalCacheUsageForOrg(ctx, "o")
   410  		if got != nil {
   411  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   412  		}
   413  		return resp, err
   414  	})
   415  }
   416  
   417  func TestActionsService_GetCacheUsageForOrg_invalidOrganization(t *testing.T) {
   418  	client, _, _, teardown := setup()
   419  	defer teardown()
   420  
   421  	ctx := context.Background()
   422  	_, _, err := client.Actions.GetTotalCacheUsageForOrg(ctx, "%")
   423  	testURLParseError(t, err)
   424  }
   425  
   426  func TestActionsService_GetCacheUsageForOrg_notFound(t *testing.T) {
   427  	client, mux, _, teardown := setup()
   428  	defer teardown()
   429  
   430  	mux.HandleFunc("/orgs/o/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) {
   431  		testMethod(t, r, "GET")
   432  		w.WriteHeader(http.StatusNotFound)
   433  	})
   434  
   435  	ctx := context.Background()
   436  	caches, resp, err := client.Actions.GetTotalCacheUsageForOrg(ctx, "o")
   437  	if err == nil {
   438  		t.Errorf("Expected HTTP 404 response")
   439  	}
   440  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
   441  		t.Errorf("Actions.GetTotalCacheUsageForOrg return status %d, want %d", got, want)
   442  	}
   443  	if caches != nil {
   444  		t.Errorf("Actions.GetTotalCacheUsageForOrg return %+v, want nil", caches)
   445  	}
   446  }
   447  
   448  func TestActionsService_GetCacheUsageForEnterprise(t *testing.T) {
   449  	client, mux, _, teardown := setup()
   450  	defer teardown()
   451  
   452  	mux.HandleFunc("/enterprises/e/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) {
   453  		testMethod(t, r, "GET")
   454  		fmt.Fprint(w,
   455  			`{
   456  				"total_active_caches_size_in_bytes":1000,
   457  				"total_active_caches_count":1
   458  			}`,
   459  		)
   460  	})
   461  
   462  	ctx := context.Background()
   463  	cache, _, err := client.Actions.GetTotalCacheUsageForEnterprise(ctx, "e")
   464  	if err != nil {
   465  		t.Errorf("Actions.GetTotalCacheUsageForEnterprise returned error: %v", err)
   466  	}
   467  
   468  	want := &TotalCacheUsage{TotalActiveCachesUsageSizeInBytes: 1000, TotalActiveCachesCount: 1}
   469  	if !cmp.Equal(cache, want) {
   470  		t.Errorf("Actions.GetTotalCacheUsageForEnterprise returned %+v, want %+v", cache, want)
   471  	}
   472  
   473  	const methodName = "GetTotalCacheUsageForEnterprise"
   474  	testBadOptions(t, methodName, func() (err error) {
   475  		_, _, err = client.Actions.GetTotalCacheUsageForEnterprise(ctx, "\n")
   476  		return err
   477  	})
   478  
   479  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   480  		got, resp, err := client.Actions.GetTotalCacheUsageForEnterprise(ctx, "e")
   481  		if got != nil {
   482  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   483  		}
   484  		return resp, err
   485  	})
   486  }
   487  
   488  func TestActionsService_GetCacheUsageForEnterprise_invalidEnterprise(t *testing.T) {
   489  	client, _, _, teardown := setup()
   490  	defer teardown()
   491  
   492  	ctx := context.Background()
   493  	_, _, err := client.Actions.GetTotalCacheUsageForEnterprise(ctx, "%")
   494  	testURLParseError(t, err)
   495  }
   496  
   497  func TestActionsService_GetCacheUsageForEnterprise_notFound(t *testing.T) {
   498  	client, mux, _, teardown := setup()
   499  	defer teardown()
   500  
   501  	mux.HandleFunc("/enterprises/e/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) {
   502  		testMethod(t, r, "GET")
   503  		w.WriteHeader(http.StatusNotFound)
   504  	})
   505  
   506  	ctx := context.Background()
   507  	caches, resp, err := client.Actions.GetTotalCacheUsageForEnterprise(ctx, "o")
   508  	if err == nil {
   509  		t.Errorf("Expected HTTP 404 response")
   510  	}
   511  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
   512  		t.Errorf("Actions.GetTotalCacheUsageForEnterprise return status %d, want %d", got, want)
   513  	}
   514  	if caches != nil {
   515  		t.Errorf("Actions.GetTotalCacheUsageForEnterprise return %+v, want nil", caches)
   516  	}
   517  }