Skip to content

Commit

Permalink
refactor: increase test coverage for mac for tty tests (#1460)
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
  • Loading branch information
Terry Howe authored Aug 2, 2024
1 parent 1d261a7 commit 5d4a835
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 93 deletions.
115 changes: 115 additions & 0 deletions cmd/oras/internal/display/status/tty_console_test.go
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/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")
}
})
}
93 changes: 0 additions & 93 deletions cmd/oras/internal/display/status/tty_test.go
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -103,101 +98,13 @@ 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 {
t.Error("TrackTarget() should return an error for non-tty file")
}
}

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 {
Expand Down

0 comments on commit 5d4a835

Please sign in to comment.