github.com/drone/runner-go@v1.12.0/pipeline/reporter/remote/remote_test.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package remote
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/drone/drone-go/drone"
    12  	"github.com/drone/runner-go/client"
    13  	"github.com/drone/runner-go/livelog"
    14  	"github.com/drone/runner-go/pipeline"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  var nocontext = context.Background()
    20  
    21  func TestReportStep(t *testing.T) {
    22  	step := &drone.Step{Name: "clone"}
    23  	state := &pipeline.State{
    24  		Stage: &drone.Stage{
    25  			Steps: []*drone.Step{step},
    26  		},
    27  	}
    28  
    29  	c := new(mockClient)
    30  	r := New(c)
    31  	err := r.ReportStep(nocontext, state, step.Name)
    32  	if err != nil {
    33  		t.Error(err)
    34  	}
    35  	if state.Stage.Steps[0] != step {
    36  		t.Errorf("Expect step updated, not replaced")
    37  	}
    38  	after := &drone.Step{
    39  		Name:    "clone",
    40  		ID:      1,
    41  		StageID: 2,
    42  		Started: 1561256080,
    43  		Stopped: 1561256090,
    44  		Version: 42,
    45  	}
    46  	if diff := cmp.Diff(after, step); diff != "" {
    47  		t.Errorf("Expect response merged with step")
    48  		t.Log(diff)
    49  	}
    50  }
    51  
    52  func TestReportStage(t *testing.T) {
    53  	stage := &drone.Stage{
    54  		Created: 0,
    55  		Updated: 0,
    56  		Version: 0,
    57  	}
    58  	state := &pipeline.State{
    59  		Stage: stage,
    60  	}
    61  
    62  	c := new(mockClient)
    63  	r := New(c)
    64  	err := r.ReportStage(nocontext, state)
    65  	if err != nil {
    66  		t.Error(err)
    67  	}
    68  	if state.Stage != stage {
    69  		t.Errorf("Expect stage updated, not replaced")
    70  	}
    71  	after := &drone.Stage{
    72  		Created: 1561256080,
    73  		Updated: 1561256090,
    74  		Version: 42,
    75  	}
    76  	if diff := cmp.Diff(after, state.Stage); diff != "" {
    77  		t.Errorf("Expect response merged with stage")
    78  		t.Log(diff)
    79  	}
    80  }
    81  
    82  func TestStream(t *testing.T) {
    83  	state := &pipeline.State{
    84  		Stage: &drone.Stage{
    85  			Steps: []*drone.Step{
    86  				{
    87  					ID:   1,
    88  					Name: "clone",
    89  				},
    90  			},
    91  		},
    92  	}
    93  
    94  	c := new(mockClient)
    95  	r := New(c)
    96  	w := r.Stream(nocontext, state, "clone")
    97  
    98  	if _, ok := w.(*livelog.Writer); !ok {
    99  		t.Errorf("Expect livelog writer")
   100  	}
   101  }
   102  
   103  type mockClient struct {
   104  	*client.HTTPClient
   105  }
   106  
   107  func (m *mockClient) Update(_ context.Context, stage *drone.Stage) error {
   108  	stage.Version = 42
   109  	stage.Created = 1561256080
   110  	stage.Updated = 1561256090
   111  	return nil
   112  }
   113  
   114  func (m *mockClient) UpdateStep(_ context.Context, step *drone.Step) error {
   115  	step.ID = 1
   116  	step.StageID = 2
   117  	step.Started = 1561256080
   118  	step.Stopped = 1561256090
   119  	step.Version = 42
   120  	return nil
   121  }