99
101
func (s *BundlesDirSuite) TestGet(c *gc.C) {
100
102
basedir := c.MkDir()
101
bunsdir := filepath.Join(basedir, "random", "bundles")
103
bunsDir := filepath.Join(basedir, "random", "bundles")
102
104
downloader := api.NewCharmDownloader(s.st.Client())
103
d := charm.NewBundlesDir(bunsdir, downloader)
105
d := charm.NewBundlesDir(bunsDir, downloader)
107
checkDownloadsEmpty := func() {
108
files, err := ioutil.ReadDir(filepath.Join(bunsDir, "downloads"))
109
c.Assert(err, jc.ErrorIsNil)
110
c.Check(files, gc.HasLen, 0)
105
113
// Check it doesn't get created until it's needed.
106
_, err := os.Stat(bunsdir)
114
_, err := os.Stat(bunsDir)
107
115
c.Assert(err, jc.Satisfies, os.IsNotExist)
109
117
// Add a charm to state that we can try to get.
112
120
// Try to get the charm when the content doesn't match.
113
121
_, err = d.Read(&fakeBundleInfo{apiCharm, nil, "..."}, nil)
114
c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/dummy-1" from API server: `)+`expected sha256 "...", got ".*"`)
122
c.Check(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/dummy-1" from API server: `)+`expected sha256 "...", got ".*"`)
123
checkDownloadsEmpty()
116
125
// Try to get a charm whose bundle doesn't exist.
117
126
otherURL := corecharm.MustParseURL("cs:quantal/spam-1")
118
127
_, err = d.Read(&fakeBundleInfo{apiCharm, otherURL, ""}, nil)
119
c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/spam-1" from API server: `)+`.* not found`)
128
c.Check(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/spam-1" from API server: `)+`.* not found`)
129
checkDownloadsEmpty()
121
131
// Get a charm whose bundle exists and whose content matches.
122
132
ch, err := d.Read(apiCharm, nil)
123
133
c.Assert(err, jc.ErrorIsNil)
124
134
assertCharm(c, ch, sch)
135
checkDownloadsEmpty()
126
137
// Get the same charm again, without preparing a response from the server.
127
138
ch, err = d.Read(apiCharm, nil)
128
139
c.Assert(err, jc.ErrorIsNil)
129
140
assertCharm(c, ch, sch)
141
checkDownloadsEmpty()
131
143
// Check the abort chan is honoured.
132
err = os.RemoveAll(bunsdir)
144
err = os.RemoveAll(bunsDir)
133
145
c.Assert(err, jc.ErrorIsNil)
134
146
abort := make(chan struct{})
137
149
ch, err = d.Read(apiCharm, abort)
138
c.Assert(ch, gc.IsNil)
139
c.Assert(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/dummy-1" from API server: aborted`))
150
c.Check(ch, gc.IsNil)
151
c.Check(err, gc.ErrorMatches, regexp.QuoteMeta(`failed to download charm "cs:quantal/dummy-1" from API server: download aborted`))
152
checkDownloadsEmpty()
142
155
func assertCharm(c *gc.C, bun charm.Bundle, sch *state.Charm) {
145
158
c.Assert(actual.Meta(), gc.DeepEquals, sch.Meta())
146
159
c.Assert(actual.Config(), gc.DeepEquals, sch.Config())
162
type ClearDownloadsSuite struct {
163
jujutesting.IsolationSuite
166
var _ = gc.Suite(&ClearDownloadsSuite{})
168
func (s *ClearDownloadsSuite) TestWorks(c *gc.C) {
170
bunsDir := filepath.Join(baseDir, "bundles")
171
downloadDir := filepath.Join(bunsDir, "downloads")
172
c.Assert(os.MkdirAll(downloadDir, 0777), jc.ErrorIsNil)
173
c.Assert(ioutil.WriteFile(filepath.Join(downloadDir, "stuff"), []byte("foo"), 0755), jc.ErrorIsNil)
174
c.Assert(ioutil.WriteFile(filepath.Join(downloadDir, "thing"), []byte("bar"), 0755), jc.ErrorIsNil)
176
err := charm.ClearDownloads(bunsDir)
177
c.Assert(err, jc.ErrorIsNil)
178
checkMissing(c, downloadDir)
181
func (s *ClearDownloadsSuite) TestEmptyOK(c *gc.C) {
183
bunsDir := filepath.Join(baseDir, "bundles")
184
downloadDir := filepath.Join(bunsDir, "downloads")
185
c.Assert(os.MkdirAll(downloadDir, 0777), jc.ErrorIsNil)
187
err := charm.ClearDownloads(bunsDir)
188
c.Assert(err, jc.ErrorIsNil)
189
checkMissing(c, downloadDir)
192
func (s *ClearDownloadsSuite) TestMissingOK(c *gc.C) {
194
bunsDir := filepath.Join(baseDir, "bundles")
196
err := charm.ClearDownloads(bunsDir)
197
c.Assert(err, jc.ErrorIsNil)
200
func checkMissing(c *gc.C, p string) {
202
if !os.IsNotExist(err) {
203
c.Fatalf("checking %s is missing: %v", p, err)