This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit de9bcfe2825da5ffd98367a12ea9e8cb8a5ded59 Author: nferraro <[email protected]> AuthorDate: Thu Oct 4 23:11:58 2018 +0200 Added tests --- pkg/trait/base.go | 6 +- pkg/trait/catalog.go | 4 +- pkg/trait/owner.go | 2 +- pkg/trait/route.go | 4 +- pkg/trait/service.go | 6 +- pkg/trait/trait.go | 4 +- pkg/trait/trait_test.go | 127 ++++++++++++++++++++++++++++++++++++++ pkg/trait/types.go | 14 ++--- pkg/util/kubernetes/collection.go | 54 ++++++++++++++++ 9 files changed, 201 insertions(+), 20 deletions(-) diff --git a/pkg/trait/base.go b/pkg/trait/base.go index d5eea62..3432b93 100644 --- a/pkg/trait/base.go +++ b/pkg/trait/base.go @@ -34,7 +34,7 @@ func (*baseTrait) id() id { return id("base") } -func (d *baseTrait) customize(environment environment, resources *kubernetes.Collection) (bool, error) { +func (d *baseTrait) customize(environment *environment, resources *kubernetes.Collection) (bool, error) { resources.Add(d.getConfigMapFor(environment)) resources.Add(d.getDeploymentFor(environment)) return true, nil @@ -46,7 +46,7 @@ func (d *baseTrait) customize(environment environment, resources *kubernetes.Col // // ********************************** -func (*baseTrait) getConfigMapFor(e environment) *corev1.ConfigMap { +func (*baseTrait) getConfigMapFor(e *environment) *corev1.ConfigMap { // combine properties of integration with context, integration // properties have the priority properties := CombineConfigurationAsMap("property", e.Context, e.Integration) @@ -82,7 +82,7 @@ func (*baseTrait) getConfigMapFor(e environment) *corev1.ConfigMap { // // ********************************** -func (*baseTrait) getDeploymentFor(e environment) *appsv1.Deployment { +func (*baseTrait) getDeploymentFor(e *environment) *appsv1.Deployment { sourceName := strings.TrimPrefix(e.Integration.Spec.Source.Name, "/") // combine environment of integration with context, integration diff --git a/pkg/trait/catalog.go b/pkg/trait/catalog.go index a2ba3b5..df8ad07 100644 --- a/pkg/trait/catalog.go +++ b/pkg/trait/catalog.go @@ -30,7 +30,7 @@ var ( ) // customizersFor returns a Catalog for the given integration details -func customizersFor(environment environment) customizer { +func customizersFor(environment *environment) customizer { switch environment.Platform.Spec.Cluster { case v1alpha1.IntegrationPlatformClusterOpenShift: return compose( @@ -66,7 +66,7 @@ func (c *chainedCustomizer) id() id { return id("") } -func (c *chainedCustomizer) customize(environment environment, resources *kubernetes.Collection) (bool, error) { +func (c *chainedCustomizer) customize(environment *environment, resources *kubernetes.Collection) (bool, error) { atLeastOne := false for _, custom := range c.customizers { if environment.isEnabled(custom.id()) { diff --git a/pkg/trait/owner.go b/pkg/trait/owner.go index 6d77ff9..8603bd5 100644 --- a/pkg/trait/owner.go +++ b/pkg/trait/owner.go @@ -28,7 +28,7 @@ func (*ownerTrait) id() id { return id("owner") } -func (*ownerTrait) customize(e environment, resources *kubernetes.Collection) (bool, error) { +func (*ownerTrait) customize(e *environment, resources *kubernetes.Collection) (bool, error) { controller := true blockOwnerDeletion := true resources.VisitMetaObject(func(res metav1.Object) { diff --git a/pkg/trait/route.go b/pkg/trait/route.go index ded0fe3..acf6984 100644 --- a/pkg/trait/route.go +++ b/pkg/trait/route.go @@ -32,7 +32,7 @@ func (*routeTrait) id() id { return id("route") } -func (e *routeTrait) customize(environment environment, resources *kubernetes.Collection) (bool, error) { +func (e *routeTrait) customize(environment *environment, resources *kubernetes.Collection) (bool, error) { var service *corev1.Service resources.VisitService(func(s *corev1.Service) { if s.ObjectMeta.Labels != nil { @@ -50,7 +50,7 @@ func (e *routeTrait) customize(environment environment, resources *kubernetes.Co return false, nil } -func (*routeTrait) getRouteFor(e environment, service *corev1.Service) *routev1.Route { +func (*routeTrait) getRouteFor(e *environment, service *corev1.Service) *routev1.Route { route := routev1.Route{ TypeMeta: metav1.TypeMeta{ Kind: "Route", diff --git a/pkg/trait/service.go b/pkg/trait/service.go index d7d116a..f52d08d 100644 --- a/pkg/trait/service.go +++ b/pkg/trait/service.go @@ -45,7 +45,7 @@ func (*serviceTrait) id() id { return id("service") } -func (e *serviceTrait) customize(environment environment, resources *kubernetes.Collection) (bool, error) { +func (e *serviceTrait) customize(environment *environment, resources *kubernetes.Collection) (bool, error) { if !e.requiresService(environment) { return false, nil } @@ -57,7 +57,7 @@ func (e *serviceTrait) customize(environment environment, resources *kubernetes. return true, nil } -func (s *serviceTrait) getServiceFor(e environment) (*corev1.Service, error) { +func (s *serviceTrait) getServiceFor(e *environment) (*corev1.Service, error) { port, err := e.getIntConfigOr(s.id(), serviceTraitPortKey, 8080) if err != nil { return nil, err @@ -93,7 +93,7 @@ func (s *serviceTrait) getServiceFor(e environment) (*corev1.Service, error) { return &svc, nil } -func (*serviceTrait) requiresService(environment environment) bool { +func (*serviceTrait) requiresService(environment *environment) bool { for _, dep := range environment.Integration.Spec.Dependencies { if decision, present := webComponents[dep]; present { return decision diff --git a/pkg/trait/trait.go b/pkg/trait/trait.go index a2b83c6..db3c56f 100644 --- a/pkg/trait/trait.go +++ b/pkg/trait/trait.go @@ -32,9 +32,9 @@ func ComputeDeployment(integration *v1alpha1.Integration) ([]runtime.Object, err return nil, err } resources := kubernetes.NewCollection() - customizers := customizersFor(*environment) + customizers := customizersFor(environment) // invoke the trait framework to determine the needed resources - if _, err = customizers.customize(*environment, resources); err != nil { + if _, err = customizers.customize(environment, resources); err != nil { return nil, errors.Wrap(err, "error during trait customization") } return resources.Items(), nil diff --git a/pkg/trait/trait_test.go b/pkg/trait/trait_test.go new file mode 100644 index 0000000..9ffea3a --- /dev/null +++ b/pkg/trait/trait_test.go @@ -0,0 +1,127 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You 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 trait + +import ( + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" + "github.com/apache/camel-k/pkg/util/kubernetes" + routev1 "github.com/openshift/api/route/v1" + "github.com/stretchr/testify/assert" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "testing" +) + +func TestOpenshiftTraits(t *testing.T) { + env := createTestEnv(v1alpha1.IntegrationPlatformClusterOpenShift, "camel:core") + res := processTestEnv(t, env) + assert.Contains(t, env.ExecutedCustomizers, id("base")) + assert.NotContains(t, env.ExecutedCustomizers, id("service")) + assert.NotContains(t, env.ExecutedCustomizers, id("route")) + assert.Contains(t, env.ExecutedCustomizers, id("owner")) + assert.NotNil(t, res.GetConfigMap(func(cm *corev1.ConfigMap) bool { + return cm.Name == "test" + })) + assert.NotNil(t, res.GetDeployment(func(deployment *appsv1.Deployment) bool { + return deployment.Name == "test" + })) +} + +func TestOpenshiftTraitsWithWeb(t *testing.T) { + env := createTestEnv(v1alpha1.IntegrationPlatformClusterOpenShift, "camel:core", "camel:undertow") + res := processTestEnv(t, env) + assert.Contains(t, env.ExecutedCustomizers, id("base")) + assert.Contains(t, env.ExecutedCustomizers, id("service")) + assert.Contains(t, env.ExecutedCustomizers, id("route")) + assert.Contains(t, env.ExecutedCustomizers, id("owner")) + assert.NotNil(t, res.GetConfigMap(func(cm *corev1.ConfigMap) bool { + return cm.Name == "test" + })) + assert.NotNil(t, res.GetDeployment(func(deployment *appsv1.Deployment) bool { + return deployment.Name == "test" + })) + assert.NotNil(t, res.GetService(func(svc *corev1.Service) bool { + return svc.Name == "test" + })) + assert.NotNil(t, res.GetRoute(func(svc *routev1.Route) bool { + return svc.Name == "test" + })) +} + +func TestKubernetesTraits(t *testing.T) { + env := createTestEnv(v1alpha1.IntegrationPlatformClusterKubernetes, "camel:core") + res := processTestEnv(t, env) + assert.Contains(t, env.ExecutedCustomizers, id("base")) + assert.NotContains(t, env.ExecutedCustomizers, id("service")) + assert.NotContains(t, env.ExecutedCustomizers, id("route")) + assert.Contains(t, env.ExecutedCustomizers, id("owner")) + assert.NotNil(t, res.GetConfigMap(func(cm *corev1.ConfigMap) bool { + return cm.Name == "test" + })) + assert.NotNil(t, res.GetDeployment(func(deployment *appsv1.Deployment) bool { + return deployment.Name == "test" + })) +} + +func TestKubernetesTraitsWithWeb(t *testing.T) { + env := createTestEnv(v1alpha1.IntegrationPlatformClusterKubernetes, "camel:core", "camel:servlet") + res := processTestEnv(t, env) + assert.Contains(t, env.ExecutedCustomizers, id("base")) + assert.Contains(t, env.ExecutedCustomizers, id("service")) + assert.NotContains(t, env.ExecutedCustomizers, id("route")) + assert.Contains(t, env.ExecutedCustomizers, id("owner")) + assert.NotNil(t, res.GetConfigMap(func(cm *corev1.ConfigMap) bool { + return cm.Name == "test" + })) + assert.NotNil(t, res.GetDeployment(func(deployment *appsv1.Deployment) bool { + return deployment.Name == "test" + })) + assert.NotNil(t, res.GetService(func(svc *corev1.Service) bool { + return svc.Name == "test" + })) +} + +func processTestEnv(t *testing.T, env *environment) *kubernetes.Collection { + resources := kubernetes.NewCollection() + customizers := customizersFor(env) + _, err := customizers.customize(env, resources) + assert.Nil(t, err) + return resources +} + +func createTestEnv(cluster v1alpha1.IntegrationPlatformCluster, dependencies ...string) *environment { + return &environment{ + Integration: &v1alpha1.Integration{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "ns", + }, + Spec: v1alpha1.IntegrationSpec{ + Dependencies: dependencies, + }, + }, + Context: &v1alpha1.IntegrationContext{}, + Platform: &v1alpha1.IntegrationPlatform{ + Spec: v1alpha1.IntegrationPlatformSpec{ + Cluster: cluster, + }, + }, + ExecutedCustomizers: make([]id, 0), + } +} diff --git a/pkg/trait/types.go b/pkg/trait/types.go index dd8c269..ad002e1 100644 --- a/pkg/trait/types.go +++ b/pkg/trait/types.go @@ -32,7 +32,7 @@ type customizer interface { // The Name of the customizer id() id // Customize executes the trait customization on the resources and return true if the resources have been changed - customize(environment environment, resources *kubernetes.Collection) (bool, error) + customize(environment *environment, resources *kubernetes.Collection) (bool, error) } // A environment provides the context where the trait is executed @@ -43,7 +43,7 @@ type environment struct { ExecutedCustomizers []id } -func (e environment) getTraitSpec(traitID id) *v1alpha1.IntegrationTraitSpec { +func (e *environment) getTraitSpec(traitID id) *v1alpha1.IntegrationTraitSpec { if e.Integration.Spec.Traits == nil { return nil } @@ -53,12 +53,12 @@ func (e environment) getTraitSpec(traitID id) *v1alpha1.IntegrationTraitSpec { return nil } -func (e environment) isEnabled(traitID id) bool { +func (e *environment) isEnabled(traitID id) bool { conf := e.getTraitSpec(traitID) return conf == nil || conf.Enabled == nil || *conf.Enabled } -func (e environment) getConfig(traitID id, key string) *string { +func (e *environment) getConfig(traitID id, key string) *string { conf := e.getTraitSpec(traitID) if conf == nil || conf.Configuration == nil { return nil @@ -69,7 +69,7 @@ func (e environment) getConfig(traitID id, key string) *string { return nil } -func (e environment) getConfigOr(traitID id, key string, defaultValue string) string { +func (e *environment) getConfigOr(traitID id, key string, defaultValue string) string { val := e.getConfig(traitID, key) if val != nil { return *val @@ -77,7 +77,7 @@ func (e environment) getConfigOr(traitID id, key string, defaultValue string) st return defaultValue } -func (e environment) getIntConfig(traitID id, key string) (*int, error) { +func (e *environment) getIntConfig(traitID id, key string) (*int, error) { val := e.getConfig(traitID, key) if val == nil { return nil, nil @@ -89,7 +89,7 @@ func (e environment) getIntConfig(traitID id, key string) (*int, error) { return &intVal, nil } -func (e environment) getIntConfigOr(traitID id, key string, defaultValue int) (int, error) { +func (e *environment) getIntConfigOr(traitID id, key string, defaultValue int) (int, error) { val, err := e.getIntConfig(traitID, key) if err != nil { return 0, err diff --git a/pkg/util/kubernetes/collection.go b/pkg/util/kubernetes/collection.go index 0b5f217..8f099c3 100644 --- a/pkg/util/kubernetes/collection.go +++ b/pkg/util/kubernetes/collection.go @@ -18,6 +18,7 @@ limitations under the License. package kubernetes import ( + routev1 "github.com/openshift/api/route/v1" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -55,6 +56,17 @@ func (c *Collection) VisitDeployment(visitor func(*appsv1.Deployment)) { }) } +// GetDeployment returns a Deployment that matches the given function +func (c *Collection) GetDeployment(filter func(*appsv1.Deployment)bool) *appsv1.Deployment { + var retValue *appsv1.Deployment + c.VisitDeployment(func(re *appsv1.Deployment) { + if filter(re) { + retValue = re + } + }) + return retValue +} + // VisitConfigMap executes the visitor function on all ConfigMap resources func (c *Collection) VisitConfigMap(visitor func(*corev1.ConfigMap)) { c.Visit(func(res runtime.Object) { @@ -64,6 +76,17 @@ func (c *Collection) VisitConfigMap(visitor func(*corev1.ConfigMap)) { }) } +// GetConfigMap returns a ConfigMap that matches the given function +func (c *Collection) GetConfigMap(filter func(*corev1.ConfigMap)bool) *corev1.ConfigMap { + var retValue *corev1.ConfigMap + c.VisitConfigMap(func(re *corev1.ConfigMap) { + if filter(re) { + retValue = re + } + }) + return retValue +} + // VisitService executes the visitor function on all Service resources func (c *Collection) VisitService(visitor func(*corev1.Service)) { c.Visit(func(res runtime.Object) { @@ -73,6 +96,37 @@ func (c *Collection) VisitService(visitor func(*corev1.Service)) { }) } +// GetService returns a Service that matches the given function +func (c *Collection) GetService(filter func(*corev1.Service)bool) *corev1.Service { + var retValue *corev1.Service + c.VisitService(func(re *corev1.Service) { + if filter(re) { + retValue = re + } + }) + return retValue +} + +// VisitRoute executes the visitor function on all Route resources +func (c *Collection) VisitRoute(visitor func(*routev1.Route)) { + c.Visit(func(res runtime.Object) { + if conv, ok := res.(*routev1.Route); ok { + visitor(conv) + } + }) +} + +// GetRoute returns a Route that matches the given function +func (c *Collection) GetRoute(filter func(*routev1.Route)bool) *routev1.Route { + var retValue *routev1.Route + c.VisitRoute(func(re *routev1.Route) { + if filter(re) { + retValue = re + } + }) + return retValue +} + // VisitMetaObject executes the visitor function on all meta.Object resources func (c *Collection) VisitMetaObject(visitor func(metav1.Object)) { c.Visit(func(res runtime.Object) {
