-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Successor getting with separation of concerns
Signed-off-by: Terry Howe <terrylhowe@gmail.com>
- Loading branch information
Showing
22 changed files
with
476 additions
and
269 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ debug | |
|
||
# Custom | ||
coverage.txt | ||
coverage-all.txt | ||
test/e2e/coverage.txt | ||
**/covcounters.* | ||
**/covmeta.* | ||
|
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
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
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,131 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package status | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/opencontainers/go-digest" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras-go/v2/content/memory" | ||
"oras.land/oras/cmd/oras/internal/output" | ||
"oras.land/oras/internal/testutils" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var ( | ||
ctx context.Context | ||
builder *strings.Builder | ||
printer *output.Printer | ||
bogus ocispec.Descriptor | ||
memStore *memory.Store | ||
memDesc ocispec.Descriptor | ||
manifestDesc ocispec.Descriptor | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
// memory store for testing | ||
memStore = memory.New() | ||
content := []byte("test") | ||
r := bytes.NewReader(content) | ||
memDesc = ocispec.Descriptor{ | ||
MediaType: "application/octet-stream", | ||
Digest: digest.FromBytes(content), | ||
Size: int64(len(content)), | ||
} | ||
if err := memStore.Push(context.Background(), memDesc, r); err != nil { | ||
fmt.Println("Setup failed:", err) | ||
os.Exit(1) | ||
} | ||
if err := memStore.Tag(context.Background(), memDesc, memDesc.Digest.String()); err != nil { | ||
fmt.Println("Setup failed:", err) | ||
os.Exit(1) | ||
} | ||
|
||
layer1Desc := memDesc | ||
layer1Desc.Annotations = map[string]string{ocispec.AnnotationTitle: "layer1"} | ||
layer2Desc := memDesc | ||
layer2Desc.Annotations = map[string]string{ocispec.AnnotationTitle: "layer2"} | ||
manifest := ocispec.Manifest{ | ||
MediaType: ocispec.MediaTypeImageManifest, | ||
Layers: []ocispec.Descriptor{layer1Desc, layer2Desc}, | ||
Config: memDesc, | ||
} | ||
manifestContent, err := json.Marshal(&manifest) | ||
if err != nil { | ||
fmt.Println("Setup failed:", err) | ||
os.Exit(1) | ||
} | ||
manifestDesc = ocispec.Descriptor{ | ||
MediaType: manifest.MediaType, | ||
Size: int64(len(manifestContent)), | ||
Digest: digest.FromBytes(manifestContent), | ||
} | ||
if err := memStore.Push(context.Background(), manifestDesc, strings.NewReader(string(manifestContent))); err != nil { | ||
fmt.Println("Setup failed:", err) | ||
os.Exit(1) | ||
} | ||
if err := memStore.Tag(context.Background(), memDesc, memDesc.Digest.String()); err != nil { | ||
fmt.Println("Setup failed:", err) | ||
os.Exit(1) | ||
} | ||
|
||
ctx = context.Background() | ||
builder = &strings.Builder{} | ||
printer = output.NewPrinter(builder, os.Stderr, false) | ||
bogus = ocispec.Descriptor{MediaType: ocispec.MediaTypeImageManifest} | ||
m.Run() | ||
} | ||
|
||
func TestTextCopyHandler_OnMounted(t *testing.T) { | ||
fetcher := testutils.NewMockFetcher(t) | ||
ch := NewTextCopyHandler(printer, fetcher.Fetcher) | ||
if ch.OnMounted(ctx, fetcher.OciImage) != nil { | ||
t.Error("OnMounted() should not return an error") | ||
} | ||
|
||
} | ||
|
||
func TestTextCopyHandler_OnCopySkipped(t *testing.T) { | ||
fetcher := testutils.NewMockFetcher(t) | ||
ch := NewTextCopyHandler(printer, fetcher.Fetcher) | ||
if ch.OnCopySkipped(ctx, fetcher.OciImage) != nil { | ||
t.Error("OnCopySkipped() should not return an error") | ||
} | ||
} | ||
|
||
func TestTextCopyHandler_PostCopy(t *testing.T) { | ||
fetcher := testutils.NewMockFetcher(t) | ||
ch := NewTextCopyHandler(printer, fetcher.Fetcher) | ||
if ch.PostCopy(ctx, fetcher.OciImage) != nil { | ||
t.Error("PostCopy() should not return an error") | ||
} | ||
if ch.PostCopy(ctx, bogus) == nil { | ||
t.Error("PostCopy() should return an error") | ||
} | ||
} | ||
|
||
func TestTextCopyHandler_PreCopy(t *testing.T) { | ||
fetcher := testutils.NewMockFetcher(t) | ||
ch := NewTextCopyHandler(printer, fetcher.Fetcher) | ||
if ch.PreCopy(ctx, fetcher.OciImage) != nil { | ||
t.Error("PreCopy() should not return an error") | ||
} | ||
} |
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
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,115 @@ | ||
//go:build freebsd || linux || netbsd || openbsd || solaris | ||
|
||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package status | ||
|
||
import ( | ||
"context" | ||
"oras.land/oras-go/v2" | ||
"oras.land/oras-go/v2/content/memory" | ||
"oras.land/oras/cmd/oras/internal/display/status/track" | ||
"oras.land/oras/internal/testutils" | ||
"testing" | ||
) | ||
|
||
func TestTTYPushHandler_TrackTarget(t *testing.T) { | ||
// prepare pty | ||
_, slave, err := testutils.NewPty() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer slave.Close() | ||
ph := NewTTYPushHandler(slave) | ||
store := memory.New() | ||
// test | ||
_, fn, err := ph.TrackTarget(store) | ||
if err != nil { | ||
t.Fatal("TrackTarget() should not return an error") | ||
} | ||
defer func() { | ||
if err := fn(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
if ttyPushHandler, ok := ph.(*TTYPushHandler); !ok { | ||
t.Fatalf("TrackTarget() should return a *TTYPushHandler, got %T", ttyPushHandler) | ||
} | ||
} | ||
|
||
func TestTTYPushHandler_UpdateCopyOptions(t *testing.T) { | ||
// prepare pty | ||
pty, slave, err := testutils.NewPty() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer slave.Close() | ||
ph := NewTTYPushHandler(slave) | ||
gt, _, err := ph.TrackTarget(memory.New()) | ||
if err != nil { | ||
t.Fatalf("TrackTarget() should not return an error: %v", err) | ||
} | ||
// test | ||
opts := oras.CopyGraphOptions{} | ||
ph.UpdateCopyOptions(&opts, memStore) | ||
if err := oras.CopyGraph(context.Background(), memStore, gt, manifestDesc, opts); err != nil { | ||
t.Fatalf("CopyGraph() should not return an error: %v", err) | ||
} | ||
if err := oras.CopyGraph(context.Background(), memStore, gt, manifestDesc, opts); err != nil { | ||
t.Fatalf("CopyGraph() should not return an error: %v", err) | ||
} | ||
if tracked, ok := gt.(track.GraphTarget); !ok { | ||
t.Fatalf("TrackTarget() should return a *track.GraphTarget, got %T", tracked) | ||
} else { | ||
_ = tracked.Close() | ||
} | ||
// validate | ||
if err = testutils.MatchPty(pty, slave, "Exists", manifestDesc.MediaType, "100.00%", manifestDesc.Digest.String()); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func Test_TTYPullHandler_TrackTarget(t *testing.T) { | ||
src := memory.New() | ||
t.Run("has TTY", func(t *testing.T) { | ||
_, device, err := testutils.NewPty() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer device.Close() | ||
ph := NewTTYPullHandler(device) | ||
got, fn, err := ph.TrackTarget(src) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := fn(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
if got == src { | ||
t.Fatal("GraphTarget not be modified on TTY") | ||
} | ||
}) | ||
|
||
t.Run("invalid TTY", func(t *testing.T) { | ||
ph := NewTTYPullHandler(nil) | ||
|
||
if _, _, err := ph.TrackTarget(src); err == nil { | ||
t.Fatal("expected error for no tty but got nil") | ||
} | ||
}) | ||
} |
Oops, something went wrong.