add dt-test
This commit is contained in:
parent
e67c2b5c8a
commit
e8ef40068a
429
0004-add-dt-test.patch
Normal file
429
0004-add-dt-test.patch
Normal file
@ -0,0 +1,429 @@
|
||||
From 312cc64247cd0d6c93e914f7d47ec27d0d1cb37b Mon Sep 17 00:00:00 2001
|
||||
From: yangjiaqi <yangjiaqi16@huawei.com>
|
||||
Date: Mon, 17 Oct 2022 16:09:13 +0800
|
||||
Subject: [PATCH] add dt-test
|
||||
|
||||
---
|
||||
Makefile | 103 ++++++++++++++--------------
|
||||
hooks/lxcfs-hook/main.go | 2 +-
|
||||
remountcmd_test.go | 140 +++++++++++++++++++++++++++++++++++++++
|
||||
umountcmd_test.go | 139 ++++++++++++++++++++++++++++++++++++++
|
||||
4 files changed, 334 insertions(+), 50 deletions(-)
|
||||
create mode 100644 remountcmd_test.go
|
||||
create mode 100644 umountcmd_test.go
|
||||
|
||||
diff --git a/Makefile b/Makefile
|
||||
index 6507429..b722b51 100644
|
||||
--- a/Makefile
|
||||
+++ b/Makefile
|
||||
@@ -1,49 +1,54 @@
|
||||
-# Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
|
||||
-# lxcfs-tools is licensed under the Mulan PSL v1.
|
||||
-# You can use this software according to the terms and conditions of the Mulan PSL v1.
|
||||
-# You may obtain a copy of Mulan PSL v1 at:
|
||||
-# http://license.coscl.org.cn/MulanPSL
|
||||
-# 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 v1 for more details.
|
||||
-# Description: makefile
|
||||
-# Author: zhangsong
|
||||
-# Create: 2019-01-18
|
||||
-
|
||||
-COMMIT=$(shell git rev-parse HEAD 2> /dev/null || true)
|
||||
-SOURCES := $(shell find . 2>&1 | grep -E '.*\.(c|h|go)$$')
|
||||
-DEPS_LINK := $(CURDIR)/vendor/
|
||||
-VERSION := $(shell cat ./VERSION)
|
||||
-TAGS="cgo static_build"
|
||||
-
|
||||
-BEP_DIR=/tmp/lxcfs-tools-build-bep
|
||||
-BEP_FLAGS=-tmpdir=/tmp/lxcfs-tools-build-bep
|
||||
-
|
||||
-GO_LDFLAGS="-buildid=IdByiSula $(BEP_FLAGS) -linkmode=external -extldflags=-static -extldflags=-Wl,-z,relro,-z,now -X main.gitCommit=${COMMIT} -X main.version=${VERSION}"
|
||||
-
|
||||
-ENV = GOPATH=${GOPATH} \
|
||||
- CGO_ENABLED=1 \
|
||||
- CGO_CFLAGS="-fstack-protector-strong -fPIE" \
|
||||
- CGO_LDFLAGS_ALLOW='-Wl,-z,relro,-z,now' \
|
||||
- CGO_LDFLAGS="-Wl,-z,relro,-z,now -Wl,-z,noexecstack"
|
||||
-
|
||||
-all: dep toolkit lxcfs-hook
|
||||
-dep:
|
||||
- mkdir -p $(BEP_DIR)
|
||||
-
|
||||
-toolkit: $(SOURCES) | $(DEPS_LINK)
|
||||
- @echo "Making lxcfs-tools..."
|
||||
- ${ENV} go build -buildmode=pie -mod=vendor -tags ${TAGS} -ldflags ${GO_LDFLAGS} -o build/lxcfs-tools .
|
||||
- @echo "Done!"
|
||||
-
|
||||
-lxcfs-hook: $(SOURCES) | $(DEPS_LINK)
|
||||
- @echo "Making lxcfs-hook..."
|
||||
- ${ENV} go build -buildmode=pie -mod=vendor -tags ${TAGS} -ldflags ${GO_LDFLAGS} -o build/lxcfs-hook ./hooks/lxcfs-hook
|
||||
- @echo "Done!"
|
||||
-
|
||||
-clean:
|
||||
- rm -rf build
|
||||
-
|
||||
-install:
|
||||
- cd hakc && ./install.shloacal:
|
||||
+# Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
|
||||
+# lxcfs-tools is licensed under the Mulan PSL v1.
|
||||
+# You can use this software according to the terms and conditions of the Mulan PSL v1.
|
||||
+# You may obtain a copy of Mulan PSL v1 at:
|
||||
+# http://license.coscl.org.cn/MulanPSL
|
||||
+# 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 v1 for more details.
|
||||
+# Description: makefile
|
||||
+# Author: zhangsong
|
||||
+# Create: 2019-01-18
|
||||
+
|
||||
+COMMIT=$(shell git rev-parse HEAD 2> /dev/null || true)
|
||||
+SOURCES := $(shell find . 2>&1 | grep -E '.*\.(c|h|go)$$')
|
||||
+DEPS_LINK := $(CURDIR)/vendor/
|
||||
+VERSION := $(shell cat ./VERSION)
|
||||
+TAGS="cgo static_build"
|
||||
+
|
||||
+BEP_DIR=/tmp/lxcfs-tools-build-bep
|
||||
+BEP_FLAGS=-tmpdir=/tmp/lxcfs-tools-build-bep
|
||||
+
|
||||
+GO_LDFLAGS="-buildid=IdByiSula $(BEP_FLAGS) -linkmode=external -extldflags=-static -extldflags=-Wl,-z,relro,-z,now -X main.gitCommit=${COMMIT} -X main.version=${VERSION}"
|
||||
+
|
||||
+ENV = GOPATH=${GOPATH} \
|
||||
+ CGO_ENABLED=1 \
|
||||
+ CGO_CFLAGS="-fstack-protector-strong -fPIE" \
|
||||
+ CGO_LDFLAGS_ALLOW='-Wl,-z,relro,-z,now' \
|
||||
+ CGO_LDFLAGS="-Wl,-z,relro,-z,now -Wl,-z,noexecstack"
|
||||
+
|
||||
+all: dep toolkit lxcfs-hook
|
||||
+dep:
|
||||
+ mkdir -p $(BEP_DIR)
|
||||
+
|
||||
+toolkit: $(SOURCES) | $(DEPS_LINK)
|
||||
+ @echo "Making lxcfs-tools..."
|
||||
+ ${ENV} go build -buildmode=pie -mod=vendor -tags ${TAGS} -ldflags ${GO_LDFLAGS} -o build/lxcfs-tools .
|
||||
+ @echo "Done!"
|
||||
+
|
||||
+lxcfs-hook: $(SOURCES) | $(DEPS_LINK)
|
||||
+ @echo "Making lxcfs-hook..."
|
||||
+ ${ENV} go build -buildmode=pie -mod=vendor -tags ${TAGS} -ldflags ${GO_LDFLAGS} -o build/lxcfs-hook ./hooks/lxcfs-hook
|
||||
+ @echo "Done!"
|
||||
+
|
||||
+tests:
|
||||
+ @echo "Checking lxcfs-tools"
|
||||
+ ${ENV} go test -mod=vendor -tags ${TAGS} -p 1 -v ./... -coverprofile=cover.out
|
||||
+ @echo "Done!"
|
||||
+
|
||||
+clean:
|
||||
+ rm -rf build
|
||||
+
|
||||
+install:
|
||||
+ cd hakc && ./install.shloacal:
|
||||
diff --git a/hooks/lxcfs-hook/main.go b/hooks/lxcfs-hook/main.go
|
||||
index f23a445..2bc0f94 100644
|
||||
--- a/hooks/lxcfs-hook/main.go
|
||||
+++ b/hooks/lxcfs-hook/main.go
|
||||
@@ -63,7 +63,7 @@ func main() {
|
||||
}
|
||||
|
||||
if state.Pid <= 0 {
|
||||
- logrus.Errorf("Can't get correct pid of container:%d", state.Bundle)
|
||||
+ logrus.Errorf("Can't get correct pid of container:%s", state.Bundle)
|
||||
}
|
||||
logrus.Infof("PID:%d", state.Pid)
|
||||
logrus.Infof("Root:%s", state.Root)
|
||||
diff --git a/remountcmd_test.go b/remountcmd_test.go
|
||||
new file mode 100644
|
||||
index 0000000..106ac5e
|
||||
--- /dev/null
|
||||
+++ b/remountcmd_test.go
|
||||
@@ -0,0 +1,140 @@
|
||||
+// Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
|
||||
+// rubik 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: Jiaqi Yang
|
||||
+// Date: 2022-10-15
|
||||
+// Description: This file is used for remountcmd.go
|
||||
+
|
||||
+// go base main package
|
||||
+package main
|
||||
+
|
||||
+import (
|
||||
+ "os"
|
||||
+ "testing"
|
||||
+)
|
||||
+
|
||||
+const (
|
||||
+ oldLxcPath = "/var/lib/lxc"
|
||||
+ newLxcPath = "/var/lib/lxc.bak"
|
||||
+)
|
||||
+
|
||||
+func pathExists(path string) bool {
|
||||
+ if _, err := os.Stat(path); err != nil {
|
||||
+ return false
|
||||
+ }
|
||||
+ return true
|
||||
+}
|
||||
+
|
||||
+func tryMovelxcfs() bool {
|
||||
+ if pathExists(oldLxcPath) {
|
||||
+ if err := rename(oldLxcPath, newLxcPath); err != nil {
|
||||
+ return false
|
||||
+ }
|
||||
+ }
|
||||
+ return true
|
||||
+}
|
||||
+
|
||||
+func tryRecoverlxcfs() {
|
||||
+ if pathExists(newLxcPath) {
|
||||
+ if err := rename(newLxcPath, oldLxcPath); err != nil {
|
||||
+ return
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+// TestWaitForLxcfs tests waitForLxcfs
|
||||
+func TestWaitForLxcfs(t *testing.T) {
|
||||
+ if !tryMovelxcfs() {
|
||||
+ return
|
||||
+ }
|
||||
+ defer tryRecoverlxcfs()
|
||||
+ if err := waitForLxcfs(); err == nil {
|
||||
+ t.Errorf("doprestart() should fail but runs successfully")
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+// TestRemountAll tests remountAll
|
||||
+func TestRemountAll(t *testing.T) {
|
||||
+ if !tryMoveiSulad() {
|
||||
+ return
|
||||
+ }
|
||||
+ defer recoveriSulad()
|
||||
+
|
||||
+ tests := []struct {
|
||||
+ name, initMountns, initUserns string
|
||||
+ wantErr bool
|
||||
+ }{
|
||||
+ {
|
||||
+ name: "TC1-absent of isulad",
|
||||
+ initMountns: "1",
|
||||
+ initUserns: "5",
|
||||
+ wantErr: true,
|
||||
+ },
|
||||
+ }
|
||||
+ for _, tt := range tests {
|
||||
+ t.Run(tt.name, func(t *testing.T) {
|
||||
+ err := remountAll(tt.initMountns, tt.initUserns)
|
||||
+ if (err != nil) != tt.wantErr {
|
||||
+ t.Errorf("remountAll() = %v, want %v", err, tt.wantErr)
|
||||
+ }
|
||||
+ })
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+// TestRemountToContainer tests remountToContainer
|
||||
+func TestRemountToContainer(t *testing.T) {
|
||||
+ if !tryMovelxcfs() {
|
||||
+ return
|
||||
+ }
|
||||
+ defer tryRecoverlxcfs()
|
||||
+ tests := []struct {
|
||||
+ name, initMountns, initUserns, containerid, pid string
|
||||
+ isAll bool
|
||||
+ wantErr bool
|
||||
+ }{
|
||||
+ {
|
||||
+ name: "TC1-remountToContainer",
|
||||
+ initMountns: "1",
|
||||
+ initUserns: "5",
|
||||
+ isAll: true,
|
||||
+ wantErr: true,
|
||||
+ },
|
||||
+ }
|
||||
+
|
||||
+ for _, tt := range tests {
|
||||
+ t.Run(tt.name, func(t *testing.T) {
|
||||
+ err := remountToContainer(tt.initMountns, tt.initUserns, tt.containerid, tt.pid, tt.isAll)
|
||||
+ if (err != nil) != tt.wantErr {
|
||||
+ t.Errorf("umountForContainer() = %v, want %v", err, tt.wantErr)
|
||||
+ }
|
||||
+ })
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+// TestIsContainerExsit tests isContainerExsit
|
||||
+func TestIsContainerExsit(t *testing.T) {
|
||||
+ tests := []struct {
|
||||
+ name, containerid string
|
||||
+ wantErr bool
|
||||
+ }{
|
||||
+ {
|
||||
+ name: "TC1-remountToContainer",
|
||||
+ wantErr: true,
|
||||
+ },
|
||||
+ }
|
||||
+
|
||||
+ for _, tt := range tests {
|
||||
+ t.Run(tt.name, func(t *testing.T) {
|
||||
+ _, err := isContainerExsit(tt.containerid)
|
||||
+ if (err != nil) != tt.wantErr {
|
||||
+ t.Errorf("isContainerExsit() = %v, want %v", err, tt.wantErr)
|
||||
+ }
|
||||
+ })
|
||||
+ }
|
||||
+}
|
||||
diff --git a/umountcmd_test.go b/umountcmd_test.go
|
||||
new file mode 100644
|
||||
index 0000000..bcae021
|
||||
--- /dev/null
|
||||
+++ b/umountcmd_test.go
|
||||
@@ -0,0 +1,139 @@
|
||||
+// Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
|
||||
+// rubik 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: Jiaqi Yang
|
||||
+// Date: 2022-10-14
|
||||
+// Description: This file is used for testing umountcmd
|
||||
+
|
||||
+// go base main package
|
||||
+package main
|
||||
+
|
||||
+import (
|
||||
+ "fmt"
|
||||
+ "os"
|
||||
+ "os/exec"
|
||||
+ "strings"
|
||||
+ "testing"
|
||||
+)
|
||||
+
|
||||
+var (
|
||||
+ oldPath, newPath string
|
||||
+)
|
||||
+
|
||||
+func isuladExisted() bool {
|
||||
+ cmd := exec.Command("isula", "ps")
|
||||
+ if err := cmd.Run(); err != nil {
|
||||
+ return false
|
||||
+ }
|
||||
+ return true
|
||||
+}
|
||||
+
|
||||
+func whereIsIsulad() string {
|
||||
+ cmd := exec.Command("whereis", "isula")
|
||||
+ out, err := cmd.CombinedOutput()
|
||||
+ if err != nil {
|
||||
+ return ""
|
||||
+ }
|
||||
+ path := strings.Trim(strings.Split(string(out), ":")[1], " ")
|
||||
+ path = strings.ReplaceAll(path, "\n", "")
|
||||
+ return path
|
||||
+}
|
||||
+
|
||||
+func rename(oldPath, newPath string) error {
|
||||
+ if err := os.Rename(oldPath, newPath); err != nil {
|
||||
+ fmt.Println(err)
|
||||
+ return err
|
||||
+ }
|
||||
+ fmt.Printf("rename isula path from %s to %s\n", oldPath, newPath)
|
||||
+ return nil
|
||||
+}
|
||||
+
|
||||
+func tryMoveiSulad() bool {
|
||||
+ oldPath = ""
|
||||
+ newPath = ""
|
||||
+ if isuladExisted() {
|
||||
+ fmt.Println("isuila existed ")
|
||||
+ oldPath = whereIsIsulad()
|
||||
+ if oldPath == "" {
|
||||
+ return false
|
||||
+ }
|
||||
+ newPath = oldPath + ".bak"
|
||||
+ if err := rename(oldPath, newPath); err != nil {
|
||||
+ return false
|
||||
+ }
|
||||
+ }
|
||||
+ return true
|
||||
+}
|
||||
+
|
||||
+func recoveriSulad() {
|
||||
+ if oldPath == "" || newPath == "" {
|
||||
+ return
|
||||
+ }
|
||||
+ if err := rename(newPath, oldPath); err != nil {
|
||||
+ return
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+// TestUmountAll tests umountAll
|
||||
+func TestUmountAll(t *testing.T) {
|
||||
+ if !tryMoveiSulad() {
|
||||
+ return
|
||||
+ }
|
||||
+ defer recoveriSulad()
|
||||
+
|
||||
+ tests := []struct {
|
||||
+ name, initMountns, initUserns string
|
||||
+ wantErr bool
|
||||
+ }{
|
||||
+ {
|
||||
+ name: "TC1-absent of isulad",
|
||||
+ initMountns: "1",
|
||||
+ initUserns: "5",
|
||||
+ wantErr: true,
|
||||
+ },
|
||||
+ }
|
||||
+ for _, tt := range tests {
|
||||
+ t.Run(tt.name, func(t *testing.T) {
|
||||
+ err := umountAll(tt.initMountns, tt.initUserns)
|
||||
+ if (err != nil) != tt.wantErr {
|
||||
+ t.Errorf("umountAll() = %v, want %v", err, tt.wantErr)
|
||||
+ }
|
||||
+ })
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+// TestUmountForContainer tests umountForContainer
|
||||
+func TestUmountForContainer(t *testing.T) {
|
||||
+ if !tryMovelxcfs() {
|
||||
+ return
|
||||
+ }
|
||||
+ defer tryRecoverlxcfs()
|
||||
+ tests := []struct {
|
||||
+ name, initMountns, initUserns, containerid, pid string
|
||||
+ isAll bool
|
||||
+ wantErr bool
|
||||
+ }{
|
||||
+ {
|
||||
+ name: "TC1-umountForContainer",
|
||||
+ initMountns: "1",
|
||||
+ initUserns: "5",
|
||||
+ isAll: true,
|
||||
+ wantErr: true,
|
||||
+ },
|
||||
+ }
|
||||
+
|
||||
+ for _, tt := range tests {
|
||||
+ t.Run(tt.name, func(t *testing.T) {
|
||||
+ err := umountForContainer(tt.initMountns, tt.initUserns, tt.containerid, tt.pid, tt.isAll)
|
||||
+ if (err != nil) != tt.wantErr {
|
||||
+ t.Errorf("umountForContainer() = %v, want %v", err, tt.wantErr)
|
||||
+ }
|
||||
+ })
|
||||
+ }
|
||||
+}
|
||||
--
|
||||
2.30.0
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#Basic Information
|
||||
Name: lxcfs-tools
|
||||
Version: 0.3
|
||||
Release: 26
|
||||
Release: 27
|
||||
Summary: toolkit for lxcfs to remount a running isulad
|
||||
License: Mulan PSL v2
|
||||
URL: https://gitee.com/openeuler/lxcfs-tools
|
||||
@ -11,6 +11,7 @@ BuildRoot: %{_tmppath}/%{name}-root
|
||||
Patch1: 0001-lxcfs-tools-build-security-option.patch
|
||||
Patch2: 0002-enable-external-linkmode-for-cgo-build.patch
|
||||
Patch3: 0003-retry-10-times-to-avoid-isulad-unavailable.patch
|
||||
Patch4: 0004-add-dt-test.patch
|
||||
|
||||
#Dependency
|
||||
BuildRequires: golang > 1.7
|
||||
@ -37,6 +38,9 @@ mkdir -p -m 0700 ${LXCFS_TOOLS_DIR}
|
||||
install -m 0750 build/lxcfs-hook ${HOOK_DIR}
|
||||
install -m 0750 build/lxcfs-tools ${LXCFS_TOOLS_DIR}
|
||||
|
||||
%check
|
||||
make tests
|
||||
|
||||
#Install and uninstall scripts
|
||||
%pre
|
||||
|
||||
@ -92,6 +96,9 @@ rm -rfv %{buildroot}
|
||||
|
||||
|
||||
%changelog
|
||||
* Mon Oct 17 2022 vegbir <yangjiaqi16@huawei.com> - 0.3-27
|
||||
- add dt-test
|
||||
|
||||
* Wed Aug 17 2022 vegbir <yangjiaqi16@huawei.com> - 0.3-26
|
||||
- retry 10 times to avoid isulad unavailable
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user