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