Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
71b03899
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看板
未验证
提交
71b03899
编写于
2月 04, 2021
作者:
K
KubeSphere CI Bot
提交者:
GitHub
2月 04, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3330 from zackzhangkai/application-unit-test
add application unit test files
上级
5331ad8f
31f5f847
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
289 addition
and
0 deletion
+289
-0
pkg/controller/application/application_controller_test.go
pkg/controller/application/application_controller_test.go
+183
-0
pkg/controller/application/application_suit_test.go
pkg/controller/application/application_suit_test.go
+106
-0
未找到文件。
pkg/controller/application/application_controller_test.go
浏览文件 @
71b03899
...
...
@@ -15,3 +15,186 @@ limitations under the License.
*/
package
application
import
(
"context"
"fmt"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
v1
"k8s.io/api/apps/v1"
corev1
"k8s.io/api/core/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"kubesphere.io/kubesphere/pkg/controller/utils/servicemesh"
"sigs.k8s.io/application/api/v1beta1"
"time"
)
var
replicas
=
int32
(
2
)
var
_
=
Describe
(
"Application"
,
func
()
{
const
timeout
=
time
.
Second
*
30
const
interval
=
time
.
Second
*
1
ctx
:=
context
.
TODO
()
service
:=
newService
(
"productpage"
)
app
:=
newAppliation
(
service
)
deployments
:=
[]
*
v1
.
Deployment
{
newDeployments
(
service
,
"v1"
)}
BeforeEach
(
func
()
{
// Create application service and deployment
Expect
(
k8sClient
.
Create
(
ctx
,
app
))
.
Should
(
Succeed
())
Expect
(
k8sClient
.
Create
(
ctx
,
service
))
.
Should
(
Succeed
())
for
i
:=
range
deployments
{
deployment
:=
deployments
[
i
]
Expect
(
k8sClient
.
Create
(
ctx
,
deployment
))
.
Should
(
Succeed
())
}
})
// Add Tests for OpenAPI validation (or additonal CRD features) specified in
// your API definition.
// Avoid adding tests for vanilla CRUD operations because they would
// test Kubernetes API server, which isn't the goal here.
Context
(
"Application Controller"
,
func
()
{
It
(
"Should create successfully"
,
func
()
{
By
(
"Reconcile Application successfully"
)
// application should have "kubesphere.io/last-updated" annotation
Eventually
(
func
()
bool
{
app
:=
&
v1beta1
.
Application
{}
_
=
k8sClient
.
Get
(
ctx
,
types
.
NamespacedName
{
Name
:
service
.
Labels
[
servicemesh
.
ApplicationNameLabel
],
Namespace
:
metav1
.
NamespaceDefault
},
app
)
time
,
ok
:=
app
.
Annotations
[
"kubesphere.io/last-updated"
]
return
len
(
time
)
>
0
&&
ok
},
timeout
,
interval
)
.
Should
(
BeTrue
())
})
})
})
func
newDeployments
(
service
*
corev1
.
Service
,
version
string
)
*
v1
.
Deployment
{
lbs
:=
service
.
Labels
lbs
[
"version"
]
=
version
deployment
:=
&
v1
.
Deployment
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
fmt
.
Sprintf
(
"%s-%s"
,
service
.
Name
,
version
),
Namespace
:
metav1
.
NamespaceDefault
,
Labels
:
lbs
,
Annotations
:
map
[
string
]
string
{
servicemesh
.
ServiceMeshEnabledAnnotation
:
"true"
},
},
Spec
:
v1
.
DeploymentSpec
{
Replicas
:
&
replicas
,
Selector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
lbs
,
},
Template
:
corev1
.
PodTemplateSpec
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Labels
:
lbs
,
Annotations
:
service
.
Annotations
,
},
Spec
:
corev1
.
PodSpec
{
Containers
:
[]
corev1
.
Container
{
{
Name
:
"c1"
,
Image
:
"nginx:latest"
,
Ports
:
[]
corev1
.
ContainerPort
{
{
Name
:
"http"
,
ContainerPort
:
80
,
Protocol
:
corev1
.
ProtocolTCP
,
},
{
Name
:
"https"
,
ContainerPort
:
443
,
Protocol
:
corev1
.
ProtocolTCP
,
},
{
Name
:
"mysql"
,
ContainerPort
:
3306
,
Protocol
:
corev1
.
ProtocolTCP
,
},
},
},
},
},
},
},
Status
:
v1
.
DeploymentStatus
{
AvailableReplicas
:
replicas
,
ReadyReplicas
:
replicas
,
Replicas
:
replicas
,
},
}
return
deployment
}
func
newService
(
name
string
)
*
corev1
.
Service
{
svc
:=
&
corev1
.
Service
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
name
,
Namespace
:
metav1
.
NamespaceDefault
,
Labels
:
map
[
string
]
string
{
"app.kubernetes.io/name"
:
"bookinfo"
,
"app.kubernetes.io/version"
:
"1"
,
"app"
:
name
,
},
Annotations
:
map
[
string
]
string
{
"servicemesh.kubesphere.io/enabled"
:
"true"
,
},
},
Spec
:
corev1
.
ServiceSpec
{
Ports
:
[]
corev1
.
ServicePort
{
{
Name
:
"http"
,
Port
:
80
,
Protocol
:
corev1
.
ProtocolTCP
,
},
{
Name
:
"https"
,
Port
:
443
,
Protocol
:
corev1
.
ProtocolTCP
,
},
{
Name
:
"mysql"
,
Port
:
3306
,
Protocol
:
corev1
.
ProtocolTCP
,
},
},
Selector
:
map
[
string
]
string
{
"app.kubernetes.io/name"
:
"bookinfo"
,
"app.kubernetes.io/version"
:
"1"
,
"app"
:
"foo"
,
},
Type
:
corev1
.
ServiceTypeClusterIP
,
},
Status
:
corev1
.
ServiceStatus
{},
}
return
svc
}
func
newAppliation
(
service
*
corev1
.
Service
)
*
v1beta1
.
Application
{
app
:=
&
v1beta1
.
Application
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
service
.
Labels
[
servicemesh
.
ApplicationNameLabel
],
Namespace
:
metav1
.
NamespaceDefault
,
Labels
:
service
.
Labels
,
Annotations
:
map
[
string
]
string
{
servicemesh
.
ServiceMeshEnabledAnnotation
:
"true"
},
},
Spec
:
v1beta1
.
ApplicationSpec
{
ComponentGroupKinds
:
[]
metav1
.
GroupKind
{
{
Group
:
""
,
Kind
:
"Service"
,
},
{
Group
:
"apps"
,
Kind
:
"Deployment"
,
},
},
AddOwnerRef
:
true
,
},
}
return
app
}
pkg/controller/application/application_suit_test.go
0 → 100644
浏览文件 @
71b03899
/*
Copyright 2020 KubeSphere Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
application
import
(
"github.com/onsi/gomega/gexec"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog"
"k8s.io/klog/klogr"
"kubesphere.io/kubesphere/pkg/apis"
"os"
"path/filepath"
appv1beta1
"sigs.k8s.io/application/api/v1beta1"
ctrl
"sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf
"sigs.k8s.io/controller-runtime/pkg/log"
"testing"
"time"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
)
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
var
k8sClient
client
.
Client
var
k8sManager
ctrl
.
Manager
var
testEnv
*
envtest
.
Environment
func
TestApplicationController
(
t
*
testing
.
T
)
{
RegisterFailHandler
(
Fail
)
RunSpecsWithDefaultAndCustomReporters
(
t
,
"Application Controller Test Suite"
,
[]
Reporter
{
printer
.
NewlineReporter
{}})
}
var
_
=
BeforeSuite
(
func
(
done
Done
)
{
logf
.
SetLogger
(
klogr
.
New
())
By
(
"bootstrapping test environment"
)
t
:=
true
if
os
.
Getenv
(
"TEST_USE_EXISTING_CLUSTER"
)
==
"true"
{
testEnv
=
&
envtest
.
Environment
{
UseExistingCluster
:
&
t
,
}
}
else
{
testEnv
=
&
envtest
.
Environment
{
CRDDirectoryPaths
:
[]
string
{
filepath
.
Join
(
".."
,
".."
,
".."
,
"config"
,
"crds"
)},
AttachControlPlaneOutput
:
false
,
}
}
sch
:=
scheme
.
Scheme
err
:=
appv1beta1
.
AddToScheme
(
sch
)
Expect
(
err
)
.
NotTo
(
HaveOccurred
())
err
=
apis
.
AddToScheme
(
sch
)
Expect
(
err
)
.
NotTo
(
HaveOccurred
())
cfg
,
err
:=
testEnv
.
Start
()
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
Expect
(
cfg
)
.
ToNot
(
BeNil
())
k8sManager
,
err
=
ctrl
.
NewManager
(
cfg
,
ctrl
.
Options
{
Scheme
:
sch
,
MetricsBindAddress
:
"0"
,
})
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
err
=
Add
(
k8sManager
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
go
func
()
{
err
=
k8sManager
.
Start
(
ctrl
.
SetupSignalHandler
())
klog
.
Error
(
err
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
}()
k8sClient
=
k8sManager
.
GetClient
()
Expect
(
k8sClient
)
.
ToNot
(
BeNil
())
close
(
done
)
},
60
)
var
_
=
AfterSuite
(
func
()
{
By
(
"tearing down the test environment"
)
gexec
.
KillAndWait
(
5
*
time
.
Second
)
err
:=
testEnv
.
Stop
()
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
})
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录