From 64eba33ff7a459c6a1aa73cb52b80d930a382da7 Mon Sep 17 00:00:00 2001 From: xingweizheng Date: Thu, 8 Dec 2022 19:28:15 +0800 Subject: [PATCH 1/2] daemon: add dt for interface build,dameon,grpc_server,info,pull --- daemon/build_test.go | 44 +++++++++++++++++++++++++++++++++++++ daemon/daemon_test.go | 45 ++++++++++++++++++++++++++++++++++++++ daemon/grpc_server_test.go | 38 ++++++++++++++++++++++++++++++++ daemon/info_test.go | 38 ++++++++++++++++++++++++++++++++ daemon/pull_test.go | 3 +++ 5 files changed, 168 insertions(+) create mode 100644 daemon/build_test.go create mode 100644 daemon/daemon_test.go create mode 100644 daemon/grpc_server_test.go create mode 100644 daemon/info_test.go diff --git a/daemon/build_test.go b/daemon/build_test.go new file mode 100644 index 0000000..aef31b4 --- /dev/null +++ b/daemon/build_test.go @@ -0,0 +1,44 @@ +// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +// isula-build licensed under the Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +// PURPOSE. +// See the Mulan PSL v2 for more details. +// Author: Weizheng Xing +// Create: 2022-11-29 +// Description: This file is for build test. + +package daemon + +import ( + "context" + "testing" + + "gotest.tools/v3/assert" + + constant "isula.org/isula-build" + pb "isula.org/isula-build/api/services" + "isula.org/isula-build/util" +) + +func TestBuild(t *testing.T) { + d := prepare(t) + defer tmpClean(d) + + _, err := d.Daemon.backend.Build(context.Background(), &pb.BuildRequest{}) + assert.ErrorContains(t, err, "is not supported") + + _, err = d.Daemon.backend.Build(context.Background(), &pb.BuildRequest{BuildType: "ctr-img"}) + assert.ErrorContains(t, err, "wrong image format provided") + + buildID := util.GenerateNonCryptoID()[:constant.DefaultIDLen] + go func() { + <-d.Daemon.backend.syncBuildStatus(buildID) + }() + _, err = d.Daemon.backend.Build(context.Background(), + &pb.BuildRequest{BuildType: "ctr-img", Format: "oci", BuildID: buildID}) + assert.ErrorContains(t, err, "parse dockerfile failed") +} diff --git a/daemon/daemon_test.go b/daemon/daemon_test.go new file mode 100644 index 0000000..9180cae --- /dev/null +++ b/daemon/daemon_test.go @@ -0,0 +1,45 @@ +// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +// isula-build licensed under the Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +// PURPOSE. +// See the Mulan PSL v2 for more details. +// Author: Weizheng Xing +// Create: 2022-11-29 +// Description: This file is for build test. + +package daemon + +import ( + "context" + "testing" + + "gotest.tools/v3/assert" + + pb "isula.org/isula-build/api/services" +) + +func TestNewDaemon(t *testing.T) { + _, err := NewDaemon(Options{}, nil) + assert.NilError(t, err) +} + +func TestRun(t *testing.T) { + d := prepare(t) + defer tmpClean(d) + + err := d.Daemon.Run() + assert.ErrorContains(t, err, "create new GRPC socket failed") + d.Daemon.Cleanup() +} + +func TestNewBuilder(t *testing.T) { + d := prepare(t) + defer tmpClean(d) + + _, err := d.Daemon.NewBuilder(context.Background(), &pb.BuildRequest{BuildType: "ctr-img", Format: "oci"}) + assert.NilError(t, err) +} diff --git a/daemon/grpc_server_test.go b/daemon/grpc_server_test.go new file mode 100644 index 0000000..3b42f7c --- /dev/null +++ b/daemon/grpc_server_test.go @@ -0,0 +1,38 @@ +// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +// isula-build licensed under the Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +// PURPOSE. +// See the Mulan PSL v2 for more details. +// Author: Weizheng Xing +// Create: 2022-11-29 +// Description: This file is for grpc server test. + +package daemon + +import ( + "context" + "testing" + + "gotest.tools/v3/assert" +) + +func TestRunGrpc(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + d := prepare(t) + defer tmpClean(d) + + d.Daemon.opts.Group = "isula" + err := d.Daemon.NewGrpcServer() + assert.NilError(t, err) + + errCh := make(chan error) + err = d.Daemon.grpc.Run(ctx, errCh, cancel) + assert.NilError(t, err) + +} diff --git a/daemon/info_test.go b/daemon/info_test.go new file mode 100644 index 0000000..932d04f --- /dev/null +++ b/daemon/info_test.go @@ -0,0 +1,38 @@ +// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +// isula-build licensed under the Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +// PURPOSE. +// See the Mulan PSL v2 for more details. +// Author: Weizheng Xing +// Create: 2022-11-29 +// Description: This file is for info test. + +package daemon + +import ( + "context" + "testing" + + "gotest.tools/v3/assert" + + pb "isula.org/isula-build/api/services" +) + +func TestInfo(t *testing.T) { + d := prepare(t) + defer tmpClean(d) + + _, err := d.Daemon.backend.Info(context.Background(), &pb.InfoRequest{}) + assert.NilError(t, err) + _, err = d.Daemon.backend.Info(context.Background(), &pb.InfoRequest{Verbose: true}) + assert.NilError(t, err) +} + +func TestGetRegistryInfo(t *testing.T) { + _, _, _, err := getRegistryInfo() + assert.NilError(t, err) +} diff --git a/daemon/pull_test.go b/daemon/pull_test.go index b296bfd..a758de9 100644 --- a/daemon/pull_test.go +++ b/daemon/pull_test.go @@ -31,6 +31,7 @@ import ( constant "isula.org/isula-build" pb "isula.org/isula-build/api/services" + "isula.org/isula-build/builder" _ "isula.org/isula-build/exporter/docker" "isula.org/isula-build/pkg/logger" "isula.org/isula-build/store" @@ -84,6 +85,8 @@ func prepare(t *testing.T) daemonTestOptions { opts: opt, localStore: &localStore, key: localKey, + builders: make(map[string]builder.Builder), + entities: make(map[string]string), } dOpt.Daemon.NewBackend() return dOpt -- Gitee From 5edff01f9bdc001980a1a5423ba4b37a1adce756 Mon Sep 17 00:00:00 2001 From: xingweizheng Date: Fri, 9 Dec 2022 11:03:12 +0800 Subject: [PATCH 2/2] cmd/daemon: add base test for runDaemon and before function --- cmd/daemon/before_test.go | 8 +++++++- cmd/daemon/main_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 cmd/daemon/main_test.go diff --git a/cmd/daemon/before_test.go b/cmd/daemon/before_test.go index 19b1bc0..d2a8f41 100644 --- a/cmd/daemon/before_test.go +++ b/cmd/daemon/before_test.go @@ -9,7 +9,7 @@ // See the Mulan PSL v2 for more details. // Author: Xiang Li // Create: 2020-01-20 -// Description: This file is used for isula-build daemon testing +// Description: This file is used for isula-build cmd/daemon testing package main @@ -27,6 +27,12 @@ import ( "isula.org/isula-build/store" ) +func TestBefor(t *testing.T) { + cmd := newDaemonCommand() + err := before(cmd) + assert.NilError(t, err) +} + func TestSetupWorkingDirectories(t *testing.T) { var testDir *fs.Dir var testcases = []struct { diff --git a/cmd/daemon/main_test.go b/cmd/daemon/main_test.go new file mode 100644 index 0000000..85e7b94 --- /dev/null +++ b/cmd/daemon/main_test.go @@ -0,0 +1,27 @@ +// Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +// isula-build licensed under the Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +// PURPOSE. +// See the Mulan PSL v2 for more details. +// Author: Weizheng Xing +// Create: 2022-12-08 +// Description: This file is used for isula-build cmd/daemon testing + +package main + +import ( + "testing" + + "gotest.tools/v3/assert" +) + +func TestRunDaemon(t *testing.T) { + cmd := newDaemonCommand() + daemonOpts.Group = "none" + err := runDaemon(cmd, []string{}) + assert.ErrorContains(t, err, "create new GRPC socket failed") +} -- Gitee