github.com/oam-dev/kubevela@v1.9.11/pkg/addon/reader_gitlab_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/base64"
    21  	"encoding/json"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"path"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/xanzy/go-gitlab"
    30  
    31  	"github.com/oam-dev/kubevela/pkg/utils"
    32  )
    33  
    34  var baseUrl = "/api/v4"
    35  
    36  func gitlabSetup() (client *gitlab.Client, mux *http.ServeMux, teardown func()) {
    37  	// mux is the HTTP request multiplexer used with the test server.
    38  	mux = http.NewServeMux()
    39  
    40  	apiHandler := http.NewServeMux()
    41  	apiHandler.Handle(baseUrl+"/", http.StripPrefix(baseUrl, mux))
    42  
    43  	// server is a test HTTP server used to provide mock API responses.
    44  	server := httptest.NewServer(apiHandler)
    45  
    46  	// client is the Gitlab client being tested and is
    47  	// configured to use test server.
    48  	client, err := gitlab.NewClient("", gitlab.WithBaseURL(server.URL+baseUrl+"/"))
    49  	if err != nil {
    50  		return
    51  	}
    52  
    53  	return client, mux, server.Close
    54  }
    55  
    56  func TestGitlabReader(t *testing.T) {
    57  	client, mux, teardown := gitlabSetup()
    58  	gitlabPattern := "/projects/9999/repository/files/"
    59  	mux.HandleFunc(gitlabPattern, func(rw http.ResponseWriter, req *http.Request) {
    60  		queryPath := strings.TrimPrefix(req.URL.Path, gitlabPattern)
    61  		localPath := path.Join(testdataPrefix, queryPath)
    62  		file, err := testdata.ReadFile(localPath)
    63  		// test if it's a file
    64  		if err == nil {
    65  			content := &gitlab.File{
    66  				FilePath: localPath,
    67  				FileName: path.Base(queryPath),
    68  				Size:     *Int(len(file)),
    69  				Encoding: "base64",
    70  				Ref:      "master",
    71  				Content:  base64.StdEncoding.EncodeToString(file),
    72  			}
    73  			res, _ := json.Marshal(content)
    74  			rw.Write(res)
    75  			return
    76  		}
    77  
    78  		// otherwise, it could be directory
    79  		dir, err := testdata.ReadDir(localPath)
    80  		if err == nil {
    81  			contents := make([]*gitlab.TreeNode, 0)
    82  			for _, item := range dir {
    83  				tp := "file"
    84  				if item.IsDir() {
    85  					tp = "dir"
    86  				}
    87  				contents = append(contents, &gitlab.TreeNode{
    88  					ID:   "",
    89  					Name: item.Name(),
    90  					Type: tp,
    91  					Path: localPath + "/" + item.Name(),
    92  					Mode: "",
    93  				})
    94  			}
    95  			dRes, _ := json.Marshal(contents)
    96  			rw.Write(dRes)
    97  			return
    98  		}
    99  
   100  		rw.Write([]byte("invalid gitlab query"))
   101  	})
   102  	defer teardown()
   103  
   104  	gith := &gitlabHelper{
   105  		Client: client,
   106  		Meta: &utils.Content{GitlabContent: utils.GitlabContent{
   107  			PId: 9999,
   108  		}},
   109  	}
   110  	var r AsyncReader = &gitlabReader{gith}
   111  	_, err := r.ReadFile("example/metadata.yaml")
   112  	assert.NoError(t, err)
   113  }