2019-01-12 22:45:25 -06:00
package container
import (
2021-05-05 11:37:17 -05:00
"context"
2019-01-12 22:45:25 -06:00
"testing"
2021-11-27 12:21:32 -06:00
"github.com/docker/cli/cli/config"
2019-01-12 22:45:25 -06:00
log "github.com/sirupsen/logrus"
2021-05-18 01:14:49 -05:00
assert "github.com/stretchr/testify/assert"
2019-01-12 22:45:25 -06:00
)
func init ( ) {
log . SetLevel ( log . DebugLevel )
}
func TestCleanImage ( t * testing . T ) {
tables := [ ] struct {
imageIn string
imageOut string
} {
{ "myhost.com/foo/bar" , "myhost.com/foo/bar" } ,
2021-11-13 13:35:45 -06:00
{ "localhost:8000/canonical/ubuntu" , "localhost:8000/canonical/ubuntu" } ,
{ "localhost/canonical/ubuntu:latest" , "localhost/canonical/ubuntu:latest" } ,
{ "localhost:8000/canonical/ubuntu:latest" , "localhost:8000/canonical/ubuntu:latest" } ,
2019-01-12 22:45:25 -06:00
{ "ubuntu" , "docker.io/library/ubuntu" } ,
{ "ubuntu:18.04" , "docker.io/library/ubuntu:18.04" } ,
{ "cibuilds/hugo:0.53" , "docker.io/cibuilds/hugo:0.53" } ,
}
for _ , table := range tables {
2022-06-17 10:55:21 -05:00
imageOut := cleanImage ( context . Background ( ) , table . imageIn )
2019-01-12 22:45:25 -06:00
assert . Equal ( t , table . imageOut , imageOut )
}
}
2021-05-05 11:37:17 -05:00
func TestGetImagePullOptions ( t * testing . T ) {
ctx := context . Background ( )
2021-11-27 12:21:32 -06:00
config . SetDir ( "/non-existent/docker" )
2021-05-05 11:37:17 -05:00
options , err := getImagePullOptions ( ctx , NewDockerPullExecutorInput { } )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , "Failed to create ImagePullOptions" )
2021-11-27 12:21:32 -06:00
assert . Equal ( t , "" , options . RegistryAuth , "RegistryAuth should be empty if no username or password is set" )
2021-05-05 11:37:17 -05:00
options , err = getImagePullOptions ( ctx , NewDockerPullExecutorInput {
Image : "" ,
Username : "username" ,
Password : "password" ,
} )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err , "Failed to create ImagePullOptions" )
2021-11-27 12:21:32 -06:00
assert . Equal ( t , "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZCJ9" , options . RegistryAuth , "Username and Password should be provided" )
config . SetDir ( "testdata/docker-pull-options" )
options , err = getImagePullOptions ( ctx , NewDockerPullExecutorInput {
Image : "nektos/act" ,
} )
assert . Nil ( t , err , "Failed to create ImagePullOptions" )
assert . Equal ( t , "eyJ1c2VybmFtZSI6InVzZXJuYW1lIiwicGFzc3dvcmQiOiJwYXNzd29yZFxuIiwic2VydmVyYWRkcmVzcyI6Imh0dHBzOi8vaW5kZXguZG9ja2VyLmlvL3YxLyJ9" , options . RegistryAuth , "RegistryAuth should be taken from local docker config" )
2021-05-05 11:37:17 -05:00
}