func (s *QuoteSuite) TestUnmodified(c *C) { // Check that a string containing only valid // chars stays unmodified. in := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-" out := charm.Quote(in) c.Assert(out, Equals, in) }
func (conn *Conn) addCharm(curl *charm.URL, ch charm.Charm) (*state.Charm, error) { var f *os.File name := charm.Quote(curl.String()) switch ch := ch.(type) { case *charm.Dir: var err error if f, err = ioutil.TempFile("", name); err != nil { return nil, err } defer os.Remove(f.Name()) defer f.Close() err = ch.BundleTo(f) if err != nil { return nil, fmt.Errorf("cannot bundle charm: %v", err) } if _, err := f.Seek(0, 0); err != nil { return nil, err } case *charm.Bundle: var err error if f, err = os.Open(ch.Path); err != nil { return nil, fmt.Errorf("cannot read charm bundle: %v", err) } defer f.Close() default: return nil, fmt.Errorf("unknown charm type %T", ch) } h := sha256.New() size, err := io.Copy(h, f) if err != nil { return nil, err } digest := hex.EncodeToString(h.Sum(nil)) if _, err := f.Seek(0, 0); err != nil { return nil, err } storage := conn.Environ.Storage() log.Infof("writing charm to storage [%d bytes]", size) if err := storage.Put(name, f, size); err != nil { return nil, fmt.Errorf("cannot put charm: %v", err) } ustr, err := storage.URL(name) if err != nil { return nil, fmt.Errorf("cannot get storage URL for charm: %v", err) } u, err := url.Parse(ustr) if err != nil { return nil, fmt.Errorf("cannot parse storage URL: %v", err) } log.Infof("adding charm to state") sch, err := conn.State.AddCharm(ch, curl, u, digest) if err != nil { return nil, fmt.Errorf("cannot add charm: %v", err) } return sch, nil }
func (s *StoreSuite) TestGetBadCache(c *C) { base := "cs:series/blah" curl := charm.MustParseURL(base) revCurl := charm.MustParseURL(base + "-23") name := charm.Quote(revCurl.String()) + ".charm" err := ioutil.WriteFile(filepath.Join(s.cache, name), nil, 0666) c.Assert(err, IsNil) ch, err := s.store.Get(curl) c.Assert(err, IsNil) c.Assert(ch, NotNil) c.Assert(s.server.downloads, DeepEquals, []*charm.URL{revCurl}) s.assertCached(c, curl) s.assertCached(c, revCurl) }
func (s *StoreSuite) TestGetBadCache(c *C) { c.Assert(os.Mkdir(filepath.Join(charm.CacheDir, "cache"), 0777), IsNil) base := "cs:series/good" charmURL := charm.MustParseURL(base) revCharmURL := charm.MustParseURL(base + "-23") name := charm.Quote(revCharmURL.String()) + ".charm" err := ioutil.WriteFile(filepath.Join(charm.CacheDir, "cache", name), nil, 0666) c.Assert(err, IsNil) ch, err := s.store.Get(charmURL) c.Assert(err, IsNil) c.Assert(ch, NotNil) c.Assert(s.server.downloads, DeepEquals, []*charm.URL{revCharmURL}) s.assertCached(c, charmURL) s.assertCached(c, revCharmURL) }
func (s *QuoteSuite) TestQuote(c *C) { // Check that invalid chars are translated correctly. in := "hello_there/how'are~you-today.sir" out := charm.Quote(in) c.Assert(out, Equals, "hello_5f_there_2f_how_27_are_7e_you-today.sir") }
// bundlePath returns the path to the location where the verified charm // bundle identified by sch will be, or has been, saved. func (d *BundlesDir) bundlePath(sch *state.Charm) string { return filepath.Join(d.path, charm.Quote(sch.URL().String())) }