github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/build/layerutils/utils_test.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package layerutils 16 17 import ( 18 "reflect" 19 "testing" 20 ) 21 22 func Test_decodeImages(t *testing.T) { 23 body := ` 24 image: cn-app-integration:v1.0.0 25 image: registry.cn-shanghai.aliyuncs.com/cnip/cn-app-integration:v1.0.0 26 imagePullPolicy: Always 27 image: cn-app-integration:v1.0.0 28 # image: cn-app-integration:v1.0.0 29 name: cn-app-demo` 30 type args struct { 31 body string 32 } 33 tests := []struct { 34 name string 35 args args 36 want []string 37 }{ 38 { 39 "test get image from yaml", 40 args{body}, 41 []string{"cn-app-integration:v1.0.0", "registry.cn-shanghai.aliyuncs.com/cnip/cn-app-integration:v1.0.0", "cn-app-integration:v1.0.0"}, 42 }, 43 } 44 for _, tt := range tests { 45 t.Run(tt.name, func(t *testing.T) { 46 if got := DecodeImages(tt.args.body); !reflect.DeepEqual(got, tt.want) { 47 t.Errorf("decodeImages() = %v, want %v", got, tt.want) 48 } 49 }) 50 } 51 } 52 53 func Test_decodeLine(t *testing.T) { 54 type args struct { 55 line string 56 } 57 tests := []struct { 58 name string 59 args args 60 want string 61 }{ 62 { 63 name: "normal", 64 args: args{ 65 line: "image: registry.cn-shanghai.aliyuncs.com/sealerio/foo:v1.0.0", 66 }, 67 want: "registry.cn-shanghai.aliyuncs.com/sealerio/foo:v1.0.0", 68 }, 69 { 70 name: "withBlank", 71 args: args{ 72 line: " image: registry.cn-shanghai.aliyuncs.com/sealerio/foo:v1.0.0", 73 }, 74 want: "registry.cn-shanghai.aliyuncs.com/sealerio/foo:v1.0.0", 75 }, 76 { 77 name: "withPrefix", 78 args: args{ 79 line: "vmagent-image: \"ack-agility-registry.cn-shanghai.cr.aliyuncs.com/sealerio/foo:v1.70.0\"", 80 }, 81 want: "ack-agility-registry.cn-shanghai.cr.aliyuncs.com/sealerio/foo:v1.70.0", 82 }, 83 { 84 name: "inArray", 85 args: args{ 86 line: "- image: \"ack-agility-registry.cn-shanghai.cr.aliyuncs.com/sealerio/foo:v1.70.0\"", 87 }, 88 want: "ack-agility-registry.cn-shanghai.cr.aliyuncs.com/sealerio/foo:v1.70.0", 89 }, 90 } 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 if got := decodeLine(tt.args.line); got != tt.want { 94 t.Errorf("decodeLine() = %v, want %v", got, tt.want) 95 } 96 }) 97 } 98 }