fix app_controller & helm related tests.

This commit is contained in:
yangsoon
2021-03-16 12:13:51 +08:00
parent 7328e572e5
commit c349c94181
5 changed files with 71 additions and 48 deletions

View File

@@ -143,16 +143,18 @@ spec:
replicas: *1 | int
}`
const workloadDefinition = `
const componenetDefinition = `
apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
kind: ComponentDefinition
metadata:
name: worker
annotations:
definition.oam.dev/description: "Long-running scalable backend worker without network endpoint"
spec:
definitionRef:
name: deployments.apps
workload:
definition:
apiVersion: apps/v1
kind: Deployment
extension:
template: |
output: {
@@ -224,8 +226,8 @@ var _ = Describe("Test application parser", func() {
tclient := test.MockClient{
MockGet: func(ctx context.Context, key types.NamespacedName, obj runtime.Object) error {
switch o := obj.(type) {
case *v1alpha2.WorkloadDefinition:
wd, err := util.UnMarshalStringToWorkloadDefinition(workloadDefinition)
case *v1alpha2.ComponentDefinition:
wd, err := util.UnMarshalStringToComponentDefinition(componenetDefinition)
if err != nil {
return err
}

View File

@@ -161,10 +161,10 @@ var _ = Describe("Test Application Controller", func() {
Scopes: map[string]string{"healthscopes.core.oam.dev": "app-with-two-comp-default-health"},
})
wd := &v1alpha2.WorkloadDefinition{}
wDDefJson, _ := yaml.YAMLToJSON([]byte(wDDefYaml))
cd := &v1alpha2.ComponentDefinition{}
cDDefJson, _ := yaml.YAMLToJSON([]byte(cDDefYaml))
webserverwd := &v1alpha2.WorkloadDefinition{}
webserverwd := &v1alpha2.ComponentDefinition{}
webserverwdJson, _ := yaml.YAMLToJSON([]byte(webserverYaml))
td := &v1alpha2.TraitDefinition{}
@@ -180,8 +180,8 @@ var _ = Describe("Test Application Controller", func() {
BeforeEach(func() {
Expect(json.Unmarshal(wDDefJson, wd)).Should(BeNil())
Expect(k8sClient.Create(ctx, wd.DeepCopy())).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
Expect(json.Unmarshal(cDDefJson, cd)).Should(BeNil())
Expect(k8sClient.Create(ctx, cd.DeepCopy())).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
Expect(json.Unmarshal(tDDefJson, td)).Should(BeNil())
Expect(k8sClient.Create(ctx, td.DeepCopy())).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
@@ -701,12 +701,12 @@ var _ = Describe("Test Application Controller", func() {
It("app with health policy for workload", func() {
By("change workload and trait definition with health policy")
nwd, owd := &v1alpha2.WorkloadDefinition{}, &v1alpha2.WorkloadDefinition{}
wDDefJson, _ := yaml.YAMLToJSON([]byte(wDDefWithHealthYaml))
Expect(json.Unmarshal(wDDefJson, nwd)).Should(BeNil())
Expect(k8sClient.Get(ctx, client.ObjectKey{Name: nwd.Name, Namespace: nwd.Namespace}, owd)).Should(BeNil())
nwd.ResourceVersion = owd.ResourceVersion
Expect(k8sClient.Update(ctx, nwd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
ncd, ocd := &v1alpha2.ComponentDefinition{}, &v1alpha2.ComponentDefinition{}
wDDefJson, _ := yaml.YAMLToJSON([]byte(cDDefWithHealthYaml))
Expect(json.Unmarshal(wDDefJson, ncd)).Should(BeNil())
Expect(k8sClient.Get(ctx, client.ObjectKey{Name: ncd.Name, Namespace: ncd.Namespace}, ocd)).Should(BeNil())
ncd.ResourceVersion = ocd.ResourceVersion
Expect(k8sClient.Update(ctx, ncd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
ntd, otd := &v1alpha2.TraitDefinition{}, &v1alpha2.TraitDefinition{}
tDDefJson, _ := yaml.YAMLToJSON([]byte(tDDefWithHealthYaml))
Expect(json.Unmarshal(tDDefJson, ntd)).Should(BeNil())
@@ -906,10 +906,10 @@ var _ = Describe("Test Application Controller", func() {
It("app with health policy and custom status for workload", func() {
By("change workload and trait definition with health policy")
nwd := &v1alpha2.WorkloadDefinition{}
wDDefJson, _ := yaml.YAMLToJSON([]byte(wdDefWithHealthStatusYaml))
Expect(json.Unmarshal(wDDefJson, nwd)).Should(BeNil())
Expect(k8sClient.Create(ctx, nwd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
ncd := &v1alpha2.ComponentDefinition{}
cDDefJson, _ := yaml.YAMLToJSON([]byte(cdDefWithHealthStatusYaml))
Expect(json.Unmarshal(cDDefJson, ncd)).Should(BeNil())
Expect(k8sClient.Create(ctx, ncd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
ntd := &v1alpha2.TraitDefinition{}
tDDefJson, _ := yaml.YAMLToJSON([]byte(tDDefWithHealthStatusYaml))
Expect(json.Unmarshal(tDDefJson, ntd)).Should(BeNil())
@@ -1087,17 +1087,19 @@ spec:
definitionRef:
name: healthscopes.core.oam.dev`
wDDefYaml = `
cDDefYaml = `
apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
kind: ComponentDefinition
metadata:
name: worker
namespace: vela-system
annotations:
definition.oam.dev/description: "Long-running scalable backend worker without network endpoint"
spec:
definitionRef:
name: deployments.apps
workload:
definition:
apiVersion: apps/v1
kind: Deployment
extension:
template: |
output: {
@@ -1149,15 +1151,17 @@ spec:
`
webserverYaml = `apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
kind: ComponentDefinition
metadata:
name: webserver
namespace: vela-system
annotations:
definition.oam.dev/description: "webserver was composed by deployment and service"
spec:
definitionRef:
name: deployments.apps
workload:
definition:
apiVersion: apps/v1
kind: Deployment
extension:
template: |
output: {
@@ -1239,17 +1243,19 @@ spec:
}
`
wDDefWithHealthYaml = `
cDDefWithHealthYaml = `
apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
kind: ComponentDefinition
metadata:
name: worker
namespace: vela-system
annotations:
definition.oam.dev/description: "Long-running scalable backend worker without network endpoint"
spec:
definitionRef:
name: deployments.apps
workload:
definition:
apiVersion: apps/v1
kind: Deployment
extension:
healthPolicy: |
isHealth: context.output.status.readyReplicas == context.output.status.replicas
@@ -1301,16 +1307,18 @@ spec:
cmd?: [...string]
}
`
wdDefWithHealthStatusYaml = `apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
cdDefWithHealthStatusYaml = `apiVersion: core.oam.dev/v1alpha2
kind: ComponentDefinition
metadata:
name: nworker
namespace: vela-system
annotations:
definition.oam.dev/description: "Describes long-running, scalable, containerized services that running at backend. They do NOT have network endpoint to receive external network traffic."
spec:
definitionRef:
name: deployments.apps
workload:
definition:
apiVersion: apps/v1
kind: Deployment
status:
healthPolicy: |
isHealth: (context.output.status.readyReplicas > 0) && (context.output.status.readyReplicas == context.output.status.replicas)

View File

@@ -22,15 +22,14 @@ import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
cpv1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

View File

@@ -132,6 +132,19 @@ func (matcher ErrorMatcher) NegatedFailureMessage(actual interface{}) (message s
return format.Message(actual, "not to equal", expectedError)
}
// UnMarshalStringToComponentDefinition parse a string to a componentDefinition object
func UnMarshalStringToComponentDefinition(s string) (*v1alpha2.ComponentDefinition, error) {
obj := &v1alpha2.ComponentDefinition{}
_body, err := yaml.YAMLToJSON([]byte(s))
if err != nil {
return nil, err
}
if err := json.Unmarshal(_body, obj); err != nil {
return nil, err
}
return obj, nil
}
// UnMarshalStringToWorkloadDefinition parse a string to a workloadDefinition object
func UnMarshalStringToWorkloadDefinition(s string) (*v1alpha2.WorkloadDefinition, error) {
obj := &v1alpha2.WorkloadDefinition{}

View File

@@ -24,7 +24,7 @@ var _ = Describe("Test application containing helm module", func() {
namespace = "helm-test-ns"
appName = "test-app"
compName = "test-comp"
wdName = "webapp-chart"
cdName = "webapp-chart"
tdName = "virtualgroup"
)
var app v1alpha2.Application
@@ -52,11 +52,11 @@ var _ = Describe("Test application containing helm module", func() {
},
time.Second*3, time.Millisecond*300).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
wd := v1alpha2.WorkloadDefinition{}
wd.SetName(wdName)
wd.SetNamespace(namespace)
wd.Spec.Reference = v1alpha2.DefinitionReference{Name: "deployments.apps", Version: "v1"}
wd.Spec.Schematic = &v1alpha2.Schematic{
cd := v1alpha2.ComponentDefinition{}
cd.SetName(cdName)
cd.SetNamespace(namespace)
cd.Spec.Workload.Definition = v1alpha2.WorkloadGVK{APIVersion: "apps/v1", Kind: "Deployment"}
cd.Spec.Schematic = &v1alpha2.Schematic{
HELM: &v1alpha2.Helm{
Release: util.Object2RawExtension(map[string]interface{}{
"chart": map[string]interface{}{
@@ -72,7 +72,7 @@ var _ = Describe("Test application containing helm module", func() {
},
}
Expect(k8sClient.Create(ctx, &wd)).Should(Succeed())
Expect(k8sClient.Create(ctx, &cd)).Should(Succeed())
By("Install a patch trait used to test CUE module")
td := v1alpha2.TraitDefinition{}
@@ -112,6 +112,7 @@ var _ = Describe("Test application containing helm module", func() {
AfterEach(func() {
By("Clean up resources after a test")
k8sClient.DeleteAllOf(ctx, &v1alpha2.Application{}, client.InNamespace(namespace))
k8sClient.DeleteAllOf(ctx, &v1alpha2.ComponentDefinition{}, client.InNamespace(namespace))
k8sClient.DeleteAllOf(ctx, &v1alpha2.WorkloadDefinition{}, client.InNamespace(namespace))
k8sClient.DeleteAllOf(ctx, &v1alpha2.TraitDefinition{}, client.InNamespace(namespace))
Expect(k8sClient.Delete(ctx, &ns, client.PropagationPolicy(metav1.DeletePropagationForeground))).Should(Succeed())
@@ -135,7 +136,7 @@ var _ = Describe("Test application containing helm module", func() {
Components: []v1alpha2.ApplicationComponent{
{
Name: compName,
WorkloadType: wdName,
WorkloadType: cdName,
Settings: util.Object2RawExtension(map[string]interface{}{
"image": map[string]interface{}{
"tag": "5.1.2",
@@ -211,7 +212,7 @@ var _ = Describe("Test application containing helm module", func() {
Components: []v1alpha2.ApplicationComponent{
{
Name: compName,
WorkloadType: wdName,
WorkloadType: cdName,
Settings: util.Object2RawExtension(map[string]interface{}{
"image": map[string]interface{}{
"tag": "5.1.3", // change 5.1.4 => 5.1.3