github.com/vvnotw/moby@v1.13.1/daemon/top_unix_test.go (about)

     1  //+build !windows
     2  
     3  package daemon
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  func TestContainerTopValidatePSArgs(t *testing.T) {
    10  	tests := map[string]bool{
    11  		"ae -o uid=PID":             true,
    12  		"ae -o \"uid= PID\"":        true,  // ascii space (0x20)
    13  		"ae -o \"uid= PID\"":        false, // unicode space (U+2003, 0xe2 0x80 0x83)
    14  		"ae o uid=PID":              true,
    15  		"aeo uid=PID":               true,
    16  		"ae -O uid=PID":             true,
    17  		"ae -o pid=PID2 -o uid=PID": true,
    18  		"ae -o pid=PID":             false,
    19  		"ae -o pid=PID -o uid=PIDX": true, // FIXME: we do not need to prohibit this
    20  		"aeo pid=PID":               false,
    21  		"ae":                        false,
    22  		"":                          false,
    23  	}
    24  	for psArgs, errExpected := range tests {
    25  		err := validatePSArgs(psArgs)
    26  		t.Logf("tested %q, got err=%v", psArgs, err)
    27  		if errExpected && err == nil {
    28  			t.Fatalf("expected error, got %v (%q)", err, psArgs)
    29  		}
    30  		if !errExpected && err != nil {
    31  			t.Fatalf("expected nil, got %v (%q)", err, psArgs)
    32  		}
    33  	}
    34  }
    35  
    36  func TestContainerTopParsePSOutput(t *testing.T) {
    37  	tests := []struct {
    38  		output      []byte
    39  		pids        []int
    40  		errExpected bool
    41  	}{
    42  		{[]byte(`  PID COMMAND
    43     42 foo
    44     43 bar
    45    100 baz
    46  `), []int{42, 43}, false},
    47  		{[]byte(`  UID COMMAND
    48     42 foo
    49     43 bar
    50    100 baz
    51  `), []int{42, 43}, true},
    52  		// unicode space (U+2003, 0xe2 0x80 0x83)
    53  		{[]byte(` PID COMMAND
    54     42 foo
    55     43 bar
    56    100 baz
    57  `), []int{42, 43}, true},
    58  		// the first space is U+2003, the second one is ascii.
    59  		{[]byte(` PID COMMAND
    60     42 foo
    61     43 bar
    62    100 baz
    63  `), []int{42, 43}, true},
    64  	}
    65  
    66  	for _, f := range tests {
    67  		_, err := parsePSOutput(f.output, f.pids)
    68  		t.Logf("tested %q, got err=%v", string(f.output), err)
    69  		if f.errExpected && err == nil {
    70  			t.Fatalf("expected error, got %v (%q)", err, string(f.output))
    71  		}
    72  		if !f.errExpected && err != nil {
    73  			t.Fatalf("expected nil, got %v (%q)", err, string(f.output))
    74  		}
    75  	}
    76  }