Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide callback progress hooks for long copies.
Signed-off-by: Erik Hollensbe <github@hollensbe.org>
- Loading branch information
Erik Hollensbe
committed
Feb 7, 2017
1 parent
4f14180
commit 21d2390
Showing
4 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package copy | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
|
||
"github.com/containers/image/docker/daemon" | ||
"github.com/containers/image/signature" | ||
"github.com/containers/image/types" | ||
"github.com/docker/docker/reference" | ||
) | ||
|
||
// THIS TEST SHOULD BE SLOW. Read inner comments for details. | ||
func TestCopyProgress(t *testing.T) { | ||
interval := 100 * time.Millisecond | ||
|
||
// we shell out to avoid "client is newer than server" errors from docker | ||
// which will happen when importing the client straight from github. | ||
// This of course presumes docker is in the path. | ||
if out, err := exec.Command("/bin/sh", "-c", "docker pull golang:latest").CombinedOutput(); err != nil { | ||
t.Fatalf("%v: %s", err, out) | ||
} | ||
|
||
ref, err := daemon.ParseReference("docker.io/library/golang:latest") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
img, err := ref.NewImage(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer img.Close() | ||
|
||
tgtRef, err := reference.ParseNamed("docker.io/containers/test:latest") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
tgt, err := daemon.NewReference("", tgtRef) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
pc, err := signature.NewPolicyContext(&signature.Policy{ | ||
Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}, | ||
}) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
signal := make(chan types.ProgressProperties) | ||
errChan := make(chan error, 1) | ||
|
||
// This validates that enough events arrived to satisfy the interval time. As | ||
// a result it may require a little bit of work to get right before it will | ||
// pass for all people all the time. Sorry in advance! | ||
go func() { | ||
var ( | ||
// if this is false, the time will be populated and this will be set to true. | ||
// This is to ensure we're measuring the progress itself and not other | ||
// operations. | ||
timed bool | ||
tm time.Time | ||
count int | ||
) | ||
|
||
for range signal { | ||
if !timed { | ||
tm = time.Now().Add(interval * 2) // account for elapsed time already before the chan will tick at all | ||
timed = true | ||
} | ||
count++ | ||
} | ||
|
||
elapsed := time.Duration(count)*interval + interval // last one | ||
since := time.Since(tm) | ||
delta := since - elapsed | ||
if delta > interval { | ||
errChan <- fmt.Errorf("counts did not equal during progress signaling: (delta: %d) (since: %d) (elapsed: %d)", delta, since, elapsed) | ||
} | ||
|
||
errChan <- nil | ||
}() | ||
|
||
err = Image( | ||
pc, | ||
tgt, | ||
ref, | ||
&Options{ | ||
ProgressInterval: interval, | ||
Progress: signal, | ||
RemoveSignatures: true, | ||
}) | ||
|
||
close(signal) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := <-errChan; err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package streamcopy | ||
|
||
import ( | ||
"io" | ||
"time" | ||
|
||
"github.com/containers/image/types" | ||
) | ||
|
||
const readerSize = 32768 | ||
|
||
// WithProgress implements io.Copy with a buffered reader, then measures data | ||
// flow and on the interval -- if still running -- reports offset and artifact | ||
// information to the signal via the channel. | ||
func WithProgress(writer *io.PipeWriter, reader io.Reader, artifact types.BlobInfo, interval time.Duration, signal chan types.ProgressProperties) { | ||
defer writer.Close() | ||
|
||
count := uint64(0) | ||
buf := make([]byte, readerSize) | ||
|
||
t := time.Now() | ||
|
||
for { | ||
rn, err := reader.Read(buf) | ||
count += uint64(rn) | ||
|
||
if rn > 0 && err == io.EOF { | ||
goto write | ||
} | ||
|
||
if err != nil { | ||
writer.CloseWithError(err) | ||
return | ||
} | ||
|
||
if interval > 0 && signal != nil { | ||
if time.Since(t) > interval { | ||
signal <- types.ProgressProperties{Offset: count, Artifact: artifact} | ||
t = time.Now() | ||
} | ||
} | ||
|
||
write: | ||
_, werr := writer.Write(buf[:rn]) | ||
if werr != nil { | ||
writer.CloseWithError(werr) | ||
return | ||
} | ||
|
||
if err == io.EOF { | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters