github.com/kubevela/workflow@v0.6.0/pkg/webhook/v1alpha1/workflowrun/validating_handler_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 workflowrun
    18  
    19  import (
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  	admissionv1 "k8s.io/api/admission/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    26  )
    27  
    28  var _ = Describe("Test WorkflowRun Validator", func() {
    29  	BeforeEach(func() {
    30  		Expect(handler.InjectClient(k8sClient)).Should(BeNil())
    31  		Expect(handler.InjectDecoder(decoder)).Should(BeNil())
    32  	})
    33  
    34  	It("Test WorkflowRun Validator [bad request]", func() {
    35  		req := admission.Request{
    36  			AdmissionRequest: admissionv1.AdmissionRequest{
    37  				Operation: admissionv1.Create,
    38  				Resource:  metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha1", Resource: "workflowruns"},
    39  				Object:    runtime.RawExtension{Raw: []byte("bad request")},
    40  			},
    41  		}
    42  		resp := handler.Handle(ctx, req)
    43  		Expect(resp.Allowed).Should(BeFalse())
    44  	})
    45  
    46  	It("Test WorkflowRun Validator [Allow]", func() {
    47  		req := admission.Request{
    48  			AdmissionRequest: admissionv1.AdmissionRequest{
    49  				Operation: admissionv1.Create,
    50  				Resource:  metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha1", Resource: "workflowruns"},
    51  				Object: runtime.RawExtension{
    52  					Raw: []byte(`{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkflowRun","metadata":{"name":"wr-sample"},"spec":{"workflowSpec":{"steps":[{"name":"step1","properties":{"duration":"3s"},"type":"suspend"}]}}}`),
    53  				},
    54  			},
    55  		}
    56  		resp := handler.Handle(ctx, req)
    57  		Expect(resp.Allowed).Should(BeTrue())
    58  	})
    59  
    60  	It("Test WorkflowRun Validator [Error]", func() {
    61  		req := admission.Request{
    62  			AdmissionRequest: admissionv1.AdmissionRequest{
    63  				Operation: admissionv1.Create,
    64  				Resource:  metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha1", Resource: "workflowruns"},
    65  				Object: runtime.RawExtension{
    66  					Raw: []byte(
    67  						`{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkflowRun","metadata":{"name":"wr-sample"},"spec":{"workflowSpec":{"steps":[{"properties":{"duration":"3s"},"type":"suspend"}]}}}`),
    68  				},
    69  			},
    70  		}
    71  		resp := handler.Handle(ctx, req)
    72  		Expect(resp.Allowed).Should(BeFalse())
    73  	})
    74  
    75  	It("Test WorkflowRun Validator workflow step name duplicate [error]", func() {
    76  		By("test duplicated step name in workflow")
    77  		req := admission.Request{
    78  			AdmissionRequest: admissionv1.AdmissionRequest{
    79  				Operation: admissionv1.Create,
    80  				Resource:  metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha1", Resource: "workflowruns"},
    81  				Object: runtime.RawExtension{
    82  					Raw: []byte(`{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkflowRun","metadata":{"name":"wr-sample"},"spec":{"workflowSpec":{"steps":[{"name":"step1","properties":{"duration":"3s"},"type":"suspend"},{"name":"step1","properties":{"duration":"3s"},"type":"suspend"}]}}}`),
    83  				},
    84  			},
    85  		}
    86  		resp := handler.Handle(ctx, req)
    87  		Expect(resp.Allowed).Should(BeFalse())
    88  
    89  		By("test duplicated sub step name in workflow")
    90  		req = admission.Request{
    91  			AdmissionRequest: admissionv1.AdmissionRequest{
    92  				Operation: admissionv1.Create,
    93  				Resource:  metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha1", Resource: "workflowruns"},
    94  				Object: runtime.RawExtension{
    95  					Raw: []byte(`{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkflowRun","metadata":{"name":"wr-sample"},"spec":{"workflowSpec":{"steps":[{"name":"step1","subSteps":[{"name":"sub1","type":"suspend"},{"name":"sub1","type":"suspend"}],"type":"step-group"}]}}}`),
    96  				},
    97  			},
    98  		}
    99  		resp = handler.Handle(ctx, req)
   100  		Expect(resp.Allowed).Should(BeFalse())
   101  
   102  		By("test duplicated sub and parent step name in workflow")
   103  		req = admission.Request{
   104  			AdmissionRequest: admissionv1.AdmissionRequest{
   105  				Operation: admissionv1.Create,
   106  				Resource:  metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha1", Resource: "workflowruns"},
   107  				Object: runtime.RawExtension{
   108  					Raw: []byte(`{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkflowRun","metadata":{"name":"wr-sample"},"spec":{"workflowSpec":{"steps":[{"name":"step1","subSteps":[{"name":"step1","type":"suspend"},{"name":"sub1","type":"suspend"}],"type":"step-group"}]}}}`),
   109  				},
   110  			},
   111  		}
   112  		resp = handler.Handle(ctx, req)
   113  		Expect(resp.Allowed).Should(BeFalse())
   114  	})
   115  
   116  	It("Test WorkflowRun Validator workflow step invalid timeout [error]", func() {
   117  		req := admission.Request{
   118  			AdmissionRequest: admissionv1.AdmissionRequest{
   119  				Operation: admissionv1.Create,
   120  				Resource:  metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha1", Resource: "workflowruns"},
   121  				Object: runtime.RawExtension{
   122  					Raw: []byte(`{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkflowRun","metadata":{"name":"wr-sample"},"spec":{"workflowSpec":{"steps":[{"name":"step1","properties":{"duration":"3s"},"timeout":"test","type":"suspend"}]}}}`),
   123  				},
   124  			},
   125  		}
   126  		resp := handler.Handle(ctx, req)
   127  		Expect(resp.Allowed).Should(BeFalse())
   128  	})
   129  
   130  	It("Test WorkflowRun Validator workflow step valid timeout [allow]", func() {
   131  		req := admission.Request{
   132  			AdmissionRequest: admissionv1.AdmissionRequest{
   133  				Operation: admissionv1.Create,
   134  				Resource:  metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha1", Resource: "workflowruns"},
   135  				Object: runtime.RawExtension{
   136  					Raw: []byte(`{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkflowRun","metadata":{"name":"wr-sample"},"spec":{"workflowSpec":{"steps":[{"name":"step1","properties":{"duration":"3s"},"timeout":"5s","type":"suspend"}]}}}`),
   137  				},
   138  			},
   139  		}
   140  		resp := handler.Handle(ctx, req)
   141  		Expect(resp.Allowed).Should(BeTrue())
   142  	})
   143  
   144  })