github.com/oam-dev/kubevela@v1.9.11/references/cli/utils_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 "strings" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 25 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 27 "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 28 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 29 ) 30 31 func TestFormatApplicationString(t *testing.T) { 32 var ( 33 str string 34 err error 35 ) 36 37 app := &v1beta1.Application{} 38 app.SetGroupVersionKind(v1beta1.ApplicationKindVersionKind) 39 // This should not be preset in the formatted string 40 app.ManagedFields = []v1.ManagedFieldsEntry{ 41 { 42 Manager: "", 43 Operation: "", 44 APIVersion: "", 45 Time: nil, 46 FieldsType: "", 47 FieldsV1: nil, 48 Subresource: "", 49 }, 50 } 51 app.SetName("app-name") 52 53 _, err = formatApplicationString("", app) 54 assert.ErrorContains(t, err, "no format", "no format provided, should error out") 55 56 _, err = formatApplicationString("invalid", app) 57 assert.ErrorContains(t, err, "not supported", "invalid format provided, should error out") 58 59 str, err = formatApplicationString("yaml", app) 60 assert.NoError(t, err) 61 assert.Equal(t, true, strings.Contains(str, `apiVersion: core.oam.dev/v1beta1 62 kind: Application 63 metadata: 64 creationTimestamp: null 65 name: app-name 66 spec: 67 components: null 68 status: {} 69 `), "formatted yaml is not correct") 70 71 str, err = formatApplicationString("json", app) 72 assert.NoError(t, err) 73 assert.Equal(t, true, strings.Contains(str, `{ 74 "kind": "Application", 75 "apiVersion": "core.oam.dev/v1beta1", 76 "metadata": { 77 "name": "app-name", 78 "creationTimestamp": null 79 }, 80 "spec": { 81 "components": null 82 }, 83 "status": {} 84 }`), "formatted json is not correct") 85 86 _, err = formatApplicationString("jsonpath", app) 87 assert.ErrorContains(t, err, "jsonpath template", "no jsonpath template provided, should not pass") 88 89 str, err = formatApplicationString("jsonpath={.apiVersion}", app) 90 assert.NoError(t, err) 91 assert.Equal(t, str, "core.oam.dev/v1beta1") 92 93 str, err = formatApplicationString("jsonpath={.spec.components[?(@.name==\"test-server\")].type}", &v1beta1.Application{ 94 ObjectMeta: v1.ObjectMeta{ 95 Name: "test-app", 96 Namespace: "dev", 97 }, 98 Spec: v1beta1.ApplicationSpec{ 99 Components: []common.ApplicationComponent{ 100 { 101 Name: "test-server", 102 Type: "webservice", 103 }, 104 }, 105 }, 106 }) 107 assert.NoError(t, err) 108 assert.Equal(t, str, "webservice") 109 } 110 111 func TestConvertApplicationRevisionTo(t *testing.T) { 112 113 type Exp struct { 114 out string 115 err string 116 } 117 118 cases := map[string]struct { 119 format string 120 apprev *v1beta1.ApplicationRevision 121 exp Exp 122 }{ 123 "no format": {format: "", apprev: &v1beta1.ApplicationRevision{}, exp: Exp{out: "", err: "no format provided"}}, 124 "no support format": {format: "jsonnet", apprev: &v1beta1.ApplicationRevision{}, exp: Exp{out: "", err: "jsonnet is not supported"}}, 125 "yaml": {format: "yaml", apprev: &v1beta1.ApplicationRevision{ 126 TypeMeta: v1.TypeMeta{ 127 Kind: "ApplicationRevision", 128 APIVersion: "core.oam.dev/v1beta1", 129 }, 130 ObjectMeta: v1.ObjectMeta{ 131 Name: "test-apprev", 132 Namespace: "dev", 133 }, 134 Spec: v1beta1.ApplicationRevisionSpec{ 135 ApplicationRevisionCompressibleFields: v1beta1.ApplicationRevisionCompressibleFields{ 136 Application: v1beta1.Application{ 137 ObjectMeta: v1.ObjectMeta{ 138 Name: "test-app", 139 Namespace: "dev", 140 }, 141 }, 142 }, 143 }, 144 }, exp: Exp{out: `apiVersion: core.oam.dev/v1beta1 145 kind: ApplicationRevision 146 metadata: 147 creationTimestamp: null 148 name: test-apprev 149 namespace: dev 150 spec: 151 application: 152 metadata: 153 creationTimestamp: null 154 name: test-app 155 namespace: dev 156 spec: 157 components: null`, err: ""}}, 158 "json": {format: "json", apprev: &v1beta1.ApplicationRevision{ 159 TypeMeta: v1.TypeMeta{ 160 Kind: "ApplicationRevision", 161 APIVersion: "core.oam.dev/v1beta1", 162 }, 163 ObjectMeta: v1.ObjectMeta{ 164 Name: "test-apprev", 165 Namespace: "dev", 166 }, 167 Spec: v1beta1.ApplicationRevisionSpec{ 168 ApplicationRevisionCompressibleFields: v1beta1.ApplicationRevisionCompressibleFields{ 169 Application: v1beta1.Application{ 170 ObjectMeta: v1.ObjectMeta{ 171 Name: "test-app", 172 Namespace: "dev", 173 }, 174 }, 175 }, 176 }, 177 }, exp: Exp{out: `{ 178 "kind": "ApplicationRevision", 179 "apiVersion": "core.oam.dev/v1beta1", 180 "metadata": { 181 "name": "test-apprev", 182 "namespace": "dev", 183 "creationTimestamp": null 184 }, 185 "spec": { 186 "application": { 187 "metadata": { 188 "name": "test-app", 189 "namespace": "dev", 190 "creationTimestamp": null 191 }, 192 "spec": { 193 "components": null`, err: ""}}, 194 "jsonpath": {format: "jsonpath={.apiVersion}", apprev: &v1beta1.ApplicationRevision{ 195 TypeMeta: v1.TypeMeta{ 196 Kind: "ApplicationRevision", 197 APIVersion: "core.oam.dev/v1beta1", 198 }, 199 ObjectMeta: v1.ObjectMeta{ 200 Name: "test-apprev", 201 Namespace: "dev", 202 }, 203 Spec: v1beta1.ApplicationRevisionSpec{ 204 ApplicationRevisionCompressibleFields: v1beta1.ApplicationRevisionCompressibleFields{ 205 Application: v1beta1.Application{ 206 ObjectMeta: v1.ObjectMeta{ 207 Name: "test-app", 208 Namespace: "dev", 209 }, 210 }, 211 }, 212 }, 213 }, exp: Exp{out: "core.oam.dev/v1beta1", err: ""}}, 214 "jsonpath filter expression": {format: "jsonpath={.spec.application.spec.components[?(@.name==\"test-server\")].type}", apprev: &v1beta1.ApplicationRevision{ 215 TypeMeta: v1.TypeMeta{ 216 Kind: "ApplicationRevision", 217 APIVersion: "core.oam.dev/v1beta1", 218 }, 219 ObjectMeta: v1.ObjectMeta{ 220 Name: "test-apprev", 221 Namespace: "dev", 222 }, 223 Spec: v1beta1.ApplicationRevisionSpec{ 224 ApplicationRevisionCompressibleFields: v1beta1.ApplicationRevisionCompressibleFields{ 225 Application: v1beta1.Application{ 226 ObjectMeta: v1.ObjectMeta{ 227 Name: "test-app", 228 Namespace: "dev", 229 }, 230 Spec: v1beta1.ApplicationSpec{ 231 Components: []common.ApplicationComponent{ 232 { 233 Name: "test-server", 234 Type: "webservice", 235 }, 236 }, 237 }, 238 }, 239 }, 240 }, 241 }, exp: Exp{out: "webservice", err: ""}}, 242 "jsonpath with error": {format: "jsonpath", apprev: &v1beta1.ApplicationRevision{ 243 TypeMeta: v1.TypeMeta{ 244 Kind: "ApplicationRevision", 245 APIVersion: "core.oam.dev/v1beta1", 246 }, 247 ObjectMeta: v1.ObjectMeta{ 248 Name: "test-apprev", 249 Namespace: "dev1", 250 }, 251 Spec: v1beta1.ApplicationRevisionSpec{ 252 ApplicationRevisionCompressibleFields: v1beta1.ApplicationRevisionCompressibleFields{ 253 Application: v1beta1.Application{ 254 ObjectMeta: v1.ObjectMeta{ 255 Name: "test-app", 256 Namespace: "dev1", 257 }, 258 }, 259 }, 260 }, 261 }, exp: Exp{out: "", err: "jsonpath template format specified but no template given"}}, 262 } 263 264 for name, tc := range cases { 265 t.Run(name, func(t *testing.T) { 266 out, err := convertApplicationRevisionTo(tc.format, tc.apprev) 267 if err != nil { 268 assert.Equal(t, tc.exp.err, err.Error()) 269 } 270 assert.Contains(t, out, tc.exp.out) 271 }) 272 } 273 }