github.com/oam-dev/kubevela@v1.9.11/pkg/addon/reader_gitee_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  	"encoding/json"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"net/url"
    24  	"path"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/google/go-github/v32/github"
    29  	"github.com/stretchr/testify/assert"
    30  
    31  	"github.com/oam-dev/kubevela/pkg/utils"
    32  )
    33  
    34  func giteeSetup() (client *Client, mux *http.ServeMux, teardown func()) {
    35  	// mux is the HTTP request multiplexer used with the test server.
    36  	mux = http.NewServeMux()
    37  
    38  	apiHandler := http.NewServeMux()
    39  	apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux))
    40  
    41  	// server is a test HTTP server used to provide mock API responses.
    42  	server := httptest.NewServer(apiHandler)
    43  
    44  	// client is the GitHub client being tested and is
    45  	// configured to use test server.
    46  	URL, _ := url.Parse(server.URL + baseURLPath + "/")
    47  	httpClient := &http.Client{}
    48  	client = NewGiteeClient(httpClient, URL)
    49  
    50  	return client, mux, server.Close
    51  }
    52  
    53  func TestGiteeReader(t *testing.T) {
    54  	client, mux, teardown := giteeSetup()
    55  	giteePattern := "/repos/o/r/contents/"
    56  	mux.HandleFunc(giteePattern, func(rw http.ResponseWriter, req *http.Request) {
    57  		queryPath := strings.TrimPrefix(req.URL.Path, giteePattern)
    58  		localPath := path.Join(testdataPrefix, queryPath)
    59  		file, err := testdata.ReadFile(localPath)
    60  		// test if it's a file
    61  		if err == nil {
    62  			content := &github.RepositoryContent{Type: String("file"), Name: String(path.Base(queryPath)), Size: Int(len(file)), Encoding: String(""), Path: String(queryPath), Content: String(string(file))}
    63  			res, _ := json.Marshal(content)
    64  			rw.Write(res)
    65  			return
    66  		}
    67  
    68  		// otherwise, it could be directory
    69  		dir, err := testdata.ReadDir(localPath)
    70  		if err == nil {
    71  			contents := make([]*github.RepositoryContent, 0)
    72  			for _, item := range dir {
    73  				tp := "file"
    74  				if item.IsDir() {
    75  					tp = "dir"
    76  				}
    77  				contents = append(contents, &github.RepositoryContent{Type: String(tp), Name: String(item.Name()), Path: String(path.Join(queryPath, item.Name()))})
    78  			}
    79  			dRes, _ := json.Marshal(contents)
    80  			rw.Write(dRes)
    81  			return
    82  		}
    83  
    84  		rw.Write([]byte("invalid gitee query"))
    85  	})
    86  	defer teardown()
    87  
    88  	gith := &giteeHelper{
    89  		Client: client,
    90  		Meta: &utils.Content{GiteeContent: utils.GiteeContent{
    91  			Owner: "o",
    92  			Repo:  "r",
    93  		}},
    94  	}
    95  	var r AsyncReader = &giteeReader{gith}
    96  	_, err := r.ReadFile("example/metadata.yaml")
    97  	assert.NoError(t, err)
    98  
    99  }