diff --git a/internal/agent/rest.go b/internal/agent/rest.go index 5348abb..0c88570 100644 --- a/internal/agent/rest.go +++ b/internal/agent/rest.go @@ -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 } @@ -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) diff --git a/test/e2e/e2e_agent_test.go b/test/e2e/e2e_agent_test.go index 1d22a77..b1b9e71 100644 --- a/test/e2e/e2e_agent_test.go +++ b/test/e2e/e2e_agent_test.go @@ -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 @@ -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{ @@ -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( @@ -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 { diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index e0b2a42..f9bf62d 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -2,6 +2,7 @@ package e2e_test import ( "fmt" + "net/http" "os" "github.com/kubev2v/migration-planner/api/v1alpha1" @@ -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 { @@ -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).To(BeNil()) + Expect(res.StatusCode).To(Equal(http.StatusUnprocessableEntity)) + }) }) })