Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
6a7bf0a8
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看板
未验证
提交
6a7bf0a8
编写于
3月 19, 2020
作者:
R
runzexia
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add s2i controller test
Signed-off-by:
N
runzexia
<
runzexia@yunify.com
>
上级
93461cfb
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
271 addition
and
20 deletion
+271
-20
cmd/controller-manager/app/controllers.go
cmd/controller-manager/app/controllers.go
+5
-3
pkg/controller/s2ibinary/s2ibinary_controller.go
pkg/controller/s2ibinary/s2ibinary_controller.go
+5
-3
pkg/controller/s2ibinary/s2ibinary_controller_test.go
pkg/controller/s2ibinary/s2ibinary_controller_test.go
+243
-0
pkg/simple/client/s3/fake/fakes3.go
pkg/simple/client/s3/fake/fakes3.go
+16
-12
pkg/simple/client/s3/fake/fakes3_test.go
pkg/simple/client/s3/fake/fakes3_test.go
+2
-2
未找到文件。
cmd/controller-manager/app/controllers.go
浏览文件 @
6a7bf0a8
...
...
@@ -28,6 +28,7 @@ import (
"kubesphere.io/kubesphere/pkg/controller/virtualservice"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/simple/client/k8s"
fakeS3
"kubesphere.io/kubesphere/pkg/simple/client/s3/fake"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
...
...
@@ -69,9 +70,10 @@ func AddControllers(
jobController
:=
job
.
NewJobController
(
kubernetesInformer
.
Batch
()
.
V1
()
.
Jobs
(),
client
.
Kubernetes
())
s2iBinaryController
:=
s2ibinary
.
NewController
(
client
.
KubeSphere
(),
client
.
Kubernetes
(),
kubesphereInformer
.
Devops
()
.
V1alpha1
()
.
S2iBinaries
())
s2iBinaryController
:=
s2ibinary
.
NewController
(
client
.
Kubernetes
(),
client
.
KubeSphere
(),
kubesphereInformer
.
Devops
()
.
V1alpha1
()
.
S2iBinaries
(),
fakeS3
.
NewFakeS3
())
s2iRunController
:=
s2irun
.
NewS2iRunController
(
client
.
KubeSphere
(),
client
.
Kubernetes
(),
...
...
pkg/controller/s2ibinary/s2ibinary_controller.go
浏览文件 @
6a7bf0a8
...
...
@@ -40,9 +40,10 @@ type S2iBinaryController struct {
s3Client
s3
.
Interface
}
func
NewController
(
devopsclientset
devopsclient
.
Interface
,
client
clientset
.
Interface
,
s2ibinInformer
devopsinformers
.
S2iBinaryInformer
)
*
S2iBinaryController
{
func
NewController
(
client
clientset
.
Interface
,
devopsclientset
devopsclient
.
Interface
,
s2ibinInformer
devopsinformers
.
S2iBinaryInformer
,
s3Client
s3
.
Interface
)
*
S2iBinaryController
{
broadcaster
:=
record
.
NewBroadcaster
()
broadcaster
.
StartLogging
(
func
(
format
string
,
args
...
interface
{})
{
...
...
@@ -58,6 +59,7 @@ func NewController(devopsclientset devopsclient.Interface,
s2iBinaryLister
:
s2ibinInformer
.
Lister
(),
s2iBinarySynced
:
s2ibinInformer
.
Informer
()
.
HasSynced
,
workerLoopPeriod
:
time
.
Second
,
s3Client
:
s3Client
,
}
v
.
eventBroadcaster
=
broadcaster
...
...
pkg/controller/s2ibinary/s2ibinary_controller_test.go
0 → 100644
浏览文件 @
6a7bf0a8
package
s2ibinary
import
(
fakeS3
"kubesphere.io/kubesphere/pkg/simple/client/s3/fake"
"reflect"
"testing"
"time"
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"
k8sfake
"k8s.io/client-go/kubernetes/fake"
core
"k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
s2i
"kubesphere.io/kubesphere/pkg/apis/devops/v1alpha1"
"kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
informers
"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
)
var
(
alwaysReady
=
func
()
bool
{
return
true
}
noResyncPeriodFunc
=
func
()
time
.
Duration
{
return
0
}
)
type
fixture
struct
{
t
*
testing
.
T
client
*
fake
.
Clientset
kubeclient
*
k8sfake
.
Clientset
// Objects to put in the store.
s2ibinaryLister
[]
*
s2i
.
S2iBinary
actions
[]
core
.
Action
// Objects from here preloaded into NewSimpleFake.
objects
[]
runtime
.
Object
// Objects from here preloaded into s3
initS3Objects
[]
*
fakeS3
.
Object
expectS3Objects
[]
*
fakeS3
.
Object
}
func
newFixture
(
t
*
testing
.
T
)
*
fixture
{
f
:=
&
fixture
{}
f
.
t
=
t
f
.
objects
=
[]
runtime
.
Object
{}
return
f
}
func
newS2iBinary
(
name
string
,
spec
s2i
.
S2iBinarySpec
)
*
s2i
.
S2iBinary
{
return
&
s2i
.
S2iBinary
{
TypeMeta
:
metav1
.
TypeMeta
{
APIVersion
:
s2i
.
SchemeGroupVersion
.
String
()},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
name
,
Namespace
:
metav1
.
NamespaceDefault
,
},
Spec
:
s2i
.
S2iBinarySpec
{
},
}
}
func
newDeletingS2iBinary
(
name
string
)
*
s2i
.
S2iBinary
{
deleteTime
:=
metav1
.
Now
()
return
&
s2i
.
S2iBinary
{
TypeMeta
:
metav1
.
TypeMeta
{
APIVersion
:
s2i
.
SchemeGroupVersion
.
String
()},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
name
,
Namespace
:
metav1
.
NamespaceDefault
,
Finalizers
:
[]
string
{
s2i
.
S2iBinaryFinalizerName
},
DeletionTimestamp
:
&
deleteTime
,
},
Spec
:
s2i
.
S2iBinarySpec
{
},
}
}
func
(
f
*
fixture
)
newController
()
(
*
S2iBinaryController
,
informers
.
SharedInformerFactory
,
*
fakeS3
.
FakeS3
)
{
f
.
client
=
fake
.
NewSimpleClientset
(
f
.
objects
...
)
f
.
kubeclient
=
k8sfake
.
NewSimpleClientset
()
i
:=
informers
.
NewSharedInformerFactory
(
f
.
client
,
noResyncPeriodFunc
())
s3I
:=
fakeS3
.
NewFakeS3
(
f
.
expectS3Objects
...
)
c
:=
NewController
(
f
.
kubeclient
,
f
.
client
,
i
.
Devops
()
.
V1alpha1
()
.
S2iBinaries
(),
s3I
)
c
.
s2iBinarySynced
=
alwaysReady
c
.
eventRecorder
=
&
record
.
FakeRecorder
{}
for
_
,
f
:=
range
f
.
s2ibinaryLister
{
i
.
Devops
()
.
V1alpha1
()
.
S2iBinaries
()
.
Informer
()
.
GetIndexer
()
.
Add
(
f
)
}
return
c
,
i
,
s3I
}
func
(
f
*
fixture
)
run
(
fooName
string
)
{
f
.
runController
(
fooName
,
true
,
false
)
}
func
(
f
*
fixture
)
runExpectError
(
fooName
string
)
{
f
.
runController
(
fooName
,
true
,
true
)
}
func
(
f
*
fixture
)
runController
(
s2iBinaryName
string
,
startInformers
bool
,
expectError
bool
)
{
c
,
i
,
s3I
:=
f
.
newController
()
if
startInformers
{
stopCh
:=
make
(
chan
struct
{})
defer
close
(
stopCh
)
i
.
Start
(
stopCh
)
}
err
:=
c
.
syncHandler
(
s2iBinaryName
)
if
!
expectError
&&
err
!=
nil
{
f
.
t
.
Errorf
(
"error syncing foo: %v"
,
err
)
}
else
if
expectError
&&
err
==
nil
{
f
.
t
.
Error
(
"expected error syncing foo, got nil"
)
}
actions
:=
filterInformerActions
(
f
.
client
.
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
)
:
])
}
if
len
(
s3I
.
Storage
)
!=
len
(
f
.
expectS3Objects
){
f
.
t
.
Errorf
(
" unexpected objects: %v"
,
s3I
.
Storage
)
}
}
// checkAction verifies that expected and actual actions are equal and both have
// same attached resources
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
)
}
}
// filterInformerActions filters list and watch actions for testing resources.
// Since list and watch don't change resource state we can filter it to lower
// nose level in our tests.
func
filterInformerActions
(
actions
[]
core
.
Action
)
[]
core
.
Action
{
ret
:=
[]
core
.
Action
{}
for
_
,
action
:=
range
actions
{
if
len
(
action
.
GetNamespace
())
==
0
&&
(
action
.
Matches
(
"list"
,
"foos"
)
||
action
.
Matches
(
"watch"
,
"foos"
)
||
action
.
Matches
(
"list"
,
"deployments"
)
||
action
.
Matches
(
"watch"
,
"deployments"
))
{
continue
}
ret
=
append
(
ret
,
action
)
}
return
ret
}
func
(
f
*
fixture
)
expectUpdateS2iBinaryAction
(
s2ibinary
*
s2i
.
S2iBinary
)
{
action
:=
core
.
NewUpdateAction
(
schema
.
GroupVersionResource
{
Resource
:
s2i
.
ResourcePluralS2iBinary
},
s2ibinary
.
Namespace
,
s2ibinary
)
f
.
actions
=
append
(
f
.
actions
,
action
)
}
func
getKey
(
s2ibinary
*
s2i
.
S2iBinary
,
t
*
testing
.
T
)
string
{
key
,
err
:=
cache
.
DeletionHandlingMetaNamespaceKeyFunc
(
s2ibinary
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error getting key for s2ibinary %v: %v"
,
s2ibinary
.
Name
,
err
)
return
""
}
return
key
}
func
TestDoNothing
(
t
*
testing
.
T
)
{
f
:=
newFixture
(
t
)
s2iBinary
:=
newS2iBinary
(
"test"
,
s2i
.
S2iBinarySpec
{})
f
.
s2ibinaryLister
=
append
(
f
.
s2ibinaryLister
,
s2iBinary
)
f
.
objects
=
append
(
f
.
objects
,
s2iBinary
)
f
.
expectUpdateS2iBinaryAction
(
s2iBinary
)
f
.
run
(
getKey
(
s2iBinary
,
t
))
}
func
TestDeleteS3Object
(
t
*
testing
.
T
)
{
f
:=
newFixture
(
t
)
s2iBinary
:=
newDeletingS2iBinary
(
"test"
)
f
.
s2ibinaryLister
=
append
(
f
.
s2ibinaryLister
,
s2iBinary
)
f
.
objects
=
append
(
f
.
objects
,
s2iBinary
)
f
.
initS3Objects
=
[]
*
fakeS3
.
Object
{
&
fakeS3
.
Object
{
Key
:
"default-test"
,
},}
f
.
expectS3Objects
=
[]
*
fakeS3
.
Object
{}
f
.
expectUpdateS2iBinaryAction
(
s2iBinary
)
f
.
run
(
getKey
(
s2iBinary
,
t
))
}
pkg/simple/client/s3/fake/fakes3.go
浏览文件 @
6a7bf0a8
...
...
@@ -8,31 +8,35 @@ import (
)
type
FakeS3
struct
{
Storage
map
[
string
]
*
o
bject
Storage
map
[
string
]
*
O
bject
}
func
NewFakeS3
()
*
FakeS3
{
return
&
FakeS3
{
Storage
:
map
[
string
]
*
object
{}}
func
NewFakeS3
(
objects
...*
Object
)
*
FakeS3
{
s3
:=
&
FakeS3
{
Storage
:
map
[
string
]
*
Object
{}}
for
_
,
object
:=
range
objects
{
s3
.
Storage
[
object
.
Key
]
=
object
}
return
s3
}
type
o
bject
struct
{
k
ey
string
f
ileName
string
b
ody
io
.
Reader
type
O
bject
struct
{
K
ey
string
F
ileName
string
B
ody
io
.
Reader
}
func
(
s
*
FakeS3
)
Upload
(
key
,
fileName
string
,
body
io
.
Reader
)
error
{
s
.
Storage
[
key
]
=
&
o
bject
{
k
ey
:
key
,
f
ileName
:
fileName
,
b
ody
:
body
,
s
.
Storage
[
key
]
=
&
O
bject
{
K
ey
:
key
,
F
ileName
:
fileName
,
B
ody
:
body
,
}
return
nil
}
func
(
s
*
FakeS3
)
GetDownloadURL
(
key
string
,
fileName
string
)
(
string
,
error
)
{
if
o
,
ok
:=
s
.
Storage
[
key
];
ok
{
return
fmt
.
Sprintf
(
"http://%s/%s"
,
o
.
k
ey
,
fileName
),
nil
return
fmt
.
Sprintf
(
"http://%s/%s"
,
o
.
K
ey
,
fileName
),
nil
}
return
""
,
awserr
.
New
(
s3
.
ErrCodeNoSuchKey
,
"no such object"
,
nil
)
}
...
...
pkg/simple/client/s3/fake/fakes3_test.go
浏览文件 @
6a7bf0a8
...
...
@@ -17,8 +17,8 @@ func TestFakeS3(t *testing.T) {
if
!
ok
{
t
.
Fatal
(
"should have hello object"
)
}
if
o
.
key
!=
key
||
o
.
f
ileName
!=
fileName
{
t
.
Fatalf
(
"
key should be %s, f
ileName should be %s"
,
key
,
fileName
)
if
o
.
Key
!=
key
||
o
.
F
ileName
!=
fileName
{
t
.
Fatalf
(
"
Key should be %s, F
ileName should be %s"
,
key
,
fileName
)
}
url
,
err
:=
s3
.
GetDownloadURL
(
key
,
fileName
+
"1"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录