github.com/chenbh/concourse/v6@v6.4.2/worker/runtime/process_test.go (about)

     1  package runtime_test
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/garden"
     8  	"github.com/chenbh/concourse/v6/worker/runtime"
     9  	"github.com/chenbh/concourse/v6/worker/runtime/libcontainerd/libcontainerdfakes"
    10  	"github.com/containerd/containerd"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  type ProcessSuite struct {
    16  	suite.Suite
    17  	*require.Assertions
    18  
    19  	io                *libcontainerdfakes.FakeIO
    20  	containerdProcess *libcontainerdfakes.FakeProcess
    21  	ch                chan containerd.ExitStatus
    22  	process           *runtime.Process
    23  }
    24  
    25  func (s *ProcessSuite) SetupTest() {
    26  	s.io = new(libcontainerdfakes.FakeIO)
    27  	s.containerdProcess = new(libcontainerdfakes.FakeProcess)
    28  	s.ch = make(chan containerd.ExitStatus, 1)
    29  
    30  	s.process = runtime.NewProcess(s.containerdProcess, s.ch)
    31  }
    32  
    33  func (s *ProcessSuite) TestID() {
    34  	s.containerdProcess.IDReturns("id")
    35  	id := s.process.ID()
    36  	s.Equal("id", id)
    37  
    38  	s.Equal(1, s.containerdProcess.IDCallCount())
    39  }
    40  
    41  func (s *ProcessSuite) TestWaitStatusErr() {
    42  	expectedErr := errors.New("status-err")
    43  	s.ch <- *containerd.NewExitStatus(0, time.Now(), expectedErr)
    44  
    45  	_, err := s.process.Wait()
    46  	s.True(errors.Is(err, expectedErr))
    47  }
    48  
    49  func (s *ProcessSuite) TestProcessWaitDeleteError() {
    50  	s.ch <- *containerd.NewExitStatus(0, time.Now(), nil)
    51  
    52  	expectedErr := errors.New("status-err")
    53  	s.containerdProcess.DeleteReturns(nil, expectedErr)
    54  
    55  	_, err := s.process.Wait()
    56  	s.True(errors.Is(err, expectedErr))
    57  }
    58  
    59  func (s *ProcessSuite) TestProcessWaitBlocksUntilIOFinishes() {
    60  	s.ch <- *containerd.NewExitStatus(0, time.Now(), nil)
    61  	s.containerdProcess.IOReturns(s.io)
    62  
    63  	_, err := s.process.Wait()
    64  	s.NoError(err)
    65  
    66  	s.Equal(1, s.containerdProcess.DeleteCallCount())
    67  	s.Equal(1, s.containerdProcess.IOCallCount())
    68  	s.Equal(1, s.io.WaitCallCount())
    69  }
    70  
    71  func (s *ProcessSuite) TestSetTTYWithNilWindowSize() {
    72  	err := s.process.SetTTY(garden.TTYSpec{})
    73  	s.NoError(err)
    74  	s.Equal(0, s.containerdProcess.ResizeCallCount())
    75  }
    76  
    77  func (s *ProcessSuite) TestSetTTYResizeError() {
    78  	expectedErr := errors.New("resize-err")
    79  	s.containerdProcess.ResizeReturns(expectedErr)
    80  
    81  	err := s.process.SetTTY(garden.TTYSpec{
    82  		WindowSize: &garden.WindowSize{
    83  			Columns: 123,
    84  			Rows:    456,
    85  		},
    86  	})
    87  	s.True(errors.Is(err, expectedErr))
    88  }
    89  
    90  func (s *ProcessSuite) TestSetTTYResize() {
    91  	err := s.process.SetTTY(garden.TTYSpec{
    92  		WindowSize: &garden.WindowSize{
    93  			Columns: 123,
    94  			Rows:    456,
    95  		},
    96  	})
    97  	s.NoError(err)
    98  
    99  	s.Equal(1, s.containerdProcess.ResizeCallCount())
   100  	_, width, height := s.containerdProcess.ResizeArgsForCall(0)
   101  	s.Equal(123, int(width))
   102  	s.Equal(456, int(height))
   103  }