gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/free/free_test.go (about) 1 // Copyright 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 package main 6 7 import ( 8 "testing" 9 ) 10 11 func TestMeminfoFromBytes(t *testing.T) { 12 input := []byte(`MemTotal: 8052976 kB 13 MemFree: 721716 kB 14 MemAvailable: 2774100 kB 15 Buffers: 244880 kB 16 Cached: 3462124 kB 17 SwapTotal: 8265724 kB 18 SwapFree: 8264956 kB 19 SReclaimable: 179852 kB`) 20 m, err := meminfoFromBytes(input) 21 if err != nil { 22 t.Fatal(err) 23 } 24 if m["MemFree"] != 721716 { 25 t.Fatalf("MemFree: got %v, want 721716", m["MemFree"]) 26 } 27 if m["MemAvailable"] != 2774100 { 28 t.Fatalf("MemAvailable: got %v, want 2774100", m["MemAvailable"]) 29 } 30 if m["Buffers"] != 244880 { 31 t.Fatalf("Buffers: got %v, want 244880", m["Buffers"]) 32 } 33 if m["Cached"] != 3462124 { 34 t.Fatalf("Cached: got %v, want 3462124", m["Cached"]) 35 } 36 if m["SwapTotal"] != 8265724 { 37 t.Fatalf("SwapTotal: got %v, want 8265724", m["SwapTotal"]) 38 } 39 if m["SwapFree"] != 8264956 { 40 t.Fatalf("SwapFree: got %v, want 8264956", m["SwapFree"]) 41 } 42 if m["SReclaimable"] != 179852 { 43 t.Fatalf("SReclaimable: got %v, want 179852", m["SReclaimable"]) 44 } 45 } 46 47 func TestPrintSwap(t *testing.T) { 48 input := []byte(`MemTotal: 8052976 kB 49 MemFree: 721716 kB 50 MemAvailable: 2774100 kB 51 Buffers: 244880 kB 52 Cached: 3462124 kB 53 SwapTotal: 8265724 kB 54 SwapFree: 8264956 kB 55 SReclaimable: 179852 kB`) 56 m, err := meminfoFromBytes(input) 57 if err != nil { 58 t.Fatal(err) 59 } 60 si, err := getSwapInfo(m, &FreeConfig{Unit: KB}) 61 if err != nil { 62 t.Fatal(err) 63 } 64 if si.Total != 8464101376 { 65 t.Fatalf("Swap.Total: got %v, want 8464101376", si.Total) 66 } 67 if si.Used != 786432 { 68 t.Fatalf("Swap.Used: got %v, want 786432", si.Used) 69 } 70 if si.Free != 8463314944 { 71 t.Fatalf("Swap.Free: got %v, want 8463314944", si.Free) 72 } 73 } 74 75 func TestPrintSwapMissingFields(t *testing.T) { 76 input := []byte(`MemTotal: 8052976 kB 77 MemFree: 721716 kB 78 MemAvailable: 2774100 kB 79 Buffers: 244880 kB 80 Cached: 3462124 kB 81 SwapTotal: 8265724 kB 82 SReclaimable: 179852 kB`) 83 m, err := meminfoFromBytes(input) 84 if err != nil { 85 t.Fatal(err) 86 } 87 _, err = getSwapInfo(m, &FreeConfig{Unit: KB}) 88 // should error out for the missing field 89 if err == nil { 90 t.Fatal("printSwap: got no error when expecting one") 91 } 92 } 93 94 func TestPrintMem(t *testing.T) { 95 input := []byte(`MemTotal: 8052976 kB 96 MemFree: 721716 kB 97 MemAvailable: 2774100 kB 98 Buffers: 244880 kB 99 Cached: 3462124 kB 100 Shmem: 1617788 kB 101 SwapTotal: 8265724 kB 102 SwapFree: 8264956 kB 103 SReclaimable: 179852 kB`) 104 m, err := meminfoFromBytes(input) 105 if err != nil { 106 t.Fatal(err) 107 } 108 mmi, err := getMainMemInfo(m, &FreeConfig{Unit: KB}) 109 if err != nil { 110 t.Fatal(err) 111 } 112 if mmi.Total != 8246247424 { 113 t.Fatalf("MainMem.Total: got %v, want 8246247424", mmi.Total) 114 } 115 if mmi.Free != 739037184 { 116 t.Fatalf("MainMem.Free: got %v, want 739037184", mmi.Free) 117 } 118 if mmi.Used != 3527069696 { 119 t.Fatalf("MainMem.Used: got %v, want 3527069696", mmi.Used) 120 } 121 if mmi.Shared != 1656614912 { 122 t.Fatalf("MainMem.Shared: got %v, want 1656614912", mmi.Shared) 123 } 124 if mmi.Cached != 3729383424 { 125 t.Fatalf("MainMem.Cached: got %v, want 3729383424", mmi.Cached) 126 } 127 if mmi.Buffers != 250757120 { 128 t.Fatalf("MainMem.Buffers: got %v, want 250757120", mmi.Buffers) 129 } 130 if mmi.Available != 2840678400 { 131 t.Fatalf("MainMem.Available: got %v, want 2840678400", mmi.Available) 132 } 133 } 134 135 func TestPrintMemMissingFields(t *testing.T) { 136 input := []byte(`MemTotal: 8052976 kB 137 MemFree: 721716 kB 138 MemAvailable: 2774100 kB 139 Buffers: 244880 kB 140 SwapTotal: 8265724 kB 141 SwapFree: 8264956 kB 142 SReclaimable: 179852 kB`) 143 m, err := meminfoFromBytes(input) 144 if err != nil { 145 t.Fatal(err) 146 } 147 _, err = getMainMemInfo(m, &FreeConfig{Unit: KB}) 148 // should error out for the missing field 149 if err == nil { 150 t.Fatal("printMem: got no error when expecting one") 151 } 152 }