github.com/kubevela/workflow@v0.6.0/pkg/generator/generator_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 generator
    18  
    19  import (
    20  	"context"
    21  	"strconv"
    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  
    29  	monitorContext "github.com/kubevela/pkg/monitor/context"
    30  
    31  	"github.com/kubevela/workflow/api/v1alpha1"
    32  	"github.com/kubevela/workflow/pkg/types"
    33  )
    34  
    35  var _ = Describe("Test workflow step runner generator", func() {
    36  	var namespaceName string
    37  	var ns corev1.Namespace
    38  	var ctx context.Context
    39  
    40  	BeforeEach(func() {
    41  		namespaceName = "generate-test-" + strconv.Itoa(time.Now().Second()) + "-" + strconv.Itoa(time.Now().Nanosecond())
    42  		ctx = context.TODO()
    43  		ns = corev1.Namespace{
    44  			ObjectMeta: metav1.ObjectMeta{
    45  				Name: namespaceName,
    46  			},
    47  		}
    48  		By("Create the Namespace for test")
    49  		Expect(k8sClient.Create(ctx, &ns)).Should(Succeed())
    50  	})
    51  
    52  	AfterEach(func() {
    53  		By("[TEST] Clean up resources after an integration test")
    54  		Expect(k8sClient.Delete(context.TODO(), &ns)).Should(Succeed())
    55  	})
    56  
    57  	It("Test generate workflow step runners", func() {
    58  		wr := &v1alpha1.WorkflowRun{
    59  			TypeMeta: metav1.TypeMeta{
    60  				Kind:       "WorkflowRun",
    61  				APIVersion: "core.oam.dev/v1alpha1",
    62  			},
    63  			ObjectMeta: metav1.ObjectMeta{
    64  				Name:      "wr",
    65  				Namespace: namespaceName,
    66  			},
    67  			Spec: v1alpha1.WorkflowRunSpec{
    68  				WorkflowSpec: &v1alpha1.WorkflowSpec{
    69  					Steps: []v1alpha1.WorkflowStep{
    70  						{
    71  							WorkflowStepBase: v1alpha1.WorkflowStepBase{
    72  								Name: "step-1",
    73  								Type: "suspend",
    74  								Inputs: v1alpha1.StepInputs{
    75  									{
    76  										From:         "test",
    77  										ParameterKey: "test",
    78  									},
    79  								},
    80  							},
    81  						},
    82  					},
    83  				},
    84  			},
    85  		}
    86  		instance, err := GenerateWorkflowInstance(ctx, k8sClient, wr)
    87  		Expect(err).Should(BeNil())
    88  		ctx := monitorContext.NewTraceContext(ctx, "test-wr")
    89  		runners, err := GenerateRunners(ctx, instance, types.StepGeneratorOptions{Client: k8sClient})
    90  		Expect(err).Should(BeNil())
    91  		Expect(len(runners)).Should(BeEquivalentTo(1))
    92  		Expect(runners[0].Name()).Should(BeEquivalentTo("step-1"))
    93  	})
    94  
    95  	It("Test generate workflow step runners with sub steps", func() {
    96  		wr := &v1alpha1.WorkflowRun{
    97  			TypeMeta: metav1.TypeMeta{
    98  				Kind:       "WorkflowRun",
    99  				APIVersion: "core.oam.dev/v1alpha1",
   100  			},
   101  			ObjectMeta: metav1.ObjectMeta{
   102  				Name:      "wf-with-sub-steps",
   103  				Namespace: namespaceName,
   104  			},
   105  			Spec: v1alpha1.WorkflowRunSpec{
   106  				WorkflowSpec: &v1alpha1.WorkflowSpec{
   107  					Steps: []v1alpha1.WorkflowStep{
   108  						{
   109  							WorkflowStepBase: v1alpha1.WorkflowStepBase{
   110  								Name: "step-1",
   111  								Type: "step-group",
   112  							},
   113  							SubSteps: []v1alpha1.WorkflowStepBase{
   114  								{
   115  									Name: "step-1-1",
   116  									Type: "suspend",
   117  								},
   118  								{
   119  									Name: "step-1-2",
   120  									Type: "suspend",
   121  								},
   122  							},
   123  						},
   124  					},
   125  				},
   126  			},
   127  		}
   128  		ctx := monitorContext.NewTraceContext(ctx, "test-wr-sub")
   129  		instance, err := GenerateWorkflowInstance(ctx, k8sClient, wr)
   130  		Expect(err).Should(BeNil())
   131  		runners, err := GenerateRunners(ctx, instance, types.StepGeneratorOptions{Client: k8sClient})
   132  		Expect(err).Should(BeNil())
   133  		Expect(len(runners)).Should(BeEquivalentTo(1))
   134  		Expect(runners[0].Name()).Should(BeEquivalentTo("step-1"))
   135  	})
   136  })