Skip to content

Commit

Permalink
add unittest for validateImageRepository and optimize validateImageRe…
Browse files Browse the repository at this point in the history
…pository
  • Loading branch information
yxxhero committed Mar 10, 2021
1 parent 416f92a commit f653337
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
20 changes: 16 additions & 4 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ func validateFlags(cmd *cobra.Command, drvName string) {
}

if cmd.Flags().Changed(imageRepository) {
validateImageRepository(viper.GetString(imageRepository))
viper.Set(imageRepository, validateImageRepository(viper.GetString(imageRepository)))
}

if cmd.Flags().Changed(containerRuntime) {
Expand Down Expand Up @@ -1214,14 +1214,26 @@ func validateRegistryMirror() {

// This function validates if the --image-repository
// args match the format of registry.cn-hangzhou.aliyuncs.com/google_containers
func validateImageRepository(imagRepo string) {
func validateImageRepository(imagRepo string) (vaildImageRepo string) {

if strings.ToLower(imagRepo) == "auto" {
vaildImageRepo = "auto"
}
URL, err := url.Parse(imagRepo)
if err != nil {
klog.Errorln("Error Parsing URL: ", err)
}
if URL.Scheme != "" || strings.HasSuffix(URL.Path, "/") {
exit.Message(reason.Usage, "Sorry, the url provided with the --image-repository flag is invalid: {{.url}}", out.V{"url": imagRepo})
// tips when imagRepo ended with a trailing /.
if strings.HasSuffix(imagRepo, "/") {
out.Infof("The --image-repository flag your provided ended with a trailing / that could cause conflict in kuberentes, removed automatically")
}
// tips when imageRepo started with scheme.
if URL.Scheme != "" {
out.Infof("The --image-repository flag your provided contains Scheme: {{.scheme}}, it will be as a domian, removed automatically", out.V{"scheme": URL.Scheme})
}

vaildImageRepo = URL.Hostname() + strings.TrimSuffix(URL.Path, "/")
return
}

// This function validates if the --listen-address
Expand Down
43 changes: 43 additions & 0 deletions cmd/minikube/cmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,46 @@ func TestBaseImageFlagDriverCombo(t *testing.T) {
})
}
}

func TestValidateImageRepository(t *testing.T) {
var tests = []struct {
imageRepository string
vaildImageRepository string
}{
{
imageRepository: "auto",
vaildImageRepository: "auto",
},
{
imageRepository: "http://registry.test.com/google_containers/",
vaildImageRepository: "registry.test.com/google_containers",
},
{
imageRepository: "https://registry.test.com/google_containers/",
vaildImageRepository: "registry.test.com/google_containers",
},
{
imageRepository: "registry.test.com/google_containers/",
vaildImageRepository: "registry.test.com/google_containers",
},
{
imageRepository: "http://registry.test.com/google_containers",
vaildImageRepository: "registry.test.com/google_containers",
},
{
imageRepository: "https://registry.test.com/google_containers",
vaildImageRepository: "registry.test.com/google_containers",
},
}

for _, test := range tests {
t.Run(test.imageRepository, func(t *testing.T) {
vaildImageRepository := validateImageRepository(test.imageRepository)
if vaildImageRepository != test.vaildImageRepository {
t.Errorf("validateImageRepository(imageRepo=%v): got %v, expected %v",
test.imageRepository, vaildImageRepository, test.vaildImageRepository)
}
})
}

}

0 comments on commit f653337

Please sign in to comment.