...

Source file src/github.com/google/go-github/v55/github/actions_workflow_jobs_test.go

Documentation: github.com/google/go-github/v55/github

     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  	"errors"
    11  	"fmt"
    12  	"net/http"
    13  	"net/url"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/google/go-cmp/cmp"
    18  )
    19  
    20  func TestActionsService_ListWorkflowJobs(t *testing.T) {
    21  	client, mux, _, teardown := setup()
    22  	defer teardown()
    23  
    24  	mux.HandleFunc("/repos/o/r/actions/runs/29679449/jobs", func(w http.ResponseWriter, r *http.Request) {
    25  		testMethod(t, r, "GET")
    26  		testFormValues(t, r, values{"per_page": "2", "page": "2"})
    27  		fmt.Fprint(w, `{"total_count":4,"jobs":[{"id":399444496,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}]}`)
    28  	})
    29  
    30  	opts := &ListWorkflowJobsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}}
    31  	ctx := context.Background()
    32  	jobs, _, err := client.Actions.ListWorkflowJobs(ctx, "o", "r", 29679449, opts)
    33  	if err != nil {
    34  		t.Errorf("Actions.ListWorkflowJobs returned error: %v", err)
    35  	}
    36  
    37  	want := &Jobs{
    38  		TotalCount: Int(4),
    39  		Jobs: []*WorkflowJob{
    40  			{ID: Int64(399444496), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    41  			{ID: Int64(399444497), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    42  		},
    43  	}
    44  	if !cmp.Equal(jobs, want) {
    45  		t.Errorf("Actions.ListWorkflowJobs returned %+v, want %+v", jobs, want)
    46  	}
    47  
    48  	const methodName = "ListWorkflowJobs"
    49  	testBadOptions(t, methodName, func() (err error) {
    50  		_, _, err = client.Actions.ListWorkflowJobs(ctx, "\n", "\n", 29679449, opts)
    51  		return err
    52  	})
    53  
    54  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    55  		got, resp, err := client.Actions.ListWorkflowJobs(ctx, "o", "r", 29679449, opts)
    56  		if got != nil {
    57  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    58  		}
    59  		return resp, err
    60  	})
    61  }
    62  
    63  func TestActionsService_ListWorkflowJobs_Filter(t *testing.T) {
    64  	client, mux, _, teardown := setup()
    65  	defer teardown()
    66  
    67  	mux.HandleFunc("/repos/o/r/actions/runs/29679449/jobs", func(w http.ResponseWriter, r *http.Request) {
    68  		testMethod(t, r, "GET")
    69  		testFormValues(t, r, values{"filter": "all", "per_page": "2", "page": "2"})
    70  		fmt.Fprint(w, `{"total_count":4,"jobs":[{"id":399444496,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}]}`)
    71  	})
    72  
    73  	opts := &ListWorkflowJobsOptions{Filter: "all", ListOptions: ListOptions{Page: 2, PerPage: 2}}
    74  	ctx := context.Background()
    75  	jobs, _, err := client.Actions.ListWorkflowJobs(ctx, "o", "r", 29679449, opts)
    76  	if err != nil {
    77  		t.Errorf("Actions.ListWorkflowJobs returned error: %v", err)
    78  	}
    79  
    80  	want := &Jobs{
    81  		TotalCount: Int(4),
    82  		Jobs: []*WorkflowJob{
    83  			{ID: Int64(399444496), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    84  			{ID: Int64(399444497), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    85  		},
    86  	}
    87  	if !cmp.Equal(jobs, want) {
    88  		t.Errorf("Actions.ListWorkflowJobs returned %+v, want %+v", jobs, want)
    89  	}
    90  }
    91  
    92  func TestActionsService_GetWorkflowJobByID(t *testing.T) {
    93  	client, mux, _, teardown := setup()
    94  	defer teardown()
    95  
    96  	mux.HandleFunc("/repos/o/r/actions/jobs/399444496", func(w http.ResponseWriter, r *http.Request) {
    97  		testMethod(t, r, "GET")
    98  		fmt.Fprint(w, `{"id":399444496,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}`)
    99  	})
   100  
   101  	ctx := context.Background()
   102  	job, _, err := client.Actions.GetWorkflowJobByID(ctx, "o", "r", 399444496)
   103  	if err != nil {
   104  		t.Errorf("Actions.GetWorkflowJobByID returned error: %v", err)
   105  	}
   106  
   107  	want := &WorkflowJob{
   108  		ID:          Int64(399444496),
   109  		StartedAt:   &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
   110  		CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
   111  	}
   112  	if !cmp.Equal(job, want) {
   113  		t.Errorf("Actions.GetWorkflowJobByID returned %+v, want %+v", job, want)
   114  	}
   115  
   116  	const methodName = "GetWorkflowJobByID"
   117  	testBadOptions(t, methodName, func() (err error) {
   118  		_, _, err = client.Actions.GetWorkflowJobByID(ctx, "\n", "\n", 399444496)
   119  		return err
   120  	})
   121  
   122  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   123  		got, resp, err := client.Actions.GetWorkflowJobByID(ctx, "o", "r", 399444496)
   124  		if got != nil {
   125  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   126  		}
   127  		return resp, err
   128  	})
   129  }
   130  
   131  func TestActionsService_GetWorkflowJobLogs(t *testing.T) {
   132  	client, mux, _, teardown := setup()
   133  	defer teardown()
   134  
   135  	mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
   136  		testMethod(t, r, "GET")
   137  		http.Redirect(w, r, "http://github.com/a", http.StatusFound)
   138  	})
   139  
   140  	ctx := context.Background()
   141  	url, resp, err := client.Actions.GetWorkflowJobLogs(ctx, "o", "r", 399444496, true)
   142  	if err != nil {
   143  		t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err)
   144  	}
   145  	if resp.StatusCode != http.StatusFound {
   146  		t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound)
   147  	}
   148  	want := "http://github.com/a"
   149  	if url.String() != want {
   150  		t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want)
   151  	}
   152  
   153  	const methodName = "GetWorkflowJobLogs"
   154  	testBadOptions(t, methodName, func() (err error) {
   155  		_, _, err = client.Actions.GetWorkflowJobLogs(ctx, "\n", "\n", 399444496, true)
   156  		return err
   157  	})
   158  
   159  	// Add custom round tripper
   160  	client.client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
   161  		return nil, errors.New("failed to get workflow logs")
   162  	})
   163  	testBadOptions(t, methodName, func() (err error) {
   164  		_, _, err = client.Actions.GetWorkflowJobLogs(ctx, "o", "r", 399444496, true)
   165  		return err
   166  	})
   167  }
   168  
   169  func TestActionsService_GetWorkflowJobLogs_StatusMovedPermanently_dontFollowRedirects(t *testing.T) {
   170  	client, mux, _, teardown := setup()
   171  	defer teardown()
   172  
   173  	mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
   174  		testMethod(t, r, "GET")
   175  		http.Redirect(w, r, "http://github.com/a", http.StatusMovedPermanently)
   176  	})
   177  
   178  	ctx := context.Background()
   179  	_, resp, _ := client.Actions.GetWorkflowJobLogs(ctx, "o", "r", 399444496, false)
   180  	if resp.StatusCode != http.StatusMovedPermanently {
   181  		t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusMovedPermanently)
   182  	}
   183  }
   184  
   185  func TestActionsService_GetWorkflowJobLogs_StatusMovedPermanently_followRedirects(t *testing.T) {
   186  	client, mux, serverURL, teardown := setup()
   187  	defer teardown()
   188  
   189  	// Mock a redirect link, which leads to an archive link
   190  	mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
   191  		testMethod(t, r, "GET")
   192  		redirectURL, _ := url.Parse(serverURL + baseURLPath + "/redirect")
   193  		http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently)
   194  	})
   195  
   196  	mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
   197  		testMethod(t, r, "GET")
   198  		http.Redirect(w, r, "http://github.com/a", http.StatusFound)
   199  	})
   200  
   201  	ctx := context.Background()
   202  	url, resp, err := client.Actions.GetWorkflowJobLogs(ctx, "o", "r", 399444496, true)
   203  	if err != nil {
   204  		t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err)
   205  	}
   206  
   207  	if resp.StatusCode != http.StatusFound {
   208  		t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound)
   209  	}
   210  
   211  	want := "http://github.com/a"
   212  	if url.String() != want {
   213  		t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want)
   214  	}
   215  }
   216  
   217  func TestTaskStep_Marshal(t *testing.T) {
   218  	testJSONMarshal(t, &TaskStep{}, "{}")
   219  
   220  	u := &TaskStep{
   221  		Name:        String("n"),
   222  		Status:      String("s"),
   223  		Conclusion:  String("c"),
   224  		Number:      Int64(1),
   225  		StartedAt:   &Timestamp{referenceTime},
   226  		CompletedAt: &Timestamp{referenceTime},
   227  	}
   228  
   229  	want := `{
   230  		"name": "n",
   231  		"status": "s",
   232  		"conclusion": "c",
   233  		"number": 1,
   234  		"started_at": ` + referenceTimeStr + `,
   235  		"completed_at": ` + referenceTimeStr + `
   236  	}`
   237  
   238  	testJSONMarshal(t, u, want)
   239  }
   240  
   241  func TestWorkflowJob_Marshal(t *testing.T) {
   242  	testJSONMarshal(t, &WorkflowJob{}, "{}")
   243  
   244  	u := &WorkflowJob{
   245  		ID:          Int64(1),
   246  		RunID:       Int64(1),
   247  		RunURL:      String("r"),
   248  		NodeID:      String("n"),
   249  		HeadBranch:  String("b"),
   250  		HeadSHA:     String("h"),
   251  		URL:         String("u"),
   252  		HTMLURL:     String("h"),
   253  		Status:      String("s"),
   254  		Conclusion:  String("c"),
   255  		CreatedAt:   &Timestamp{referenceTime},
   256  		StartedAt:   &Timestamp{referenceTime},
   257  		CompletedAt: &Timestamp{referenceTime},
   258  		Name:        String("n"),
   259  		Steps: []*TaskStep{
   260  			{
   261  				Name:        String("n"),
   262  				Status:      String("s"),
   263  				Conclusion:  String("c"),
   264  				Number:      Int64(1),
   265  				StartedAt:   &Timestamp{referenceTime},
   266  				CompletedAt: &Timestamp{referenceTime},
   267  			},
   268  		},
   269  		CheckRunURL:  String("c"),
   270  		WorkflowName: String("w"),
   271  	}
   272  
   273  	want := `{
   274  		"id": 1,
   275  		"run_id": 1,
   276  		"run_url": "r",
   277  		"node_id": "n",
   278  		"head_branch": "b",
   279  		"head_sha": "h",
   280  		"url": "u",
   281  		"html_url": "h",
   282  		"status": "s",
   283  		"conclusion": "c",
   284  		"created_at": ` + referenceTimeStr + `,
   285  		"started_at": ` + referenceTimeStr + `,
   286  		"completed_at": ` + referenceTimeStr + `,
   287  		"name": "n",
   288  		"steps": [{
   289  			"name": "n",
   290  			"status": "s",
   291  			"conclusion": "c",
   292  			"number": 1,
   293  			"started_at": ` + referenceTimeStr + `,
   294  			"completed_at": ` + referenceTimeStr + `
   295  		}],
   296  		"check_run_url": "c",
   297  		"workflow_name": "w"
   298  	}`
   299  
   300  	testJSONMarshal(t, u, want)
   301  }
   302  
   303  func TestJobs_Marshal(t *testing.T) {
   304  	testJSONMarshal(t, &Jobs{}, "{}")
   305  
   306  	u := &Jobs{
   307  		TotalCount: Int(1),
   308  		Jobs: []*WorkflowJob{
   309  			{
   310  				ID:          Int64(1),
   311  				RunID:       Int64(1),
   312  				RunURL:      String("r"),
   313  				NodeID:      String("n"),
   314  				HeadBranch:  String("b"),
   315  				HeadSHA:     String("h"),
   316  				URL:         String("u"),
   317  				HTMLURL:     String("h"),
   318  				Status:      String("s"),
   319  				Conclusion:  String("c"),
   320  				CreatedAt:   &Timestamp{referenceTime},
   321  				StartedAt:   &Timestamp{referenceTime},
   322  				CompletedAt: &Timestamp{referenceTime},
   323  				Name:        String("n"),
   324  				Steps: []*TaskStep{
   325  					{
   326  						Name:        String("n"),
   327  						Status:      String("s"),
   328  						Conclusion:  String("c"),
   329  						Number:      Int64(1),
   330  						StartedAt:   &Timestamp{referenceTime},
   331  						CompletedAt: &Timestamp{referenceTime},
   332  					},
   333  				},
   334  				CheckRunURL:  String("c"),
   335  				RunAttempt:   Int64(2),
   336  				WorkflowName: String("w"),
   337  			},
   338  		},
   339  	}
   340  
   341  	want := `{
   342  		"total_count": 1,
   343  		"jobs": [{
   344  			"id": 1,
   345  			"run_id": 1,
   346  			"run_url": "r",
   347  			"node_id": "n",
   348  			"head_branch": "b",
   349  			"head_sha": "h",
   350  			"url": "u",
   351  			"html_url": "h",
   352  			"status": "s",
   353  			"conclusion": "c",
   354  			"created_at": ` + referenceTimeStr + `,
   355  			"started_at": ` + referenceTimeStr + `,
   356  			"completed_at": ` + referenceTimeStr + `,
   357  			"name": "n",
   358  			"steps": [{
   359  				"name": "n",
   360  				"status": "s",
   361  				"conclusion": "c",
   362  				"number": 1,
   363  				"started_at": ` + referenceTimeStr + `,
   364  				"completed_at": ` + referenceTimeStr + `
   365  			}],
   366  			"check_run_url": "c",
   367  			"run_attempt": 2,
   368  			"workflow_name": "w"
   369  		}]
   370  	}`
   371  
   372  	testJSONMarshal(t, u, want)
   373  }
   374  

View as plain text