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 <[email protected]>
  • Loading branch information
aniruddha2000 committed Oct 10, 2022
1 parent df0fb87 commit a5ad52e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/clusterctl/client/cluster/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ func (t *templateClient) getURLContent(templateURL string) ([]byte, error) {

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

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

func (t *templateClient) getRawUrlFileContent(rURL string) ([]byte, error) {
res, err := http.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
60 changes: 60 additions & 0 deletions cmd/clusterctl/client/cluster/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ kind: Cluster
apiVersion: cluster.x-k8s.io/v1beta1
kind: Machine`

var rawURLTemplate = `apiVersion: v1
kind: Pod
metadata:
name: command-demo
labels:
purpose: demonstrate-command
spec:
containers:
- name: command-demo-container
image: debian
command: ["printenv"]
args: ["HOSTNAME", "KUBERNETES_PORT"]
restartPolicy: OnFailure
`

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

Expand Down Expand Up @@ -225,6 +240,51 @@ func Test_templateClient_getGitHubFileContent(t *testing.T) {
}
}

func Test_templateClient_getRawUrlFileContent(t *testing.T) {
type args struct {
rURL string
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
name: "Return custom template",
args: args{
rURL: "https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/commands.yaml",
},
want: []byte(rawURLTemplate),
wantErr: false,
},
{
name: "Wrong url",
args: args{
rURL: "https://foo.com/path/to/cluster-template.yaml",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

c := &templateClient{}
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 a5ad52e

Please sign in to comment.