github.com/freetocompute/snapd@v0.0.0-20210618182524-2fb355d72fd9/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 "time" 28 29 "github.com/juju/ratelimit" 30 "gopkg.in/retry.v1" 31 32 "github.com/snapcore/snapd/overlord/auth" 33 "github.com/snapcore/snapd/progress" 34 "github.com/snapcore/snapd/snap" 35 "github.com/snapcore/snapd/testutil" 36 ) 37 38 var ( 39 HardLinkCount = hardLinkCount 40 ApiURL = apiURL 41 Download = download 42 43 UseDeltas = useDeltas 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 // MockDefaultRetryStrategy mocks the retry strategy used by several store requests 66 func MockDefaultRetryStrategy(t *testutil.BaseTest, strategy retry.Strategy) { 67 originalDefaultRetryStrategy := defaultRetryStrategy 68 defaultRetryStrategy = strategy 69 t.AddCleanup(func() { 70 defaultRetryStrategy = originalDefaultRetryStrategy 71 }) 72 } 73 74 func MockDownloadRetryStrategy(t *testutil.BaseTest, strategy retry.Strategy) { 75 originalDownloadRetryStrategy := downloadRetryStrategy 76 downloadRetryStrategy = strategy 77 t.AddCleanup(func() { 78 downloadRetryStrategy = originalDownloadRetryStrategy 79 }) 80 } 81 82 func MockConnCheckStrategy(t *testutil.BaseTest, strategy retry.Strategy) { 83 originalConnCheckStrategy := connCheckStrategy 84 connCheckStrategy = strategy 85 t.AddCleanup(func() { 86 connCheckStrategy = originalConnCheckStrategy 87 }) 88 } 89 90 func MockDownloadSpeedParams(measureWindow time.Duration, minSpeed float64) (restore func()) { 91 oldSpeedMeasureWindow := downloadSpeedMeasureWindow 92 oldSpeedMin := downloadSpeedMin 93 downloadSpeedMeasureWindow = measureWindow 94 downloadSpeedMin = minSpeed 95 return func() { 96 downloadSpeedMeasureWindow = oldSpeedMeasureWindow 97 downloadSpeedMin = oldSpeedMin 98 } 99 } 100 101 func IsTransferSpeedError(err error) (ok bool, speed float64) { 102 de, ok := err.(*transferSpeedError) 103 if !ok { 104 return false, 0 105 } 106 return true, de.Speed 107 } 108 109 func (w *TransferSpeedMonitoringWriter) MeasuredWindowsCount() int { 110 return w.measuredWindows 111 } 112 113 func (cm *CacheManager) CacheDir() string { 114 return cm.cacheDir 115 } 116 117 func (cm *CacheManager) Cleanup() error { 118 return cm.cleanup() 119 } 120 121 func (cm *CacheManager) Count() int { 122 return cm.count() 123 } 124 125 func MockOsRemove(f func(name string) error) func() { 126 oldOsRemove := osRemove 127 osRemove = f 128 return func() { 129 osRemove = oldOsRemove 130 } 131 } 132 133 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()) { 134 origDownload := download 135 download = f 136 return func() { 137 download = origDownload 138 } 139 } 140 141 func MockDoDownloadReq(f func(ctx context.Context, storeURL *url.URL, cdnHeader string, resume int64, s *Store, user *auth.UserState) (*http.Response, error)) (restore func()) { 142 orig := doDownloadReq 143 doDownloadReq = f 144 return func() { 145 doDownloadReq = orig 146 } 147 } 148 149 func MockApplyDelta(f func(name string, deltaPath string, deltaInfo *snap.DeltaInfo, targetPath string, targetSha3_384 string) error) (restore func()) { 150 origApplyDelta := applyDelta 151 applyDelta = f 152 return func() { 153 applyDelta = origApplyDelta 154 } 155 } 156 157 func (sto *Store) MockCacher(obs downloadCache) (restore func()) { 158 oldCacher := sto.cacher 159 sto.cacher = obs 160 return func() { 161 sto.cacher = oldCacher 162 } 163 } 164 165 func (sto *Store) SetDeltaFormat(dfmt string) { 166 sto.deltaFormat = dfmt 167 } 168 169 func (sto *Store) DownloadDelta(deltaName string, downloadInfo *snap.DownloadInfo, w io.ReadWriteSeeker, pbar progress.Meter, user *auth.UserState, dlOpts *DownloadOptions) error { 170 return sto.downloadDelta(deltaName, downloadInfo, w, pbar, user, dlOpts) 171 } 172 173 func (sto *Store) DoRequest(ctx context.Context, client *http.Client, reqOptions *requestOptions, user *auth.UserState) (*http.Response, error) { 174 return sto.doRequest(ctx, client, reqOptions, user) 175 } 176 177 func (sto *Store) Client() *http.Client { 178 return sto.client 179 } 180 181 func (sto *Store) DetailFields() []string { 182 return sto.detailFields 183 } 184 185 func (sto *Store) DecorateOrders(snaps []*snap.Info, user *auth.UserState) error { 186 return sto.decorateOrders(snaps, user) 187 } 188 189 func (sto *Store) SessionLock() { 190 sto.sessionMu.Lock() 191 } 192 193 func (sto *Store) SessionUnlock() { 194 sto.sessionMu.Unlock() 195 } 196 197 func (sto *Store) FindFields() []string { 198 return sto.findFields 199 } 200 201 func (cfg *Config) SetBaseURL(u *url.URL) error { 202 return cfg.setBaseURL(u) 203 } 204 205 func NewHashError(name, sha3_384, targetSha3_384 string) HashError { 206 return HashError{name, sha3_384, targetSha3_384} 207 } 208 209 func NewRequestOptions(mth string, url *url.URL) *requestOptions { 210 return &requestOptions{ 211 Method: mth, 212 URL: url, 213 } 214 } 215 216 func MockRatelimitReader(f func(r io.Reader, bucket *ratelimit.Bucket) io.Reader) (restore func()) { 217 oldRatelimitReader := ratelimitReader 218 ratelimitReader = f 219 return func() { 220 ratelimitReader = oldRatelimitReader 221 } 222 } 223 224 type ( 225 ErrorListEntryJSON = errorListEntry 226 SnapActionResultJSON = snapActionResult 227 ) 228 229 var ReportFetchAssertionsError = reportFetchAssertionsError