github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/image/reference/reference_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 reference 16 17 import ( 18 "fmt" 19 "testing" 20 ) 21 22 func TestParseToNamed(t *testing.T) { 23 type namedTest struct { 24 name string 25 desired Named 26 } 27 28 ts := []namedTest{ 29 { 30 name: "xxx.com/abc/tag:v1", 31 desired: Named{ 32 raw: "xxx.com/abc/tag:v1", 33 domain: "xxx.com", 34 repo: "abc/tag", 35 tag: "v1", 36 repoTag: "abc/tag:v1", 37 }, 38 }, 39 { 40 name: "abc/tag:v1", 41 desired: Named{ 42 raw: "abc/tag:v1", 43 domain: defaultDomain, 44 repo: "abc/tag", 45 tag: "v1", 46 repoTag: "abc/tag:v1", 47 }, 48 }, 49 { 50 name: "tag:v1", 51 desired: Named{ 52 raw: "tag:v1", 53 domain: defaultDomain, 54 repo: defaultRepo + "/tag", 55 tag: "v1", 56 repoTag: defaultRepo + "/tag:v1", 57 }, 58 }, 59 { 60 name: "tag", 61 desired: Named{ 62 raw: "tag:" + defaultTag, 63 domain: defaultDomain, 64 repo: defaultRepo + "/tag", 65 tag: defaultTag, 66 repoTag: defaultRepo + "/tag:" + defaultTag, 67 }, 68 }, 69 { 70 name: "xxx.com:5000/abc/tag", 71 desired: Named{ 72 raw: "xxx.com:5000/abc/tag:" + defaultTag, 73 domain: "xxx.com:5000", 74 repo: "abc/tag", 75 tag: defaultTag, 76 repoTag: "abc/tag:" + defaultTag, 77 }, 78 }, 79 } 80 81 for _, tt := range ts { 82 named, err := ParseToNamed(tt.name) 83 if err != nil { 84 t.Fatalf(err.Error()) 85 } 86 err = compareNamed(named, tt.desired) 87 if err != nil { 88 t.Fatalf(err.Error()) 89 } 90 } 91 } 92 93 func compareNamed(a, b Named) error { 94 type compare struct { 95 c, d string 96 } 97 cs := []compare{{ 98 c: a.raw, 99 d: b.raw, 100 }, { 101 c: a.tag, 102 d: b.tag, 103 }, { 104 c: a.repoTag, 105 d: b.repoTag, 106 }, { 107 c: a.repo, 108 d: b.repo, 109 }, { 110 c: a.domain, 111 d: b.domain, 112 }} 113 for _, c := range cs { 114 if c.d != c.c { 115 return fmt.Errorf("%s does not equal to %s", c.c, c.d) 116 } 117 } 118 return nil 119 }