Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
df49775a
K
kubesphere
项目概览
水淹萌龙
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
df49775a
编写于
5月 18, 2020
作者:
J
jian.zhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add unit test for job
Signed-off-by:
N
jian.zhang
<
jian.zhang@jhlinux.com
>
上级
17200172
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
191 addition
and
1 deletion
+191
-1
pkg/controller/job/job_controller_test.go
pkg/controller/job/job_controller_test.go
+190
-0
pkg/controller/s2ibinary/s2ibinary_controller_test.go
pkg/controller/s2ibinary/s2ibinary_controller_test.go
+1
-1
未找到文件。
pkg/controller/job/job_controller_test.go
浏览文件 @
df49775a
...
...
@@ -16,3 +16,193 @@
*/
package
job
import
(
batchv1
"k8s.io/api/batch/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/client-go/informers"
kubeinformers
"k8s.io/client-go/informers"
k8sfake
"k8s.io/client-go/kubernetes/fake"
core
"k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
"reflect"
"testing"
"time"
)
var
(
noResyncPeriodFunc
=
func
()
time
.
Duration
{
return
0
}
)
type
fixture
struct
{
t
*
testing
.
T
kubeclient
*
k8sfake
.
Clientset
jobController
*
JobController
jobLister
[]
*
batchv1
.
Job
kubeactions
[]
core
.
Action
actions
[]
core
.
Action
kubeobjects
[]
runtime
.
Object
objects
[]
runtime
.
Object
}
func
filterInformerActions
(
actions
[]
core
.
Action
)
[]
core
.
Action
{
ret
:=
[]
core
.
Action
{}
for
_
,
action
:=
range
actions
{
if
len
(
action
.
GetNamespace
())
==
0
&&
(
action
.
Matches
(
"list"
,
"jobs"
)
||
action
.
Matches
(
"watch"
,
"jobs"
))
{
continue
}
ret
=
append
(
ret
,
action
)
}
return
ret
}
func
newJob
(
name
string
,
spec
batchv1
.
JobSpec
)
*
batchv1
.
Job
{
job
:=
&
batchv1
.
Job
{
TypeMeta
:
metav1
.
TypeMeta
{
APIVersion
:
batchv1
.
SchemeGroupVersion
.
String
()},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
name
,
Namespace
:
metav1
.
NamespaceDefault
,
},
Spec
:
spec
,
}
return
job
}
func
newFixture
(
t
*
testing
.
T
)
*
fixture
{
f
:=
&
fixture
{}
f
.
t
=
t
f
.
objects
=
[]
runtime
.
Object
{}
f
.
kubeobjects
=
[]
runtime
.
Object
{}
return
f
}
func
checkAction
(
expected
,
actual
core
.
Action
,
t
*
testing
.
T
)
{
if
!
(
expected
.
Matches
(
actual
.
GetVerb
(),
actual
.
GetResource
()
.
Resource
)
&&
actual
.
GetSubresource
()
==
expected
.
GetSubresource
())
{
t
.
Errorf
(
"Expected
\n\t
%#v
\n
got
\n\t
%#v"
,
expected
,
actual
)
return
}
if
reflect
.
TypeOf
(
actual
)
!=
reflect
.
TypeOf
(
expected
)
{
t
.
Errorf
(
"Action has wrong type. Expected: %t. Got: %t"
,
expected
,
actual
)
return
}
switch
a
:=
actual
.
(
type
)
{
case
core
.
CreateActionImpl
:
e
,
_
:=
expected
.
(
core
.
CreateActionImpl
)
expObject
:=
e
.
GetObject
()
object
:=
a
.
GetObject
()
if
!
reflect
.
DeepEqual
(
expObject
,
object
)
{
t
.
Errorf
(
"Action %s %s has wrong object
\n
Diff:
\n
%s"
,
a
.
GetVerb
(),
a
.
GetResource
()
.
Resource
,
diff
.
ObjectGoPrintSideBySide
(
expObject
,
object
))
}
case
core
.
UpdateActionImpl
:
e
,
_
:=
expected
.
(
core
.
UpdateActionImpl
)
expObject
:=
e
.
GetObject
()
object
:=
a
.
GetObject
()
if
!
reflect
.
DeepEqual
(
expObject
,
object
)
{
t
.
Errorf
(
"Action %s %s has wrong object
\n
Diff:
\n
%s"
,
a
.
GetVerb
(),
a
.
GetResource
()
.
Resource
,
diff
.
ObjectGoPrintSideBySide
(
expObject
,
object
))
}
case
core
.
PatchActionImpl
:
e
,
_
:=
expected
.
(
core
.
PatchActionImpl
)
expPatch
:=
e
.
GetPatch
()
patch
:=
a
.
GetPatch
()
if
!
reflect
.
DeepEqual
(
expPatch
,
patch
)
{
t
.
Errorf
(
"Action %s %s has wrong patch
\n
Diff:
\n
%s"
,
a
.
GetVerb
(),
a
.
GetResource
()
.
Resource
,
diff
.
ObjectGoPrintSideBySide
(
expPatch
,
patch
))
}
default
:
t
.
Errorf
(
"Uncaptured Action %s %s, you should explicitly add a case to capture it"
,
actual
.
GetVerb
(),
actual
.
GetResource
()
.
Resource
)
}
}
func
(
f
*
fixture
)
newController
()
(
*
JobController
,
informers
.
SharedInformerFactory
)
{
f
.
kubeclient
=
k8sfake
.
NewSimpleClientset
(
f
.
kubeobjects
...
)
k8sI
:=
kubeinformers
.
NewSharedInformerFactory
(
f
.
kubeclient
,
noResyncPeriodFunc
())
jobController
:=
NewJobController
(
k8sI
.
Batch
()
.
V1
()
.
Jobs
(),
f
.
kubeclient
)
for
_
,
job
:=
range
f
.
jobLister
{
_
=
k8sI
.
Batch
()
.
V1
()
.
Jobs
()
.
Informer
()
.
GetIndexer
()
.
Add
(
job
)
}
return
jobController
,
k8sI
}
func
(
f
*
fixture
)
runController
(
jobName
string
,
startInformers
bool
,
expectError
bool
)
{
c
,
k8sI
:=
f
.
newController
()
if
startInformers
{
stopCh
:=
make
(
chan
struct
{})
defer
close
(
stopCh
)
k8sI
.
Start
(
stopCh
)
}
err
:=
c
.
syncJob
(
jobName
)
if
!
expectError
&&
err
!=
nil
{
f
.
t
.
Errorf
(
"error syncing job: %v"
,
err
)
}
else
if
expectError
&&
err
==
nil
{
f
.
t
.
Error
(
"expected error syncing job, got nil"
)
}
actions
:=
filterInformerActions
(
f
.
kubeclient
.
Actions
())
for
i
,
action
:=
range
actions
{
if
len
(
f
.
actions
)
<
i
+
1
{
f
.
t
.
Errorf
(
"%d unexpected actions: %+v"
,
len
(
actions
)
-
len
(
f
.
actions
),
actions
[
i
:
])
break
}
expectedAction
:=
f
.
actions
[
i
]
checkAction
(
expectedAction
,
action
,
f
.
t
)
}
if
len
(
f
.
actions
)
>
len
(
actions
)
{
f
.
t
.
Errorf
(
"%d additional expected actions:%+v"
,
len
(
f
.
actions
)
-
len
(
actions
),
f
.
actions
[
len
(
actions
)
:
])
}
}
func
(
f
*
fixture
)
expectAddAnnotationAction
(
job
*
batchv1
.
Job
)
{
action
:=
core
.
NewUpdateAction
(
schema
.
GroupVersionResource
{
Resource
:
"jobs"
},
job
.
Namespace
,
job
)
f
.
actions
=
append
(
f
.
actions
,
action
)
}
func
(
f
*
fixture
)
run
(
jobName
string
)
{
f
.
runController
(
jobName
,
true
,
false
)
}
func
TestAddAnnotation
(
t
*
testing
.
T
)
{
f
:=
newFixture
(
t
)
job
:=
newJob
(
"test"
,
batchv1
.
JobSpec
{})
f
.
jobLister
=
append
(
f
.
jobLister
,
job
)
f
.
objects
=
append
(
f
.
objects
,
job
)
f
.
kubeobjects
=
append
(
f
.
kubeobjects
,
job
)
f
.
expectAddAnnotationAction
(
job
)
f
.
run
(
getKey
(
job
,
t
))
}
func
getKey
(
job
*
batchv1
.
Job
,
t
*
testing
.
T
)
string
{
key
,
err
:=
cache
.
DeletionHandlingMetaNamespaceKeyFunc
(
job
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error getting key for job %v: %v"
,
job
.
Name
,
err
)
return
""
}
return
key
}
pkg/controller/s2ibinary/s2ibinary_controller_test.go
浏览文件 @
df49775a
...
...
@@ -53,7 +53,7 @@ func newS2iBinary(name string, spec s2i.S2iBinarySpec) *s2i.S2iBinary {
Name
:
name
,
Namespace
:
metav1
.
NamespaceDefault
,
},
Spec
:
s
2i
.
S2iBinarySpec
{}
,
Spec
:
s
pec
,
}
}
func
newDeletingS2iBinary
(
name
string
)
*
s2i
.
S2iBinary
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录