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