func (s *filestorageSuite) TestListHidesTempDir(c *gc.C) { err := s.writer.Put("test-write", bytes.NewReader(nil), 0) c.Assert(err, gc.IsNil) files, err := storage.List(s.reader, "") c.Assert(err, gc.IsNil) c.Check(files, gc.DeepEquals, []string{"test-write"}) files, err = storage.List(s.reader, "no-such-directory") c.Assert(err, gc.IsNil) c.Check(files, gc.DeepEquals, []string(nil)) // We also pretend the .tmp directory doesn't exist. If you call a // directory that doesn't exist, we just return an empty list of // strings, so we force the same behavior for '.tmp' // we poke in a file so it would have something to return s.createFile(c, ".tmp/test-file") files, err = storage.List(s.reader, ".tmp") c.Assert(err, gc.IsNil) c.Check(files, gc.DeepEquals, []string(nil)) // For consistency, we refuse all other possibilities as well s.createFile(c, ".tmp/foo/bar") files, err = storage.List(s.reader, ".tmp/foo") c.Assert(err, gc.IsNil) c.Check(files, gc.DeepEquals, []string(nil)) s.createFile(c, ".tmpother/foo") files, err = storage.List(s.reader, ".tmpother") c.Assert(err, gc.IsNil) c.Check(files, gc.DeepEquals, []string(nil)) }
func (s *filestorageSuite) TestList(c *gc.C) { names := []string{ "a/b/c", "a/bb", "a/c", "aa", "b/c/d", } for _, name := range names { s.createFile(c, name) } type test struct { prefix string expected []string } for i, test := range []test{ {"a", []string{"a/b/c", "a/bb", "a/c", "aa"}}, {"a/b", []string{"a/b/c", "a/bb"}}, {"a/b/c", []string{"a/b/c"}}, {"", names}, } { c.Logf("test %d: prefix=%q", i, test.prefix) files, err := storage.List(s.reader, test.prefix) c.Assert(err, gc.IsNil) c.Assert(files, gc.DeepEquals, test.expected) } }
// RemoveAll is specified in the StorageWriter interface. func (stor *maasStorage) RemoveAll() error { names, err := storage.List(stor, "") if err != nil { return err } // Remove all the objects in parallel so that we incur fewer round-trips. // If we're in danger of having hundreds of objects, // we'll want to change this to limit the number // of concurrent operations. var wg sync.WaitGroup wg.Add(len(names)) errc := make(chan error, len(names)) for _, name := range names { name := name go func() { defer wg.Done() if err := stor.Remove(name); err != nil { errc <- err } }() } wg.Wait() select { case err := <-errc: return fmt.Errorf("cannot delete all provider state: %v", err) default: } return nil }
func (s *storageSuite) TestList(c *gc.C) { listener, _, _ := startServer(c) defer listener.Close() stor := httpstorage.Client(listener.Addr().String()) names, err := storage.List(stor, "a/b/c") c.Assert(err, gc.IsNil) c.Assert(names, gc.HasLen, 0) }
// RemoveTools deletes all tools from the supplied storage. func RemoveTools(c *gc.C, stor storage.Storage) { names, err := storage.List(stor, "tools/releases/juju-") c.Assert(err, gc.IsNil) c.Logf("removing files: %v", names) for _, name := range names { err = stor.Remove(name) c.Check(err, gc.IsNil) } RemoveFakeToolsMetadata(c, stor) }
func checkList(c *gc.C, stor storage.StorageReader, prefix string, names []string) { lnames, err := storage.List(stor, prefix) c.Assert(err, gc.IsNil) // TODO(dfc) gocheck should grow an SliceEquals checker. expected := copyslice(lnames) sort.Strings(expected) actual := copyslice(names) sort.Strings(actual) c.Assert(expected, gc.DeepEquals, actual) }
// ReadList returns a List of the tools in store with the given major.minor version. // If minorVersion = -1, then only majorVersion is considered. // If store contains no such tools, it returns ErrNoMatches. func ReadList(stor storage.StorageReader, majorVersion, minorVersion int) (coretools.List, error) { if minorVersion >= 0 { logger.Debugf("reading v%d.%d tools", majorVersion, minorVersion) } else { logger.Debugf("reading v%d.* tools", majorVersion) } names, err := storage.List(stor, toolPrefix) if err != nil { return nil, err } var list coretools.List var foundAnyTools bool for _, name := range names { if !strings.HasPrefix(name, toolPrefix) || !strings.HasSuffix(name, toolSuffix) { continue } var t coretools.Tools vers := name[len(toolPrefix) : len(name)-len(toolSuffix)] if t.Version, err = version.ParseBinary(vers); err != nil { logger.Debugf("failed to parse version %q: %v", vers, err) continue } foundAnyTools = true // Major version must match specified value. if t.Version.Major != majorVersion { continue } // If specified minor version value supplied, minor version must match. if minorVersion >= 0 && t.Version.Minor != minorVersion { continue } logger.Debugf("found %s", vers) if t.URL, err = stor.URL(name); err != nil { return nil, err } list = append(list, &t) } if len(list) == 0 { if foundAnyTools { return nil, coretools.ErrNoMatches } return nil, ErrNoTools } return list, nil }
func checkRemoveAll(c *gc.C, stor storage.Storage) { contents := []byte("File contents.") aFile := "a-file.txt" err := stor.Put(aFile, bytes.NewBuffer(contents), int64(len(contents))) c.Assert(err, gc.IsNil) err = stor.Put("empty-file", bytes.NewBuffer(nil), 0) c.Assert(err, gc.IsNil) err = stor.RemoveAll() c.Assert(err, gc.IsNil) files, err := storage.List(stor, "") c.Assert(err, gc.IsNil) c.Check(files, gc.HasLen, 0) _, err = storage.Get(stor, aFile) c.Assert(err, gc.NotNil) c.Check(err, gc.ErrorMatches, fmt.Sprintf("file %q not found", aFile)) }
func (suite *environSuite) TestDestroy(c *gc.C) { env := suite.makeEnviron() suite.getInstance("test1") suite.testMAASObject.TestServer.OwnedNodes()["test1"] = true // simulate acquire data := makeRandomBytes(10) suite.testMAASObject.TestServer.NewFile("filename", data) stor := env.Storage() err := env.Destroy() c.Check(err, gc.IsNil) // Instances have been stopped. operations := suite.testMAASObject.TestServer.NodesOperations() c.Check(operations, gc.DeepEquals, []string{"release"}) c.Check(suite.testMAASObject.TestServer.OwnedNodes()["test1"], jc.IsFalse) // Files have been cleaned up. listing, err := storage.List(stor, "") c.Assert(err, gc.IsNil) c.Check(listing, gc.DeepEquals, []string{}) }
func (s *storageSuite) TestClientTLS(c *gc.C) { listener, _, storageDir := startServerTLS(c) defer listener.Close() stor, err := httpstorage.ClientTLS(listener.Addr().String(), coretesting.CACert, testAuthkey) c.Assert(err, gc.IsNil) data := []byte("hello") err = ioutil.WriteFile(filepath.Join(storageDir, "filename"), data, 0644) c.Assert(err, gc.IsNil) names, err := storage.List(stor, "filename") c.Assert(err, gc.IsNil) c.Assert(names, gc.DeepEquals, []string{"filename"}) checkFileHasContents(c, stor, "filename", data) // Put, Remove and RemoveAll should all succeed. checkPutFile(c, stor, "filenamethesecond", data) checkFileHasContents(c, stor, "filenamethesecond", data) c.Assert(stor.Remove("filenamethesecond"), gc.IsNil) c.Assert(stor.RemoveAll(), gc.IsNil) }
func (s *ec2storage) RemoveAll() error { names, err := storage.List(s, "") if err != nil { return err } // Remove all the objects in parallel to minimize round-trips. // If we're in danger of having hundreds of objects, // we'll want to change this to limit the number // of concurrent operations. var wg sync.WaitGroup wg.Add(len(names)) errc := make(chan error, len(names)) for _, name := range names { name := name go func() { if err := s.Remove(name); err != nil { errc <- err } wg.Done() }() } wg.Wait() select { case err := <-errc: return fmt.Errorf("cannot delete all provider state: %v", err) default: } s.Lock() defer s.Unlock() // Even DelBucket fails, it won't harm if we try again - the operation // might have succeeded even if we get an error. s.madeBucket = false err = deleteBucket(s) err = s.bucket.DelBucket() if s3ErrorStatusCode(err) == 404 { return nil } return err }
func (s *storageSuite) TestClientTLSInvalidAuth(c *gc.C) { listener, _, storageDir := startServerTLS(c) defer listener.Close() const invalidAuthkey = testAuthkey + "!" stor, err := httpstorage.ClientTLS(listener.Addr().String(), coretesting.CACert, invalidAuthkey) c.Assert(err, gc.IsNil) // Get and List should succeed. data := []byte("hello") err = ioutil.WriteFile(filepath.Join(storageDir, "filename"), data, 0644) c.Assert(err, gc.IsNil) names, err := storage.List(stor, "filename") c.Assert(err, gc.IsNil) c.Assert(names, gc.DeepEquals, []string{"filename"}) checkFileHasContents(c, stor, "filename", data) // Put, Remove and RemoveAll should all fail. const authErrorPattern = ".*401 Unauthorized" err = putFile(c, stor, "filenamethesecond", data) c.Assert(err, gc.ErrorMatches, authErrorPattern) c.Assert(stor.Remove("filenamethesecond"), gc.ErrorMatches, authErrorPattern) c.Assert(stor.RemoveAll(), gc.ErrorMatches, authErrorPattern) }
func (s *EmptyStorageSuite) TestList(c *gc.C) { names, err := storage.List(environs.EmptyStorage, "anything") c.Assert(names, gc.IsNil) c.Assert(err, gc.IsNil) }
func checkList(c *gc.C, stor storage.StorageReader, prefix string, names []string) { lnames, err := storage.List(stor, prefix) c.Assert(err, gc.IsNil) c.Assert(lnames, gc.DeepEquals, names) }