libct/int: better test container names

1. Do not create the same container named "test" over and over.

2. Fix randomization issues when generating container and cgroup names.
   The issues were:

    * math/rand used without seeding
    * complex rand/md5/hexencode sequence

   In both cases, replace with nanosecond time encoded with digits and
   lowercase letters.

3. Add test name to container and cgroup names. For example, this is
   how systemd log has changed:

   Before: Started libcontainer container test16ddfwutxgjte.
   After: Started libcontainer container TestPidsSystemd-4oaqvr.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2021-04-14 16:05:10 -07:00
parent fce58ab2d5
commit 7b802a7da4
6 changed files with 121 additions and 126 deletions

View File

@@ -2,8 +2,6 @@ package integration
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
@@ -11,6 +9,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
"testing"
@@ -152,13 +151,8 @@ func copyBusybox(dest string) error {
return nil
}
func newContainer(config *configs.Config) (libcontainer.Container, error) {
h := md5.New()
h.Write([]byte(time.Now().String()))
return newContainerWithName(hex.EncodeToString(h.Sum(nil)), config)
}
func newContainerWithName(name string, config *configs.Config) (libcontainer.Container, error) {
func newContainer(t *testing.T, config *configs.Config) (libcontainer.Container, error) {
name := t.Name() + strconv.FormatInt(int64(time.Now().Nanosecond()), 35)
root, err := newTestRoot()
if err != nil {
return nil, err
@@ -181,8 +175,8 @@ func newContainerWithName(name string, config *configs.Config) (libcontainer.Con
//
// buffers are returned containing the STDOUT and STDERR output for the run
// along with the exit code and any go error
func runContainer(config *configs.Config, console string, args ...string) (buffers *stdBuffers, exitCode int, err error) {
container, err := newContainer(config)
func runContainer(t *testing.T, config *configs.Config, console string, args ...string) (buffers *stdBuffers, exitCode int, err error) {
container, err := newContainer(t, config)
if err != nil {
return nil, -1, err
}