github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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 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 63 // MockDefaultRetryStrategy mocks the retry strategy used by several store requests 64 func MockDefaultRetryStrategy(t *testutil.BaseTest, strategy retry.Strategy) { 65 originalDefaultRetryStrategy := defaultRetryStrategy 66 defaultRetryStrategy = strategy 67 t.AddCleanup(func() { 68 defaultRetryStrategy = originalDefaultRetryStrategy 69 }) 70 } 71 72 func MockDownloadRetryStrategy(t *testutil.BaseTest, strategy retry.Strategy) { 73 originalDownloadRetryStrategy := downloadRetryStrategy 74 downloadRetryStrategy = strategy 75 t.AddCleanup(func() { 76 downloadRetryStrategy = originalDownloadRetryStrategy 77 }) 78 } 79 80 func MockConnCheckStrategy(t *testutil.BaseTest, strategy retry.Strategy) { 81 originalConnCheckStrategy := connCheckStrategy 82 connCheckStrategy = strategy 83 t.AddCleanup(func() { 84 connCheckStrategy = originalConnCheckStrategy 85 }) 86 } 87 88 func (cm *CacheManager) CacheDir() string { 89 return cm.cacheDir 90 } 91 92 func (cm *CacheManager) Cleanup() error { 93 return cm.cleanup() 94 } 95 96 func (cm *CacheManager) Count() int { 97 return cm.count() 98 } 99 100 func MockOsRemove(f func(name string) error) func() { 101 oldOsRemove := osRemove 102 osRemove = f 103 return func() { 104 osRemove = oldOsRemove 105 } 106 } 107 108 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()) { 109 origDownload := download 110 download = f 111 return func() { 112 download = origDownload 113 } 114 } 115 116 func MockDoDownloadReq(f func(ctx context.Context, storeURL *url.URL, cdnHeader string, s *Store, user *auth.UserState) (*http.Response, error)) (restore func()) { 117 orig := doDownloadReq 118 doDownloadReq = f 119 return func() { 120 doDownloadReq = orig 121 } 122 } 123 124 func MockApplyDelta(f func(name string, deltaPath string, deltaInfo *snap.DeltaInfo, targetPath string, targetSha3_384 string) error) (restore func()) { 125 origApplyDelta := applyDelta 126 applyDelta = f 127 return func() { 128 applyDelta = origApplyDelta 129 } 130 } 131 132 func (sto *Store) MockCacher(obs downloadCache) (restore func()) { 133 oldCacher := sto.cacher 134 sto.cacher = obs 135 return func() { 136 sto.cacher = oldCacher 137 } 138 } 139 140 func (sto *Store) SetDeltaFormat(dfmt string) { 141 sto.deltaFormat = dfmt 142 } 143 144 func (sto *Store) DownloadDelta(deltaName string, downloadInfo *snap.DownloadInfo, w io.ReadWriteSeeker, pbar progress.Meter, user *auth.UserState, dlOpts *DownloadOptions) error { 145 return sto.downloadDelta(deltaName, downloadInfo, w, pbar, user, dlOpts) 146 } 147 148 func (sto *Store) DoRequest(ctx context.Context, client *http.Client, reqOptions *requestOptions, user *auth.UserState) (*http.Response, error) { 149 return sto.doRequest(ctx, client, reqOptions, user) 150 } 151 152 func (sto *Store) Client() *http.Client { 153 return sto.client 154 } 155 156 func (sto *Store) DetailFields() []string { 157 return sto.detailFields 158 } 159 160 func (sto *Store) DecorateOrders(snaps []*snap.Info, user *auth.UserState) error { 161 return sto.decorateOrders(snaps, user) 162 } 163 164 func (sto *Store) SessionLock() { 165 sto.sessionMu.Lock() 166 } 167 168 func (sto *Store) SessionUnlock() { 169 sto.sessionMu.Unlock() 170 } 171 172 func (cfg *Config) SetBaseURL(u *url.URL) error { 173 return cfg.setBaseURL(u) 174 } 175 176 func NewHashError(name, sha3_384, targetSha3_384 string) HashError { 177 return HashError{name, sha3_384, targetSha3_384} 178 } 179 180 func NewRequestOptions(mth string, url *url.URL) *requestOptions { 181 return &requestOptions{ 182 Method: mth, 183 URL: url, 184 } 185 } 186 187 func MockRatelimitReader(f func(r io.Reader, bucket *ratelimit.Bucket) io.Reader) (restore func()) { 188 oldRatelimitReader := ratelimitReader 189 ratelimitReader = f 190 return func() { 191 ratelimitReader = oldRatelimitReader 192 } 193 }