func copyOne( tool *state.Tools, source environs.StorageReader, target environs.Storage, ctx *cmd.Context, ) error { toolsName := tools.StorageName(tool.Binary) fmt.Fprintf(ctx.Stderr, "copying %v", toolsName) srcFile, err := source.Get(toolsName) if err != nil { return err } defer srcFile.Close() // We have to buffer the content, because Put requires the content // length, but Get only returns us a ReadCloser buf := &bytes.Buffer{} nBytes, err := io.Copy(buf, srcFile) if err != nil { return err } log.Infof("downloaded %v (%dkB), uploading", toolsName, (nBytes+512)/1024) fmt.Fprintf(ctx.Stderr, ", download %dkB, uploading\n", (nBytes+512)/1024) if err := target.Put(toolsName, buf, nBytes); err != nil { return err } return nil }
func checkFileHasContents(c *C, storage environs.StorageReader, name string, contents []byte, attempt trivial.AttemptStrategy) { r, err := storage.Get(name) c.Assert(err, IsNil) c.Check(r, NotNil) defer r.Close() data, err := ioutil.ReadAll(r) c.Check(err, IsNil) c.Check(data, DeepEquals, contents) url, err := storage.URL(name) c.Assert(err, IsNil) var resp *http.Response for a := attempt.Start(); a.Next(); { resp, err = http.Get(url) c.Assert(err, IsNil) if resp.StatusCode != 404 { break } c.Logf("get retrying after earlier get succeeded. *sigh*.") } c.Assert(err, IsNil) data, err = ioutil.ReadAll(resp.Body) c.Assert(err, IsNil) defer resp.Body.Close() c.Assert(resp.StatusCode, Equals, 200, Commentf("error response: %s", data)) c.Check(data, DeepEquals, contents) }
func checkFileDoesNotExist(c *C, storage environs.StorageReader, name string, attempt trivial.AttemptStrategy) { var r io.ReadCloser var err error for a := attempt.Start(); a.Next(); { r, err = storage.Get(name) if err != nil { break } } c.Assert(r, IsNil) var notFoundError *environs.NotFoundError c.Assert(err, FitsTypeOf, notFoundError) }
func checkFileHasContents(c *C, storage environs.StorageReader, name string, contents []byte) { r, err := storage.Get(name) c.Assert(err, IsNil) c.Check(r, NotNil) defer r.Close() data, err := ioutil.ReadAll(r) c.Check(err, IsNil) c.Check(data, DeepEquals, contents) url, err := storage.URL(name) c.Assert(err, IsNil) resp, err := http.Get(url) c.Assert(err, IsNil) data, err = ioutil.ReadAll(resp.Body) c.Assert(err, IsNil) defer resp.Body.Close() c.Assert(resp.StatusCode, Equals, http.StatusOK, Commentf("error response: %s", data)) c.Check(data, DeepEquals, contents) }
func checkFileDoesNotExist(c *C, storage environs.StorageReader, name string) { var notFoundError *errors.NotFoundError r, err := storage.Get(name) c.Assert(r, IsNil) c.Assert(err, FitsTypeOf, notFoundError) }