Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace mock OCI client with working local client #525

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions pkg/oci/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package oci

import (
"context"
"io"
"os"
"path/filepath"
"sync"

"github.com/opencontainers/go-digest"
)

var _ Client = &LocalClient{}

type LocalClient struct {
mx sync.RWMutex
rootDir string
imageEventCh chan ImageEvent
errCh chan error
}

func NewLocalClient(rootDir string) *LocalClient {
return &LocalClient{
rootDir: rootDir,
}
}

func (m *LocalClient) Add() error {
return nil
}

func (m *LocalClient) Name() string {
return "local"
}

func (m *LocalClient) Verify(ctx context.Context) error {
_, err := os.Stat(m.rootDir)
if err != nil {
return err
}
return nil
}

func (m *LocalClient) Subscribe(ctx context.Context) (<-chan ImageEvent, <-chan error, error) {
return nil, nil, nil
}

func (m *LocalClient) ListImages(ctx context.Context) ([]Image, error) {
m.mx.RLock()
defer m.mx.RUnlock()

return nil, nil
}

func (m *LocalClient) AllIdentifiers(ctx context.Context, img Image) ([]string, error) {
return []string{img.Digest.String()}, nil
}

func (m *LocalClient) Resolve(ctx context.Context, ref string) (digest.Digest, error) {
return "", nil
}

func (m *LocalClient) Size(ctx context.Context, dgst digest.Digest) (int64, error) {
m.mx.RLock()
defer m.mx.RUnlock()

p := m.pathForDigest(dgst)
fi, err := os.Stat(p)
if err != nil {
return 0, err
}
return fi.Size(), nil
}

func (m *LocalClient) GetManifest(ctx context.Context, dgst digest.Digest) ([]byte, string, error) {
m.mx.RLock()
defer m.mx.RUnlock()

p := m.pathForDigest(dgst)
b, err := os.ReadFile(p)
if err != nil {
return nil, "", err
}
return b, "", nil
}

func (m *LocalClient) GetBlob(ctx context.Context, dgst digest.Digest) (io.ReadCloser, error) {
m.mx.RLock()
defer m.mx.RUnlock()

p := m.pathForDigest(dgst)
f, err := os.Open(p)
if err != nil {
return nil, err
}
return f, nil
}

func (m *LocalClient) pathForDigest(dgst digest.Digest) string {
return filepath.Join(m.rootDir, "blobs", dgst.Algorithm().String(), dgst.Encoded())
}
56 changes: 0 additions & 56 deletions pkg/oci/mock.go

This file was deleted.

5 changes: 4 additions & 1 deletion pkg/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func TestOCIClient(t *testing.T) {
client: containerdClient,
}

for _, ociClient := range []Client{remoteContainerd, localContainerd} {
localClient := NewLocalClient(t.TempDir())

ociClients := []Client{remoteContainerd, localContainerd, localClient}
for _, ociClient := range ociClients {
t.Run(ociClient.Name(), func(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion pkg/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
require.NoError(t, err)
imgs = append(imgs, img)
}
ociClient := oci.NewMockClient(imgs)
ociClient := oci.NewLocalClient(imgs)

Check failure on line 48 in pkg/state/state_test.go

View workflow job for this annotation

GitHub Actions / unit

cannot use imgs (variable of type []"github.com/spegel-org/spegel/pkg/oci".Image) as string value in argument to oci.NewLocalClient

Check failure on line 48 in pkg/state/state_test.go

View workflow job for this annotation

GitHub Actions / lint

cannot use imgs (variable of type []"github.com/spegel-org/spegel/pkg/oci".Image) as string value in argument to oci.NewLocalClient (typecheck)
router := routing.NewMemoryRouter(map[string][]netip.AddrPort{}, netip.MustParseAddrPort("127.0.0.1:5000"))

ctx, cancel := context.WithCancel(context.TODO())
Expand Down
Loading