Files
kubevela/test/e2e-test/resource_policy_test.go
Ai Ranthem a5606b7808
Some checks failed
CodeQL / Analyze (go) (push) Failing after 6m23s
Definition-Lint / definition-doc (push) Failing after 3m8s
E2E MultiCluster Test / detect-noop (push) Successful in 6s
E2E Test / detect-noop (push) Successful in 3s
Go / detect-noop (push) Successful in 2s
license / Check for unapproved licenses (push) Failing after 18s
Registry / publish-core-images (push) Failing after 1m4s
Unit-Test / detect-noop (push) Successful in 20s
Sync SDK / sync_sdk (push) Failing after 3m9s
Go / staticcheck (push) Successful in 6m17s
Go / check-diff (push) Failing after 19m4s
Go / check-core-image-build (push) Failing after 5m44s
Go / check-cli-image-build (push) Failing after 3m31s
Unit-Test / unit-tests (push) Failing after 13m54s
Go / lint (push) Failing after 1h53m27s
Scorecards supply-chain security / Scorecards analysis (push) Failing after 1m27s
E2E MultiCluster Test / e2e-multi-cluster-tests (v1.29) (push) Has been cancelled
E2E Test / e2e-tests (v1.29) (push) Has been cancelled
Go / check-windows (push) Has been cancelled
Chore: (deps): Update k8s to 1.29 (#6654)
* chore: update k8s to 1.29

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: unit test

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: lint

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: lint

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: e2e

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix: lint and e2e test

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* test(e2e): increase timeout

Signed-off-by: phantomnat <w.nattadej@gmail.com>

* fix e2e and scripts

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>

* make reviewable

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>

* rollback a unnecessary ut change

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>

* update go.mod to import merged workflow

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>

---------

Signed-off-by: phantomnat <w.nattadej@gmail.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Co-authored-by: phantomnat <w.nattadej@gmail.com>
2025-01-03 07:54:42 +08:00

241 lines
11 KiB
Go

/*
Copyright 2021 The KubeVela 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 controllers_test
import (
"context"
"fmt"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v13 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/kubevela/pkg/util/rand"
common2 "github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/utils/common"
)
var _ = Describe("Application Resource-Related Policy Tests", func() {
ctx := context.Background()
var namespace string
BeforeEach(func() {
namespace = "test-resource-policy-" + rand.RandomString(4)
Expect(k8sClient.Create(ctx, &v1.Namespace{ObjectMeta: v12.ObjectMeta{Name: namespace}})).Should(Succeed())
})
AfterEach(func() {
ns := &v1.Namespace{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: namespace}, ns)).Should(Succeed())
Expect(k8sClient.Delete(ctx, ns)).Should(Succeed())
})
It("Test ApplyOnce Policy", func() {
By("create apply-once app(apply-once disabled)")
app := &v1beta1.Application{}
Expect(common.ReadYamlToObject("testdata/app/app_apply_once.yaml", app)).Should(BeNil())
app.SetNamespace(namespace)
Expect(k8sClient.Create(ctx, app)).Should(Succeed())
appKey := client.ObjectKeyFromObject(app)
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
g.Expect(app.Status.Phase).Should(Equal(common2.ApplicationRunning))
}, 30*time.Second, time.Second*3).Should(Succeed())
By("test state-keep")
deploy := &v13.Deployment{}
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "hello-world"}, deploy)).Should(Succeed())
deploy.Spec.Replicas = ptr.To(int32(0))
g.Expect(k8sClient.Update(ctx, deploy)).Should(Succeed())
}, 10*time.Second, time.Second*2).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
app.Status.SetConditions(condition.Condition{Type: "StateKeep", Status: "True", Reason: condition.ReasonAvailable, LastTransitionTime: v12.Now()})
g.Expect(k8sClient.Status().Update(ctx, app)).Should(Succeed())
}, 10*time.Second, time.Second*2).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(deploy), deploy)).Should(Succeed())
g.Expect(deploy.Spec.Replicas).Should(Equal(ptr.To(int32(1))))
}, 30*time.Second, time.Second*3).Should(Succeed())
By("test apply-once policy(apply-once enabled)")
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
app.Spec.Policies[0].Properties = &runtime.RawExtension{Raw: []byte(`{"enable":true}`)}
g.Expect(k8sClient.Update(ctx, app)).Should(Succeed())
}, 10*time.Second).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
g.Expect(app.Status.ObservedGeneration).Should(Equal(app.Generation))
g.Expect(app.Status.Phase).Should(Equal(common2.ApplicationRunning))
}, 30*time.Second).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "hello-world"}, deploy)).Should(Succeed())
deploy.Spec.Replicas = ptr.To(int32(0))
g.Expect(k8sClient.Update(ctx, deploy)).Should(Succeed())
}, 10*time.Second).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
app.Status.SetConditions(condition.Condition{Type: "ApplyOnce", Status: "True", Reason: condition.ReasonAvailable, LastTransitionTime: v12.Now()})
g.Expect(k8sClient.Status().Update(ctx, app)).Should(Succeed())
}, 10*time.Second).Should(Succeed())
time.Sleep(30 * time.Second)
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(deploy), deploy)).Should(Succeed())
Expect(deploy.Spec.Replicas).Should(Equal(ptr.To(int32(0))))
})
It("Test GarbageCollect Policy", func() {
By("create garbage-collect app")
app := &v1beta1.Application{}
Expect(common.ReadYamlToObject("testdata/app/app_garbage_collect.yaml", app)).Should(BeNil())
app.SetNamespace(namespace)
Expect(k8sClient.Create(ctx, app)).Should(Succeed())
appKey := client.ObjectKeyFromObject(app)
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
g.Expect(app.Status.Phase).Should(Equal(common2.ApplicationRunning))
}, 30*time.Second).Should(Succeed())
By("upgrade to v2 (same component)")
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
app.Spec.Components[0].Traits[0].Properties = &runtime.RawExtension{Raw: []byte(`{"port":[8001]}`)}
g.Expect(k8sClient.Update(ctx, app)).Should(Succeed())
}, 10*time.Second).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
g.Expect(app.Status.ObservedGeneration).Should(Equal(app.Generation))
g.Expect(app.Status.Phase).Should(Equal(common2.ApplicationRunning))
}, 30*time.Second).Should(Succeed())
By("upgrade to v3 (new component)")
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
app.Spec.Components[0].Name = "hello-world-new"
g.Expect(k8sClient.Update(ctx, app)).Should(Succeed())
}, 10*time.Second).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
g.Expect(app.Status.ObservedGeneration).Should(Equal(app.Generation))
g.Expect(app.Status.Phase).Should(Equal(common2.ApplicationRunning))
}, 30*time.Second).Should(Succeed())
By("upgrade to v4 (new component)")
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
app.Spec.Components[0].Name = "hello-world-latest"
g.Expect(k8sClient.Update(ctx, app)).Should(Succeed())
}, 10*time.Second).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
g.Expect(app.Status.ObservedGeneration).Should(Equal(app.Generation))
g.Expect(app.Status.Phase).Should(Equal(common2.ApplicationRunning))
}, 30*time.Second).Should(Succeed())
deployments := &v13.DeploymentList{}
Expect(k8sClient.List(ctx, deployments, client.InNamespace(namespace))).Should(Succeed())
Expect(len(deployments.Items)).Should(Equal(3))
services := &v1.ServiceList{}
Expect(k8sClient.List(ctx, services, client.InNamespace(namespace))).Should(Succeed())
Expect(len(services.Items)).Should(Equal(3))
By("delete v3")
rt := &v1beta1.ResourceTracker{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("%s-v3-%s", app.Name, namespace)}, rt)).Should(Succeed())
Expect(k8sClient.Delete(ctx, rt)).Should(Succeed())
Eventually(func(g Gomega) {
err := k8sClient.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("%s-v3-%s", app.Name, namespace)}, rt)
g.Expect(errors.IsNotFound(err)).Should(BeTrue())
}, 15*time.Second).Should(Succeed())
Expect(k8sClient.List(ctx, deployments, client.InNamespace(namespace))).Should(Succeed())
Expect(len(deployments.Items)).Should(Equal(2))
Expect(k8sClient.List(ctx, services, client.InNamespace(namespace))).Should(Succeed())
Expect(len(services.Items)).Should(Equal(3))
By("delete latest deploy, auto gc rt v4")
deploy := &v13.Deployment{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "hello-world-latest"}, deploy)).Should(Succeed())
Expect(k8sClient.Delete(ctx, deploy)).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
app.Status.SetConditions(condition.Condition{Type: "GC", Status: "True", Reason: condition.ReasonAvailable, LastTransitionTime: v12.Now()})
g.Expect(k8sClient.Status().Update(ctx, app)).Should(Succeed())
}, 10*time.Second).Should(Succeed())
Eventually(func(g Gomega) {
err := k8sClient.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("%s-%s-v4", app.Name, namespace)}, rt)
g.Expect(errors.IsNotFound(err)).Should(BeTrue())
}, 15*time.Second).Should(Succeed())
Expect(k8sClient.List(ctx, deployments, client.InNamespace(namespace))).Should(Succeed())
Expect(len(deployments.Items)).Should(Equal(1))
Expect(k8sClient.List(ctx, services, client.InNamespace(namespace))).Should(Succeed())
Expect(len(services.Items)).Should(Equal(3))
By("delete application")
Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
Expect(k8sClient.Delete(ctx, app)).Should(Succeed())
Eventually(func(g Gomega) {
err := k8sClient.Get(ctx, appKey, app)
g.Expect(errors.IsNotFound(err)).Should(BeTrue())
}, 15*time.Second).Should(Succeed())
Expect(k8sClient.List(ctx, deployments, client.InNamespace(namespace))).Should(Succeed())
Expect(len(deployments.Items)).Should(Equal(0))
Expect(k8sClient.List(ctx, services, client.InNamespace(namespace))).Should(Succeed())
Expect(len(services.Items)).Should(Equal(0))
})
It("Test state keep during suspending", func() {
By("create suspending app")
app := &v1beta1.Application{}
Expect(common.ReadYamlToObject("testdata/app/app_suspending.yaml", app)).Should(BeNil())
app.SetNamespace(namespace)
Expect(k8sClient.Create(ctx, app)).Should(Succeed())
appKey := client.ObjectKeyFromObject(app)
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
g.Expect(app.Status.Phase).Should(Equal(common2.ApplicationWorkflowSuspending))
}, 30*time.Second).Should(Succeed())
By("test suspending app state-keep")
deploy := &v13.Deployment{}
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "busybox"}, deploy)).Should(Succeed())
deploy.Spec.Replicas = ptr.To(int32(0))
g.Expect(k8sClient.Update(ctx, deploy)).Should(Succeed())
}, 10*time.Second).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, appKey, app)).Should(Succeed())
app.Status.SetConditions(condition.Condition{Type: "StateKeep", Status: "True", Reason: condition.ReasonAvailable, LastTransitionTime: v12.Now()})
g.Expect(k8sClient.Status().Update(ctx, app)).Should(Succeed())
}, 10*time.Second).Should(Succeed())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(deploy), deploy)).Should(Succeed())
g.Expect(deploy.Spec.Replicas).Should(Equal(ptr.To(int32(1))))
}, 30*time.Second).Should(Succeed())
})
})