github.com/oam-dev/kubevela@v1.9.11/pkg/addon/reader_github_test.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package addon
    18  
    19  import (
    20  	"embed"
    21  	"encoding/json"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"net/url"
    25  	"path"
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/google/go-github/v32/github"
    30  	"github.com/stretchr/testify/assert"
    31  
    32  	"github.com/oam-dev/kubevela/pkg/utils"
    33  )
    34  
    35  const (
    36  	// baseURLPath is a non-empty Client.BaseURL path to use during tests,
    37  	// to ensure relative URLs are used for all endpoints. See issue #752.
    38  	baseURLPath = "/api-v3"
    39  )
    40  
    41  var (
    42  	//go:embed testdata
    43  	testdata       embed.FS
    44  	testdataPrefix = "testdata"
    45  )
    46  
    47  func setup() (client *github.Client, mux *http.ServeMux, teardown func()) {
    48  	// mux is the HTTP request multiplexer used with the test server.
    49  	mux = http.NewServeMux()
    50  
    51  	apiHandler := http.NewServeMux()
    52  	apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux))
    53  
    54  	// server is a test HTTP server used to provide mock API responses.
    55  	server := httptest.NewServer(apiHandler)
    56  
    57  	// client is the GitHub client being tested and is
    58  	// configured to use test server.
    59  	client = github.NewClient(nil)
    60  	URL, _ := url.Parse(server.URL + baseURLPath + "/")
    61  	client.BaseURL = URL
    62  	client.UploadURL = URL
    63  
    64  	return client, mux, server.Close
    65  }
    66  
    67  func TestGitHubReader(t *testing.T) {
    68  	client, mux, teardown := setup()
    69  	githubPattern := "/repos/o/r/contents/"
    70  	mux.HandleFunc(githubPattern, func(rw http.ResponseWriter, req *http.Request) {
    71  		queryPath := strings.TrimPrefix(req.URL.Path, githubPattern)
    72  		localPath := path.Join(testdataPrefix, queryPath)
    73  		file, err := testdata.ReadFile(localPath)
    74  		// test if it's a file
    75  		if err == nil {
    76  			content := &github.RepositoryContent{Type: String("file"), Name: String(path.Base(queryPath)), Size: Int(len(file)), Encoding: String(""), Path: String(queryPath), Content: String(string(file))}
    77  			res, _ := json.Marshal(content)
    78  			rw.Write(res)
    79  		}
    80  
    81  		// otherwise, it could be directory
    82  		dir, err := testdata.ReadDir(localPath)
    83  		if err == nil {
    84  			contents := make([]*github.RepositoryContent, 0)
    85  			for _, item := range dir {
    86  				tp := "file"
    87  				if item.IsDir() {
    88  					tp = "dir"
    89  				}
    90  				contents = append(contents, &github.RepositoryContent{Type: String(tp), Name: String(item.Name()), Path: String(path.Join(queryPath, item.Name()))})
    91  			}
    92  			dRes, _ := json.Marshal(contents)
    93  			rw.Write(dRes)
    94  		}
    95  
    96  		rw.Write([]byte("invalid github query"))
    97  	})
    98  	defer teardown()
    99  
   100  	gith := &gitHelper{
   101  		Client: client,
   102  		Meta: &utils.Content{GithubContent: utils.GithubContent{
   103  			Owner: "o",
   104  			Repo:  "r",
   105  		}},
   106  	}
   107  	var r AsyncReader = &gitReader{gith}
   108  	_, err := r.ReadFile("example/metadata.yaml")
   109  	assert.NoError(t, err)
   110  
   111  }
   112  
   113  // Int is a helper routine that allocates a new int value
   114  // to store v and returns a pointer to it.
   115  func Int(v int) *int { return &v }
   116  
   117  // String is a helper routine that allocates a new string value
   118  // to store v and returns a pointer to it.
   119  func String(v string) *string { return &v }