github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/iostream_test.go (about)

     1  // Copyright (c) 2018 HyperHQ Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestIOStream(t *testing.T) {
    15  	hConfig := newHypervisorConfig(nil, nil)
    16  	s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, []ContainerConfig{}, nil)
    17  	assert.NoError(t, err)
    18  	defer cleanUp()
    19  
    20  	contID := "foo"
    21  	processID := "bar"
    22  	config := newTestContainerConfigNoop(contID)
    23  	c := &Container{
    24  		sandbox: s,
    25  		config:  &config,
    26  	}
    27  
    28  	stream := newIOStream(s, c, processID)
    29  	stdin := stream.stdin()
    30  	stdout := stream.stdout()
    31  	stderr := stream.stderr()
    32  
    33  	buffer := []byte("randombufferdata")
    34  	_, err = stdin.Write(buffer)
    35  	assert.Nil(t, err, "stdin write failed: %s", err)
    36  
    37  	_, err = stdout.Read(buffer)
    38  	assert.Nil(t, err, "stdout read failed: %s", err)
    39  
    40  	_, err = stderr.Read(buffer)
    41  	assert.Nil(t, err, "stderr read failed: %s", err)
    42  
    43  	err = stdin.Close()
    44  	assert.Nil(t, err, "stream close failed: %s", err)
    45  
    46  	_, err = stdin.Write(buffer)
    47  	assert.NotNil(t, err, "stdin write closed should fail")
    48  
    49  	_, err = stdout.Read(buffer)
    50  	assert.NotNil(t, err, "stdout read closed should fail")
    51  
    52  	_, err = stderr.Read(buffer)
    53  	assert.NotNil(t, err, "stderr read closed should fail")
    54  
    55  	err = stdin.Close()
    56  	assert.NotNil(t, err, "stdin close closed should fail")
    57  }