github.com/drone/runner-go@v1.12.0/pipeline/state_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 pipeline
     6  
     7  import (
     8  	"errors"
     9  	"testing"
    10  
    11  	"github.com/drone/drone-go/drone"
    12  )
    13  
    14  func TestStateKill(t *testing.T) {
    15  	step := &drone.Step{Name: "clone", Status: drone.StatusPending}
    16  	state := &State{
    17  		Stage: &drone.Stage{
    18  			Steps: []*drone.Step{step},
    19  		},
    20  	}
    21  
    22  	state.kill(step)
    23  	if got, want := step.Status, drone.StatusPending; got != want {
    24  		t.Errorf("Expect a non-running step cannot be killed")
    25  	}
    26  
    27  	step.Status = drone.StatusRunning
    28  	state.kill(step)
    29  	if got, want := step.Status, drone.StatusKilled; got != want {
    30  		t.Errorf("Want status %s, got %s", want, got)
    31  	}
    32  	if got, want := step.Error, ""; got != want {
    33  		t.Errorf("Want error %q, got %q", want, got)
    34  	}
    35  	if got, want := step.ExitCode, 137; got != want {
    36  		t.Errorf("Want exit code %d, got %d", want, got)
    37  	}
    38  	if got, want := step.Stopped, step.Started; got != want {
    39  		t.Errorf("Want stopped %d, got %d", want, got)
    40  	}
    41  	if step.Started == 0 {
    42  		t.Errorf("Expect step started is non-zero value")
    43  	}
    44  }
    45  
    46  func TestStateKilled(t *testing.T) {
    47  	state := &State{}
    48  	state.Stage = &drone.Stage{Status: drone.StatusError}
    49  	if state.killed() == true {
    50  		t.Errorf("Expect killed false, got true")
    51  	}
    52  	state.Stage.Status = drone.StatusKilled
    53  	if state.killed() == false {
    54  		t.Errorf("Expect killed true, got false")
    55  	}
    56  }
    57  
    58  func TestStateFinished(t *testing.T) {
    59  	step := &drone.Step{Name: "test"}
    60  	state := &State{
    61  		Stage: &drone.Stage{
    62  			Steps: []*drone.Step{step},
    63  		},
    64  	}
    65  	step.Status = drone.StatusRunning
    66  	if state.Finished(step.Name) == true {
    67  		t.Errorf("Expect finished false")
    68  	}
    69  	step.Status = drone.StatusPending
    70  	if state.Finished(step.Name) == true {
    71  		t.Errorf("Expect finished false")
    72  	}
    73  	step.Status = drone.StatusKilled
    74  	if state.Finished(step.Name) == false {
    75  		t.Errorf("Expect finished true")
    76  	}
    77  	step.Status = drone.StatusSkipped
    78  	if state.Finished(step.Name) == false {
    79  		t.Errorf("Expect finished true")
    80  	}
    81  }
    82  
    83  func TestStateStarted(t *testing.T) {
    84  	step := &drone.Step{Name: "clone", Status: drone.StatusPending}
    85  	state := &State{
    86  		Stage: &drone.Stage{
    87  			Steps: []*drone.Step{step},
    88  		},
    89  	}
    90  
    91  	state.start(step)
    92  	if got, want := step.Status, drone.StatusRunning; got != want {
    93  		t.Errorf("Want status %s, got %s", want, got)
    94  	}
    95  	if got, want := step.Error, ""; got != want {
    96  		t.Errorf("Want error %q, got %q", want, got)
    97  	}
    98  	if got, want := step.ExitCode, 0; got != want {
    99  		t.Errorf("Want exit code %d, got %d", want, got)
   100  	}
   101  	if got, want := step.Stopped, int64(0); got != want {
   102  		t.Errorf("Want stopped %d, got %d", want, got)
   103  	}
   104  	if step.Started == 0 {
   105  		t.Errorf("Expect step started is non-zero value")
   106  	}
   107  }
   108  
   109  func TestStateFinish(t *testing.T) {
   110  	t.Skip()
   111  }
   112  
   113  func TestStateFail(t *testing.T) {
   114  	step := &drone.Step{Name: "clone"}
   115  	state := &State{
   116  		Stage: &drone.Stage{
   117  			Steps: []*drone.Step{step},
   118  		},
   119  	}
   120  	state.fail(step, errors.New("this is an error"))
   121  	if got, want := step.Status, drone.StatusError; got != want {
   122  		t.Errorf("Want status %s, got %s", want, got)
   123  	}
   124  	if got, want := step.Error, "this is an error"; got != want {
   125  		t.Errorf("Want error %q, got %q", want, got)
   126  	}
   127  	if got, want := step.ExitCode, 255; got != want {
   128  		t.Errorf("Want exit code %d, got %d", want, got)
   129  	}
   130  	if got, want := step.Stopped, step.Started; got != want {
   131  		t.Errorf("Want started %d, got %d", want, got)
   132  	}
   133  	if step.Stopped == 0 {
   134  		t.Errorf("Expect step stopped is non-zero value")
   135  	}
   136  }
   137  
   138  func TestStateFind(t *testing.T) {
   139  	step := &drone.Step{Name: "clone"}
   140  	state := &State{
   141  		Stage: &drone.Stage{
   142  			Steps: []*drone.Step{step},
   143  		},
   144  	}
   145  	if got, want := state.find("clone"), step; got != want {
   146  		t.Errorf("Expect find returns the named step")
   147  	}
   148  
   149  	defer func() {
   150  		if r := recover(); r == nil {
   151  			t.Errorf("Expect recover from panic")
   152  		}
   153  	}()
   154  
   155  	state.find("test")
   156  }