github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/cmds/core/ps/ps_test.go (about)

     1  // Copyright 2016-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // created by Manoel Vilela (manoel_vilela@engineer.com)
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"os"
    14  	"path/filepath"
    15  	"reflect"
    16  	"testing"
    17  
    18  	"github.com/u-root/u-root/pkg/testutil"
    19  )
    20  
    21  // Simple Test trying execute the ps
    22  // If no errors returns, it's okay
    23  func TestPsExecution(t *testing.T) {
    24  	t.Logf("TestPsExecution")
    25  	d, err := ioutil.TempDir("", fmt.Sprintf("ps"))
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	//defer os.Rmdirall(d)
    30  	var tests = []struct {
    31  		n     string
    32  		pid   string
    33  		files map[string]string
    34  		o     string
    35  		err   error
    36  	}{
    37  		{n: "missing files", pid: "1", files: map[string]string{"stat": "bad status file"}},
    38  		{n: "one process", pid: "1", files: map[string]string{"stat": "bad status file",
    39  			"status": `Name:	systemd
    40  TracerPid:	0
    41  Uid:	0	0	0	0
    42  Gid:	0	0	0	0
    43  FDSize:	128
    44  `,
    45  			"cmdline": "/sbin/init"},
    46  			o: "PID PGRP SID TTY    STAT        TIME  COMMAND \n1     ?    file    00:00:00   status \n",
    47  		},
    48  		// Fix things up
    49  		{n: "correct pid 1", pid: "1", files: map[string]string{"stat": "1 (systemd) S 0 1 1 0 -1 4194560 82923 51272244 88 3457 153 671 103226 39563 20 0 1 0 2 230821888 2325 18446744073709551615 1 1 0 0 0 0 671173123 4096 1260 0 0 0 17 1 0 0 69 0 0 0 0 0 0 0 0 0 0",
    50  			"status": `Name:	systemd
    51  TracerPid:	0
    52  Uid:	0	0	0	0
    53  Gid:	0	0	0	0
    54  FDSize:	128
    55  `,
    56  			"cmdline": "/sbin/init"},
    57  			o: "PID PGRP SID TTY    STAT         TIME  COMMAND \n1 1 1 ?    S        00:00:08  systemd \n",
    58  		},
    59  		{n: "second process", pid: "1996", files: map[string]string{"stat": "1996 (dnsmasq) S 1 1995 1995 0 -1 4194624 64 0 0 0 1 10 0 0 20 0 1 0 1208 51163136 91 18446744073709551615 1 1 0 0 0 0 0 4096 92675 0 0 0 17 2 0 0 0 0 0 0 0 0 0 0 0 0 0",
    60  
    61  			"status": `Name:	dnsmasq
    62  Umask:	0022
    63  Uid:	110	110	110	110
    64  `,
    65  			"cmdline": "/usr/sbin/dnsmasq\000--conf-file=/var/lib/libvirt/dnsmasq/default.conf\000--leasefile-ro\000--dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper\000"},
    66  			o: " PID PGRP  SID TTY    STAT         TIME  COMMAND \n   1    1    1 ?    S        00:00:08  systemd \n1996 1995 1995 ?    S        00:00:00  dnsmasq \n",
    67  		},
    68  		{n: "nethost process", pid: "srv/1996", files: map[string]string{"stat": "1996 (dnsmasq) S 1 1995 1995 0 -1 4194624 64 0 0 0 1 10 0 0 20 0 1 0 1208 51163136 91 18446744073709551615 1 1 0 0 0 0 0 4096 92675 0 0 0 17 2 0 0 0 0 0 0 0 0 0 0 0 0 0",
    69  
    70  			"status": `Name:	dnsmasq
    71  Umask:	0022
    72  Uid:	110	110	110	110
    73  `,
    74  			"cmdline": "/usr/sbin/dnsmasq\000--conf-file=/var/lib/libvirt/dnsmasq/default.conf\000--leasefile-ro\000--dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper\000"},
    75  			o: "     PID     PGRP      SID TTY    STAT         TIME  COMMAND \n       1        1        1 ?    S        00:00:08  systemd \n    1996     1995     1995 ?    S        00:00:00  dnsmasq \nsrv/1996     1995     1995 ?    S        00:00:00  dnsmasq \n",
    76  		},
    77  	}
    78  
    79  	for _, tt := range tests {
    80  		pd := filepath.Join(d, tt.pid)
    81  		t.Logf("Create %v", pd)
    82  		if err := os.MkdirAll(pd, 0777); err != nil {
    83  			t.Fatalf("Make proc dir: %v", err)
    84  		}
    85  		for n, f := range tt.files {
    86  			procf := filepath.Join(pd, n)
    87  			t.Logf("Write %v", procf)
    88  			if err := ioutil.WriteFile(procf, []byte(f), 0666); err != nil {
    89  				t.Fatal(err)
    90  			}
    91  		}
    92  		c := testutil.Command(t, "aux")
    93  		psp := fmt.Sprintf("UROOT_PSPATH=%s:%s", d, filepath.Join(d, "srv"))
    94  		c.Env = append(c.Env, psp)
    95  		o, err := c.CombinedOutput()
    96  		t.Logf("%s: %s %v", tt.n, string(o), err)
    97  		if string(o) != tt.o {
    98  			t.Errorf("%v: got %q, want %q", tt.n, string(o), tt.o)
    99  		}
   100  		if !reflect.DeepEqual(err, tt.err) {
   101  			t.Errorf("%v: got %v, want %v", tt.n, err, tt.err)
   102  		}
   103  	}
   104  
   105  }
   106  
   107  // Test Parsing of stat
   108  func TestParse(t *testing.T) {
   109  	var tests = []struct {
   110  		name string
   111  		p    *Process
   112  		out  string
   113  		err  error
   114  	}{
   115  		{
   116  			name: "no status file",
   117  			p: &Process{
   118  				stat: "1 (systemd) S 0 1 1 0 -1 4194560 45535 23809816 88 2870 76 378 35944 9972 20 0 1 0 2 230821888 2325 18446744073709551615 1 1 0 0 0 0 671173123 4096 1260 0 0 0 17 2 0 0 69 0 0 0 0 0 0 0 0 0 0",
   119  			},
   120  			err: fmt.Errorf("no Uid string in "),
   121  		},
   122  		{
   123  			name: "Valid output",
   124  			out:  "PID TTY        TIME CMD     \n1 ?    00:00:04 systemd \n",
   125  			p: &Process{
   126  				stat: "1 (systemd) S 0 1 1 0 -1 4194560 45535 23809816 88 2870 76 378 35944 9972 20 0 1 0 2 230821888 2325 18446744073709551615 1 1 0 0 0 0 671173123 4096 1260 0 0 0 17 2 0 0 69 0 0 0 0 0 0 0 0 0 0",
   127  				status: `Name:	systemd
   128  Umask:	0000
   129  State:	S (sleeping)
   130  Tgid:	1
   131  Ngid:	0
   132  Pid:	1
   133  PPid:	0
   134  TracerPid:	0
   135  Uid:	0	0	0	0
   136  Gid:	0	0	0	0
   137  FDSize:	128
   138  Groups:	 
   139  NStgid:	1
   140  NSpid:	1
   141  NSpgid:	1
   142  NSsid:	1
   143  VmPeak:	  290768 kB
   144  VmSize:	  225412 kB
   145  VmLck:	       0 kB
   146  VmPin:	       0 kB
   147  VmHWM:	    9308 kB
   148  VmRSS:	    9300 kB
   149  RssAnon:	    2524 kB
   150  RssFile:	    6776 kB
   151  RssShmem:	       0 kB
   152  VmData:	   18696 kB
   153  VmStk:	     132 kB
   154  VmExe:	    1336 kB
   155  VmLib:	   10008 kB
   156  VmPTE:	     204 kB
   157  VmSwap:	       0 kB
   158  HugetlbPages:	       0 kB
   159  CoreDumping:	0
   160  Threads:	1
   161  SigQ:	0/31573
   162  SigPnd:	0000000000000000
   163  ShdPnd:	0000000000000000
   164  SigBlk:	7be3c0fe28014a03
   165  SigIgn:	0000000000001000
   166  SigCgt:	00000001800004ec
   167  CapInh:	0000000000000000
   168  CapPrm:	0000003fffffffff
   169  CapEff:	0000003fffffffff
   170  CapBnd:	0000003fffffffff
   171  CapAmb:	0000000000000000
   172  NoNewPrivs:	0
   173  Seccomp:	0
   174  Speculation_Store_Bypass:	thread vulnerable
   175  Cpus_allowed:	ffffffff,ffffffff,ffffffff,ffffffff
   176  Cpus_allowed_list:	0-127
   177  Mems_allowed:	00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
   178  Mems_allowed_list:	0
   179  voluntary_ctxt_switches:	10168
   180  nonvoluntary_ctxt_switches:	3746
   181  `},
   182  			err: nil,
   183  		},
   184  	}
   185  
   186  	flags.all = true
   187  	for _, tt := range tests {
   188  		t.Logf("%v", tt.name)
   189  		err := tt.p.Parse()
   190  		if !reflect.DeepEqual(err, tt.err) {
   191  			t.Errorf("Check %v: got %v, want %v", tt.p.stat, err, tt.err)
   192  			continue
   193  		}
   194  		if err != nil {
   195  			continue
   196  		}
   197  		pT := NewProcessTable()
   198  		pT.table = []*Process{tt.p}
   199  		pT.mProc = tt.p
   200  		var b bytes.Buffer
   201  		err = ps(pT, &b)
   202  		t.Logf("ps out is %s", b.String())
   203  		if !reflect.DeepEqual(err, tt.err) {
   204  			t.Errorf("%v: got %v, want %v", pT, err, tt.err)
   205  			continue
   206  		}
   207  		if b.String() != tt.out {
   208  			t.Errorf("%s: got %q, want %q", tt.p.stat, b.String(), tt.out)
   209  			continue
   210  		}
   211  
   212  	}
   213  }
   214  
   215  func TestMain(m *testing.M) {
   216  	testutil.Run(m, main)
   217  }