~snappy-dev/snappy/trunk-github

« back to all changes in this revision

Viewing changes to snappy/snap_remote_repo_test.go

  • Committer: Zygmunt Krynicki
  • Date: 2016-02-05 11:26:28 UTC
  • mfrom: (1131)
  • mto: This revision was merged to the branch mainline in revision 1137.
  • Revision ID: git-v1:2f4cd12f5eb1dde16f4e7b59561026d0d125f377
Merge branch 'master' of github.com:ubuntu-core/snappy into cmd-output-testing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: Go; indent-tabs-mode: t -*-
 
2
 
 
3
/*
 
4
 * Copyright (C) 2014-2015 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 snappy
 
21
 
 
22
import (
 
23
        "fmt"
 
24
        "io"
 
25
        "io/ioutil"
 
26
        "net/http"
 
27
        "os"
 
28
 
 
29
        "github.com/ubuntu-core/snappy/helpers"
 
30
        "github.com/ubuntu-core/snappy/progress"
 
31
 
 
32
        . "gopkg.in/check.v1"
 
33
)
 
34
 
 
35
type remoteRepoTestSuite struct {
 
36
        store *SnapUbuntuStoreRepository
 
37
 
 
38
        origDownloadFunc func(string, io.Writer, *http.Request, progress.Meter) error
 
39
}
 
40
 
 
41
var _ = Suite(&remoteRepoTestSuite{})
 
42
 
 
43
func (t *remoteRepoTestSuite) SetUpTest(c *C) {
 
44
        t.store = NewUbuntuStoreSnapRepository()
 
45
        t.origDownloadFunc = download
 
46
}
 
47
 
 
48
func (t *remoteRepoTestSuite) TearDownTest(c *C) {
 
49
        download = t.origDownloadFunc
 
50
}
 
51
 
 
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"))
 
55
                return nil
 
56
        }
 
57
 
 
58
        path, err := t.store.Download(&RemoteSnapPart{}, nil)
 
59
        c.Assert(err, IsNil)
 
60
        defer os.Remove(path)
 
61
 
 
62
        content, err := ioutil.ReadFile(path)
 
63
        c.Assert(err, IsNil)
 
64
        c.Assert(string(content), Equals, "I was downloaded")
 
65
}
 
66
 
 
67
func (t *remoteRepoTestSuite) TestDownloadFails(c *C) {
 
68
        var tmpfile *os.File
 
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")
 
72
        }
 
73
 
 
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)
 
80
}
 
81
 
 
82
func (t *remoteRepoTestSuite) TestDownloadSyncFails(c *C) {
 
83
        var tmpfile *os.File
 
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()
 
88
                c.Assert(err, IsNil)
 
89
                return nil
 
90
        }
 
91
 
 
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)
 
98
}