libct/int: remove logger from init

Currently, TestInit sets up logrus, and init uses it to log an error
from StartInitialization(). This is solely used by TestExecInError
to check that error returned from StartInitialization is the one it
expects.

Note that the very same error is communicated to the runc init parent
and is ultimately returned by container.Run(), so checking what
StartInitialization returned is redundant.

Remove logrus setup and use from TestMain/init.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2021-12-10 12:58:35 -08:00
parent eba31a7c6c
commit 7e481ee2eb
2 changed files with 12 additions and 16 deletions
+3 -8
View File
@@ -215,12 +215,10 @@ func TestExecInError(t *testing.T) {
ok(t, err) ok(t, err)
for i := 0; i < 42; i++ { for i := 0; i < 42; i++ {
var out bytes.Buffer
unexistent := &libcontainer.Process{ unexistent := &libcontainer.Process{
Cwd: "/", Cwd: "/",
Args: []string{"unexistent"}, Args: []string{"unexistent"},
Env: standardEnvironment, Env: standardEnvironment,
Stderr: &out,
} }
err = container.Run(unexistent) err = container.Run(unexistent)
if err == nil { if err == nil {
@@ -229,9 +227,6 @@ func TestExecInError(t *testing.T) {
if !strings.Contains(err.Error(), "executable file not found") { if !strings.Contains(err.Error(), "executable file not found") {
t.Fatalf("Should be error about not found executable, got %s", err) t.Fatalf("Should be error about not found executable, got %s", err)
} }
if !bytes.Contains(out.Bytes(), []byte("executable file not found")) {
t.Fatalf("executable file not found error not delivered to stdio:\n%s", out.String())
}
} }
} }
+9 -8
View File
@@ -1,6 +1,7 @@
package integration package integration
import ( import (
"fmt"
"os" "os"
"runtime" "runtime"
"testing" "testing"
@@ -9,27 +10,27 @@ import (
//nolint:revive // Enable cgroup manager to manage devices //nolint:revive // Enable cgroup manager to manage devices
_ "github.com/opencontainers/runc/libcontainer/cgroups/devices" _ "github.com/opencontainers/runc/libcontainer/cgroups/devices"
_ "github.com/opencontainers/runc/libcontainer/nsenter" _ "github.com/opencontainers/runc/libcontainer/nsenter"
"github.com/sirupsen/logrus"
) )
// init runs the libcontainer initialization code because of the busybox style needs // Same as ../../init.go but for libcontainer/integration.
// to work around the go runtime and the issues with forking
func init() { func init() {
if len(os.Args) < 2 || os.Args[1] != "init" { if len(os.Args) < 2 || os.Args[1] != "init" {
return return
} }
// This is the golang entry point for runc init, executed
// before TestMain() but after libcontainer/nsenter's nsexec().
runtime.GOMAXPROCS(1) runtime.GOMAXPROCS(1)
runtime.LockOSThread() runtime.LockOSThread()
if err := libcontainer.StartInitialization(); err != nil { if err := libcontainer.StartInitialization(); err != nil {
logrus.Fatal(err) // logrus is not initialized
fmt.Fprintln(os.Stderr, err)
} }
// Normally, StartInitialization() never returns, meaning
// if we are here, it had failed.
os.Exit(1)
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
logrus.SetOutput(os.Stderr)
logrus.SetLevel(logrus.InfoLevel)
ret := m.Run() ret := m.Run()
os.Exit(ret) os.Exit(ret)
} }