Projects
Eulaceura:Factory
lxcfs-tools
_service:obs_scm:0004-add-dt-test.patch
Sign Up
Log In
Username
Password
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File _service:obs_scm:0004-add-dt-test.patch of Package lxcfs-tools
From 3fa8fc64608fe7c089d0586db4103494d35c68eb Mon Sep 17 00:00:00 2001 From: yangjiaqi <yangjiaqi16@huawei.com> Date: Thu, 17 Nov 2022 20:58:38 +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 c6eb33f..cceab41 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 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/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 v2 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 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/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 v2 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 b661c28..aa6d855 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
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.
浙ICP备2022010568号-2