mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-05 05:20:34 +00:00
Refactor to separate FakeDownloader, DownloadWithCompression, repo download.
This commit is contained in:
Vendored
+34
-1
@@ -3,6 +3,7 @@ package debian
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/smira/aptly/database"
|
||||
"github.com/smira/aptly/utils"
|
||||
debc "github.com/smira/godebiancontrol"
|
||||
"net/url"
|
||||
@@ -52,10 +53,17 @@ func (repo *RemoteRepo) ReleaseURL() *url.URL {
|
||||
return repo.archiveRootURL.ResolveReference(path)
|
||||
}
|
||||
|
||||
// BinaryURL returns URL of Packages file for given component and
|
||||
// architecture
|
||||
func (repo *RemoteRepo) BinaryURL(component string, architecture string) *url.URL {
|
||||
path := &url.URL{Path: fmt.Sprintf("dists/%s/%s/binary-%s/Packages", repo.Distribution, component, architecture)}
|
||||
return repo.archiveRootURL.ResolveReference(path)
|
||||
}
|
||||
|
||||
// Fetch updates information about repository
|
||||
func (repo *RemoteRepo) Fetch(d utils.Downloader) error {
|
||||
// Download release file to temporary URL
|
||||
release, err := d.DownloadTemp(repo.ReleaseURL().String())
|
||||
release, err := utils.DownloadTemp(d, repo.ReleaseURL().String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -96,3 +104,28 @@ func (repo *RemoteRepo) Fetch(d utils.Downloader) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Download downloads all repo files
|
||||
func (repo *RemoteRepo) Download(d utils.Downloader, db database.Storage) error {
|
||||
for _, component := range repo.Components {
|
||||
for _, architecture := range repo.Architectures {
|
||||
packagesReader, packagesFile, err := utils.DownloadTryCompression(d, repo.BinaryURL(component, architecture).String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer packagesFile.Close()
|
||||
|
||||
paras, err := debc.Parse(packagesReader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, para := range paras {
|
||||
p := NewPackageFromControlFile(para)
|
||||
db.Put(p.Key(), p.Encode())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Vendored
+9
-37
@@ -2,46 +2,10 @@ package debian
|
||||
|
||||
import (
|
||||
"github.com/smira/aptly/utils"
|
||||
"io/ioutil"
|
||||
. "launchpad.net/gocheck"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type FakeDownloader struct {
|
||||
Err error
|
||||
Response string
|
||||
}
|
||||
|
||||
func (f *FakeDownloader) DownloadTemp(url string) (*os.File, error) {
|
||||
if f.Err != nil {
|
||||
return nil, f.Err
|
||||
}
|
||||
|
||||
tempfile, _ := ioutil.TempFile(os.TempDir(), "aptly-test")
|
||||
defer os.Remove(tempfile.Name())
|
||||
|
||||
tempfile.Write([]byte(f.Response))
|
||||
tempfile.Seek(0, 0)
|
||||
|
||||
return tempfile, nil
|
||||
}
|
||||
|
||||
func (f *FakeDownloader) Download(url string, filename string) <-chan error {
|
||||
result := make(chan error)
|
||||
if f.Err != nil {
|
||||
result <- f.Err
|
||||
return result
|
||||
}
|
||||
|
||||
// TODO
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (f *FakeDownloader) Shutdown() {
|
||||
}
|
||||
|
||||
// Launch gocheck tests
|
||||
func Test(t *testing.T) {
|
||||
TestingT(t)
|
||||
@@ -56,7 +20,7 @@ var _ = Suite(&RemoteRepoSuite{})
|
||||
|
||||
func (s *RemoteRepoSuite) SetUpTest(c *C) {
|
||||
s.repo, _ = NewRemoteRepo("http://mirror.yandex.ru/debian/", "squeeze", []string{"main"}, []string{})
|
||||
s.downloader = &FakeDownloader{Response: exampleReleaseFile}
|
||||
s.downloader = utils.NewFakeDownloader().ExpectResponse("http://mirror.yandex.ru/debian/dists/squeeze/Release", exampleReleaseFile)
|
||||
}
|
||||
|
||||
func (s *RemoteRepoSuite) TestInvalidURL(c *C) {
|
||||
@@ -64,6 +28,14 @@ func (s *RemoteRepoSuite) TestInvalidURL(c *C) {
|
||||
c.Assert(err, ErrorMatches, ".*hexadecimal escape in host.*")
|
||||
}
|
||||
|
||||
func (s *RemoteRepoSuite) TestReleaseURL(c *C) {
|
||||
c.Assert(s.repo.ReleaseURL().String(), Equals, "http://mirror.yandex.ru/debian/dists/squeeze/Release")
|
||||
}
|
||||
|
||||
func (s *RemoteRepoSuite) TestBinaryURL(c *C) {
|
||||
c.Assert(s.repo.BinaryURL("main", "amd64").String(), Equals, "http://mirror.yandex.ru/debian/dists/squeeze/main/binary-amd64/Packages")
|
||||
}
|
||||
|
||||
func (s *RemoteRepoSuite) TestFetch(c *C) {
|
||||
err := s.repo.Fetch(s.downloader)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
Reference in New Issue
Block a user