Skip to content

Commit

Permalink
Merge pull request #618 from tonistiigi/test-local
Browse files Browse the repository at this point in the history
integration: use local registry mirrors
  • Loading branch information
tonistiigi authored Sep 19, 2018
2 parents aa73951 + 087e11e commit 95e6139
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 18 deletions.
38 changes: 38 additions & 0 deletions util/contentutil/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package contentutil
import (
"context"
"io"
"sync"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/remotes"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)

func Copy(ctx context.Context, ingester content.Ingester, provider content.Provider, desc ocispec.Descriptor) error {
Expand Down Expand Up @@ -41,3 +44,38 @@ func (r *rc) Read(b []byte) (int, error) {
}
return n, err
}

func CopyChain(ctx context.Context, ingester content.Ingester, provider content.Provider, desc ocispec.Descriptor) error {
var m sync.Mutex
manifestStack := []ocispec.Descriptor{}

filterHandler := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
switch desc.MediaType {
case images.MediaTypeDockerSchema2Manifest, ocispec.MediaTypeImageManifest,
images.MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex:
m.Lock()
manifestStack = append(manifestStack, desc)
m.Unlock()
return nil, images.ErrStopHandler
default:
return nil, nil
}
})
handlers := []images.Handler{
images.ChildrenHandler(provider),
filterHandler,
remotes.FetchHandler(ingester, &localFetcher{provider}),
}

if err := images.Dispatch(ctx, images.Handlers(handlers...), desc); err != nil {
return errors.WithStack(err)
}

for i := len(manifestStack) - 1; i >= 0; i-- {
if err := Copy(ctx, ingester, provider, manifestStack[i]); err != nil {
return errors.WithStack(err)
}
}

return nil
}
3 changes: 3 additions & 0 deletions util/contentutil/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (r *readerAt) ReadAt(b []byte, off int64) (int, error) {
var totalN int
for len(b) > 0 {
n, err := r.Reader.Read(b)
if err == io.EOF && n == len(b) {
err = nil
}
r.offset += int64(n)
totalN += n
b = b[n:]
Expand Down
57 changes: 57 additions & 0 deletions util/contentutil/refs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package contentutil

import (
"context"
"net/http"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

func ProviderFromRef(ref string) (ocispec.Descriptor, content.Provider, error) {
remote := docker.NewResolver(docker.ResolverOptions{
Client: http.DefaultClient,
})

name, desc, err := remote.Resolve(context.TODO(), ref)
if err != nil {
return ocispec.Descriptor{}, nil, err
}

fetcher, err := remote.Fetcher(context.TODO(), name)
if err != nil {
return ocispec.Descriptor{}, nil, err
}
return desc, FromFetcher(fetcher), nil
}

func IngesterFromRef(ref string) (content.Ingester, error) {
remote := docker.NewResolver(docker.ResolverOptions{
Client: http.DefaultClient,
})

pusher, err := remote.Pusher(context.TODO(), ref)
if err != nil {
return nil, err
}

return &ingester{
pusher: pusher,
}, nil
}

type ingester struct {
pusher remotes.Pusher
}

func (w *ingester) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
var wo content.WriterOpts
for _, o := range opts {
if err := o(&wo); err != nil {
return nil, err
}
}
return w.pusher.Push(ctx, wo.Desc)
}
24 changes: 21 additions & 3 deletions util/testutil/integration/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func (c *containerd) Name() string {
return c.name
}

func (c *containerd) New() (sb Sandbox, cl func() error, err error) {
func (c *containerd) New(opt ...SandboxOpt) (sb Sandbox, cl func() error, err error) {
var conf SandboxConf
for _, o := range opt {
o(&conf)
}

if err := lookupBinary(c.containerd); err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -115,12 +120,25 @@ disabled_plugins = ["cri"]
}
deferF.append(ctdStop)

buildkitdSock, stop, err := runBuildkitd([]string{"buildkitd",
buildkitdArgs := []string{"buildkitd",
"--oci-worker=false",
"--containerd-worker=true",
"--containerd-worker-addr", address,
"--containerd-worker-labels=org.mobyproject.buildkit.worker.sandbox=true", // Include use of --containerd-worker-labels to trigger https://github.com/moby/buildkit/pull/603
}, logs, 0, 0)
}

if conf.mirror != "" {
dir, err := configWithMirror(conf.mirror)
if err != nil {
return nil, nil, err
}
deferF.append(func() error {
return os.RemoveAll(dir)
})
buildkitdArgs = append(buildkitdArgs, "--config="+filepath.Join(dir, "buildkitd.toml"))
}

buildkitdSock, stop, err := runBuildkitd(buildkitdArgs, logs, 0, 0)
if err != nil {
return nil, nil, err
}
Expand Down
27 changes: 24 additions & 3 deletions util/testutil/integration/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
register(&oci{uid: uid, gid: gid})
}
}

}

