github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/snapdenv/snapdenv.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2020 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 snapdenv presents common environment (and related) options 21 // for snapd components. 22 package snapdenv 23 24 import ( 25 "os" 26 27 "github.com/snapcore/snapd/osutil" 28 ) 29 30 var mockTesting *bool 31 32 // is this a testing binary? (see withtestkeys.go) 33 var testingBinary = false 34 35 // Testing returns whether snapd components are under testing. 36 func Testing() bool { 37 if mockTesting != nil { 38 return *mockTesting 39 } 40 ok := osutil.GetenvBool("SNAPPY_TESTING") 41 if !ok { 42 // assume testing if we are a testing binary and the 43 // env is not set explicitly to the contrary 44 if testingBinary && os.Getenv("SNAPPY_TESTING") == "" { 45 return true 46 } 47 } 48 return ok 49 } 50 51 func MockTesting(testing bool) (restore func()) { 52 old := mockTesting 53 mockTesting = &testing 54 return func() { 55 mockTesting = old 56 } 57 } 58 59 var mockUseStagingStore *bool 60 61 // UseStagingStore returns whether snapd compontents should use the staging store. 62 func UseStagingStore() bool { 63 if mockUseStagingStore != nil { 64 return *mockUseStagingStore 65 } 66 return osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") 67 } 68 69 func MockUseStagingStore(useStaging bool) (restore func()) { 70 old := mockUseStagingStore 71 mockUseStagingStore = &useStaging 72 return func() { 73 mockUseStagingStore = old 74 } 75 } 76 77 var mockPreseeding *bool 78 79 // Preseeding returns whether snapd is preseeding, i.e. performing a 80 // partial first boot updating only filesystem state inside a chroot. 81 func Preseeding() bool { 82 if mockPreseeding != nil { 83 return *mockPreseeding 84 } 85 return osutil.GetenvBool("SNAPD_PRESEED") 86 } 87 88 func MockPreseeding(preseeding bool) (restore func()) { 89 old := mockPreseeding 90 mockPreseeding = &preseeding 91 return func() { 92 mockPreseeding = old 93 } 94 }