github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/monitor_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  	"errors"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestMonitorSuccess(t *testing.T) {
    16  	contID := "505"
    17  	contConfig := newTestContainerConfigNoop(contID)
    18  	hConfig := newHypervisorConfig(nil, nil)
    19  	assert := assert.New(t)
    20  
    21  	// create a sandbox
    22  	s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, []ContainerConfig{contConfig}, nil)
    23  	assert.NoError(err)
    24  	defer cleanUp()
    25  
    26  	m := newMonitor(s)
    27  
    28  	ch, err := m.newWatcher()
    29  	assert.Nil(err, "newWatcher failed: %v", err)
    30  
    31  	fakeErr := errors.New("foobar error")
    32  	m.notify(fakeErr)
    33  	resultErr := <-ch
    34  	assert.True(resultErr == fakeErr, "monitor notification mismatch %v vs. %v", resultErr, fakeErr)
    35  
    36  	m.stop()
    37  }
    38  
    39  func TestMonitorClosedChannel(t *testing.T) {
    40  	contID := "505"
    41  	contConfig := newTestContainerConfigNoop(contID)
    42  	hConfig := newHypervisorConfig(nil, nil)
    43  	assert := assert.New(t)
    44  
    45  	// create a sandbox
    46  	s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, []ContainerConfig{contConfig}, nil)
    47  	assert.NoError(err)
    48  	defer cleanUp()
    49  
    50  	m := newMonitor(s)
    51  
    52  	ch, err := m.newWatcher()
    53  	assert.Nil(err, "newWatcher failed: %v", err)
    54  
    55  	close(ch)
    56  	fakeErr := errors.New("foobar error")
    57  	m.notify(fakeErr)
    58  
    59  	m.stop()
    60  }