mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-22 08:44:25 +01:00
It monitors the sandbox status and returns an error channel to let caller watch it. Fixes: #251 Signed-off-by: Peng Tao <bergwolf@gmail.com>
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
// Copyright (c) 2018 HyperHQ Inc.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package virtcontainers
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMonitorSuccess(t *testing.T) {
|
|
contID := "505"
|
|
contConfig := newTestContainerConfigNoop(contID)
|
|
hConfig := newHypervisorConfig(nil, nil)
|
|
|
|
// create a sandbox
|
|
s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NoopNetworkModel, NetworkConfig{}, []ContainerConfig{contConfig}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cleanUp()
|
|
|
|
m := newMonitor(s)
|
|
|
|
ch, err := m.newWatcher()
|
|
assert.Nil(t, err, "newWatcher failed: %v", err)
|
|
|
|
fakeErr := errors.New("foobar error")
|
|
m.notify(fakeErr)
|
|
resultErr := <-ch
|
|
assert.True(t, resultErr == fakeErr, "monitor notification mismatch %v vs. %v", resultErr, fakeErr)
|
|
|
|
m.stop()
|
|
}
|
|
|
|
func TestMonitorClosedChannel(t *testing.T) {
|
|
contID := "505"
|
|
contConfig := newTestContainerConfigNoop(contID)
|
|
hConfig := newHypervisorConfig(nil, nil)
|
|
|
|
// create a sandbox
|
|
s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NoopNetworkModel, NetworkConfig{}, []ContainerConfig{contConfig}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cleanUp()
|
|
|
|
m := newMonitor(s)
|
|
|
|
ch, err := m.newWatcher()
|
|
assert.Nil(t, err, "newWatcher failed: %v", err)
|
|
|
|
close(ch)
|
|
fakeErr := errors.New("foobar error")
|
|
m.notify(fakeErr)
|
|
|
|
m.stop()
|
|
}
|