github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/daemon/top_unix_test.go (about)

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