github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/imageengine/buildah/inspect_test.go (about) 1 // Copyright © 2023 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 buildah 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "reflect" 21 "testing" 22 23 "github.com/sealerio/sealer/build/kubefile/command" 24 25 image_v1 "github.com/sealerio/sealer/pkg/define/image/v1" 26 ) 27 28 func TestGetImageExtensionFromAnnotations(t *testing.T) { 29 type args struct { 30 annotations map[string]string 31 } 32 tests := []struct { 33 name string 34 args args 35 want image_v1.ImageExtension 36 wantErr bool 37 }{ 38 { 39 name: "", 40 args: args{ 41 annotations: map[string]string{ 42 "sealer.image.extension": `{"buildClient":{"sealerVersion":"0.9.0","buildahVersion":"1.27.1"},"schemaVersion":"v1alpha1","type":"kube-installer","launch":{"cmds":["ls","-l"]}}`, 43 }, 44 }, 45 want: image_v1.ImageExtension{ 46 BuildClient: image_v1.BuildClient{ 47 SealerVersion: "0.9.0", 48 BuildahVersion: "1.27.1", 49 }, 50 SchemaVersion: "v1alpha1", 51 Type: "kube-installer", 52 Applications: nil, 53 Launch: image_v1.Launch{ 54 Cmds: []string{"ls", "-l"}, 55 AppNames: nil, 56 }, 57 }, 58 wantErr: false, 59 }, 60 } 61 for _, tt := range tests { 62 t.Run(tt.name, func(t *testing.T) { 63 got, err := getImageExtensionFromAnnotations(tt.args.annotations) 64 if (err != nil) != tt.wantErr { 65 t.Errorf("GetImageExtensionFromAnnotations() error = %v, wantErr %v", err, tt.wantErr) 66 return 67 } 68 gotJSON, _ := json.Marshal(got) 69 wantJSON, _ := json.Marshal(tt.want) 70 if !reflect.DeepEqual(gotJSON, wantJSON) { 71 t.Errorf("GetImageExtensionFromAnnotations() got = %s\n, want %s", string(gotJSON), string(wantJSON)) 72 } 73 }) 74 } 75 } 76 77 func TestGetContainerImagesFromAnnotations(t *testing.T) { 78 type args struct { 79 annotations map[string]string 80 } 81 tests := []struct { 82 name string 83 args args 84 want []*image_v1.ContainerImage 85 wantErr bool 86 }{ 87 { 88 name: "", 89 args: args{ 90 annotations: map[string]string{}, 91 }, 92 want: nil, 93 wantErr: false, 94 }, 95 { 96 name: "", 97 args: args{ 98 annotations: map[string]string{ 99 image_v1.SealerImageContainerImageList: ``, 100 }, 101 }, 102 want: nil, 103 wantErr: false, 104 }, 105 { 106 name: "", 107 args: args{ 108 annotations: map[string]string{ 109 image_v1.SealerImageContainerImageList: `[{"image":"nginx:latest","appName":"nginx"},{"image":"registry.cn-qingdao.aliyuncs.com/sealer-io/dashboard:latest","appName":"dashboard"},{"image":"busybox:latest"}]`, 110 }, 111 }, 112 want: []*image_v1.ContainerImage{ 113 { 114 Image: "nginx:latest", 115 AppName: "nginx", 116 }, 117 { 118 Image: "registry.cn-qingdao.aliyuncs.com/sealer-io/dashboard:latest", 119 AppName: "dashboard", 120 }, 121 { 122 Image: "busybox:latest", 123 }, 124 }, 125 wantErr: false, 126 }, 127 } 128 for _, tt := range tests { 129 t.Run(tt.name, func(t *testing.T) { 130 got, err := getContainerImagesFromAnnotations(tt.args.annotations) 131 if (err != nil) != tt.wantErr { 132 t.Errorf("GetContainerImagesFromAnnotations() error = %v, wantErr %v", err, tt.wantErr) 133 return 134 } 135 if !reflect.DeepEqual(got, tt.want) { 136 t.Errorf("GetContainerImagesFromAnnotations() got = %v, want %v", got, tt.want) 137 } 138 }) 139 } 140 } 141 142 func Test_handleLabelOutput(t *testing.T) { 143 type args struct { 144 labels map[string]string 145 } 146 tests := []struct { 147 name string 148 args args 149 want map[string]string 150 }{ 151 { 152 name: "", 153 args: args{ 154 labels: map[string]string{ 155 fmt.Sprintf("%s%s", command.LabelKubeCNIPrefix, "calico"): "true", 156 fmt.Sprintf("%s%s", command.LabelKubeCNIPrefix, "flannel"): "true", 157 fmt.Sprintf("%s%s", command.LabelKubeCSIPrefix, "alibaba-csi"): "true", 158 "key1": "value1", 159 }, 160 }, 161 want: map[string]string{ 162 command.LabelSupportedKubeCNIAlpha: `["calico","flannel"]`, 163 command.LabelSupportedKubeCSIAlpha: `["alibaba-csi"]`, 164 "key1": "value1", 165 }, 166 }, 167 { 168 name: "", 169 args: args{ 170 labels: map[string]string{}, 171 }, 172 want: map[string]string{}, 173 }, 174 } 175 for _, tt := range tests { 176 t.Run(tt.name, func(t *testing.T) { 177 if got := handleImageLabelOutput(tt.args.labels); !reflect.DeepEqual(got, tt.want) { 178 t.Errorf("handleLabelOutput() = %v, want %v", got, tt.want) 179 } 180 }) 181 } 182 }