mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-14 13:29:31 +00:00
@@ -16,6 +16,7 @@ import (
|
||||
pb "github.com/kata-containers/kata-containers/src/runtime/protocols/cache"
|
||||
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/factory/base"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type template struct {
|
||||
@@ -24,6 +25,7 @@ type template struct {
|
||||
}
|
||||
|
||||
var templateWaitForAgent = 2 * time.Second
|
||||
var templateLog = logrus.WithField("source", "virtcontainers/factory/template")
|
||||
|
||||
// Fetch finds and returns a pre-built template factory.
|
||||
// TODO: save template metadata and fetch from storage.
|
||||
@@ -86,8 +88,13 @@ func (t *template) GetVMStatus() []*pb.GrpcVMStatus {
|
||||
}
|
||||
|
||||
func (t *template) close() {
|
||||
syscall.Unmount(t.statePath, 0)
|
||||
os.RemoveAll(t.statePath)
|
||||
if err := syscall.Unmount(t.statePath, syscall.MNT_DETACH); err != nil {
|
||||
t.Logger().WithError(err).Errorf("failed to unmount %s", t.statePath)
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(t.statePath); err != nil {
|
||||
t.Logger().WithError(err).Errorf("failed to remove %s", t.statePath)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *template) prepareTemplateFiles() error {
|
||||
@@ -168,3 +175,19 @@ func (t *template) checkTemplateVM() error {
|
||||
_, err = os.Stat(t.statePath + "/state")
|
||||
return err
|
||||
}
|
||||
|
||||
// Logger returns a logrus logger appropriate for logging template messages
|
||||
func (t *template) Logger() *logrus.Entry {
|
||||
return templateLog.WithFields(logrus.Fields{
|
||||
"subsystem": "template",
|
||||
})
|
||||
}
|
||||
|
||||
// SetLogger sets the logger for the factory template.
|
||||
func SetLogger(ctx context.Context, logger logrus.FieldLogger) {
|
||||
fields := logrus.Fields{
|
||||
"source": "virtcontainers",
|
||||
}
|
||||
|
||||
templateLog = logger.WithFields(fields)
|
||||
}
|
||||
|
@@ -121,7 +121,14 @@ func TestTemplateFactory(t *testing.T) {
|
||||
err = vm.Stop(ctx)
|
||||
assert.Nil(err)
|
||||
|
||||
// CloseFactory
|
||||
// make tt.statePath is busy
|
||||
os.Chdir(tt.statePath)
|
||||
|
||||
// CloseFactory, there is no need to call tt.CloseFactory(ctx)
|
||||
f.CloseFactory(ctx)
|
||||
tt.CloseFactory(ctx)
|
||||
|
||||
// expect tt.statePath not exist, if exist, it means this case failed.
|
||||
_, err = os.Stat(tt.statePath)
|
||||
assert.Error(err)
|
||||
assert.True(os.IsNotExist(err))
|
||||
}
|
||||
|
Reference in New Issue
Block a user