Skip to content

Commit

Permalink
Add getRawUrlFileContent method to handle raw url & Unit Test
Browse files Browse the repository at this point in the history
Signed-off-by: Aniruddha Basak <codewithaniruddha@gmail.com>
  • Loading branch information
Aniruddha Basak authored and Aniruddha Basak committed Nov 20, 2022
1 parent df0fb87 commit e76e20e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
24 changes: 22 additions & 2 deletions cmd/clusterctl/client/cluster/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type templateClient struct {
configClient config.Client
gitHubClientFactory func(configVariablesClient config.VariablesClient) (*github.Client, error)
processor yaml.Processor
httpClient *http.Client
}

// ensure templateClient implements TemplateClient.
Expand All @@ -70,6 +71,7 @@ func newTemplateClient(input TemplateClientInput) *templateClient {
configClient: input.configClient,
gitHubClientFactory: getGitHubClient,
processor: input.processor,
httpClient: http.DefaultClient,
}
}

Expand Down Expand Up @@ -143,8 +145,12 @@ func (t *templateClient) getURLContent(templateURL string) ([]byte, error) {
return nil, errors.Wrapf(err, "failed to parse %q", templateURL)
}

if rURL.Scheme == "https" && rURL.Host == "github.com" {
return t.getGitHubFileContent(rURL)
if rURL.Scheme == "https" {
if rURL.Host == "github.com" {
return t.getGitHubFileContent(rURL)
} else {
return t.getRawUrlFileContent(templateURL)
}
}

if rURL.Scheme == "file" || rURL.Scheme == "" {
Expand Down Expand Up @@ -210,6 +216,20 @@ func (t *templateClient) getGitHubFileContent(rURL *url.URL) ([]byte, error) {
return content, nil
}

func (t *templateClient) getRawUrlFileContent(rURL string) ([]byte, error) {
res, err := t.httpClient.Get(rURL)
if err != nil {
return nil, err
}

content, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

return content, nil
}

func getGitHubClient(configVariablesClient config.VariablesClient) (*github.Client, error) {
var authenticatingHTTPClient *http.Client
if token, err := configVariablesClient.Get(config.GitHubTokenVariable); err == nil {
Expand Down
46 changes: 46 additions & 0 deletions cmd/clusterctl/client/cluster/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/base64"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -225,6 +226,51 @@ func Test_templateClient_getGitHubFileContent(t *testing.T) {
}
}

func Test_templateClient_getRawUrlFileContent(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, template)
}))

defer fakeServer.Close()

type args struct {
rURL string
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
name: "Return custom template",
args: args{
rURL: fakeServer.URL,
},
want: []byte(template),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

c := &templateClient{
httpClient: &http.Client{},
}
got, err := c.getRawUrlFileContent(tt.args.rURL)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
return
}

g.Expect(err).NotTo(HaveOccurred())

g.Expect(got).To(Equal(tt.want))
})
}
}

func Test_templateClient_getLocalFileContent(t *testing.T) {
g := NewWithT(t)

Expand Down

0 comments on commit e76e20e

Please sign in to comment.