github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/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  		- -
    46    100 baz
    47  `), []int{42, 43}, false},
    48  		{[]byte(`  UID COMMAND
    49     42 foo
    50     43 bar
    51  		- -
    52    100 baz
    53  `), []int{42, 43}, true},
    54  		// unicode space (U+2003, 0xe2 0x80 0x83)
    55  		{[]byte(` PID COMMAND
    56     42 foo
    57     43 bar
    58  		- -
    59    100 baz
    60  `), []int{42, 43}, true},
    61  		// the first space is U+2003, the second one is ascii.
    62  		{[]byte(` PID COMMAND
    63     42 foo
    64     43 bar
    65    100 baz
    66  `), []int{42, 43}, true},
    67  	}
    68  
    69  	for _, f := range tests {
    70  		_, err := parsePSOutput(f.output, f.pids)
    71  		t.Logf("tested %q, got err=%v", string(f.output), err)
    72  		if f.errExpected && err == nil {
    73  			t.Fatalf("expected error, got %v (%q)", err, string(f.output))
    74  		}
    75  		if !f.errExpected && err != nil {
    76  			t.Fatalf("expected nil, got %v (%q)", err, string(f.output))
    77  		}
    78  	}
    79  }