code.gitea.io/gitea@v1.22.3/modules/actions/task_state_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package actions
     5  
     6  import (
     7  	"testing"
     8  
     9  	actions_model "code.gitea.io/gitea/models/actions"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestFullSteps(t *testing.T) {
    15  	tests := []struct {
    16  		name string
    17  		task *actions_model.ActionTask
    18  		want []*actions_model.ActionTaskStep
    19  	}{
    20  		{
    21  			name: "regular",
    22  			task: &actions_model.ActionTask{
    23  				Steps: []*actions_model.ActionTaskStep{
    24  					{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
    25  				},
    26  				Status:    actions_model.StatusSuccess,
    27  				Started:   10000,
    28  				Stopped:   10100,
    29  				LogLength: 100,
    30  			},
    31  			want: []*actions_model.ActionTaskStep{
    32  				{Name: preStepName, Status: actions_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
    33  				{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
    34  				{Name: postStepName, Status: actions_model.StatusSuccess, LogIndex: 90, LogLength: 10, Started: 10090, Stopped: 10100},
    35  			},
    36  		},
    37  		{
    38  			name: "failed step",
    39  			task: &actions_model.ActionTask{
    40  				Steps: []*actions_model.ActionTaskStep{
    41  					{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 20, Started: 10010, Stopped: 10020},
    42  					{Status: actions_model.StatusFailure, LogIndex: 30, LogLength: 60, Started: 10020, Stopped: 10090},
    43  					{Status: actions_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
    44  				},
    45  				Status:    actions_model.StatusFailure,
    46  				Started:   10000,
    47  				Stopped:   10100,
    48  				LogLength: 100,
    49  			},
    50  			want: []*actions_model.ActionTaskStep{
    51  				{Name: preStepName, Status: actions_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
    52  				{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 20, Started: 10010, Stopped: 10020},
    53  				{Status: actions_model.StatusFailure, LogIndex: 30, LogLength: 60, Started: 10020, Stopped: 10090},
    54  				{Status: actions_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
    55  				{Name: postStepName, Status: actions_model.StatusFailure, LogIndex: 90, LogLength: 10, Started: 10090, Stopped: 10100},
    56  			},
    57  		},
    58  		{
    59  			name: "first step is running",
    60  			task: &actions_model.ActionTask{
    61  				Steps: []*actions_model.ActionTaskStep{
    62  					{Status: actions_model.StatusRunning, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 0},
    63  				},
    64  				Status:    actions_model.StatusRunning,
    65  				Started:   10000,
    66  				Stopped:   10100,
    67  				LogLength: 100,
    68  			},
    69  			want: []*actions_model.ActionTaskStep{
    70  				{Name: preStepName, Status: actions_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
    71  				{Status: actions_model.StatusRunning, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 0},
    72  				{Name: postStepName, Status: actions_model.StatusWaiting, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
    73  			},
    74  		},
    75  		{
    76  			name: "first step has canceled",
    77  			task: &actions_model.ActionTask{
    78  				Steps: []*actions_model.ActionTaskStep{
    79  					{Status: actions_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
    80  				},
    81  				Status:    actions_model.StatusFailure,
    82  				Started:   10000,
    83  				Stopped:   10100,
    84  				LogLength: 100,
    85  			},
    86  			want: []*actions_model.ActionTaskStep{
    87  				{Name: preStepName, Status: actions_model.StatusFailure, LogIndex: 0, LogLength: 100, Started: 10000, Stopped: 10100},
    88  				{Status: actions_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
    89  				{Name: postStepName, Status: actions_model.StatusFailure, LogIndex: 100, LogLength: 0, Started: 10100, Stopped: 10100},
    90  			},
    91  		},
    92  		{
    93  			name: "empty steps",
    94  			task: &actions_model.ActionTask{
    95  				Steps:     []*actions_model.ActionTaskStep{},
    96  				Status:    actions_model.StatusSuccess,
    97  				Started:   10000,
    98  				Stopped:   10100,
    99  				LogLength: 100,
   100  			},
   101  			want: []*actions_model.ActionTaskStep{
   102  				{Name: preStepName, Status: actions_model.StatusSuccess, LogIndex: 0, LogLength: 100, Started: 10000, Stopped: 10100},
   103  				{Name: postStepName, Status: actions_model.StatusSuccess, LogIndex: 100, LogLength: 0, Started: 10100, Stopped: 10100},
   104  			},
   105  		},
   106  		{
   107  			name: "all steps finished but task is running",
   108  			task: &actions_model.ActionTask{
   109  				Steps: []*actions_model.ActionTaskStep{
   110  					{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
   111  				},
   112  				Status:    actions_model.StatusRunning,
   113  				Started:   10000,
   114  				Stopped:   0,
   115  				LogLength: 100,
   116  			},
   117  			want: []*actions_model.ActionTaskStep{
   118  				{Name: preStepName, Status: actions_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
   119  				{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
   120  				{Name: postStepName, Status: actions_model.StatusRunning, LogIndex: 90, LogLength: 10, Started: 10090, Stopped: 0},
   121  			},
   122  		},
   123  		{
   124  			name: "skipped task",
   125  			task: &actions_model.ActionTask{
   126  				Steps: []*actions_model.ActionTaskStep{
   127  					{Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
   128  				},
   129  				Status:    actions_model.StatusSkipped,
   130  				Started:   0,
   131  				Stopped:   0,
   132  				LogLength: 0,
   133  			},
   134  			want: []*actions_model.ActionTaskStep{
   135  				{Name: preStepName, Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
   136  				{Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
   137  				{Name: postStepName, Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
   138  			},
   139  		},
   140  		{
   141  			name: "first step is skipped",
   142  			task: &actions_model.ActionTask{
   143  				Steps: []*actions_model.ActionTaskStep{
   144  					{Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
   145  					{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
   146  				},
   147  				Status:    actions_model.StatusSuccess,
   148  				Started:   10000,
   149  				Stopped:   10100,
   150  				LogLength: 100,
   151  			},
   152  			want: []*actions_model.ActionTaskStep{
   153  				{Name: preStepName, Status: actions_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
   154  				{Status: actions_model.StatusSkipped, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
   155  				{Status: actions_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
   156  				{Name: postStepName, Status: actions_model.StatusSuccess, LogIndex: 90, LogLength: 10, Started: 10090, Stopped: 10100},
   157  			},
   158  		},
   159  	}
   160  	for _, tt := range tests {
   161  		t.Run(tt.name, func(t *testing.T) {
   162  			assert.Equalf(t, tt.want, FullSteps(tt.task), "FullSteps(%v)", tt.task)
   163  		})
   164  	}
   165  }