1
// -*- Mode: Go; indent-tabs-mode: t -*-
4
* Copyright (C) 2014-2015 Canonical Ltd
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.
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.
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/>.
29
"github.com/ubuntu-core/snappy/helpers"
30
"github.com/ubuntu-core/snappy/progress"
35
type remoteRepoTestSuite struct {
36
store *SnapUbuntuStoreRepository
38
origDownloadFunc func(string, io.Writer, *http.Request, progress.Meter) error
41
var _ = Suite(&remoteRepoTestSuite{})
43
func (t *remoteRepoTestSuite) SetUpTest(c *C) {
44
t.store = NewUbuntuStoreSnapRepository()
45
t.origDownloadFunc = download
48
func (t *remoteRepoTestSuite) TearDownTest(c *C) {
49
download = t.origDownloadFunc
52
func (t *remoteRepoTestSuite) TestDownloadOK(c *C) {
53
download = func(name string, w io.Writer, req *http.Request, pbar progress.Meter) error {
54
w.Write([]byte("I was downloaded"))
58
path, err := t.store.Download(&RemoteSnapPart{}, nil)
62
content, err := ioutil.ReadFile(path)
64
c.Assert(string(content), Equals, "I was downloaded")
67
func (t *remoteRepoTestSuite) TestDownloadFails(c *C) {
69
download = func(name string, w io.Writer, req *http.Request, pbar progress.Meter) error {
70
tmpfile = w.(*os.File)
71
return fmt.Errorf("uh, it failed")
74
// simulate a failed download
75
path, err := t.store.Download(&RemoteSnapPart{}, nil)
76
c.Assert(err, ErrorMatches, "uh, it failed")
77
c.Assert(path, Equals, "")
78
// ... and ensure that the tempfile is removed
79
c.Assert(helpers.FileExists(tmpfile.Name()), Equals, false)
82
func (t *remoteRepoTestSuite) TestDownloadSyncFails(c *C) {
84
download = func(name string, w io.Writer, req *http.Request, pbar progress.Meter) error {
85
tmpfile = w.(*os.File)
86
w.Write([]byte("sync will fail"))
87
err := tmpfile.Close()
92
// simulate a failed sync
93
path, err := t.store.Download(&RemoteSnapPart{}, nil)
94
c.Assert(err, ErrorMatches, "fsync:.*")
95
c.Assert(path, Equals, "")
96
// ... and ensure that the tempfile is removed
97
c.Assert(helpers.FileExists(tmpfile.Name()), Equals, false)