github.com/oam-dev/kubevela@v1.9.11/pkg/addon/source_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  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestPathWithParent(t *testing.T) {
    26  	testCases := []struct {
    27  		readPath       string
    28  		parentPath     string
    29  		actualReadPath string
    30  	}{
    31  		{
    32  			readPath:       "example",
    33  			parentPath:     "experimental",
    34  			actualReadPath: "experimental/example",
    35  		},
    36  		{
    37  			readPath:       "example/",
    38  			parentPath:     "experimental",
    39  			actualReadPath: "experimental/example/",
    40  		},
    41  	}
    42  	for _, tc := range testCases {
    43  		res := pathWithParent(tc.readPath, tc.parentPath)
    44  		assert.Equal(t, res, tc.actualReadPath)
    45  	}
    46  }
    47  
    48  func TestConvert2OssItem(t *testing.T) {
    49  	subPath := "sub-addons"
    50  	reader, err := NewAsyncReader("ep-beijing.com", "bucket", "", subPath, "", ossType)
    51  
    52  	assert.NoError(t, err)
    53  
    54  	o, ok := reader.(*ossReader)
    55  	assert.Equal(t, ok, true)
    56  	var testFiles = []File{
    57  		{
    58  			Name: "sub-addons/fluxcd",
    59  			Size: 0,
    60  		},
    61  		{
    62  			Name: "sub-addons/fluxcd/metadata.yaml",
    63  			Size: 100,
    64  		},
    65  		{
    66  			Name: "sub-addons/fluxcd/definitions/",
    67  			Size: 0,
    68  		},
    69  		{
    70  			Name: "sub-addons/fluxcd/definitions/helm-release.yaml",
    71  			Size: 100,
    72  		},
    73  		{
    74  			Name: "sub-addons/example/resources/configmap.yaml",
    75  			Size: 100,
    76  		},
    77  		{
    78  			Name: "sub-addons/example/metadata.yaml",
    79  			Size: 100,
    80  		},
    81  	}
    82  	var expectItemCase = map[string]SourceMeta{
    83  		"fluxcd": {
    84  			Name: "fluxcd",
    85  			Items: []Item{
    86  				&OSSItem{
    87  					tp:   FileType,
    88  					path: "fluxcd/definitions/helm-release.yaml",
    89  					name: "helm-release.yaml",
    90  				},
    91  				&OSSItem{
    92  					tp:   FileType,
    93  					path: "fluxcd/metadata.yaml",
    94  					name: "metadata.yaml",
    95  				},
    96  			},
    97  		},
    98  		"example": {
    99  			Name: "example",
   100  			Items: []Item{
   101  				&OSSItem{
   102  					tp:   FileType,
   103  					path: "example/metadata.yaml",
   104  					name: "metadata.yaml",
   105  				},
   106  				&OSSItem{
   107  					tp:   FileType,
   108  					path: "example/resources/configmap.yaml",
   109  					name: "configmap.yaml",
   110  				},
   111  			},
   112  		},
   113  	}
   114  	addonMetas := o.convertOSSFiles2Addons(testFiles)
   115  	assert.Equal(t, expectItemCase, addonMetas)
   116  
   117  }
   118  
   119  func TestSafeCopy(t *testing.T) {
   120  	var git *GitAddonSource
   121  	sgit := git.SafeCopy()
   122  	assert.Nil(t, sgit)
   123  	git = &GitAddonSource{URL: "http://github.com/kubevela", Path: "addons", Token: "123456"}
   124  	sgit = git.SafeCopy()
   125  	assert.Empty(t, sgit.Token)
   126  	assert.Equal(t, "http://github.com/kubevela", sgit.URL)
   127  	assert.Equal(t, "addons", sgit.Path)
   128  
   129  	var gitee *GiteeAddonSource
   130  	sgitee := gitee.SafeCopy()
   131  	assert.Nil(t, sgitee)
   132  	gitee = &GiteeAddonSource{URL: "http://gitee.com/kubevela", Path: "addons", Token: "123456"}
   133  	sgitee = gitee.SafeCopy()
   134  	assert.Empty(t, sgitee.Token)
   135  	assert.Equal(t, "http://gitee.com/kubevela", sgitee.URL)
   136  	assert.Equal(t, "addons", sgitee.Path)
   137  
   138  	var gitlab *GitlabAddonSource
   139  	sgitlab := gitlab.SafeCopy()
   140  	assert.Nil(t, sgitlab)
   141  	gitlab = &GitlabAddonSource{URL: "http://gitlab.com/kubevela", Repo: "vela", Path: "addons", Token: "123456"}
   142  	sgitlab = gitlab.SafeCopy()
   143  	assert.Empty(t, sgitlab.Token)
   144  	assert.Equal(t, "http://gitlab.com/kubevela", sgitlab.URL)
   145  	assert.Equal(t, "addons", sgitlab.Path)
   146  	assert.Equal(t, "vela", sgitlab.Repo)
   147  
   148  	var helm *HelmSource
   149  	shelm := helm.SafeCopy()
   150  	assert.Nil(t, shelm)
   151  	helm = &HelmSource{URL: "https://hub.vela.com/chartrepo/addons", Username: "user123", Password: "pass456"}
   152  	shelm = helm.SafeCopy()
   153  	assert.Empty(t, shelm.Username)
   154  	assert.Empty(t, shelm.Password)
   155  	assert.Equal(t, "https://hub.vela.com/chartrepo/addons", shelm.URL)
   156  }