2021-03-10 10:44:58 +08:00
/ *
2021-03-10 11:51:53 +08:00
Copyright 2021 The KubeVela Authors .
2021-03-10 10:44:58 +08:00
2021-03-10 11:51:53 +08:00
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
2021-03-10 10:44:58 +08:00
2021-03-10 11:51:53 +08:00
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 .
2021-03-10 10:44:58 +08:00
* /
package utils
import (
"context"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"sigs.k8s.io/yaml"
2021-05-23 14:16:06 +08:00
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
2021-03-10 10:44:58 +08:00
"github.com/oam-dev/kubevela/pkg/oam/util"
)
var _ = Describe ( "Test Capability" , func ( ) {
ctx := context . Background ( )
var (
namespace = "ns-cap"
ns corev1 . Namespace
)
2021-08-03 11:30:02 +08:00
BeforeEach ( func ( ) {
ns = corev1 . Namespace {
ObjectMeta : metav1 . ObjectMeta {
Name : namespace ,
} ,
}
By ( "Create a namespace" )
Expect ( k8sClient . Create ( ctx , & ns ) ) . Should ( SatisfyAny ( Succeed ( ) , & util . AlreadyExistMatcher { } ) )
} )
2021-03-15 11:08:46 +08:00
Context ( "When the definition is ComponentDefinition" , func ( ) {
var componentDefinitionName = "web1"
2021-03-10 10:44:58 +08:00
2021-03-15 11:08:46 +08:00
It ( "Test CapabilityComponentDefinition" , func ( ) {
By ( "Apply ComponentDefinition" )
var validComponentDefinition = `
2021-03-10 10:44:58 +08:00
apiVersion : core . oam . dev / v1alpha2
2021-03-15 11:08:46 +08:00
kind : ComponentDefinition
2021-03-10 10:44:58 +08:00
metadata :
name : web1
namespace : ns - cap
annotations :
definition . oam . dev / description : "test"
spec :
2021-03-15 11:08:46 +08:00
workload :
type : deployments . apps
2021-03-10 10:44:58 +08:00
schematic :
cue :
template : |
outputs : {
apiVersion : "apps/v1"
kind : "Deployment"
spec : {
selector : matchLabels : {
"app.oam.dev/component" : context . name
}
template : {
metadata : labels : {
"app.oam.dev/component" : context . name
}
spec : {
containers : [ {
name : context . name
image : parameter . image
if parameter [ "cmd" ] != _ | _ {
command : parameter . cmd
}
} ]
}
}
}
}
parameter : {
// +usage=Which image would you like to use for your service
// +short=i
image : string
// +usage=Commands to run in the container
cmd ? : [ ... string ]
}
`
2021-05-23 14:16:06 +08:00
var componentDefinition v1beta1 . ComponentDefinition
2021-03-15 11:08:46 +08:00
Expect ( yaml . Unmarshal ( [ ] byte ( validComponentDefinition ) , & componentDefinition ) ) . Should ( BeNil ( ) )
Expect ( k8sClient . Create ( ctx , & componentDefinition ) ) . Should ( Succeed ( ) )
2021-03-10 10:44:58 +08:00
By ( "Test GetCapabilityObject" )
2021-05-23 14:16:06 +08:00
def := & CapabilityComponentDefinition { Name : componentDefinitionName , ComponentDefinition : * componentDefinition . DeepCopy ( ) }
2021-03-10 10:44:58 +08:00
By ( "Test GetOpenAPISchema" )
2021-05-23 14:16:06 +08:00
schema , err := def . GetOpenAPISchema ( pd , namespace )
2021-03-10 10:44:58 +08:00
Expect ( err ) . Should ( BeNil ( ) )
Expect ( schema ) . Should ( Not ( BeNil ( ) ) )
} )
} )
Context ( "When the definition is TraitDefinition" , func ( ) {
var traitDefinitionName = "scaler1"
It ( "Test CapabilityTraitDefinition" , func ( ) {
By ( "Apply TraitDefinition" )
var validTraitDefinition = `
apiVersion : core . oam . dev / v1alpha2
kind : TraitDefinition
metadata :
namespace : ns - cap
annotations :
definition . oam . dev / description : "Configures replicas for your service."
name : scaler1
spec :
appliesToWorkloads :
2021-05-20 19:06:38 +08:00
- deployments . apps
2021-03-10 10:44:58 +08:00
definitionRef :
name : manualscalertraits . core . oam . dev
workloadRefPath : spec . workloadRef
schematic :
cue :
template : |
outputs : scaler : {
apiVersion : "core.oam.dev/v1alpha2"
kind : "ManualScalerTrait"
spec : {
replicaCount : parameter . replicas
}
}
parameter : {
//+short=r
//+usage=Replicas of the workload
replicas : * 1 | int
}
`
2021-05-23 14:16:06 +08:00
var traitDefinition v1beta1 . TraitDefinition
2021-03-10 10:44:58 +08:00
Expect ( yaml . Unmarshal ( [ ] byte ( validTraitDefinition ) , & traitDefinition ) ) . Should ( BeNil ( ) )
Expect ( k8sClient . Create ( ctx , & traitDefinition ) ) . Should ( Succeed ( ) )
2021-05-23 14:16:06 +08:00
def := & CapabilityTraitDefinition { Name : traitDefinitionName , TraitDefinition : * traitDefinition . DeepCopy ( ) }
2021-03-10 10:44:58 +08:00
By ( "Test GetOpenAPISchema" )
var expectedSchema = "{\"properties\":{\"replicas\":{\"default\":1,\"description\":\"Replicas of the workload\",\"title\":\"replicas\",\"type\":\"integer\"}},\"required\":[\"replicas\"],\"type\":\"object\"}"
2021-05-23 14:16:06 +08:00
schema , err := def . GetOpenAPISchema ( pd , traitDefinitionName )
2021-03-10 10:44:58 +08:00
Expect ( err ) . Should ( BeNil ( ) )
Expect ( string ( schema ) ) . Should ( Equal ( expectedSchema ) )
} )
} )
Context ( "When the definition is CapabilityBaseDefinition" , func ( ) {
It ( "Test CapabilityTraitDefinition" , func ( ) {
By ( "Test CreateOrUpdateConfigMap" )
definitionName := "n1"
def := & CapabilityBaseDefinition { }
ownerReference := [ ] metav1 . OwnerReference { {
APIVersion : "v1" ,
Kind : "k1" ,
Name : definitionName ,
UID : "123456" ,
Controller : pointer . BoolPtr ( true ) ,
BlockOwnerDeletion : pointer . BoolPtr ( true ) ,
} }
2021-11-05 16:07:04 +08:00
_ , err := def . CreateOrUpdateConfigMap ( ctx , k8sClient , namespace , definitionName , typeTraitDefinition , [ ] byte ( "" ) , ownerReference )
2021-03-10 10:44:58 +08:00
Expect ( err ) . Should ( BeNil ( ) )
} )
} )
} )