@@ -164,6 +164,67 @@ func Test__Restore(t *testing.T) {
164164 os .Remove (tempDir )
165165 })
166166 })
167+
168+ runTestForSingleBackend (t , "sftp" , func (storage storage.Storage ) {
169+ t .Run ("restoring using HTTP works" , func (t * testing.T ) {
170+ storage .Clear ()
171+
172+ tempDir , _ := ioutil .TempDir (os .TempDir (), "*" )
173+ tempFile , _ := ioutil .TempFile (tempDir , "*" )
174+ _ = tempFile .Close ()
175+
176+ archiver := archive .NewShellOutArchiver (metrics .NewNoOpMetricsManager ())
177+ compressAndStore (storage , archiver , "abc" , tempDir )
178+
179+ // set the environment variables to download using HTTP instead before restoring
180+ os .Setenv ("SEMAPHORE_CACHE_CDN_URL" , "http://sftp-server:80" )
181+ os .Setenv ("SEMAPHORE_CACHE_CDN_KEY" , "test" )
182+ os .Setenv ("SEMAPHORE_CACHE_CDN_SECRET" , "test" )
183+ defer func () {
184+ os .Unsetenv ("SEMAPHORE_CACHE_CDN_URL" )
185+ os .Unsetenv ("SEMAPHORE_CACHE_CDN_KEY" )
186+ os .Unsetenv ("SEMAPHORE_CACHE_CDN_SECRET" )
187+ }()
188+
189+ RunRestore (restoreCmd , []string {"^abc" })
190+ output := readOutputFromFile (t )
191+
192+ restoredPath := filepath .FromSlash (fmt .Sprintf ("%s/" , tempDir ))
193+ assert .Contains (t , output , "HIT: '^abc', using key 'abc'." )
194+ assert .Contains (t , output , fmt .Sprintf ("Restored: %s." , restoredPath ))
195+
196+ os .Remove (tempFile .Name ())
197+ os .Remove (tempDir )
198+ })
199+
200+ t .Run ("restoring defaults to use SFTP if not all variables are available" , func (t * testing.T ) {
201+ storage .Clear ()
202+
203+ tempDir , _ := ioutil .TempDir (os .TempDir (), "*" )
204+ tempFile , _ := ioutil .TempFile (tempDir , "*" )
205+ _ = tempFile .Close ()
206+
207+ archiver := archive .NewShellOutArchiver (metrics .NewNoOpMetricsManager ())
208+ compressAndStore (storage , archiver , "abc" , tempDir )
209+
210+ // Set just the URL, but not the user/pass
211+ // This means SFTP will still be used.
212+ os .Setenv ("SEMAPHORE_CACHE_CDN_URL" , "http://sftp-server:80" )
213+ defer func () {
214+ os .Unsetenv ("SEMAPHORE_CACHE_CDN_URL" )
215+ }()
216+
217+ RunRestore (restoreCmd , []string {"^abc" })
218+ output := readOutputFromFile (t )
219+
220+ restoredPath := filepath .FromSlash (fmt .Sprintf ("%s/" , tempDir ))
221+ assert .Contains (t , output , "HIT: '^abc', using key 'abc'." )
222+ assert .Contains (t , output , fmt .Sprintf ("Restored: %s." , restoredPath ))
223+
224+ os .Remove (tempFile .Name ())
225+ os .Remove (tempDir )
226+ })
227+ })
167228}
168229
169230func Test__AutomaticRestore (t * testing.T ) {
0 commit comments