github.com/oam-dev/kubevela@v1.9.11/pkg/appfile/dryrun/dryrun_test.go (about) 1 /* 2 Copyright 2021 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 dryrun 18 19 import ( 20 "bytes" 21 "context" 22 "encoding/json" 23 "os" 24 "strings" 25 26 "github.com/oam-dev/kubevela/apis/types" 27 28 "github.com/google/go-cmp/cmp" 29 . "github.com/onsi/ginkgo/v2" 30 . "github.com/onsi/gomega" 31 "sigs.k8s.io/yaml" 32 33 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 34 ) 35 36 var _ = Describe("Test DryRun", func() { 37 It("Test DryRun", func() { 38 appYAML := readDataFromFile("./testdata/dryrun-app.yaml") 39 By("Prepare test data") 40 41 app := &v1beta1.Application{} 42 b, err := yaml.YAMLToJSON([]byte(appYAML)) 43 Expect(err).Should(BeNil()) 44 err = json.Unmarshal(b, app) 45 Expect(err).Should(BeNil()) 46 47 By("Validate App With Empty Namespace") 48 err = dryrunOpt.ValidateApp(context.Background(), "./testdata/dryrun-app.yaml") 49 Expect(err).Should(BeNil()) 50 51 By("Execute DryRun") 52 comps, _, err := dryrunOpt.ExecuteDryRun(context.Background(), app) 53 Expect(err).Should(BeNil()) 54 55 expectCompYAML := readDataFromFile("./testdata/dryrun-exp-comp.yaml") 56 By("Verify generated Comp") 57 Expect(comps).ShouldNot(BeEmpty()) 58 var expC = types.ComponentManifest{} 59 err = yaml.Unmarshal([]byte(expectCompYAML), &expC) 60 Expect(err).Should(BeNil()) 61 diff := cmp.Diff(&expC, comps[0]) 62 Expect(diff).Should(BeEmpty()) 63 }) 64 }) 65 66 var _ = Describe("Test dry run with policies", func() { 67 It("Test dry run with override policy", func() { 68 69 webservice, err := os.ReadFile("../../../charts/vela-core/templates/defwithtemplate/webservice.yaml") 70 Expect(err).Should(BeNil()) 71 webserviceYAML := strings.Replace(string(webservice), "{{ include \"systemDefinitionNamespace\" . }}", types.DefaultKubeVelaNS, 1) 72 wwd := v1beta1.ComponentDefinition{} 73 Expect(yaml.Unmarshal([]byte(webserviceYAML), &wwd)).Should(BeNil()) 74 Expect(k8sClient.Create(context.TODO(), &wwd)).Should(BeNil()) 75 76 scaler, err := os.ReadFile("../../../charts/vela-core/templates/defwithtemplate/scaler.yaml") 77 Expect(err).Should(BeNil()) 78 scalerYAML := strings.Replace(string(scaler), "{{ include \"systemDefinitionNamespace\" . }}", types.DefaultKubeVelaNS, 1) 79 var td v1beta1.TraitDefinition 80 Expect(yaml.Unmarshal([]byte(scalerYAML), &td)).Should(BeNil()) 81 Expect(k8sClient.Create(context.TODO(), &td)).Should(BeNil()) 82 83 appYAML := readDataFromFile("./testdata/testing-dry-run-1.yaml") 84 app := &v1beta1.Application{} 85 Expect(yaml.Unmarshal([]byte(appYAML), &app)).Should(BeNil()) 86 87 var buff = bytes.Buffer{} 88 err = dryrunOpt.ExecuteDryRunWithPolicies(context.TODO(), app, &buff) 89 Expect(err).Should(BeNil()) 90 Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology target-default)")) 91 Expect(buff.String()).Should(ContainSubstring("# Application(testing-app with topology target-prod)")) 92 Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun")) 93 Expect(buff.String()).Should(ContainSubstring("kind: Deployment")) 94 Expect(buff.String()).Should(ContainSubstring("replicas: 1")) 95 Expect(buff.String()).Should(ContainSubstring("replicas: 3")) 96 Expect(buff.String()).Should(ContainSubstring("kind: Service")) 97 }) 98 99 It("Test dry run only with override policy", func() { 100 101 appYAML := readDataFromFile("./testdata/testing-dry-run-2.yaml") 102 app := &v1beta1.Application{} 103 Expect(yaml.Unmarshal([]byte(appYAML), &app)).Should(BeNil()) 104 105 var buff = bytes.Buffer{} 106 err := dryrunOpt.ExecuteDryRunWithPolicies(context.TODO(), app, &buff) 107 Expect(err).Should(BeNil()) 108 Expect(buff.String()).Should(ContainSubstring("# Application(testing-app only with override policies)")) 109 Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun")) 110 Expect(buff.String()).Should(ContainSubstring("kind: Deployment")) 111 Expect(buff.String()).Should(ContainSubstring("replicas: 3")) 112 Expect(buff.String()).Should(ContainSubstring("kind: Service")) 113 }) 114 115 It("Test dry run without deploy workflow", func() { 116 117 appYAML := readDataFromFile("./testdata/testing-dry-run-3.yaml") 118 app := &v1beta1.Application{} 119 Expect(yaml.Unmarshal([]byte(appYAML), &app)).Should(BeNil()) 120 121 var buff = bytes.Buffer{} 122 err := dryrunOpt.ExecuteDryRunWithPolicies(context.TODO(), app, &buff) 123 Expect(err).Should(BeNil()) 124 Expect(buff.String()).Should(ContainSubstring("# Application(testing-app)")) 125 Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun")) 126 Expect(buff.String()).Should(ContainSubstring("kind: Deployment")) 127 }) 128 129 It("Test dry run without custom policy", func() { 130 131 topo, err := os.ReadFile("./testdata/pd-mypolicy.yaml") 132 Expect(err).Should(BeNil()) 133 var pd v1beta1.PolicyDefinition 134 Expect(yaml.Unmarshal([]byte(topo), &pd)).Should(BeNil()) 135 Expect(k8sClient.Create(context.TODO(), &pd)).Should(BeNil()) 136 137 appYAML := readDataFromFile("./testdata/testing-dry-run-4.yaml") 138 app := &v1beta1.Application{} 139 Expect(yaml.Unmarshal([]byte(appYAML), &app)).Should(BeNil()) 140 141 var buff = bytes.Buffer{} 142 err = dryrunOpt.ExecuteDryRunWithPolicies(context.TODO(), app, &buff) 143 Expect(err).Should(BeNil()) 144 Expect(buff.String()).Should(ContainSubstring("# Application(testing-app) -- Component(testing-dryrun)")) 145 Expect(buff.String()).Should(ContainSubstring("# Application(testing-app) -- Policy(mypolicy)")) 146 Expect(buff.String()).Should(ContainSubstring("name: my-policy")) 147 }) 148 149 It("Test dry run with trait", func() { 150 151 nocalhost, err := os.ReadFile("../../../charts/vela-core/templates/defwithtemplate/nocalhost.yaml") 152 Expect(err).Should(BeNil()) 153 nocalhostYAML := strings.Replace(string(nocalhost), "{{ include \"systemDefinitionNamespace\" . }}", types.DefaultKubeVelaNS, 1) 154 var td v1beta1.TraitDefinition 155 Expect(yaml.Unmarshal([]byte(nocalhostYAML), &td)).Should(BeNil()) 156 Expect(k8sClient.Create(context.TODO(), &td)).Should(BeNil()) 157 158 appYAML := readDataFromFile("./testdata/testing-dry-run-5.yaml") 159 app := &v1beta1.Application{} 160 Expect(yaml.Unmarshal([]byte(appYAML), &app)).Should(BeNil()) 161 162 var buff = bytes.Buffer{} 163 err = dryrunOpt.ExecuteDryRunWithPolicies(context.TODO(), app, &buff) 164 Expect(err).Should(BeNil()) 165 Expect(buff.String()).Should(ContainSubstring("# Application(testing-app) -- Component(testing-dryrun)")) 166 Expect(buff.String()).Should(ContainSubstring("## From the trait nocalhost")) 167 Expect(buff.String()).Should(ContainSubstring("trait.oam.dev/type: nocalhost")) 168 }) 169 })