github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/store/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 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 store 21 22 import ( 23 "context" 24 "io" 25 "net/http" 26 "net/url" 27 "os/exec" 28 "time" 29 30 "github.com/juju/ratelimit" 31 "gopkg.in/retry.v1" 32 33 "github.com/snapcore/snapd/overlord/auth" 34 "github.com/snapcore/snapd/progress" 35 "github.com/snapcore/snapd/snap" 36 "github.com/snapcore/snapd/testutil" 37 ) 38 39 var ( 40 HardLinkCount = hardLinkCount 41 ApiURL = apiURL 42 Download = download 43 44 ApplyDelta = applyDelta 45 46 AuthLocation = authLocation 47 AuthURL = authURL 48 StoreURL = storeURL 49 StoreDeveloperURL = storeDeveloperURL 50 MustBuy = mustBuy 51 52 RequestStoreMacaroon = requestStoreMacaroon 53 DischargeAuthCaveat = dischargeAuthCaveat 54 RefreshDischargeMacaroon = refreshDischargeMacaroon 55 RequestStoreDeviceNonce = requestStoreDeviceNonce 56 RequestDeviceSession = requestDeviceSession 57 LoginCaveatID = loginCaveatID 58 59 JsonContentType = jsonContentType 60 SnapActionFields = snapActionFields 61 62 Cancelled = cancelled 63 ) 64 65 func MockSnapdtoolCommandFromSystemSnap(f func(name string, args ...string) (*exec.Cmd, error)) (restore func()) { 66 old := commandFromSystemSnap 67 commandFromSystemSnap = f 68 return func() { 69 commandFromSystemSnap = old 70 } 71 } 72 73 // MockDefaultRetryStrategy mocks the retry strategy used by several store requests 74 func MockDefaultRetryStrategy(t *testutil.BaseTest, strategy retry.Strategy) { 75 originalDefaultRetryStrategy := defaultRetryStrategy 76 defaultRetryStrategy = strategy 77 t.AddCleanup(func() { 78 defaultRetryStrategy = originalDefaultRetryStrategy 79 }) 80 } 81 82 func MockDownloadRetryStrategy(t *testutil.BaseTest, strategy retry.Strategy) { 83 originalDownloadRetryStrategy := downloadRetryStrategy 84 downloadRetryStrategy = strategy 85 t.AddCleanup(func() { 86 downloadRetryStrategy = originalDownloadRetryStrategy 87 }) 88 } 89 90 func MockConnCheckStrategy(t *testutil.BaseTest, strategy retry.Strategy) { 91 originalConnCheckStrategy := connCheckStrategy 92 connCheckStrategy = strategy 93 t.AddCleanup(func() { 94 connCheckStrategy = originalConnCheckStrategy 95 }) 96 } 97 98 func MockDownloadSpeedParams(measureWindow time.Duration, minSpeed float64) (restore func()) { 99 oldSpeedMeasureWindow := downloadSpeedMeasureWindow 100 oldSpeedMin := downloadSpeedMin 101 downloadSpeedMeasureWindow = measureWindow 102 downloadSpeedMin = minSpeed 103 return func() { 104 downloadSpeedMeasureWindow = oldSpeedMeasureWindow 105 downloadSpeedMin = oldSpeedMin 106 } 107 } 108 109 func IsTransferSpeedError(err error) (ok bool, speed float64) { 110 de, ok := err.(*transferSpeedError) 111 if !ok { 112 return false, 0 113 } 114 return true, de.Speed 115 } 116 117 func (w *TransferSpeedMonitoringWriter) MeasuredWindowsCount() int { 118 return w.measuredWindows 119 } 120 121 func (cm *CacheManager) CacheDir() string { 122 return cm.cacheDir 123 } 124 125 func (cm *CacheManager) Cleanup() error { 126 return cm.cleanup() 127 } 128 129 func (cm *CacheManager) Count() int { 130 return cm.count() 131 } 132 133 func MockOsRemove(f func(name string) error) func() { 134 oldOsRemove := osRemove 135 osRemove = f 136 return func() { 137 osRemove = oldOsRemove 138 } 139 } 140 141 func MockDownload(f func(ctx context.Context, name, sha3_384, downloadURL string, user *auth.UserState, s *Store, w io.ReadWriteSeeker, resume int64, pbar progress.Meter, dlOpts *DownloadOptions) error) (restore func()) { 142 origDownload := download 143 download = f 144 return func() { 145 download = origDownload 146 } 147 } 148 149 func MockDoDownloadReq(f func(ctx context.Context, storeURL *url.URL, cdnHeader string, resume int64, s *Store, user *auth.UserState) (*http.Response, error)) (restore func()) { 150 orig := doDownloadReq 151 doDownloadReq = f 152 return func() { 153 doDownloadReq = orig 154 } 155 } 156 157 func MockApplyDelta(f func(s *Store, name string, deltaPath string, deltaInfo *snap.DeltaInfo, targetPath string, targetSha3_384 string) error) (restore func()) { 158 origApplyDelta := applyDelta 159 applyDelta = f 160 return func() { 161 applyDelta = origApplyDelta 162 } 163 } 164 165 func (sto *Store) MockCacher(obs downloadCache) (restore func()) { 166 oldCacher := sto.cacher 167 sto.cacher = obs 168 return func() { 169 sto.cacher = oldCacher 170 } 171 } 172 173 func (sto *Store) SetDeltaFormat(dfmt string) { 174 sto.deltaFormat = dfmt 175 } 176 177 func (sto *Store) DownloadDelta(deltaName string, downloadInfo *snap.DownloadInfo, w io.ReadWriteSeeker, pbar progress.Meter, user *auth.UserState, dlOpts *DownloadOptions) error { 178 return sto.downloadDelta(deltaName, downloadInfo, w, pbar, user, dlOpts) 179 } 180 181 func (sto *Store) DoRequest(ctx context.Context, client *http.Client, reqOptions *requestOptions, user *auth.UserState) (*http.Response, error) { 182 return sto.doRequest(ctx, client, reqOptions, user) 183 } 184 185 func (sto *Store) Client() *http.Client { 186 return sto.client 187 } 188 189 func (sto *Store) DetailFields() []string { 190 return sto.detailFields 191 } 192 193 func (sto *Store) DecorateOrders(snaps []*snap.Info, user *auth.UserState) error { 194 return sto.decorateOrders(snaps, user) 195 } 196 197 func (sto *Store) SessionLock() { 198 sto.sessionMu.Lock() 199 } 200 201 func (sto *Store) SessionUnlock() { 202 sto.sessionMu.Unlock() 203 } 204 205 func (sto *Store) FindFields() []string { 206 return sto.findFields 207 } 208 209 func (sto *Store) UseDeltas() bool { 210 return sto.useDeltas() 211 } 212 213 func (sto *Store) Xdelta3Cmd(args ...string) *exec.Cmd { 214 return sto.xdelta3CmdFunc(args...) 215 } 216 217 func (cfg *Config) SetBaseURL(u *url.URL) error { 218 return cfg.setBaseURL(u) 219 } 220 221 func NewHashError(name, sha3_384, targetSha3_384 string) HashError { 222 return HashError{name, sha3_384, targetSha3_384} 223 } 224 225 func NewRequestOptions(mth string, url *url.URL) *requestOptions { 226 return &requestOptions{ 227 Method: mth, 228 URL: url, 229 } 230 } 231 232 func MockRatelimitReader(f func(r io.Reader, bucket *ratelimit.Bucket) io.Reader) (restore func()) { 233 oldRatelimitReader := ratelimitReader 234 ratelimitReader = f 235 return func() { 236 ratelimitReader = oldRatelimitReader 237 } 238 } 239 240 type ( 241 ErrorListEntryJSON = errorListEntry 242 SnapActionResultJSON = snapActionResult 243 ) 244 245 var ReportFetchAssertionsError = reportFetchAssertionsError