Skip to content

Commit

Permalink
Return HTTP 422 in case of wrong URL
Browse files Browse the repository at this point in the history
Fixes: #11

Signed-off-by: Ondra Machacek <[email protected]>
  • Loading branch information
machacekondra committed Nov 8, 2024
1 parent 3f210ee commit b8f7c80
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
6 changes: 3 additions & 3 deletions internal/agent/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func credentialHandler(log *log.PrefixLogger, dataDir string, w http.ResponseWri
}

log.Info("successfully wrote credentials to file")
w.WriteHeader(204)
w.WriteHeader(http.StatusNoContent)
}

func parseUrl(credentials *Credentials) (*url.URL, error) {
u, err := url.Parse(credentials.URL)
u, err := url.ParseRequestURI(credentials.URL)
if err != nil {
return nil, err
}
Expand All @@ -107,7 +107,7 @@ func parseUrl(credentials *Credentials) (*url.URL, error) {
func testVmwareConnection(requestCtx context.Context, log *log.PrefixLogger, credentials *Credentials) (status int, err error) {
u, err := parseUrl(credentials)
if err != nil {
return http.StatusBadRequest, liberr.Wrap(err)
return http.StatusUnprocessableEntity, liberr.Wrap(err)
}

ctx, cancel := context.WithTimeout(requestCtx, 10*time.Second)
Expand Down
14 changes: 7 additions & 7 deletions test/e2e/e2e_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (

type PlannerAgent interface {
Run(string) error
Login(url string, user string, pass string) error
Login(url string, user string, pass string) (*http.Response, error)
Remove() error
GetIp() (string, error)
IsServiceRunning(string, string) bool
Expand Down Expand Up @@ -111,10 +111,10 @@ func (p *plannerAgentLibvirt) prepareImage(sourceId string) error {
return nil
}

func (p *plannerAgentLibvirt) Login(url string, user string, pass string) error {
func (p *plannerAgentLibvirt) Login(url string, user string, pass string) (*http.Response, error) {
agentIP, err := p.GetIp()
if err != nil {
return fmt.Errorf("failed to get agent IP: %w", err)
return nil, fmt.Errorf("failed to get agent IP: %w", err)
}

credentials := map[string]string{
Expand All @@ -125,7 +125,7 @@ func (p *plannerAgentLibvirt) Login(url string, user string, pass string) error

jsonData, err := json.Marshal(credentials)
if err != nil {
return fmt.Errorf("failed to marshal credentials: %w", err)
return nil, fmt.Errorf("failed to marshal credentials: %w", err)
}

resp, err := http.NewRequest(
Expand All @@ -134,18 +134,18 @@ func (p *plannerAgentLibvirt) Login(url string, user string, pass string) error
bytes.NewBuffer(jsonData),
)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
return nil, fmt.Errorf("failed to create request: %w", err)
}
resp.Header.Set("Content-Type", "application/json")

client := &http.Client{}
response, err := client.Do(resp)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
return response, fmt.Errorf("failed to send request: %w", err)
}
defer response.Body.Close()

return nil
return response, nil
}

func (p *plannerAgentLibvirt) RestartService() error {
Expand Down
10 changes: 9 additions & 1 deletion test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2e_test

import (
"fmt"
"net/http"
"os"

"github.com/kubev2v/migration-planner/api/v1alpha1"
Expand Down Expand Up @@ -71,8 +72,9 @@ var _ = Describe("e2e", func() {
Expect(r).To(BeTrue())

// Put the vCenter credentials and check that source is up to date eventually
err = agent.Login(fmt.Sprintf("https://%s:8989/sdk", systemIP), "user", "pass")
res, err := agent.Login(fmt.Sprintf("https://%s:8989/sdk", systemIP), "user", "pass")
Expect(err).To(BeNil())
Expect(res.StatusCode).To(Equal(http.StatusNoContent))
Eventually(func() bool {
source, err := svc.GetSource()
if err != nil {
Expand All @@ -81,5 +83,11 @@ var _ = Describe("e2e", func() {
return source.Status == v1alpha1.SourceStatusUpToDate
}, "1m", "2s").Should(BeTrue())
})
It("Return 422 in case of wrong URL", func() {
// Put the vCenter credentials with wrong URL and check it return HTTP 422 error code
res, err := agent.Login("this is not URL", "user", "pass")
Expect(err).ToNot(BeNil())
Expect(res.StatusCode).To(Equal(http.StatusUnprocessableEntity))
})
})
})

0 comments on commit b8f7c80

Please sign in to comment.