github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/image/save/save_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 save 16 17 import ( 18 "context" 19 "testing" 20 21 v1 "github.com/alibaba/sealer/types/api/v1" 22 ) 23 24 func TestSaveImages(t *testing.T) { 25 tests := []string{"ubuntu", "ubuntu:18.04", "registry.aliyuncs.com/google_containers/coredns:1.6.5", "fanux/lvscare", "kubernetesui/dashboard:v2.2.0", "multiarch/ubuntu-core:arm64-focal"} 26 is := NewImageSaver(context.Background()) 27 err := is.SaveImages(tests, "/var/lib/registry", v1.Platform{OS: "linux", Architecture: "amd64"}) 28 if err != nil { 29 t.Error(err) 30 } 31 } 32 33 func Test_splitDockerDomain(t *testing.T) { 34 tests := []struct { 35 name string 36 imageName string 37 wantDomain string 38 wantRemain string 39 }{ 40 { 41 name: "test1", 42 imageName: "docker.io/library/alpine:latest", 43 wantDomain: defaultDomain, 44 wantRemain: "library/alpine:latest", 45 }, 46 { 47 name: "test2", 48 imageName: "ubuntu", 49 wantDomain: defaultDomain, 50 wantRemain: "library/ubuntu", 51 }, 52 { 53 name: "test3", 54 imageName: "k8s.gcr.io/kube-apiserver", 55 wantDomain: "k8s.gcr.io", 56 wantRemain: "kube-apiserver", 57 }, 58 } 59 for _, tt := range tests { 60 t.Run(tt.name, func(t *testing.T) { 61 if domain, remainer := splitDockerDomain(tt.imageName, ""); domain != tt.wantDomain || remainer != tt.wantRemain { 62 t.Errorf("split image %s error", tt.name) 63 } 64 }) 65 } 66 } 67 68 func Test_parseNormalizedNamed(t *testing.T) { 69 tests := []struct { 70 name string 71 imageName string 72 wantDomain string 73 wantRepo string 74 wantTag string 75 }{ 76 { 77 name: "test1", 78 imageName: "docker.io/library/alpine:latest", 79 wantDomain: defaultDomain, 80 wantRepo: "library/alpine", 81 wantTag: defaultTag, 82 }, 83 { 84 name: "test2", 85 imageName: "ubuntu", 86 wantDomain: defaultDomain, 87 wantRepo: "library/ubuntu", 88 wantTag: defaultTag, 89 }, 90 { 91 name: "test3", 92 imageName: "k8s.gcr.io/kube-apiserver", 93 wantDomain: "k8s.gcr.io", 94 wantRepo: "kube-apiserver", 95 wantTag: defaultTag, 96 }, 97 { 98 name: "test4", 99 imageName: "fanux/lvscare", 100 wantDomain: defaultDomain, 101 wantRepo: "fanux/lvscare", 102 wantTag: defaultTag, 103 }, 104 { 105 name: "test5", 106 imageName: "alpine", 107 wantDomain: defaultDomain, 108 wantRepo: "library/alpine", 109 wantTag: defaultTag, 110 }, 111 } 112 113 for _, tt := range tests { 114 t.Run(tt.name, func(t *testing.T) { 115 if named, err := ParseNormalizedNamed(tt.imageName, ""); err != nil || named.Domain() != tt.wantDomain || named.Repo() != tt.wantRepo || named.tag != tt.wantTag { 116 t.Errorf("parse image %s error", tt.name) 117 } 118 }) 119 } 120 }