github.com/criyle/go-sandbox@v0.10.3/container/benchmark_linux_test.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"os"
     7  	"runtime"
     8  	"syscall"
     9  	"testing"
    10  
    11  	"github.com/criyle/go-sandbox/runner"
    12  )
    13  
    14  func init() {
    15  	Init()
    16  }
    17  
    18  func BenchmarkContainer(b *testing.B) {
    19  	tmpDir, err := os.MkdirTemp("", "")
    20  	if err != nil {
    21  		b.Error(err)
    22  	}
    23  	builder := &Builder{
    24  		Root:   tmpDir,
    25  		Stderr: os.Stderr,
    26  	}
    27  	n := runtime.GOMAXPROCS(0)
    28  	ch := make(chan Environment, n)
    29  	for i := 0; i < n; i++ {
    30  		m, err := builder.Build()
    31  		if err != nil {
    32  			b.Error(err)
    33  		}
    34  		b.Cleanup(func() {
    35  			m.Destroy()
    36  		})
    37  		ch <- m
    38  	}
    39  	b.ResetTimer()
    40  	b.RunParallel(func(pb *testing.PB) {
    41  		m := <-ch
    42  		for pb.Next() {
    43  			r := m.Execve(context.TODO(), ExecveParam{
    44  				Args: []string{"/bin/true"},
    45  				Env:  []string{"PATH=/bin"},
    46  			})
    47  			if r.Status != runner.StatusNormal {
    48  				b.Error(r.Status, r.Error)
    49  			}
    50  		}
    51  	})
    52  }
    53  
    54  func TestContainerSuccess(t *testing.T) {
    55  	t.Parallel()
    56  	m := getEnv(t, nil)
    57  	r := m.Execve(context.TODO(), ExecveParam{
    58  		Args: []string{"/bin/true"},
    59  		Env:  []string{"PATH=/bin"},
    60  	})
    61  	if r.Status != runner.StatusNormal {
    62  		t.Fatal(r.Status, r.Error, r)
    63  	}
    64  }
    65  
    66  type credgen struct{}
    67  
    68  func (c credgen) Get() syscall.Credential {
    69  	return syscall.Credential{
    70  		Uid: 10000,
    71  		Gid: 10000,
    72  	}
    73  }
    74  
    75  func TestContainerSetCred(t *testing.T) {
    76  	t.Parallel()
    77  	if os.Getpid() != 1 {
    78  		t.Skip("root required for this test")
    79  	}
    80  	m := getEnv(t, credgen{})
    81  	r := m.Execve(context.TODO(), ExecveParam{
    82  		Args: []string{"/bin/true"},
    83  		Env:  []string{"PATH=/bin"},
    84  	})
    85  	if r.Status != runner.StatusNormal {
    86  		t.Fatal(r.Status, r.Error)
    87  	}
    88  }
    89  
    90  func TestContainerNotExists(t *testing.T) {
    91  	t.Parallel()
    92  	m := getEnv(t, nil)
    93  	r := m.Execve(context.TODO(), ExecveParam{
    94  		Args: []string{"not_exists"},
    95  		Env:  []string{"PATH=/bin"},
    96  	})
    97  	if r.Status != runner.StatusRunnerError {
    98  		t.Fatal(r.Status, r.Error)
    99  	}
   100  }
   101  
   102  func TestContainerSyncFuncFail(t *testing.T) {
   103  	t.Parallel()
   104  	m := getEnv(t, nil)
   105  	err := errors.New("test error")
   106  	r := m.Execve(context.TODO(), ExecveParam{
   107  		Args: []string{"/bin/true"},
   108  		Env:  []string{"PATH=/bin"},
   109  		SyncFunc: func(pid int) error {
   110  			return err
   111  		},
   112  	})
   113  	if r.Status != runner.StatusRunnerError {
   114  		t.Fatal(r.Status, r.Error)
   115  	}
   116  }
   117  
   118  func getEnv(t *testing.T, credGen CredGenerator) Environment {
   119  	tmpDir, err := os.MkdirTemp("", "")
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	t.Cleanup(func() {
   124  		os.Remove(tmpDir)
   125  	})
   126  	builder := &Builder{
   127  		Root:          tmpDir,
   128  		CredGenerator: credGen,
   129  		Stderr:        os.Stderr,
   130  	}
   131  	m, err := builder.Build()
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	t.Cleanup(func() {
   136  		m.Destroy()
   137  	})
   138  	return m
   139  }