github.com/oam-dev/kubevela@v1.9.11/references/cli/dryrun_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 cli
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  
    25  	wfv1alpha1 "github.com/kubevela/workflow/api/v1alpha1"
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  	"sigs.k8s.io/yaml"
    29  
    30  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
    31  
    32  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    33  	"github.com/oam-dev/kubevela/apis/types"
    34  	common2 "github.com/oam-dev/kubevela/pkg/utils/common"
    35  )
    36  
    37  var _ = Describe("Testing dry-run", func() {
    38  
    39  	It("Testing dry-run", func() {
    40  
    41  		webservice, err := os.ReadFile("../../charts/vela-core/templates/defwithtemplate/webservice.yaml")
    42  		Expect(err).Should(BeNil())
    43  		webserviceYAML := strings.Replace(string(webservice), "{{ include \"systemDefinitionNamespace\" . }}", types.DefaultKubeVelaNS, 1)
    44  		wwd := v1beta1.ComponentDefinition{}
    45  		Expect(yaml.Unmarshal([]byte(webserviceYAML), &wwd)).Should(BeNil())
    46  		Expect(k8sClient.Create(context.TODO(), &wwd)).Should(BeNil())
    47  
    48  		scaler, err := os.ReadFile("../../charts/vela-core/templates/defwithtemplate/scaler.yaml")
    49  		Expect(err).Should(BeNil())
    50  		scalerYAML := strings.Replace(string(scaler), "{{ include \"systemDefinitionNamespace\" . }}", types.DefaultKubeVelaNS, 1)
    51  		var td v1beta1.TraitDefinition
    52  		Expect(yaml.Unmarshal([]byte(scalerYAML), &td)).Should(BeNil())
    53  		Expect(k8sClient.Create(context.TODO(), &td)).Should(BeNil())
    54  
    55  		c := common2.Args{}
    56  		c.SetConfig(cfg)
    57  		c.SetClient(k8sClient)
    58  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-1.yaml"}, OfflineMode: false}
    59  		buff, err := DryRunApplication(&opt, c, "")
    60  		Expect(err).Should(BeNil())
    61  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
    62  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
    63  		Expect(buff.String()).Should(ContainSubstring("replicas: 1"))
    64  	})
    65  
    66  	It("Testing dry-run with policy", func() {
    67  		deploy, err := os.ReadFile("../../charts/vela-core/templates/defwithtemplate/deploy.yaml")
    68  		Expect(err).Should(BeNil())
    69  		deployYAML := strings.Replace(string(deploy), "{{ include \"systemDefinitionNamespace\" . }}", types.DefaultKubeVelaNS, 1)
    70  		var wfsd v1beta1.WorkflowStepDefinition
    71  		Expect(yaml.Unmarshal([]byte(deployYAML), &wfsd)).Should(BeNil())
    72  		Expect(k8sClient.Create(context.TODO(), &wfsd)).Should(BeNil())
    73  
    74  		c := common2.Args{}
    75  		c.SetConfig(cfg)
    76  		c.SetClient(k8sClient)
    77  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-2.yaml"}, OfflineMode: false}
    78  		buff, err := DryRunApplication(&opt, c, "")
    79  		Expect(err).Should(BeNil())
    80  		Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology target-default)"))
    81  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
    82  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
    83  		Expect(buff.String()).Should(ContainSubstring("replicas: 1"))
    84  	})
    85  
    86  	It("Testing dry-run with workflow", func() {
    87  
    88  		c := common2.Args{}
    89  		c.SetConfig(cfg)
    90  		c.SetClient(k8sClient)
    91  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-3.yaml"}, OfflineMode: false}
    92  		buff, err := DryRunApplication(&opt, c, "")
    93  		Expect(err).Should(BeNil())
    94  		Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology target-default)"))
    95  		Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology target-prod)"))
    96  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
    97  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
    98  		Expect(buff.String()).Should(ContainSubstring("replicas: 1"))
    99  		Expect(buff.String()).Should(ContainSubstring("replicas: 3"))
   100  	})
   101  
   102  	It("Testing dry-run with ref workflow", func() {
   103  
   104  		policy, err := os.ReadFile("test-data/dry-run/testing-policy.yaml")
   105  		Expect(err).Should(BeNil())
   106  		var p v1alpha1.Policy
   107  		Expect(yaml.Unmarshal([]byte(policy), &p)).Should(BeNil())
   108  		Expect(k8sClient.Create(context.TODO(), &p)).Should(BeNil())
   109  
   110  		workflow, err := os.ReadFile("test-data/dry-run/testing-wf.yaml")
   111  		Expect(err).Should(BeNil())
   112  		var wf wfv1alpha1.Workflow
   113  		Expect(yaml.Unmarshal([]byte(workflow), &wf)).Should(BeNil())
   114  		Expect(k8sClient.Create(context.TODO(), &wf)).Should(BeNil())
   115  
   116  		c := common2.Args{}
   117  		c.SetConfig(cfg)
   118  		c.SetClient(k8sClient)
   119  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-4.yaml"}, OfflineMode: false}
   120  		buff, err := DryRunApplication(&opt, c, "")
   121  		Expect(err).Should(BeNil())
   122  		Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology deploy-somewhere)"))
   123  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
   124  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
   125  	})
   126  
   127  	It("Testing dry-run without application provided", func() {
   128  
   129  		c := common2.Args{}
   130  		c.SetConfig(cfg)
   131  		c.SetClient(k8sClient)
   132  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-policy.yaml"}, OfflineMode: false}
   133  		_, err := DryRunApplication(&opt, c, "")
   134  		Expect(err).ShouldNot(BeNil())
   135  		Expect(err.Error()).Should(ContainSubstring("no application provided"))
   136  
   137  	})
   138  
   139  	It("Testing dry-run with more than one applications provided", func() {
   140  
   141  		c := common2.Args{}
   142  		c.SetConfig(cfg)
   143  		c.SetClient(k8sClient)
   144  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-1.yaml", "test-data/dry-run/testing-dry-run-2.yaml"}, OfflineMode: false}
   145  		_, err := DryRunApplication(&opt, c, "")
   146  		Expect(err).ShouldNot(BeNil())
   147  		Expect(err.Error()).Should(ContainSubstring("more than one applications provided"))
   148  
   149  	})
   150  
   151  	It("Testing dry-run with more than one workflow provided", func() {
   152  
   153  		c := common2.Args{}
   154  		c.SetConfig(cfg)
   155  		c.SetClient(k8sClient)
   156  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-1.yaml", "test-data/dry-run/testing-wf.yaml", "test-data/dry-run/testing-wf.yaml"}, OfflineMode: false}
   157  		_, err := DryRunApplication(&opt, c, "")
   158  		Expect(err).ShouldNot(BeNil())
   159  		Expect(err.Error()).Should(ContainSubstring("more than one external workflow provided"))
   160  
   161  	})
   162  
   163  	It("Testing dry-run with unexpected file", func() {
   164  
   165  		c := common2.Args{}
   166  		c.SetConfig(cfg)
   167  		c.SetClient(k8sClient)
   168  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-trait.yaml"}, OfflineMode: false}
   169  		_, err := DryRunApplication(&opt, c, "")
   170  		Expect(err).ShouldNot(BeNil())
   171  		Expect(err.Error()).Should(ContainSubstring("is not application, policy or workflow"))
   172  
   173  	})
   174  
   175  	It("Testing dry-run with unexpected file", func() {
   176  
   177  		c := common2.Args{}
   178  		c.SetConfig(cfg)
   179  		c.SetClient(k8sClient)
   180  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-trait.yaml"}, OfflineMode: false}
   181  		_, err := DryRunApplication(&opt, c, "")
   182  		Expect(err).ShouldNot(BeNil())
   183  		Expect(err.Error()).Should(ContainSubstring("is not application, policy or workflow"))
   184  
   185  	})
   186  
   187  	It("Testing dry-run merging with external workflow and policy", func() {
   188  
   189  		c := common2.Args{}
   190  		c.SetConfig(cfg)
   191  		c.SetClient(k8sClient)
   192  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-5.yaml", "test-data/dry-run/testing-wf.yaml", "test-data/dry-run/testing-policy.yaml"}, OfflineMode: false, MergeStandaloneFiles: true}
   193  		buff, err := DryRunApplication(&opt, c, "")
   194  		Expect(err).Should(BeNil())
   195  		Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology deploy-somewhere)"))
   196  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
   197  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
   198  	})
   199  
   200  	It("Testing dry-run with standalone policy", func() {
   201  
   202  		c := common2.Args{}
   203  		c.SetConfig(cfg)
   204  		c.SetClient(k8sClient)
   205  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-5.yaml", "test-data/dry-run/testing-policy.yaml"}, OfflineMode: false, MergeStandaloneFiles: false}
   206  		buff, err := DryRunApplication(&opt, c, "")
   207  		Expect(err).Should(BeNil())
   208  		Expect(buff.String()).Should(ContainSubstring("WARNING: policy deploy-somewhere not referenced by application"))
   209  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
   210  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
   211  	})
   212  
   213  	It("Testing dry-run with standalone workflow", func() {
   214  
   215  		c := common2.Args{}
   216  		c.SetConfig(cfg)
   217  		c.SetClient(k8sClient)
   218  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-5.yaml", "test-data/dry-run/testing-wf.yaml"}, OfflineMode: false, MergeStandaloneFiles: false}
   219  		buff, err := DryRunApplication(&opt, c, "")
   220  		Expect(err).Should(BeNil())
   221  		Expect(buff.String()).Should(ContainSubstring("WARNING: workflow testing-wf not referenced by application"))
   222  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
   223  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
   224  	})
   225  
   226  	It("Testing dry-run offline with definition file", func() {
   227  		c := common2.Args{}
   228  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-6.yaml"}, DefinitionFile: "test-data/dry-run/definitions/testing-worker-def.yaml", OfflineMode: true}
   229  		buff, err := DryRunApplication(&opt, c, "")
   230  		Expect(err).Should(BeNil())
   231  		Expect(buff.String()).Should(ContainSubstring("# Application(testing-app)"))
   232  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
   233  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
   234  		Expect(buff.String()).Should(ContainSubstring("workload.oam.dev/type: myworker"))
   235  	})
   236  
   237  	It("Testing dry-run offline with definition directory", func() {
   238  		c := common2.Args{}
   239  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-6.yaml"}, DefinitionFile: "test-data/dry-run/definitions", OfflineMode: true}
   240  		buff, err := DryRunApplication(&opt, c, "")
   241  		Expect(err).Should(BeNil())
   242  		Expect(buff.String()).Should(ContainSubstring("# Application(testing-app)"))
   243  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
   244  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
   245  		Expect(buff.String()).Should(ContainSubstring("workload.oam.dev/type: myworker"))
   246  	})
   247  
   248  	It("Testing dry-run offline with deploy workflow step", func() {
   249  		c := common2.Args{}
   250  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-7.yaml"}, DefinitionFile: "test-data/dry-run/definitions/testing-worker-def.yaml", OfflineMode: true}
   251  		buff, err := DryRunApplication(&opt, c, "")
   252  		Expect(err).Should(BeNil())
   253  		Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology target-prod)"))
   254  		Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology target-default)"))
   255  		Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
   256  		Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
   257  		Expect(buff.String()).Should(ContainSubstring("workload.oam.dev/type: myworker"))
   258  	})
   259  
   260  	It("Testing dry-run with default application namespace", func() {
   261  		c := common2.Args{}
   262  		c.SetConfig(cfg)
   263  		c.SetClient(k8sClient)
   264  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-1.yaml"}, OfflineMode: false}
   265  		buff, err := DryRunApplication(&opt, c, "")
   266  		Expect(err).Should(BeNil())
   267  		Expect(buff.String()).Should(ContainSubstring("namespace: default"))
   268  	})
   269  
   270  	It("Testing dry-run with customized application namespace", func() {
   271  		appNamespace := "test-namespace"
   272  		c := common2.Args{}
   273  		c.SetConfig(cfg)
   274  		c.SetClient(k8sClient)
   275  		opt := DryRunCmdOptions{ApplicationFiles: []string{"test-data/dry-run/testing-dry-run-1.yaml"}, OfflineMode: false}
   276  		buff, err := DryRunApplication(&opt, c, appNamespace)
   277  		Expect(err).Should(BeNil())
   278  		Expect(buff.String()).Should(ContainSubstring(fmt.Sprintf("namespace: %s", appNamespace)))
   279  	})
   280  
   281  })