github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/internal/testing/apitests/streamingsync_test.go (about)

     1  package apitests
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/hashicorp/nomad/ci"
     9  	"github.com/hashicorp/nomad/plugins/drivers"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // TestExecStreamingInputIsInSync asserts that a rountrip of exec streaming input doesn't lose any data
    14  func TestExecStreamingInputIsInSync(t *testing.T) {
    15  	ci.Parallel(t)
    16  
    17  	cases := []struct {
    18  		name  string
    19  		input api.ExecStreamingInput
    20  	}{
    21  		{
    22  			"stdin_data",
    23  			api.ExecStreamingInput{Stdin: &api.ExecStreamingIOOperation{Data: []byte("hello there")}},
    24  		},
    25  		{
    26  			"stdin_close",
    27  			api.ExecStreamingInput{Stdin: &api.ExecStreamingIOOperation{Close: true}},
    28  		},
    29  		{
    30  			"tty_size",
    31  			api.ExecStreamingInput{TTYSize: &api.TerminalSize{Height: 10, Width: 20}},
    32  		},
    33  	}
    34  
    35  	for _, c := range cases {
    36  		t.Run(c.name, func(t *testing.T) {
    37  			b, err := json.Marshal(c.input)
    38  			require.NoError(t, err)
    39  
    40  			var proto drivers.ExecTaskStreamingRequestMsg
    41  			err = json.Unmarshal(b, &proto)
    42  			require.NoError(t, err)
    43  
    44  			protoB, err := json.Marshal(proto)
    45  			require.NoError(t, err)
    46  
    47  			var roundtrip api.ExecStreamingInput
    48  			err = json.Unmarshal(protoB, &roundtrip)
    49  			require.NoError(t, err)
    50  
    51  			require.EqualValues(t, c.input, roundtrip)
    52  		})
    53  	}
    54  }
    55  
    56  // TestExecStreamingOutputIsInSync asserts that a rountrip of exec streaming input doesn't lose any data
    57  func TestExecStreamingOutputIsInSync(t *testing.T) {
    58  	ci.Parallel(t)
    59  
    60  	cases := []struct {
    61  		name  string
    62  		input api.ExecStreamingOutput
    63  	}{
    64  		{
    65  			"stdout_data",
    66  			api.ExecStreamingOutput{Stdout: &api.ExecStreamingIOOperation{Data: []byte("hello there")}},
    67  		},
    68  		{
    69  			"stdout_close",
    70  			api.ExecStreamingOutput{Stdout: &api.ExecStreamingIOOperation{Close: true}},
    71  		},
    72  		{
    73  			"stderr_data",
    74  			api.ExecStreamingOutput{Stderr: &api.ExecStreamingIOOperation{Data: []byte("hello there")}},
    75  		},
    76  		{
    77  			"stderr_close",
    78  			api.ExecStreamingOutput{Stderr: &api.ExecStreamingIOOperation{Close: true}},
    79  		},
    80  		{
    81  			"exited",
    82  			api.ExecStreamingOutput{Exited: true, Result: &api.ExecStreamingExitResult{ExitCode: 21}},
    83  		},
    84  	}
    85  
    86  	for _, c := range cases {
    87  		t.Run(c.name, func(t *testing.T) {
    88  			b, err := json.Marshal(c.input)
    89  			require.NoError(t, err)
    90  
    91  			var proto drivers.ExecTaskStreamingResponseMsg
    92  			err = json.Unmarshal(b, &proto)
    93  			require.NoError(t, err)
    94  
    95  			protoB, err := json.Marshal(proto)
    96  			require.NoError(t, err)
    97  
    98  			var roundtrip api.ExecStreamingOutput
    99  			err = json.Unmarshal(protoB, &roundtrip)
   100  			require.NoError(t, err)
   101  
   102  			require.EqualValues(t, c.input, roundtrip)
   103  		})
   104  	}
   105  }