Skip to content

Commit

Permalink
Merge pull request #1981 from annastopel/virtctl_image_upload_bugfix
Browse files Browse the repository at this point in the history
Bug fix on: virtctl image-upload fails when a trailing slash is added to the upload proxy URL
  • Loading branch information
Artyom Lukianov committed Feb 13, 2019
2 parents 8639f8f + be5944d commit ea50334
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/virtctl/imageupload/imageupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"net/http"
"net/url"
"os"
"path"
"strings"
"time"

"github.com/spf13/cobra"
Expand All @@ -51,7 +53,8 @@ const (

uploadPodWaitInterval = 2 * time.Second

uploadProxyURI = "/v1alpha1/upload"
//UploadProxyURI is a URI of the upoad proxy
UploadProxyURI = "/v1alpha1/upload"
)

var (
Expand Down Expand Up @@ -202,8 +205,27 @@ func getHTTPClient(insecure bool) *http.Client {
return client
}

//ConstructUploadProxyPath - receives uploadproxy adress and concatenates to it URI
func ConstructUploadProxyPath(uploadProxyURL string) (string, error) {
u, err := url.Parse(uploadProxyURL)

if err != nil {
return "", err
}

if !strings.Contains(uploadProxyURL, UploadProxyURI) {
u.Path = path.Join(u.Path, UploadProxyURI)
}

return u.String(), nil
}

func uploadData(uploadProxyURL, token string, file *os.File, insecure bool) error {
url := uploadProxyURL + uploadProxyURI

url, err := ConstructUploadProxyPath(uploadProxyURL)
if err != nil {
return err
}

fi, err := file.Stat()
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/virtctl/imageupload/imageupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"time"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -152,6 +153,7 @@ var _ = Describe("ImageUpload", func() {
server = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(statusCode)
}))

imageupload.SetHTTPClientCreator(func(bool) *http.Client {
return server.Client()
})
Expand Down Expand Up @@ -261,4 +263,17 @@ var _ = Describe("ImageUpload", func() {
testDone()
})
})

Context("URL validation", func() {
serverURL := "http://localhost:12345"
DescribeTable("Server URL validations", func(serverUrl string, expected string) {
path, err := imageupload.ConstructUploadProxyPath(serverUrl)
Expect(err).To(BeNil())
Expect(strings.Compare(path, expected)).To(BeZero())
},
Entry("Server URL with trailing slash should pass", serverURL+"/", serverURL+imageupload.UploadProxyURI),
Entry("Server URL with URI should pass", serverURL+imageupload.UploadProxyURI, serverURL+imageupload.UploadProxyURI),
Entry("Server URL only should pass", serverURL, serverURL+imageupload.UploadProxyURI),
)
})
})

0 comments on commit ea50334

Please sign in to comment.