~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/uniter/charm/bundles_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
package charm_test
5
5
 
6
6
import (
 
7
        "io/ioutil"
7
8
        "os"
8
9
        "path/filepath"
9
10
        "regexp"
10
11
 
 
12
        jujutesting "github.com/juju/testing"
11
13
        jc "github.com/juju/testing/checkers"
12
14
        "github.com/juju/utils"
13
15
        gc "gopkg.in/check.v1"
98
100
 
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)
 
106
 
 
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)
 
111
        }
104
112
 
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)
108
116
 
109
117
        // Add a charm to state that we can try to get.
111
119
 
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()
115
124
 
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()
120
130
 
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()
125
136
 
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()
130
142
 
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{})
135
147
        close(abort)
136
148
 
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()
140
153
}
141
154
 
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())
147
160
}
 
161
 
 
162
type ClearDownloadsSuite struct {
 
163
        jujutesting.IsolationSuite
 
164
}
 
165
 
 
166
var _ = gc.Suite(&ClearDownloadsSuite{})
 
167
 
 
168
func (s *ClearDownloadsSuite) TestWorks(c *gc.C) {
 
169
        baseDir := c.MkDir()
 
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)
 
175
 
 
176
        err := charm.ClearDownloads(bunsDir)
 
177
        c.Assert(err, jc.ErrorIsNil)
 
178
        checkMissing(c, downloadDir)
 
179
}
 
180
 
 
181
func (s *ClearDownloadsSuite) TestEmptyOK(c *gc.C) {
 
182
        baseDir := c.MkDir()
 
183
        bunsDir := filepath.Join(baseDir, "bundles")
 
184
        downloadDir := filepath.Join(bunsDir, "downloads")
 
185
        c.Assert(os.MkdirAll(downloadDir, 0777), jc.ErrorIsNil)
 
186
 
 
187
        err := charm.ClearDownloads(bunsDir)
 
188
        c.Assert(err, jc.ErrorIsNil)
 
189
        checkMissing(c, downloadDir)
 
190
}
 
191
 
 
192
func (s *ClearDownloadsSuite) TestMissingOK(c *gc.C) {
 
193
        baseDir := c.MkDir()
 
194
        bunsDir := filepath.Join(baseDir, "bundles")
 
195
 
 
196
        err := charm.ClearDownloads(bunsDir)
 
197
        c.Assert(err, jc.ErrorIsNil)
 
198
}
 
199
 
 
200
func checkMissing(c *gc.C, p string) {
 
201
        _, err := os.Stat(p)
 
202
        if !os.IsNotExist(err) {
 
203
                c.Fatalf("checking %s is missing: %v", p, err)
 
204
        }
 
205
}