2015-01-28 08:27:46 +01:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
"github.com/docker/distribution"
|
2015-01-28 08:27:46 +01:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest"
|
2015-02-11 02:41:09 +01:00
|
|
|
"github.com/docker/distribution/registry/storage"
|
2015-04-02 01:41:33 +02:00
|
|
|
"github.com/docker/distribution/registry/storage/cache"
|
2015-02-11 03:14:23 +01:00
|
|
|
"github.com/docker/distribution/registry/storage/driver/inmemory"
|
2015-01-28 08:27:46 +01:00
|
|
|
"github.com/docker/distribution/testutil"
|
|
|
|
"github.com/docker/libtrust"
|
2015-02-09 23:44:58 +01:00
|
|
|
"golang.org/x/net/context"
|
2015-01-28 08:27:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestListener(t *testing.T) {
|
2015-04-02 01:41:33 +02:00
|
|
|
registry := storage.NewRegistryWithDriver(inmemory.New(), cache.NewInMemoryLayerInfoCache())
|
2015-01-28 08:27:46 +01:00
|
|
|
tl := &testListener{
|
|
|
|
ops: make(map[string]int),
|
|
|
|
}
|
2015-02-09 23:44:58 +01:00
|
|
|
ctx := context.Background()
|
2015-02-13 22:59:50 +01:00
|
|
|
repository, err := registry.Repository(ctx, "foo/bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error getting repo: %v", err)
|
|
|
|
}
|
|
|
|
repository = Listen(repository, tl)
|
2015-01-28 08:27:46 +01:00
|
|
|
|
|
|
|
// Now take the registry through a number of operations
|
|
|
|
checkExerciseRepository(t, repository)
|
|
|
|
|
|
|
|
expectedOps := map[string]int{
|
|
|
|
"manifest:push": 1,
|
2015-02-27 00:47:04 +01:00
|
|
|
"manifest:pull": 2,
|
2015-01-28 08:27:46 +01:00
|
|
|
// "manifest:delete": 0, // deletes not supported for now
|
|
|
|
"layer:push": 2,
|
|
|
|
"layer:pull": 2,
|
|
|
|
// "layer:delete": 0, // deletes not supported for now
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tl.ops, expectedOps) {
|
|
|
|
t.Fatalf("counts do not match:\n%v\n !=\n%v", tl.ops, expectedOps)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type testListener struct {
|
|
|
|
ops map[string]int
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (tl *testListener) ManifestPushed(repo distribution.Repository, sm *manifest.SignedManifest) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
tl.ops["manifest:push"]++
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (tl *testListener) ManifestPulled(repo distribution.Repository, sm *manifest.SignedManifest) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
tl.ops["manifest:pull"]++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (tl *testListener) ManifestDeleted(repo distribution.Repository, sm *manifest.SignedManifest) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
tl.ops["manifest:delete"]++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (tl *testListener) LayerPushed(repo distribution.Repository, layer distribution.Layer) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
tl.ops["layer:push"]++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (tl *testListener) LayerPulled(repo distribution.Repository, layer distribution.Layer) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
tl.ops["layer:pull"]++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:49:49 +01:00
|
|
|
func (tl *testListener) LayerDeleted(repo distribution.Repository, layer distribution.Layer) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
tl.ops["layer:delete"]++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkExerciseRegistry takes the registry through all of its operations,
|
|
|
|
// carrying out generic checks.
|
2015-02-12 01:49:49 +01:00
|
|
|
func checkExerciseRepository(t *testing.T, repository distribution.Repository) {
|
2015-01-28 08:27:46 +01:00
|
|
|
// TODO(stevvooe): This would be a nice testutil function. Basically, it
|
|
|
|
// takes the registry through a common set of operations. This could be
|
|
|
|
// used to make cross-cutting updates by changing internals that affect
|
|
|
|
// update counts. Basically, it would make writing tests a lot easier.
|
|
|
|
|
|
|
|
tag := "thetag"
|
|
|
|
m := manifest.Manifest{
|
|
|
|
Versioned: manifest.Versioned{
|
|
|
|
SchemaVersion: 1,
|
|
|
|
},
|
|
|
|
Name: repository.Name(),
|
|
|
|
Tag: tag,
|
|
|
|
}
|
|
|
|
|
|
|
|
layers := repository.Layers()
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
rs, ds, err := testutil.CreateRandomTarFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating test layer: %v", err)
|
|
|
|
}
|
|
|
|
dgst := digest.Digest(ds)
|
|
|
|
upload, err := layers.Upload()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating layer upload: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the resumes, as well!
|
|
|
|
upload, err = layers.Resume(upload.UUID())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error resuming layer upload: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
io.Copy(upload, rs)
|
|
|
|
|
|
|
|
if _, err := upload.Finish(dgst); err != nil {
|
|
|
|
t.Fatalf("unexpected error finishing upload: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.FSLayers = append(m.FSLayers, manifest.FSLayer{
|
|
|
|
BlobSum: dgst,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Then fetch the layers
|
|
|
|
if _, err := layers.Fetch(dgst); err != nil {
|
|
|
|
t.Fatalf("error fetching layer: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pk, err := libtrust.GenerateECP256PrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error generating key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sm, err := manifest.Sign(&m, pk)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error signing manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifests := repository.Manifests()
|
|
|
|
|
2015-02-27 00:47:04 +01:00
|
|
|
if err := manifests.Put(sm); err != nil {
|
2015-01-28 08:27:46 +01:00
|
|
|
t.Fatalf("unexpected error putting the manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-02-27 00:47:04 +01:00
|
|
|
p, err := sm.Payload()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error getting manifest payload: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dgst, err := digest.FromBytes(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error digesting manifest payload: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchedByManifest, err := manifests.Get(dgst)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error fetching manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fetchedByManifest.Tag != sm.Tag {
|
|
|
|
t.Fatalf("retrieved unexpected manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fetched, err := manifests.GetByTag(tag)
|
2015-01-28 08:27:46 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error fetching manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-02-27 00:47:04 +01:00
|
|
|
if fetched.Tag != fetchedByManifest.Tag {
|
2015-01-28 08:27:46 +01:00
|
|
|
t.Fatalf("retrieved unexpected manifest: %v", err)
|
|
|
|
}
|
|
|
|
}
|