github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/daemon/top_unix_test.go (about)

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