github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/utils/utils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"syscall"
     9  	"testing"
    10  )
    11  
    12  func TestGenerateName(t *testing.T) {
    13  	name, err := GenerateRandomName("veth", 5)
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  
    18  	expected := 5 + len("veth")
    19  	if len(name) != expected {
    20  		t.Fatalf("expected name to be %d chars but received %d", expected, len(name))
    21  	}
    22  
    23  	name, err = GenerateRandomName("veth", 65)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	expected = 64 + len("veth")
    29  	if len(name) != expected {
    30  		t.Fatalf("expected name to be %d chars but received %d", expected, len(name))
    31  	}
    32  }
    33  
    34  var labelTest = []struct {
    35  	labels        []string
    36  	query         string
    37  	expectedValue string
    38  }{
    39  	{[]string{"bundle=/path/to/bundle"}, "bundle", "/path/to/bundle"},
    40  	{[]string{"test=a", "test=b"}, "bundle", ""},
    41  	{[]string{"bundle=a", "test=b", "bundle=c"}, "bundle", "a"},
    42  	{[]string{"", "test=a", "bundle=b"}, "bundle", "b"},
    43  	{[]string{"test", "bundle=a"}, "bundle", "a"},
    44  	{[]string{"test=a", "bundle="}, "bundle", ""},
    45  }
    46  
    47  func TestSearchLabels(t *testing.T) {
    48  	for _, tt := range labelTest {
    49  		if v := SearchLabels(tt.labels, tt.query); v != tt.expectedValue {
    50  			t.Errorf("expected value '%s' for query '%s'; got '%s'", tt.expectedValue, tt.query, v)
    51  		}
    52  	}
    53  }
    54  
    55  func TestResolveRootfs(t *testing.T) {
    56  	dir := "rootfs"
    57  	os.Mkdir(dir, 0600)
    58  	defer os.Remove(dir)
    59  
    60  	path, err := ResolveRootfs(dir)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	pwd, err := os.Getwd()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	if path != fmt.Sprintf("%s/%s", pwd, "rootfs") {
    69  		t.Errorf("expected rootfs to be abs and was %s", path)
    70  	}
    71  }
    72  
    73  func TestResolveRootfsWithSymlink(t *testing.T) {
    74  	dir := "rootfs"
    75  	tmpDir, _ := filepath.EvalSymlinks(os.TempDir())
    76  	os.Symlink(tmpDir, dir)
    77  	defer os.Remove(dir)
    78  
    79  	path, err := ResolveRootfs(dir)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	if path != tmpDir {
    85  		t.Errorf("expected rootfs to be the real path %s and was %s", path, os.TempDir())
    86  	}
    87  }
    88  
    89  func TestResolveRootfsWithNonExistingDir(t *testing.T) {
    90  	_, err := ResolveRootfs("foo")
    91  	if err == nil {
    92  		t.Error("expected error to happen but received nil")
    93  	}
    94  }
    95  
    96  func TestExitStatus(t *testing.T) {
    97  	status := syscall.WaitStatus(0)
    98  	ex := ExitStatus(status)
    99  	if ex != 0 {
   100  		t.Errorf("expected exit status to equal 0 and received %d", ex)
   101  	}
   102  }
   103  
   104  func TestExitStatusSignaled(t *testing.T) {
   105  	status := syscall.WaitStatus(2)
   106  	ex := ExitStatus(status)
   107  	if ex != 130 {
   108  		t.Errorf("expected exit status to equal 130 and received %d", ex)
   109  	}
   110  }
   111  
   112  func TestWriteJSON(t *testing.T) {
   113  	person := struct {
   114  		Name string
   115  		Age  int
   116  	}{
   117  		Name: "Alice",
   118  		Age:  30,
   119  	}
   120  
   121  	var b bytes.Buffer
   122  	err := WriteJSON(&b, person)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	expected := `{"Name":"Alice","Age":30}`
   128  	if b.String() != expected {
   129  		t.Errorf("expected to write %s but was %s", expected, b.String())
   130  	}
   131  }
   132  
   133  func TestCleanPath(t *testing.T) {
   134  	path := CleanPath("")
   135  	if path != "" {
   136  		t.Errorf("expected to receive empty string and received %s", path)
   137  	}
   138  
   139  	path = CleanPath("rootfs")
   140  	if path != "rootfs" {
   141  		t.Errorf("expected to receive 'rootfs' and received %s", path)
   142  	}
   143  
   144  	path = CleanPath("../../../var")
   145  	if path != "var" {
   146  		t.Errorf("expected to receive 'var' and received %s", path)
   147  	}
   148  
   149  	path = CleanPath("/../../../var")
   150  	if path != "/var" {
   151  		t.Errorf("expected to receive '/var' and received %s", path)
   152  	}
   153  }