github.com/drone/runner-go@v1.12.0/pipeline/reporter/history/history_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 history
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/drone/drone-go/drone"
    12  	"github.com/drone/runner-go/pipeline"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestReportStage(t *testing.T) {
    18  	r := &drone.Repo{ID: 1}
    19  	b := &drone.Build{ID: 2, Params: map[string]string{}}
    20  	s := &drone.Stage{ID: 3, Labels: map[string]string{}}
    21  
    22  	v := New(&nopReporter{})
    23  	v.ReportStage(nil, &pipeline.State{
    24  		Repo:  r,
    25  		Build: b,
    26  		Stage: s,
    27  	})
    28  
    29  	if v.items[0].Repo == r {
    30  		t.Errorf("Expect copy of repository")
    31  	}
    32  	if v.items[0].Build == b {
    33  		t.Errorf("Expect copy of build")
    34  	}
    35  	if v.items[0].Stage == s {
    36  		t.Errorf("Expect copy of stage")
    37  	}
    38  
    39  	if diff := cmp.Diff(v.items[0].Repo, r); diff != "" {
    40  		t.Errorf("Expect repository data copied")
    41  		t.Log(diff)
    42  	}
    43  	if diff := cmp.Diff(v.items[0].Build, b); diff != "" {
    44  		t.Errorf("Expect build data copied")
    45  		t.Log(diff)
    46  	}
    47  	if diff := cmp.Diff(v.items[0].Stage, s); diff != "" {
    48  		t.Errorf("Expect stage data copied")
    49  		t.Log(diff)
    50  	}
    51  	if v.items[0].Updated.IsZero() {
    52  		t.Errorf("Expect created timestamp non-zero")
    53  	}
    54  	if v.items[0].Created.IsZero() {
    55  		t.Errorf("Expect updated timestamp non-zero")
    56  	}
    57  }
    58  
    59  func TestReportStep(t *testing.T) {
    60  	r := &drone.Repo{ID: 1}
    61  	b := &drone.Build{ID: 2, Params: map[string]string{}}
    62  	s := &drone.Stage{ID: 3, Labels: map[string]string{}}
    63  
    64  	v := New(&nopReporter{})
    65  	v.ReportStep(nil, &pipeline.State{
    66  		Repo:  r,
    67  		Build: b,
    68  		Stage: s,
    69  	}, "foo")
    70  
    71  	if v.items[0].Repo == r {
    72  		t.Errorf("Expect copy of repository")
    73  	}
    74  	if v.items[0].Build == b {
    75  		t.Errorf("Expect copy of build")
    76  	}
    77  	if v.items[0].Stage == s {
    78  		t.Errorf("Expect copy of stage")
    79  	}
    80  
    81  	if diff := cmp.Diff(v.items[0].Repo, r); diff != "" {
    82  		t.Errorf("Expect repository data copied")
    83  		t.Log(diff)
    84  	}
    85  	if diff := cmp.Diff(v.items[0].Build, b); diff != "" {
    86  		t.Errorf("Expect build data copied")
    87  		t.Log(diff)
    88  	}
    89  	if diff := cmp.Diff(v.items[0].Stage, s); diff != "" {
    90  		t.Errorf("Expect stage data copied")
    91  		t.Log(diff)
    92  	}
    93  	if v.items[0].Updated.IsZero() {
    94  		t.Errorf("Expect created timestamp non-zero")
    95  	}
    96  	if v.items[0].Created.IsZero() {
    97  		t.Errorf("Expect updated timestamp non-zero")
    98  	}
    99  }
   100  
   101  func TestEntries(t *testing.T) {
   102  	s := new(drone.Stage)
   103  	v := History{}
   104  	v.items = append(v.items, &Entry{Stage: s})
   105  
   106  	list := v.Entries()
   107  	if got, want := len(list), len(v.items); got != want {
   108  		t.Errorf("Want %d entries, got %d", want, got)
   109  	}
   110  
   111  	if v.items[0] == list[0] {
   112  		t.Errorf("Expect copy of Entry, got reference")
   113  	}
   114  	if v.items[0].Stage != list[0].Stage {
   115  		t.Errorf("Expect reference to Stage, got copy")
   116  	}
   117  }
   118  
   119  func TestEntry(t *testing.T) {
   120  	s1 := &drone.Stage{ID: 1}
   121  	s2 := &drone.Stage{ID: 2}
   122  	v := History{}
   123  	v.items = append(v.items, &Entry{Stage: s1}, &Entry{Stage: s2})
   124  
   125  	if got := v.Entry(99); got != nil {
   126  		t.Errorf("Want nil when stage not found")
   127  	}
   128  	if got := v.Entry(s1.ID); got == nil {
   129  		t.Errorf("Want entry by stage ID, got nil")
   130  		return
   131  	}
   132  }
   133  
   134  func TestLimit(t *testing.T) {
   135  	v := History{}
   136  	if got, want := v.Limit(), defaultLimit; got != want {
   137  		t.Errorf("Want default limit %d, got %d", want, got)
   138  	}
   139  	v.limit = 5
   140  	if got, want := v.Limit(), 5; got != want {
   141  		t.Errorf("Want custom limit %d, got %d", want, got)
   142  	}
   143  }
   144  
   145  func TestInsert(t *testing.T) {
   146  	stage := &drone.Stage{ID: 42, Labels: map[string]string{}}
   147  	state := &pipeline.State{
   148  		Stage: stage,
   149  		Repo:  &drone.Repo{},
   150  		Build: &drone.Build{},
   151  	}
   152  
   153  	v := History{}
   154  	v.update(state)
   155  
   156  	if v.items[0].Stage == stage {
   157  		t.Errorf("Expect stage replaced")
   158  	}
   159  	if v.items[0].Updated.IsZero() {
   160  		t.Errorf("Expect entry timestamp updated")
   161  	}
   162  	if diff := cmp.Diff(v.items[0].Stage, stage); diff != "" {
   163  		t.Errorf("Expect stage data copied")
   164  		t.Log(diff)
   165  	}
   166  }
   167  
   168  func TestUpdate(t *testing.T) {
   169  	stage1 := &drone.Stage{ID: 42, Labels: map[string]string{}}
   170  	stage2 := &drone.Stage{ID: 42, Labels: map[string]string{}}
   171  	state := &pipeline.State{
   172  		Stage: stage1,
   173  		Repo:  &drone.Repo{},
   174  		Build: &drone.Build{},
   175  	}
   176  
   177  	v := History{}
   178  	v.items = append(v.items, &Entry{Stage: stage1})
   179  	v.update(state)
   180  
   181  	if v.items[0].Stage == stage1 {
   182  		t.Errorf("Expect stage replaced")
   183  	}
   184  	if v.items[0].Stage == stage2 {
   185  		t.Errorf("Expect stage replaced")
   186  	}
   187  	if v.items[0].Updated.IsZero() {
   188  		t.Errorf("Expect entry timestamp updated")
   189  	}
   190  	if diff := cmp.Diff(v.items[0].Stage, stage1); diff != "" {
   191  		t.Errorf("Expect stage data copied")
   192  		t.Log(diff)
   193  	}
   194  }
   195  
   196  func TestPrune(t *testing.T) {
   197  	v := History{}
   198  	v.limit = 3
   199  	v.items = append(v.items, nil)
   200  	v.items = append(v.items, nil)
   201  	v.items = append(v.items, nil)
   202  	v.items = append(v.items, nil)
   203  	v.items = append(v.items, nil)
   204  	v.prune()
   205  	if got, want := len(v.items), 2; got != want {
   206  		t.Errorf("Want pruned entry len %d, got %d", want, got)
   207  	}
   208  }
   209  
   210  type nopReporter struct{}
   211  
   212  func (*nopReporter) ReportStage(context.Context, *pipeline.State) error        { return nil }
   213  func (*nopReporter) ReportStep(context.Context, *pipeline.State, string) error { return nil }