github.com/oam-dev/kubevela@v1.9.11/pkg/workflow/providers/oam/apply_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 oam 18 19 import ( 20 "context" 21 "strings" 22 "testing" 23 24 "github.com/pkg/errors" 25 "github.com/stretchr/testify/require" 26 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 27 "k8s.io/apimachinery/pkg/runtime" 28 29 "github.com/kubevela/workflow/pkg/cue/model/value" 30 "github.com/kubevela/workflow/pkg/mock" 31 32 "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 33 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 34 ) 35 36 func TestParser(t *testing.T) { 37 r := require.New(t) 38 p := &provider{ 39 apply: simpleComponentApplyForTest, 40 } 41 act := &mock.Action{} 42 v, err := value.NewValue("", nil, "") 43 r.NoError(err) 44 err = p.ApplyComponent(nil, nil, v, act) 45 r.Equal(err.Error(), "failed to lookup value: var(path=value) not exist") 46 v.FillObject(map[string]interface{}{}, "value") 47 err = p.ApplyComponent(nil, nil, v, act) 48 r.NoError(err) 49 output, err := v.LookupValue("output") 50 r.NoError(err) 51 outStr, err := output.String() 52 r.NoError(err) 53 r.Equal(outStr, `apiVersion: "v1" 54 kind: "Pod" 55 metadata: { 56 labels: { 57 app: "web" 58 } 59 name: "rss-site" 60 } 61 `) 62 63 outputs, err := v.LookupValue("outputs") 64 r.NoError(err) 65 outsStr, err := outputs.String() 66 r.NoError(err) 67 r.Equal(outsStr, `service: { 68 apiVersion: "v1" 69 kind: "Service" 70 metadata: { 71 labels: { 72 "trait.oam.dev/resource": "service" 73 } 74 name: "service" 75 } 76 } 77 `) 78 79 r.Equal(act.Phase, "Wait") 80 testHealthy = true 81 act = &mock.Action{} 82 _, err = value.NewValue("", nil, "") 83 r.NoError(err) 84 r.Equal(act.Phase, "") 85 } 86 87 func TestLoadComponent(t *testing.T) { 88 r := require.New(t) 89 p := &provider{ 90 app: &v1beta1.Application{ 91 Spec: v1beta1.ApplicationSpec{ 92 Components: []common.ApplicationComponent{ 93 { 94 Name: "c1", 95 Type: "web", 96 Properties: &runtime.RawExtension{Raw: []byte(`{"image": "busybox"}`)}, 97 }, 98 }, 99 }, 100 }, 101 } 102 v, err := value.NewValue(``, nil, "") 103 r.NoError(err) 104 err = p.LoadComponent(nil, nil, v, nil) 105 r.NoError(err) 106 s, err := v.String() 107 r.NoError(err) 108 r.Equal(s, `value: { 109 c1: { 110 name: *"c1" | _ 111 type: *"web" | _ 112 properties: { 113 image: *"busybox" | _ 114 } 115 } 116 } 117 `) 118 overrideApp := `app: { 119 apiVersion: "core.oam.dev/v1beta1" 120 kind: "Application" 121 metadata: { 122 name: "test" 123 namespace: "default" 124 } 125 spec: { 126 components: [{ 127 name: "c2" 128 type: "web" 129 properties: { 130 image: "busybox" 131 } 132 }] 133 } 134 } 135 ` 136 overrideValue, err := value.NewValue(overrideApp, nil, "") 137 r.NoError(err) 138 err = p.LoadComponent(nil, nil, overrideValue, nil) 139 r.NoError(err) 140 _, err = overrideValue.LookupValue("value", "c2") 141 r.NoError(err) 142 } 143 144 func TestLoadComponentInOrder(t *testing.T) { 145 r := require.New(t) 146 p := &provider{ 147 app: &v1beta1.Application{ 148 Spec: v1beta1.ApplicationSpec{ 149 Components: []common.ApplicationComponent{ 150 { 151 Name: "c1", 152 Type: "web", 153 Properties: &runtime.RawExtension{Raw: []byte(`{"image": "busybox"}`)}, 154 }, 155 { 156 Name: "c2", 157 Type: "web2", 158 Properties: &runtime.RawExtension{Raw: []byte(`{"image": "busybox"}`)}, 159 }, 160 }, 161 }, 162 }, 163 } 164 v, err := value.NewValue(``, nil, "") 165 r.NoError(err) 166 err = p.LoadComponentInOrder(nil, nil, v, nil) 167 r.NoError(err) 168 s, err := v.String() 169 r.NoError(err) 170 r.Equal(s, `value: [{ 171 name: "c1" 172 type: "web" 173 properties: { 174 image: "busybox" 175 } 176 }, { 177 name: "c2" 178 type: "web2" 179 properties: { 180 image: "busybox" 181 } 182 }] 183 `) 184 } 185 186 var testHealthy bool 187 188 func simpleComponentApplyForTest(_ context.Context, comp common.ApplicationComponent, _ *value.Value, _, _ string) (*unstructured.Unstructured, []*unstructured.Unstructured, bool, error) { 189 workload := new(unstructured.Unstructured) 190 workload.UnmarshalJSON([]byte(`{ 191 "apiVersion": "v1", 192 "kind": "Pod", 193 "metadata": { 194 "name": "rss-site", 195 "labels": { 196 "app": "web" 197 } 198 } 199 }`)) 200 if comp.Name != "" { 201 workload.SetName(comp.Name) 202 if strings.Contains(comp.Name, "error") { 203 return nil, nil, false, errors.Errorf("bad component") 204 } 205 } 206 trait := new(unstructured.Unstructured) 207 trait.UnmarshalJSON([]byte(`{ 208 "apiVersion": "v1", 209 "kind": "Service", 210 "metadata": { 211 "name": "service", 212 "labels": { 213 "trait.oam.dev/resource": "service" 214 } 215 } 216 }`)) 217 if comp.Name != "" { 218 trait.SetName(comp.Name) 219 } 220 traits := []*unstructured.Unstructured{trait} 221 return workload, traits, testHealthy, nil 222 }