From fa98d546f9ced2552c6df203cb049c6570b202b8 Mon Sep 17 00:00:00 2001
From: xingweizheng 00591739 <xingweizheng@huawei.com>
Date: Thu, 26 Nov 2020 16:19:43 +0800
Subject: [PATCH 2/5] imporve daemon push and pull unit test

---
 daemon/pull_test.go | 18 ++++++++++++++
 daemon/push_test.go | 60 +++++++++++++++++++++++++++++++++++++++------
 2 files changed, 71 insertions(+), 7 deletions(-)

diff --git a/daemon/pull_test.go b/daemon/pull_test.go
index 7a77c44c..43c011be 100644
--- a/daemon/pull_test.go
+++ b/daemon/pull_test.go
@@ -21,6 +21,7 @@ import (
 
 	"github.com/containers/storage/pkg/reexec"
 	"github.com/containers/storage/pkg/stringid"
+	"golang.org/x/sync/errgroup"
 	"golang.org/x/sys/unix"
 	"google.golang.org/grpc"
 	"gotest.tools/assert"
@@ -28,6 +29,7 @@ import (
 
 	constant "isula.org/isula-build"
 	pb "isula.org/isula-build/api/services"
+	"isula.org/isula-build/pkg/logger"
 	"isula.org/isula-build/store"
 )
 
@@ -100,3 +102,19 @@ func TestPull(t *testing.T) {
 	assert.ErrorContains(t, err, "failed to get the image")
 	tmpClean(d)
 }
+
+func TestPullHandler(t *testing.T) {
+	stream := &controlPullServer{}
+	cliLogger := logger.NewCliLogger(constant.CliLogBufferLen)
+
+	ctx := context.TODO()
+	eg, _ := errgroup.WithContext(ctx)
+	eg.Go(pullMessageHandler(stream, cliLogger))
+	eg.Go(func() error {
+		cliLogger.Print("Pull Response")
+		cliLogger.CloseContent()
+		return nil
+	})
+
+	eg.Wait()
+}
diff --git a/daemon/push_test.go b/daemon/push_test.go
index 3fc363ec..97040b86 100644
--- a/daemon/push_test.go
+++ b/daemon/push_test.go
@@ -17,13 +17,16 @@ import (
 	"context"
 	"testing"
 
+	"github.com/containers/storage"
 	"github.com/containers/storage/pkg/reexec"
 	"github.com/containers/storage/pkg/stringid"
+	"golang.org/x/sync/errgroup"
 	"google.golang.org/grpc"
 	"gotest.tools/assert"
 
 	constant "isula.org/isula-build"
 	pb "isula.org/isula-build/api/services"
+	"isula.org/isula-build/pkg/logger"
 )
 
 type controlPushServer struct {
@@ -45,13 +48,56 @@ func init() {
 
 func TestPush(t *testing.T) {
 	d := prepare(t)
-	pushID := stringid.GenerateNonCryptoID()[:constant.DefaultIDLen]
-	req := &pb.PushRequest{
-		PushID:    pushID,
-		ImageName: "255.255.255.255/no-repository/no-name",
-	}
 	stream := &controlPushServer{}
-	err := d.Daemon.backend.Push(req, stream)
-	assert.ErrorContains(t, err, "error: locating image")
+
+	testCases := []struct {
+		testName    string
+		pushRequest *pb.PushRequest
+	}{
+		{
+			testName: "localNotExist",
+			pushRequest: &pb.PushRequest{
+				PushID:    stringid.GenerateNonCryptoID()[:constant.DefaultIDLen],
+				ImageName: "255.255.255.255/no-repository/no-name",
+			},
+		},
+		{
+			testName: "manifestNotExist",
+			pushRequest: &pb.PushRequest{
+				PushID:    stringid.GenerateNonCryptoID()[:constant.DefaultIDLen],
+				ImageName: "127.0.0.1/no-repository/no-name:latest",
+			},
+		},
+	}
+
+	options := &storage.ImageOptions{}
+	d.Daemon.localStore.CreateImage(stringid.GenerateRandomID(), []string{"127.0.0.1/no-repository/no-name:latest"}, "", "", options)
+
+	for _, tc := range testCases {
+		err := d.Daemon.backend.Push(tc.pushRequest, stream)
+		if tc.testName == "localNotExist" {
+			assert.ErrorContains(t, err, "error: locating image")
+		}
+		if tc.testName == "manifestNotExist" {
+			assert.ErrorContains(t, err, "file does not exist")
+		}
+	}
+
 	tmpClean(d)
 }
+
+func TestPushHandler(t *testing.T) {
+	stream := &controlPushServer{}
+	cliLogger := logger.NewCliLogger(constant.CliLogBufferLen)
+
+	ctx := context.TODO()
+	eg, _ := errgroup.WithContext(ctx)
+	eg.Go(pushMessageHandler(stream, cliLogger))
+	eg.Go(func() error {
+		cliLogger.Print("Push Response")
+		cliLogger.CloseContent()
+		return nil
+	})
+
+	eg.Wait()
+}
-- 
2.27.0

