Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
isula-build
提交
cf69507f
I
isula-build
项目概览
openeuler
/
isula-build
通知
5
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
isula-build
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
cf69507f
编写于
7月 28, 2020
作者:
O
openeuler-ci-bot
提交者:
Gitee
7月 28, 2020
浏览文件
操作
浏览文件
下载
差异文件
!5 tests: add more unit tests for builder and pkgs
Merge pull request !5 from jingxiaolu/add_tests
上级
791549c9
dfade45c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
263 addition
and
1 deletion
+263
-1
builder/builder_test.go
builder/builder_test.go
+82
-0
builder/dockerfile/builder_test.go
builder/dockerfile/builder_test.go
+92
-1
pkg/stack/stack_test.go
pkg/stack/stack_test.go
+71
-0
tests/util/util.go
tests/util/util.go
+18
-0
未找到文件。
builder/builder_test.go
0 → 100644
浏览文件 @
cf69507f
// 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: Jingxiao Lu
// Create: 2020-07-24
// Description: Test cases for Builder package
package
builder
import
(
"context"
"reflect"
"testing"
"gotest.tools/fs"
constant
"isula.org/isula-build"
pb
"isula.org/isula-build/api/services"
"isula.org/isula-build/builder/dockerfile"
"isula.org/isula-build/store"
)
func
TestNewBuilder
(
t
*
testing
.
T
)
{
tmpDir
:=
fs
.
NewDir
(
t
,
t
.
Name
())
defer
tmpDir
.
Remove
()
type
args
struct
{
ctx
context
.
Context
store
store
.
Store
req
*
pb
.
BuildRequest
runtimePath
string
buildDir
string
runDir
string
}
tests
:=
[]
struct
{
name
string
args
args
want
Builder
wantErr
bool
}{
{
name
:
"ctr-img"
,
args
:
args
{
ctx
:
context
.
Background
(),
store
:
store
.
Store
{},
req
:
&
pb
.
BuildRequest
{
BuildType
:
constant
.
BuildContainerImageType
},
buildDir
:
tmpDir
.
Path
(),
runDir
:
tmpDir
.
Path
(),
},
want
:
&
dockerfile
.
Builder
{},
wantErr
:
false
,
},
{
name
:
"No supported type"
,
args
:
args
{
ctx
:
context
.
Background
(),
store
:
store
.
Store
{},
req
:
&
pb
.
BuildRequest
{
BuildType
:
"Unknown"
},
},
want
:
nil
,
wantErr
:
true
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
got
,
err
:=
NewBuilder
(
tt
.
args
.
ctx
,
tt
.
args
.
store
,
tt
.
args
.
req
,
tt
.
args
.
runtimePath
,
tt
.
args
.
buildDir
,
tt
.
args
.
runDir
)
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
t
.
Errorf
(
"NewBuilder() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
return
}
if
reflect
.
TypeOf
(
got
)
!=
reflect
.
TypeOf
(
tt
.
want
)
{
t
.
Errorf
(
"NewBuilder() got = %v, want %v"
,
reflect
.
TypeOf
(
got
),
reflect
.
TypeOf
(
tt
.
want
))
}
})
}
}
builder/dockerfile/builder_test.go
浏览文件 @
cf69507f
...
...
@@ -17,6 +17,8 @@ import (
"context"
"crypto/sha256"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
...
...
@@ -26,9 +28,12 @@ import (
"gotest.tools/fs"
constant
"isula.org/isula-build"
pb
"isula.org/isula-build/api/services"
"isula.org/isula-build/image"
"isula.org/isula-build/pkg/logger"
"isula.org/isula-build/pkg/parser"
"isula.org/isula-build/store"
testutil
"isula.org/isula-build/tests/util"
"isula.org/isula-build/util"
)
...
...
@@ -1181,7 +1186,7 @@ func TestSearchArg(t *testing.T) {
}
}
func
TestPaseRequestBuildArgs
(
t
*
testing
.
T
)
{
func
TestPa
r
seRequestBuildArgs
(
t
*
testing
.
T
)
{
var
tests
=
[]
struct
{
name
string
buildArgs
[]
string
...
...
@@ -1301,3 +1306,89 @@ func TestParseTag(t *testing.T) {
assert
.
Equal
(
t
,
tag
,
tc
.
tag
,
tc
.
name
)
}
}
func
TestNewBuilder
(
t
*
testing
.
T
)
{
// tmpfs doesn't not support chattr +i to immutable
tmpDir
,
err
:=
ioutil
.
TempDir
(
"/var/tmp"
,
t
.
Name
())
if
err
!=
nil
{
t
.
Log
(
err
)
t
.
FailNow
()
}
defer
os
.
RemoveAll
(
tmpDir
)
immutablePath
:=
filepath
.
Join
(
tmpDir
,
"run"
)
os
.
Mkdir
(
immutablePath
,
0644
)
if
err
=
testutil
.
Immutable
(
immutablePath
,
true
);
err
!=
nil
{
t
.
Log
(
err
)
t
.
FailNow
()
}
defer
testutil
.
Immutable
(
immutablePath
,
false
)
type
args
struct
{
ctx
context
.
Context
store
store
.
Store
req
*
pb
.
BuildRequest
runtimePath
string
buildDir
string
runDir
string
}
tests
:=
[]
struct
{
name
string
args
args
want
*
Builder
wantErr
bool
}{
{
name
:
"NewBuilder - decrypts fail - wrong key"
,
args
:
args
{
ctx
:
context
.
Background
(),
store
:
store
.
Store
{},
req
:
&
pb
.
BuildRequest
{
BuildArgs
:
[]
string
{
"foo=bar"
,
"http_proxy=test"
},
EncryptKey
:
"1"
,
},
buildDir
:
tmpDir
,
runDir
:
tmpDir
,
},
want
:
nil
,
wantErr
:
true
,
},
{
name
:
"NewBuilder - wrong rundir"
,
args
:
args
{
ctx
:
context
.
Background
(),
store
:
store
.
Store
{},
req
:
&
pb
.
BuildRequest
{},
buildDir
:
tmpDir
,
runDir
:
""
,
},
want
:
nil
,
wantErr
:
true
,
},
{
name
:
"NewBuilder - parseOutput fail"
,
args
:
args
{
ctx
:
context
.
Background
(),
store
:
store
.
Store
{},
req
:
&
pb
.
BuildRequest
{
Output
:
"docker-archive:/home/test/aa.tar"
,
},
buildDir
:
tmpDir
,
runDir
:
immutablePath
,
},
want
:
nil
,
wantErr
:
true
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
got
,
err
:=
NewBuilder
(
tt
.
args
.
ctx
,
tt
.
args
.
store
,
tt
.
args
.
req
,
tt
.
args
.
runtimePath
,
tt
.
args
.
buildDir
,
tt
.
args
.
runDir
)
if
(
err
!=
nil
)
!=
tt
.
wantErr
{
t
.
Errorf
(
"NewBuilder() error = %v, wantErr %v"
,
err
,
tt
.
wantErr
)
return
}
if
!
reflect
.
DeepEqual
(
got
,
tt
.
want
)
{
t
.
Errorf
(
"NewBuilder() got = %v, want %v"
,
got
,
tt
.
want
)
}
})
}
}
pkg/stack/stack_test.go
浏览文件 @
cf69507f
...
...
@@ -33,6 +33,7 @@ import (
"gotest.tools/fs"
constant
"isula.org/isula-build"
testutil
"isula.org/isula-build/tests/util"
)
var
(
...
...
@@ -184,6 +185,34 @@ func TestDumpStack(t *testing.T) {
assert
.
Equal
(
t
,
found
,
true
)
}
// TestDumpStackWriteFail dumpStack fail at WriteFile to the path
func
TestDumpStackWriteFail
(
t
*
testing
.
T
)
{
tmpDir
,
err
:=
ioutil
.
TempDir
(
"/var/tmp"
,
t
.
Name
())
if
err
!=
nil
{
t
.
Log
(
err
)
t
.
FailNow
()
}
defer
os
.
RemoveAll
(
tmpDir
)
if
err
=
testutil
.
Immutable
(
tmpDir
,
true
);
err
!=
nil
{
t
.
Log
(
err
)
t
.
FailNow
()
}
defer
testutil
.
Immutable
(
tmpDir
,
false
)
var
regStackLog
=
regexp
.
MustCompile
(
stackLogRegStr
)
dumpStack
(
tmpDir
)
var
found
bool
items
,
err
:=
ioutil
.
ReadDir
(
tmpDir
)
assert
.
NilError
(
t
,
err
)
for
_
,
fi
:=
range
items
{
if
regStackLog
.
MatchString
(
fi
.
Name
())
{
found
=
true
}
}
assert
.
Equal
(
t
,
found
,
false
)
}
func
TestLogRotate
(
t
*
testing
.
T
)
{
dir
:=
fs
.
NewDir
(
t
,
"TestLogRotate"
)
defer
dir
.
Remove
()
...
...
@@ -204,3 +233,45 @@ func TestLogRotate(t *testing.T) {
// logRotate will keep only "maxStackLogs-1" items. Then will the new dumped stack, we have maxStackLogs items
assert
.
Equal
(
t
,
len
(
items
),
maxStackLogs
-
1
)
}
func
TestLogRotateWrongPath
(
t
*
testing
.
T
)
{
dirPath
:=
"/abc/foo"
if
err
:=
logRotate
(
dirPath
);
err
==
nil
{
t
.
FailNow
()
}
}
// TestLogRotateFail fails for rotating old logs for removing
func
TestLogRotateFail
(
t
*
testing
.
T
)
{
tmpDir
,
err
:=
ioutil
.
TempDir
(
"/var/tmp"
,
t
.
Name
())
if
err
!=
nil
{
t
.
Log
(
err
)
t
.
FailNow
()
}
defer
os
.
RemoveAll
(
tmpDir
)
for
i
:=
0
;
i
<
maxStackLogs
+
2
;
i
++
{
fp
:=
filepath
.
Join
(
tmpDir
,
fmt
.
Sprintf
(
stackLogFormat
,
time
.
Now
()
.
Format
(
"2006-01-02T150405+0800"
)))
err
=
ioutil
.
WriteFile
(
fp
,
[]
byte
(
"goroutines"
),
constant
.
DefaultRootFileMode
)
assert
.
NilError
(
t
,
err
)
time
.
Sleep
(
1
*
time
.
Second
)
}
items
,
err
:=
ioutil
.
ReadDir
(
tmpDir
)
assert
.
NilError
(
t
,
err
)
assert
.
Equal
(
t
,
len
(
items
),
maxStackLogs
+
2
)
if
err
=
testutil
.
Immutable
(
tmpDir
,
true
);
err
!=
nil
{
t
.
Log
(
err
)
t
.
FailNow
()
}
defer
testutil
.
Immutable
(
tmpDir
,
false
)
if
err
=
logRotate
(
tmpDir
);
err
==
nil
{
t
.
Log
(
err
)
t
.
FailNow
()
}
items
,
err
=
ioutil
.
ReadDir
(
tmpDir
)
assert
.
NilError
(
t
,
err
)
// logRotate failed for immutable dir, so the items are still maxStackLogs+2
assert
.
Equal
(
t
,
len
(
items
),
maxStackLogs
+
2
)
}
tests/util/util.go
浏览文件 @
cf69507f
...
...
@@ -17,8 +17,11 @@ package util
import
(
"flag"
"fmt"
"os/exec"
"strings"
"testing"
"github.com/pkg/errors"
)
const
(
...
...
@@ -53,3 +56,18 @@ func GetTestingArgs(t *testing.T) map[string]string {
return
args
}
func
Immutable
(
path
string
,
set
bool
)
error
{
var
op
string
if
set
{
op
=
"+i"
// set immutable
}
else
{
op
=
"-i"
// set mutable
}
cmd
:=
exec
.
Command
(
"chattr"
,
op
,
path
)
err
:=
cmd
.
Run
()
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"chattr %s for %s failed"
,
op
,
path
)
}
return
nil
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录