diff --git a/cmd/oras/internal/display/status/tty_console_test.go b/cmd/oras/internal/display/status/tty_console_test.go new file mode 100644 index 000000000..49166b87a --- /dev/null +++ b/cmd/oras/internal/display/status/tty_console_test.go @@ -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/console/testutils" + "oras.land/oras/cmd/oras/internal/display/status/track" + "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") + } + }) +} diff --git a/cmd/oras/internal/display/status/tty_test.go b/cmd/oras/internal/display/status/tty_test.go index 7e8e2c733..cc6fc6935 100644 --- a/cmd/oras/internal/display/status/tty_test.go +++ b/cmd/oras/internal/display/status/tty_test.go @@ -1,5 +1,3 @@ -//go:build freebsd || linux || netbsd || openbsd || solaris - /* Copyright The ORAS Authors. Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,10 +26,7 @@ import ( "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" - "oras.land/oras-go/v2" "oras.land/oras-go/v2/content/memory" - "oras.land/oras/cmd/oras/internal/display/status/console/testutils" - "oras.land/oras/cmd/oras/internal/display/status/track" ) var ( @@ -103,30 +98,6 @@ func TestTTYPushHandler_OnEmptyArtifact(t *testing.T) { } } -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_TrackTarget_invalidTTY(t *testing.T) { ph := NewTTYPushHandler(os.Stdin) if _, _, err := ph.TrackTarget(nil); err == nil { @@ -134,70 +105,6 @@ func TestTTYPushHandler_TrackTarget_invalidTTY(t *testing.T) { } } -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") - } - }) -} - func TestTTYPullHandler_OnNodeDownloading(t *testing.T) { ph := NewTTYPullHandler(nil) if err := ph.OnNodeDownloading(ocispec.Descriptor{}); err != nil {