github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/configstate/configcore/proxy_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 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 configcore_test 21 22 import ( 23 "fmt" 24 "io/ioutil" 25 "os" 26 "path/filepath" 27 "time" 28 29 . "gopkg.in/check.v1" 30 31 "github.com/snapcore/snapd/asserts" 32 "github.com/snapcore/snapd/asserts/assertstest" 33 "github.com/snapcore/snapd/dirs" 34 "github.com/snapcore/snapd/overlord/assertstate" 35 "github.com/snapcore/snapd/overlord/assertstate/assertstatetest" 36 "github.com/snapcore/snapd/overlord/configstate/configcore" 37 "github.com/snapcore/snapd/release" 38 "github.com/snapcore/snapd/testutil" 39 ) 40 41 type proxySuite struct { 42 configcoreSuite 43 44 mockEtcEnvironment string 45 46 storeSigning *assertstest.StoreStack 47 } 48 49 var _ = Suite(&proxySuite{}) 50 51 func (s *proxySuite) SetUpTest(c *C) { 52 s.configcoreSuite.SetUpTest(c) 53 54 dirs.SetRootDir(c.MkDir()) 55 err := os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/"), 0755) 56 c.Assert(err, IsNil) 57 s.mockEtcEnvironment = filepath.Join(dirs.GlobalRootDir, "/etc/environment") 58 59 s.storeSigning = assertstest.NewStoreStack("canonical", nil) 60 61 db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{ 62 Backstore: asserts.NewMemoryBackstore(), 63 Trusted: s.storeSigning.Trusted, 64 OtherPredefined: s.storeSigning.Generic, 65 }) 66 c.Assert(err, IsNil) 67 68 s.state.Lock() 69 assertstate.ReplaceDB(s.state, db) 70 s.state.Unlock() 71 72 err = db.Add(s.storeSigning.StoreAccountKey("")) 73 c.Assert(err, IsNil) 74 } 75 76 func (s *proxySuite) TearDownTest(c *C) { 77 dirs.SetRootDir("/") 78 } 79 80 func (s *proxySuite) makeMockEtcEnvironment(c *C) { 81 err := ioutil.WriteFile(s.mockEtcEnvironment, []byte(` 82 PATH="/usr/bin" 83 `), 0644) 84 c.Assert(err, IsNil) 85 } 86 87 func (s *proxySuite) TestConfigureProxy(c *C) { 88 restore := release.MockOnClassic(false) 89 defer restore() 90 91 for _, proto := range []string{"http", "https", "ftp"} { 92 // populate with content 93 s.makeMockEtcEnvironment(c) 94 95 err := configcore.Run(&mockConf{ 96 state: s.state, 97 conf: map[string]interface{}{ 98 fmt.Sprintf("proxy.%s", proto): fmt.Sprintf("%s://example.com", proto), 99 }, 100 }) 101 c.Assert(err, IsNil) 102 103 c.Check(s.mockEtcEnvironment, testutil.FileEquals, fmt.Sprintf(` 104 PATH="/usr/bin" 105 %[1]s_proxy=%[1]s://example.com`, proto)) 106 } 107 } 108 109 func (s *proxySuite) TestConfigureNoProxy(c *C) { 110 restore := release.MockOnClassic(false) 111 defer restore() 112 113 // populate with content 114 s.makeMockEtcEnvironment(c) 115 err := configcore.Run(&mockConf{ 116 state: s.state, 117 conf: map[string]interface{}{ 118 "proxy.no-proxy": "example.com,bar.com", 119 }, 120 }) 121 c.Assert(err, IsNil) 122 123 c.Check(s.mockEtcEnvironment, testutil.FileEquals, ` 124 PATH="/usr/bin" 125 no_proxy=example.com,bar.com`) 126 } 127 128 func (s *proxySuite) TestConfigureProxyStore(c *C) { 129 // set to "" 130 err := configcore.Run(&mockConf{ 131 state: s.state, 132 conf: map[string]interface{}{ 133 "proxy.store": "", 134 }, 135 }) 136 c.Check(err, IsNil) 137 138 // no assertion 139 conf := &mockConf{ 140 state: s.state, 141 conf: map[string]interface{}{ 142 "proxy.store": "foo", 143 }, 144 } 145 146 err = configcore.Run(conf) 147 c.Check(err, ErrorMatches, `cannot set proxy.store to "foo" without a matching store assertion`) 148 149 operatorAcct := assertstest.NewAccount(s.storeSigning, "foo-operator", nil, "") 150 // have a store assertion 151 stoAs, err := s.storeSigning.Sign(asserts.StoreType, map[string]interface{}{ 152 "store": "foo", 153 "operator-id": operatorAcct.AccountID(), 154 "url": "http://store.interal:9943", 155 "timestamp": time.Now().Format(time.RFC3339), 156 }, nil, "") 157 c.Assert(err, IsNil) 158 func() { 159 s.state.Lock() 160 defer s.state.Unlock() 161 assertstatetest.AddMany(s.state, operatorAcct, stoAs) 162 }() 163 164 err = configcore.Run(conf) 165 c.Check(err, IsNil) 166 } 167 168 func (s *proxySuite) TestConfigureProxyStoreNoURL(c *C) { 169 conf := &mockConf{ 170 state: s.state, 171 conf: map[string]interface{}{ 172 "proxy.store": "foo", 173 }, 174 } 175 176 operatorAcct := assertstest.NewAccount(s.storeSigning, "foo-operator", nil, "") 177 // have a store assertion but no url 178 stoAs, err := s.storeSigning.Sign(asserts.StoreType, map[string]interface{}{ 179 "store": "foo", 180 "operator-id": operatorAcct.AccountID(), 181 "timestamp": time.Now().Format(time.RFC3339), 182 }, nil, "") 183 c.Assert(err, IsNil) 184 func() { 185 s.state.Lock() 186 defer s.state.Unlock() 187 assertstatetest.AddMany(s.state, operatorAcct, stoAs) 188 }() 189 190 err = configcore.Run(conf) 191 c.Check(err, ErrorMatches, `cannot set proxy.store to "foo" with a matching store assertion with url unset`) 192 }