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