github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/admin/commands/execution/stop_at_height_test.go (about)

     1  package execution
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/rs/zerolog"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/onflow/flow-go/admin"
    12  	"github.com/onflow/flow-go/engine"
    13  	"github.com/onflow/flow-go/engine/execution/ingestion/stop"
    14  	"github.com/onflow/flow-go/model/flow"
    15  )
    16  
    17  func TestCommandParsing(t *testing.T) {
    18  	cmd := StopAtHeightCommand{}
    19  
    20  	t.Run("happy path", func(t *testing.T) {
    21  
    22  		req := &admin.CommandRequest{
    23  			Data: map[string]interface{}{
    24  				"height": float64(21), // raw json parses to float64
    25  				"crash":  true,
    26  			},
    27  		}
    28  
    29  		err := cmd.Validator(req)
    30  		require.NoError(t, err)
    31  
    32  		require.IsType(t, StopAtHeightReq{}, req.ValidatorData)
    33  
    34  		parsedReq := req.ValidatorData.(StopAtHeightReq)
    35  
    36  		require.Equal(t, uint64(21), parsedReq.height)
    37  		require.Equal(t, true, parsedReq.crash)
    38  	})
    39  
    40  	t.Run("empty", func(t *testing.T) {
    41  
    42  		req := &admin.CommandRequest{
    43  			Data: map[string]interface{}{},
    44  		}
    45  
    46  		err := cmd.Validator(req)
    47  
    48  		require.True(t, admin.IsInvalidAdminParameterError(err))
    49  	})
    50  
    51  	t.Run("wrong height type", func(t *testing.T) {
    52  
    53  		req := &admin.CommandRequest{
    54  			Data: map[string]interface{}{
    55  				"height": "abc",
    56  			},
    57  		}
    58  
    59  		err := cmd.Validator(req)
    60  
    61  		require.True(t, admin.IsInvalidAdminParameterError(err))
    62  	})
    63  
    64  	t.Run("wrong height type", func(t *testing.T) {
    65  
    66  		req := &admin.CommandRequest{
    67  			Data: map[string]interface{}{
    68  				"height": "abc",
    69  			},
    70  		}
    71  
    72  		err := cmd.Validator(req)
    73  
    74  		require.True(t, admin.IsInvalidAdminParameterError(err))
    75  	})
    76  
    77  	t.Run("negative", func(t *testing.T) {
    78  
    79  		req := &admin.CommandRequest{
    80  			Data: map[string]interface{}{
    81  				"height": -12,
    82  			},
    83  		}
    84  
    85  		err := cmd.Validator(req)
    86  
    87  		require.True(t, admin.IsInvalidAdminParameterError(err))
    88  	})
    89  
    90  }
    91  
    92  func TestCommandsSetsValues(t *testing.T) {
    93  
    94  	stopControl := stop.NewStopControl(
    95  		engine.NewUnit(),
    96  		time.Second,
    97  		zerolog.Nop(),
    98  		nil,
    99  		nil,
   100  		nil,
   101  		nil,
   102  		&flow.Header{Height: 1},
   103  		false,
   104  		false,
   105  	)
   106  
   107  	cmd := NewStopAtHeightCommand(stopControl)
   108  
   109  	req := &admin.CommandRequest{
   110  		ValidatorData: StopAtHeightReq{
   111  			height: 37,
   112  			crash:  true,
   113  		},
   114  	}
   115  
   116  	_, err := cmd.Handler(context.TODO(), req)
   117  	require.NoError(t, err)
   118  
   119  	s := stopControl.GetStopParameters()
   120  
   121  	require.NotNil(t, s)
   122  	require.Equal(t, uint64(37), s.StopBeforeHeight)
   123  	require.Equal(t, true, s.ShouldCrash)
   124  }