提交 98c0580e 编写于 作者: O openeuler-ci-bot 提交者: Gitee

!54 bugfix: fix --from can not pull image from remote registry

Merge pull request !54 from DCCooper/master
......@@ -138,6 +138,13 @@ func (c *cmdBuilder) getCopyContextDir(from string) (string, func(), error) {
}
}
// update cert path in case it is different between FROM and --from
server, err := util.ParseServer(from)
if err != nil {
return "", nil, err
}
c.stage.buildOpt.systemContext.DockerCertPath = filepath.Join(constant.DefaultCertRoot, server)
// "from" is neither name nor index of stage, consider that "from" is image description
imgDesc, err := prepareImage(&image.PrepareImageOptions{
Ctx: c.ctx,
......
......@@ -141,7 +141,7 @@ func pullAndGetImageInfo(opt *PrepareImageOptions) (types.ImageReference, *stora
// record the last pull error
var errPull error
const tagSeperator = ":"
const tagSeperator = "://"
for _, strImage := range candidates {
if transport != util.DefaultTransport {
transport += tagSeperator
......
......@@ -145,11 +145,14 @@ func ParseServer(server string) (string, error) {
}
// first trim prefix https:// and http://
server = strings.TrimPrefix(strings.TrimPrefix(server, "https://"), "http://")
// then trim prefix docker://
server = strings.TrimPrefix(server, DefaultTransport)
// always get first part split by "/"
fields := strings.Split(server, "/")
if fields[0] == "" {
return "", errors.Errorf("invalid registry address %s", server)
}
return fields[0], nil
}
......
......@@ -88,3 +88,82 @@ func TestCheckFileSize(t *testing.T) {
})
}
}
func TestParseServer(t *testing.T) {
type args struct {
server string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "TC1 - normal server address with http prefix",
args: args{server: "http://mydockerhub.org"},
want: "mydockerhub.org",
wantErr: false,
},
{
name: "TC2 - normal server address with https prefix",
args: args{server: "https://mydockerhub.org"},
want: "mydockerhub.org",
wantErr: false,
},
{
name: "TC3 - normal server address with docker prefix",
args: args{server: "docker://mydockerhub.org"},
want: "mydockerhub.org",
wantErr: false,
},
{
name: "TC4 - normal server address with none prefix",
args: args{server: "mydockerhub.org"},
want: "mydockerhub.org",
wantErr: false,
},
{
name: "TC5 - normal server address with other suffix",
args: args{server: "mydockerhub.org/test/test1"},
want: "mydockerhub.org",
wantErr: false,
},
{
name: "TC6 - normal server address with other suffix",
args: args{server: "https://mydockerhub.org/test/test1"},
want: "mydockerhub.org",
wantErr: false,
},
{
name: "TC7 - normal server address with other suffix 2",
args: args{server: "mydockerhub.org/test/test1:3030"},
want: "mydockerhub.org",
wantErr: false,
},
{
name: "TC8 - abnormal server address",
args: args{server: "/mydockerhub.org"},
want: "",
wantErr: true,
},
{
name: "TC9 - abnormal server address with wrong prefix 2",
args: args{server: "http:///mydockerhub.org"},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseServer(tt.args.server)
if (err != nil) != tt.wantErr {
t.Errorf("ParseServer() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseServer() got = %v, want %v", got, tt.want)
}
})
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册