github.com/freetocompute/snapd@v0.0.0-20210618182524-2fb355d72fd9/osutil/meminfo_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2021 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package osutil_test 21 22 import ( 23 "io/ioutil" 24 "path/filepath" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/osutil" 29 ) 30 31 type meminfoSuite struct{} 32 33 var _ = Suite(&meminfoSuite{}) 34 35 const meminfoExampleFromLiveSystem = `MemTotal: 32876680 kB 36 MemFree: 3478104 kB 37 MemAvailable: 20527364 kB 38 Buffers: 1584432 kB 39 Cached: 14550292 kB 40 SwapCached: 0 kB 41 Active: 8344864 kB 42 Inactive: 16209412 kB 43 Active(anon): 139828 kB 44 Inactive(anon): 8944656 kB 45 Active(file): 8205036 kB 46 Inactive(file): 7264756 kB 47 Unevictable: 2152 kB 48 Mlocked: 2152 kB 49 SwapTotal: 0 kB 50 SwapFree: 0 kB 51 Dirty: 804 kB 52 Writeback: 4 kB 53 AnonPages: 8201352 kB 54 Mapped: 1223272 kB 55 Shmem: 697812 kB 56 KReclaimable: 2043404 kB 57 Slab: 2423344 kB 58 SReclaimable: 2043404 kB 59 SUnreclaim: 379940 kB 60 KernelStack: 37392 kB 61 PageTables: 97644 kB 62 NFS_Unstable: 0 kB 63 Bounce: 0 kB 64 WritebackTmp: 0 kB 65 CommitLimit: 16438340 kB 66 Committed_AS: 30191756 kB 67 VmallocTotal: 34359738367 kB 68 VmallocUsed: 160500 kB 69 VmallocChunk: 0 kB 70 Percpu: 24256 kB 71 HardwareCorrupted: 0 kB 72 AnonHugePages: 120832 kB 73 ShmemHugePages: 0 kB 74 ShmemPmdMapped: 0 kB 75 FileHugePages: 0 kB 76 FilePmdMapped: 0 kB 77 CmaTotal: 0 kB 78 CmaFree: 0 kB 79 HugePages_Total: 0 80 HugePages_Free: 0 81 HugePages_Rsvd: 0 82 HugePages_Surp: 0 83 Hugepagesize: 2048 kB 84 Hugetlb: 0 kB 85 DirectMap4k: 3059376 kB 86 DirectMap2M: 23089152 kB 87 DirectMap1G: 8388608 kB 88 ` 89 90 func (s *meminfoSuite) TestMemInfoHappy(c *C) { 91 p := filepath.Join(c.MkDir(), "meminfo") 92 restore := osutil.MockProcMeminfo(p) 93 defer restore() 94 95 c.Assert(ioutil.WriteFile(p, []byte(meminfoExampleFromLiveSystem), 0644), IsNil) 96 97 mem, err := osutil.TotalSystemMemory() 98 c.Assert(err, IsNil) 99 c.Check(mem, Equals, uint64(32876680)*1024) 100 101 c.Assert(ioutil.WriteFile(p, []byte(`MemTotal: 1234 kB`), 0644), IsNil) 102 103 mem, err = osutil.TotalSystemMemory() 104 c.Assert(err, IsNil) 105 c.Check(mem, Equals, uint64(1234)*1024) 106 107 const meminfoReorderedWithEmptyLine = `MemAvailable: 20527370 kB 108 109 MemTotal: 32876699 kB 110 MemFree: 3478104 kB 111 ` 112 c.Assert(ioutil.WriteFile(p, []byte(meminfoReorderedWithEmptyLine), 0644), IsNil) 113 114 mem, err = osutil.TotalSystemMemory() 115 c.Assert(err, IsNil) 116 c.Check(mem, Equals, uint64(32876699)*1024) 117 } 118 119 func (s *meminfoSuite) TestMemInfoFromHost(c *C) { 120 mem, err := osutil.TotalSystemMemory() 121 c.Assert(err, IsNil) 122 c.Check(mem > uint64(32*1024*1024), 123 Equals, true, Commentf("unexpected system memory %v", mem)) 124 } 125 126 func (s *meminfoSuite) TestMemInfoUnhappy(c *C) { 127 p := filepath.Join(c.MkDir(), "meminfo") 128 restore := osutil.MockProcMeminfo(p) 129 defer restore() 130 131 const noTotalMem = `MemFree: 3478104 kB 132 MemAvailable: 20527364 kB 133 Buffers: 1584432 kB 134 Cached: 14550292 kB 135 ` 136 const notkBTotalMem = `MemTotal: 3478104 MB 137 ` 138 const missingFieldsTotalMem = `MemTotal: 1234 139 ` 140 const badTotalMem = `MemTotal: abcdef kB 141 ` 142 const hexTotalMem = `MemTotal: 0xabcdef kB 143 ` 144 145 for _, tc := range []struct { 146 content, err string 147 }{ 148 { 149 content: noTotalMem, 150 err: `cannot determine the total amount of memory in the system from .*/meminfo`, 151 }, { 152 content: notkBTotalMem, 153 err: `cannot process unexpected meminfo entry "MemTotal: 3478104 MB"`, 154 }, { 155 content: missingFieldsTotalMem, 156 err: `cannot process unexpected meminfo entry "MemTotal: 1234"`, 157 }, { 158 content: badTotalMem, 159 err: `cannot convert memory size value: strconv.ParseUint: parsing "abcdef": invalid syntax`, 160 }, { 161 content: hexTotalMem, 162 err: `cannot convert memory size value: strconv.ParseUint: parsing "0xabcdef": invalid syntax`, 163 }, 164 } { 165 c.Assert(ioutil.WriteFile(p, []byte(tc.content), 0644), IsNil) 166 mem, err := osutil.TotalSystemMemory() 167 c.Assert(err, ErrorMatches, tc.err) 168 c.Check(mem, Equals, uint64(0)) 169 } 170 }