server: implemented 'flush' and 'refresh' API

Added test that verifies that when client performs Flush (which happens
at the end of each snapshot and when repository is closed), the
server writes new blobs to the storage.

Fixes #464
This commit is contained in:
Jarek Kowalski committed 2020-06-07 19:38:13 -07:00
1 parent 293cb10471
commit 79757672ca
3 files changed
+37 -5

No files matched your search

+8 -2
View File
@@ -135,12 +135,18 @@ func (s *Server) handleAPIPossiblyNotConnected(f func(ctx context.Context, r *ht
} }
func (s *Server) handleRefresh(ctx context.Context, r *http.Request) (interface{}, *apiError) { func (s *Server) handleRefresh(ctx context.Context, r *http.Request) (interface{}, *apiError) {
log(ctx).Infof("refreshing") if err := s.rep.Refresh(ctx); err != nil {
return nil, internalServerError(err)
}
return &serverapi.Empty{}, nil return &serverapi.Empty{}, nil
} }
func (s *Server) handleFlush(ctx context.Context, r *http.Request) (interface{}, *apiError) { func (s *Server) handleFlush(ctx context.Context, r *http.Request) (interface{}, *apiError) {
log(ctx).Infof("flushing") if err := s.rep.Flush(ctx); err != nil {
return nil, internalServerError(err)
}
return &serverapi.Empty{}, nil return &serverapi.Empty{}, nil
} }
+2 -2
View File
@@ -118,7 +118,7 @@ func (r *apiServerRepository) Refresh(ctx context.Context) error {
} }
func (r *apiServerRepository) Flush(ctx context.Context) error { func (r *apiServerRepository) Flush(ctx context.Context) error {
return nil return r.cli.Post(ctx, "flush", nil, nil)
} }
func (r *apiServerRepository) Close(ctx context.Context) error { func (r *apiServerRepository) Close(ctx context.Context) error {
@@ -126,7 +126,7 @@ func (r *apiServerRepository) Close(ctx context.Context) error {
return errors.Wrap(err, "error closing object manager") return errors.Wrap(err, "error closing object manager")
} }
return nil return r.Flush(ctx)
} }
func (r *apiServerRepository) ContentInfo(ctx context.Context, contentID content.ID) (content.Info, error) { func (r *apiServerRepository) ContentInfo(ctx context.Context, contentID content.ID) (content.Info, error) {
@@ -33,6 +33,9 @@ func TestAPIServerRepository(t *testing.T) {
e1.RunAndExpectSuccess(t, "repo", "connect", "filesystem", "--path", e.RepoDir, "--override-username", "not-foo", "--override-hostname", "bar") e1.RunAndExpectSuccess(t, "repo", "connect", "filesystem", "--path", e.RepoDir, "--override-username", "not-foo", "--override-hostname", "bar")
e1.RunAndExpectSuccess(t, "snapshot", "create", sharedTestDataDir1) e1.RunAndExpectSuccess(t, "snapshot", "create", sharedTestDataDir1)
originalPBlobCount := len(e1.RunAndExpectSuccess(t, "blob", "list", "--prefix=p"))
originalQBlobCount := len(e1.RunAndExpectSuccess(t, "blob", "list", "--prefix=q"))
htpasswordFile := filepath.Join(e.ConfigDir, "htpasswd.txt") htpasswordFile := filepath.Join(e.ConfigDir, "htpasswd.txt")
ioutil.WriteFile(htpasswordFile, htpasswdFileContents, 0755) ioutil.WriteFile(htpasswordFile, htpasswdFileContents, 0755)
@@ -81,12 +84,35 @@ func TestAPIServerRepository(t *testing.T) {
t.Errorf("invalid number of snapshots for foo@bar") t.Errorf("invalid number of snapshots for foo@bar")
} }
// create very small directory
smallDataDir := filepath.Join(sharedTestDataDirBase, "dir-small")
testenv.CreateDirectoryTree(smallDataDir, testenv.DirectoryTreeOptions{
Depth: 1,
MaxSubdirsPerDirectory: 1,
MaxFilesPerDirectory: 1,
MaxFileSize: 100,
}, nil)
// create snapshot of a very small directory using remote repository client
e2.RunAndExpectSuccess(t, "snapshot", "create", smallDataDir)
// make sure snapshot created by the client resulted in blobs being created by the server
// as opposed to buffering it in memory
if got, want := len(e.RunAndExpectSuccess(t, "blob", "list", "--prefix=p")), originalPBlobCount; got <= want {
t.Errorf("unexpected number of P blobs on the server: %v, wanted > %v", got, want)
}
if got, want := len(e.RunAndExpectSuccess(t, "blob", "list", "--prefix=q")), originalQBlobCount; got <= want {
t.Errorf("unexpected number of Q blobs on the server: %v, wanted > %v", got, want)
}
// create snapshot using remote repository client // create snapshot using remote repository client
e2.RunAndExpectSuccess(t, "snapshot", "create", sharedTestDataDir2) e2.RunAndExpectSuccess(t, "snapshot", "create", sharedTestDataDir2)
// now should see two snapshots // now should see two snapshots
snapshots = e2.ListSnapshotsAndExpectSuccess(t) snapshots = e2.ListSnapshotsAndExpectSuccess(t)
if got, want := len(snapshots), 2; got != want { if got, want := len(snapshots), 3; got != want {
t.Errorf("invalid number of snapshots for foo@bar") t.Errorf("invalid number of snapshots for foo@bar")
} }
} }