github.com/kubevela/workflow@v0.6.0/e2e/definition_test.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package e2e
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"time"
    23  
    24  	. "github.com/onsi/ginkgo"
    25  	. "github.com/onsi/gomega"
    26  	corev1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/klog/v2"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  	"sigs.k8s.io/yaml"
    31  
    32  	"github.com/kubevela/workflow/api/v1alpha1"
    33  	"github.com/kubevela/workflow/pkg/utils"
    34  )
    35  
    36  var _ = Describe("Test the workflow run with the built-in definitions", func() {
    37  	ctx := context.Background()
    38  
    39  	var namespace string
    40  	var ns corev1.Namespace
    41  
    42  	BeforeEach(func() {
    43  		namespace = "wr-e2e-test"
    44  		ns = corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}
    45  
    46  		Eventually(func() error {
    47  			return k8sClient.Create(ctx, &ns)
    48  		}, time.Second*3, time.Microsecond*300).Should(SatisfyAny(BeNil(), &utils.AlreadyExistMatcher{}))
    49  	})
    50  
    51  	It("Test the workflow with config definition", func() {
    52  		wr := applyWorkflowRunFromYAML(ctx, "./test-data/config-workflow-run.yaml", namespace)
    53  		Eventually(
    54  			func() v1alpha1.WorkflowRunPhase {
    55  				var getWorkflow v1alpha1.WorkflowRun
    56  				if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: wr.Name}, &getWorkflow); err != nil {
    57  					klog.Errorf("fail to query the app %s", err.Error())
    58  				}
    59  				klog.Infof("the workflow run status is %s (%+v)", getWorkflow.Status.Phase, getWorkflow.Status.Steps)
    60  				return getWorkflow.Status.Phase
    61  			},
    62  			time.Second*30, time.Second*2).Should(Equal(v1alpha1.WorkflowStateSucceeded))
    63  	})
    64  
    65  	It("Test the workflow with message definition", func() {
    66  		wr := applyWorkflowRunFromYAML(ctx, "./test-data/message-workflow-run.yaml", namespace)
    67  		Eventually(
    68  			func() v1alpha1.WorkflowRunPhase {
    69  				var getWorkflow v1alpha1.WorkflowRun
    70  				if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: wr.Name}, &getWorkflow); err != nil {
    71  					klog.Errorf("fail to query the app %s", err.Error())
    72  				}
    73  				klog.Infof("the workflow run status is %s (%+v)", getWorkflow.Status.Phase, getWorkflow.Status.Steps)
    74  				return getWorkflow.Status.Phase
    75  			},
    76  			time.Second*30, time.Second*2).Should(Equal(v1alpha1.WorkflowStateSucceeded))
    77  	})
    78  
    79  	AfterEach(func() {
    80  		By("Clean up resources after a test")
    81  		k8sClient.DeleteAllOf(ctx, &v1alpha1.WorkflowRun{}, client.InNamespace(namespace))
    82  	})
    83  })
    84  
    85  func applyWorkflowRunFromYAML(ctx context.Context, path, ns string) v1alpha1.WorkflowRun {
    86  	content, err := os.ReadFile(path)
    87  	Expect(err).Should(BeNil())
    88  	var workflowRun v1alpha1.WorkflowRun
    89  	Expect(yaml.Unmarshal(content, &workflowRun)).Should(BeNil())
    90  	workflowRun.Namespace = ns
    91  	Expect(k8sClient.Create(ctx, &workflowRun)).Should(BeNil())
    92  	return workflowRun
    93  }