chore: add controller ut (#6043)

Signed-off-by: yyzxw <1020938856@qq.com>
This commit is contained in:
yyzxw
2023-05-29 11:55:06 +08:00
committed by GitHub
parent a849435db2
commit 99ad780261
2 changed files with 113 additions and 7 deletions
@@ -136,9 +136,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
logCtx.AddTag("resource_version", app.ResourceVersion).AddTag("generation", app.Generation)
ctx = oamutil.SetNamespaceInCtx(ctx, app.Namespace)
logCtx.SetContext(ctx)
if annotations := app.GetAnnotations(); annotations == nil || annotations[oam.AnnotationKubeVelaVersion] == "" {
metav1.SetMetaDataAnnotation(&app.ObjectMeta, oam.AnnotationKubeVelaVersion, version.VelaVersion)
}
setVelaVersion(app)
logCtx.AddTag("publish_version", app.GetAnnotations()[oam.AnnotationPublishVersion])
appParser := appfile.NewApplicationParser(r.Client, r.pd)
@@ -659,10 +657,8 @@ func (r *Reconciler) matchControllerRequirement(app *v1beta1.Application) bool {
return requireVersion == r.controllerVersion
}
}
if r.ignoreAppNoCtrlReq {
return false
}
return true
return !r.ignoreAppNoCtrlReq
}
const (
@@ -684,3 +680,9 @@ func originalAppFrom(ctx context.Context) (*v1beta1.Application, bool) {
app, ok := ctx.Value(OriginalAppKey).(*v1beta1.Application)
return app, ok
}
func setVelaVersion(app *v1beta1.Application) {
if annotations := app.GetAnnotations(); annotations == nil || annotations[oam.AnnotationKubeVelaVersion] == "" {
metav1.SetMetaDataAnnotation(&app.ObjectMeta, oam.AnnotationKubeVelaVersion, version.VelaVersion)
}
}
@@ -59,6 +59,7 @@ import (
"github.com/oam-dev/kubevela/pkg/oam/testutil"
"github.com/oam-dev/kubevela/pkg/oam/util"
common2 "github.com/oam-dev/kubevela/pkg/utils/common"
"github.com/oam-dev/kubevela/version"
)
// TODO: Refactor the tests to not copy and paste duplicated code 10 times
@@ -5196,3 +5197,106 @@ spec:
`
)
func Test_isHealthy(t *testing.T) {
tests := []struct {
name string
services []common.ApplicationComponentStatus
want bool
}{
{
name: "test service unhealthy",
services: []common.ApplicationComponentStatus{
{
Name: "test",
Healthy: false,
},
},
want: false,
},
{
name: "test trait unhealthy",
services: []common.ApplicationComponentStatus{
{
Name: "test",
Healthy: true,
Traits: []common.ApplicationTraitStatus{
{
Type: "expose",
Healthy: false,
},
{
Type: "scaler",
Healthy: true,
},
},
},
},
want: false,
},
{
name: "test service and trait all healthy",
services: []common.ApplicationComponentStatus{
{
Name: "test",
Healthy: true,
Traits: []common.ApplicationTraitStatus{
{
Type: "expose",
Healthy: true,
},
{
Type: "scaler",
Healthy: true,
},
},
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isHealthy(tt.services); got != tt.want {
t.Errorf("isHealthy() = %v, want %v", got, tt.want)
}
})
}
}
func Test_setVelaVersion(t *testing.T) {
tests := []struct {
name string
app *v1beta1.Application
validate func(app *v1beta1.Application) bool
}{
{
name: "test no vela version should add",
app: &v1beta1.Application{},
validate: func(app *v1beta1.Application) bool {
return app.Annotations != nil && app.Annotations[oam.AnnotationKubeVelaVersion] == version.VelaVersion
},
},
{
name: "test has vela version should not add",
app: &v1beta1.Application{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
oam.AnnotationKubeVelaVersion: version.VelaVersion,
},
},
},
validate: func(app *v1beta1.Application) bool {
return app.Annotations != nil && app.Annotations[oam.AnnotationKubeVelaVersion] == version.VelaVersion
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setVelaVersion(tt.app)
if tt.validate != nil && !tt.validate(tt.app) {
t.Errorf("setVelaVersion() failed")
}
})
}
}