type oci struct {
Expand All @@ -44,7 +45,12 @@ func (s *oci) Name() string {
return "oci"
}

func (s *oci) New() (Sandbox, func() error, error) {
func (s *oci) New(opt ...SandboxOpt) (Sandbox, func() error, error) {
var c SandboxConf
for _, o := range opt {
o(&c)
}

if err := lookupBinary("buildkitd"); err != nil {
return nil, nil, err
}
Expand All @@ -54,19 +60,34 @@ func (s *oci) New() (Sandbox, func() error, error) {
logs := map[string]*bytes.Buffer{}
// Include use of --oci-worker-labels to trigger https://github.com/moby/buildkit/pull/603
buildkitdArgs := []string{"buildkitd", "--oci-worker=true", "--containerd-worker=false", "--oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true"}

deferF := &multiCloser{}

if c.mirror != "" {
dir, err := configWithMirror(c.mirror)
if err != nil {
return nil, nil, err
}
deferF.append(func() error {
return os.RemoveAll(dir)
})
buildkitdArgs = append(buildkitdArgs, "--config="+filepath.Join(dir, "buildkitd.toml"))
}

if s.uid != 0 {
if s.gid == 0 {
deferF.F()()
return nil, nil, errors.Errorf("unsupported id pair: uid=%d, gid=%d", s.uid, s.gid)
}
// TODO: make sure the user exists and subuid/subgid are configured.
buildkitdArgs = append([]string{"sudo", "-u", fmt.Sprintf("#%d", s.uid), "-i", "--", "rootlesskit"}, buildkitdArgs...)
}
buildkitdSock, stop, err := runBuildkitd(buildkitdArgs, logs, s.uid, s.gid)
if err != nil {
deferF.F()()
return nil, nil, err
}

deferF := &multiCloser{}
deferF.append(stop)

return &sandbox{address: buildkitdSock, logs: logs, cleanup: deferF, rootless: s.uid != 0}, deferF.F(), nil
Expand Down Expand Up @@ -94,7 +115,7 @@ func (sb *sandbox) PrintLogs(t *testing.T) {
}

func (sb *sandbox) NewRegistry() (string, error) {
url, cl, err := newRegistry()
url, cl, err := newRegistry("")
if err != nil {
return "", err
}
Expand Down
28 changes: 18 additions & 10 deletions util/testutil/integration/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/pkg/errors"
)

func newRegistry() (url string, cl func() error, err error) {
func newRegistry(dir string) (url string, cl func() error, err error) {
if err := lookupBinary("registry"); err != nil {
return "", nil, err
}
Expand All @@ -30,26 +30,34 @@ func newRegistry() (url string, cl func() error, err error) {
}
}()

tmpdir, err := ioutil.TempDir("", "test-registry")
if err != nil {
return "", nil, err
if dir == "" {
tmpdir, err := ioutil.TempDir("", "test-registry")
if err != nil {
return "", nil, err
}
deferF.append(func() error { return os.RemoveAll(tmpdir) })
dir = tmpdir
}
deferF.append(func() error { return os.RemoveAll(tmpdir) })

template := fmt.Sprintf(`version: 0.1
if _, err := os.Stat(filepath.Join(dir, "config.yaml")); err != nil {
if !os.IsNotExist(err) {
return "", nil, err
}
template := fmt.Sprintf(`version: 0.1
loglevel: debug
storage:
filesystem:
rootdirectory: %s
http:
addr: 127.0.0.1:0
`, filepath.Join(tmpdir, "data"))
`, filepath.Join(dir, "data"))

if err := ioutil.WriteFile(filepath.Join(tmpdir, "config.yaml"), []byte(template), 0600); err != nil {
return "", nil, err
if err := ioutil.WriteFile(filepath.Join(dir, "config.yaml"), []byte(template), 0600); err != nil {
return "", nil, err
}
}

cmd := exec.Command("registry", "serve", filepath.Join(tmpdir, "config.yaml"))
cmd := exec.Command("registry", "serve", filepath.Join(dir, "config.yaml"))
rc, err := cmd.StdoutPipe()
if err != nil {
return "", nil, err
Expand Down
Loading

0 comments on commit 95e6139

Please sign in to comment.