github.com/ubuntu/ubuntu-report@v1.7.4-0.20240410144652-96f37d845fac/pkg/sysmetrics/binmocks_test.go (about)

     1  package sysmetrics
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  const (
    11  	garbageOutput = `fdsofhoidshf fods gfpds
    12  gpofgipogifd
    13  fdspfds
    14  
    15  gfoidgo
    16  gfdojoi`
    17  )
    18  
    19  // TestMetricsHelperProcess is available to both internal and package test
    20  // to mock binaries (it's sub-executed)
    21  func TestMetricsHelperProcess(*testing.T) {
    22  	if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
    23  		return
    24  	}
    25  	defer os.Exit(0)
    26  
    27  	args := os.Args
    28  	for len(args) > 0 {
    29  		if args[0] == "--" {
    30  			args = args[1:]
    31  			break
    32  		}
    33  		args = args[1:]
    34  	}
    35  	if len(args) == 0 {
    36  		fmt.Fprintf(os.Stderr, "No command\n")
    37  		os.Exit(2)
    38  	}
    39  
    40  	cmd, args := args[0], args[1:]
    41  	switch cmd {
    42  	case "lspci":
    43  		if args[0] != "-n" {
    44  			fmt.Fprintf(os.Stderr, "Unexpected lspci arguments: %v\n", args)
    45  			os.Exit(1)
    46  		}
    47  		regularOutput := `00:00.0 0600: 8086:0104 (rev 09)
    48  00:02.0 0300: 8086:0126 (rev 09)
    49  00:16.0 0780: 8086:1c3a (rev 04)
    50  00:16.3 0700: 8086:1c3d (rev 04)
    51  00:19.0 0200: 8086:1502 (rev 04)`
    52  		switch args[1] {
    53  		case "one gpu":
    54  			fmt.Println(regularOutput)
    55  		case "multiple gpus":
    56  			fmt.Println(regularOutput)
    57  			fmt.Println("00:02.0 0300: 8086:0127 (rev 09)")
    58  		case "no revision number":
    59  			fmt.Println("00:02.0 0300: 8086:0126")
    60  		case "no gpu":
    61  			fmt.Println(`00:00.0 0600: 8086:0104 (rev 09)
    62  00:16.0 0780: 8086:1c3a (rev 04)
    63  00:16.3 0700: 8086:1c3d (rev 04)
    64  00:19.0 0200: 8086:1502 (rev 04)`)
    65  		case "hexa numbers":
    66  			fmt.Println("00:02.0 0300: 8b86:a126 (rev 09)")
    67  		case "empty":
    68  		case "malformed gpu line":
    69  			fmt.Println("00:02.0 0300: 80860127 (rev 09)")
    70  		case "garbage":
    71  			fmt.Println(garbageOutput)
    72  		case "fail":
    73  			fmt.Println(regularOutput) // still print content
    74  			os.Exit(1)
    75  		}
    76  
    77  	case "lscpu":
    78  		if args[0] != "-J" {
    79  			fmt.Fprintf(os.Stderr, "Unexpected lscpu arguments: %v\n", args)
    80  			os.Exit(1)
    81  		}
    82  		regularOutput := `{
    83     "lscpu": [
    84        {"field": "Architecture:", "data": "x86_64"},
    85        {"field": "CPU op-mode(s):", "data": "32-bit, 64-bit"},
    86        {"field": "Byte Order:", "data": "Little Endian"},
    87        {"field": "CPU(s):", "data": "8"},
    88        {"field": "On-line CPU(s) list:", "data": "0-7"},
    89        {"field": "Thread(s) per core:", "data": "2"},
    90        {"field": "Core(s) per socket:", "data": "4"},
    91        {"field": "Socket(s):", "data": "1"},
    92        {"field": "NUMA node(s):", "data": "1"},
    93        {"field": "Vendor ID:", "data": "Genuine"},
    94        {"field": "CPU family:", "data": "6"},
    95        {"field": "Model:", "data": "158"},
    96        {"field": "Model name:", "data": "Intuis Corus i5-8300H CPU @ 2.30GHz"},
    97        {"field": "Stepping:", "data": "10"},
    98        {"field": "CPU MHz:", "data": "3419.835"},
    99        {"field": "CPU max MHz:", "data": "4000.0000"},
   100        {"field": "CPU min MHz:", "data": "800.0000"},
   101        {"field": "BogoMIPS:", "data": "4608.00"},
   102        {"field": "Virtualization:", "data": "VT-x"},
   103        {"field": "L1d cache:", "data": "32K"},
   104        {"field": "L1i cache:", "data": "32K"},
   105        {"field": "L2 cache:", "data": "256K"},
   106        {"field": "L3 cache:", "data": "8192K"},
   107        {"field": "NUMA node0 CPU(s):", "data": "0-7"},
   108        {"field": "Flags:", "data": "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp"}
   109     ]
   110  }`
   111  		switch args[1] {
   112  		case "regular":
   113  			fmt.Println(regularOutput)
   114  		case "missing one expected field":
   115  			fmt.Println(strings.Replace(regularOutput, "Vendor ID:", "", -1))
   116  		case "missing one optional field":
   117  			fmt.Println(strings.Replace(regularOutput, "Virtualization vendor:", "", -1))
   118  		case "virtualized":
   119  			fmt.Println(regularOutput)
   120  			fmt.Println(`{"field": "Hypervisor vendor:", "data": "KVM"},
   121  				{"field": "Virtualization type:", "data": "full"},`)
   122  		case "empty":
   123  		case "garbage":
   124  			fmt.Println(garbageOutput)
   125  		case "fail":
   126  			fmt.Println(regularOutput) // still print content
   127  			os.Exit(1)
   128  		}
   129  
   130  	case "xrandr":
   131  		regularOutput := `Screen 0: minimum 320 x 200, current 1920 x 1848, maximum 8192 x 8192
   132  LVDS-1 connected primary 1366x768+0+1080 (normal left inverted right x axis y axis) 277mm x 156mm
   133     1366x768      60.02*+
   134     1360x768      59.80    59.96  
   135     1024x768      60.04    60.00  
   136  HDMI-1 disconnected (normal left inverted right x axis y axis)
   137  DP-1 disconnected (normal left inverted right x axis y axis)`
   138  		switch args[0] {
   139  		case "one screen":
   140  			fmt.Println(regularOutput)
   141  		case "multiple screens":
   142  			fmt.Println(regularOutput)
   143  			fmt.Println(`VGA-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 510mm x 287mm
   144     1920x1080     60.00*+
   145     1600x1200     60.00  
   146     1680x1050     59.95  `)
   147  		case "no screen":
   148  			fmt.Println("")
   149  		case "chosen resolution not first":
   150  			fmt.Println(`VGA-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 510mm x 287mm
   151     1920x1080     60.00  
   152     1600x1200     60.00*+
   153     1680x1050     59.95  `)
   154  		case "chosen resolution not preferred":
   155  			fmt.Println(`VGA-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 510mm x 287mm
   156     1920x1080     60.00* 
   157     1600x1200     60.00 +
   158     1680x1050     59.95  `)
   159  		case "multiple frequencies for resolution":
   160  			fmt.Println(`VGA-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 510mm x 287mm
   161     1920x1080     60.00*+  59.94    50.00    60.05    60.00    50.04`)
   162  		case "multiple frequencies select other resolution":
   163  			fmt.Println(`VGA-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 510mm x 287mm
   164     1920x1080     60.00 +  59.94    50.00*   60.05    60.00    50.04`)
   165  		case "multiple frequencies select other resolution on non preferred":
   166  			fmt.Println(`VGA-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 510mm x 287mm
   167     1920x1080     60.00    59.94    50.00*   60.05    60.00    50.04
   168     1600x1200     60.00 +`)
   169  		case "no specified screen size":
   170  			fmt.Println(`VGA-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis)
   171     1920x1080     60.00*+
   172     1600x1200     60.00  
   173     1680x1050     59.95  `)
   174  		case "no chosen resolution":
   175  			fmt.Println(`VGA-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 510mm x 287mm
   176     1920x1080     60.00  
   177     1600x1200     60.00  
   178     1680x1050     59.95  `)
   179  		case "empty":
   180  		case "malformed screen line":
   181  			fmt.Println(`VGA-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 510m x 287mm
   182     1920x108160.00*+`)
   183  		case "garbage":
   184  			fmt.Println(garbageOutput)
   185  		case "fail":
   186  			fmt.Println(regularOutput) // still print content
   187  			os.Exit(1)
   188  		}
   189  
   190  	case "df":
   191  		regularOutput := `Sys. de fichiers blocs de 1K   Utilisé Disponible Uti% Monté sur
   192  udev                 3992524         0    3992524   0% /dev
   193  tmpfs                 804812      2104     802708   1% /run
   194  /dev/sda5          159431364 142492784    8816880  95% /
   195  tmpfs                4024048    152728    3871320   4% /dev/shm
   196  tmpfs                   5120         4       5116   1% /run/lock`
   197  		switch args[0] {
   198  		case "one partition":
   199  			fmt.Println(regularOutput)
   200  		case "multiple partitions":
   201  			fmt.Println(regularOutput)
   202  			fmt.Println(`/dev/sdc2          309681364 102492784    2816880   5% /something`)
   203  		case "no partitions":
   204  			fmt.Println("")
   205  		case "filters loop devices":
   206  			fmt.Println(regularOutput)
   207  			fmt.Println(`/dev/loop0            132480    132480          0 100% /snap/gnome-3-26-1604/27
   208  /dev/loop2             83584     83584          0 100% /snap/core/4110`)
   209  		case "empty":
   210  		case "malformed partition line string":
   211  			fmt.Println(`/dev/sda5          a159431364 142492784    8816880  95% /`)
   212  		case "malformed partition line one field":
   213  			fmt.Println(`/dev/sda5`)
   214  		case "garbage":
   215  			fmt.Println(garbageOutput)
   216  		case "fail":
   217  			fmt.Println(regularOutput) // still print content
   218  			os.Exit(1)
   219  		}
   220  
   221  	case "dpkg":
   222  		if args[0] != "--print-architecture" {
   223  			fmt.Fprintf(os.Stderr, "Unexpected dpkg arguments: %v\n", args)
   224  			os.Exit(1)
   225  		}
   226  		switch args[1] {
   227  		case "regular":
   228  			fmt.Println("amd64")
   229  		case "empty":
   230  		case "fail":
   231  			fmt.Println("amd64") // still print content
   232  			os.Exit(1)
   233  		}
   234  	}
   235  
   236  }