github.com/kubeshop/testkube@v1.17.23/pkg/executor/content/fetcher_test.go (about) 1 // this test need git to test fetchers 2 package content 3 4 import ( 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/kubeshop/testkube/pkg/utils/test" 10 11 "github.com/stretchr/testify/assert" 12 13 "github.com/kubeshop/testkube/pkg/api/v1/testkube" 14 ) 15 16 // this content is also saved in test repo 17 // in https:///github.com/kubeshop/testkube-docker-action/blob/main/action.yaml 18 // file with \n on end 19 const fileContent = `MIT License 20 21 Copyright (c) 2022 kubeshop 22 23 Permission is hereby granted, free of charge, to any person obtaining a copy 24 of this software and associated documentation files (the "Software"), to deal 25 in the Software without restriction, including without limitation the rights 26 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 27 copies of the Software, and to permit persons to whom the Software is 28 furnished to do so, subject to the following conditions: 29 30 The above copyright notice and this permission notice shall be included in all 31 copies or substantial portions of the Software. 32 33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 34 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 35 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 36 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 37 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 38 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 39 SOFTWARE. 40 ` 41 42 func TestFetcher_Integration(t *testing.T) { 43 test.IntegrationTest(t) 44 t.Parallel() 45 46 f := NewFetcher("") 47 48 t.Run("test fetch uri", func(t *testing.T) { 49 t.Parallel() 50 51 content := &testkube.TestContent{ 52 Type_: string(testkube.TestContentTypeString), 53 Data: fileContent, 54 } 55 56 path, err := f.Fetch(content) 57 assert.NoError(t, err) 58 assert.FileExists(t, path) 59 60 d, err := os.ReadFile(path) 61 assert.NoError(t, err) 62 assert.Equal(t, string(fileContent), string(d)) 63 }) 64 65 t.Run("test fetch git repo based file", func(t *testing.T) { 66 t.Parallel() 67 68 content := &testkube.TestContent{ 69 Type_: string(testkube.TestContentTypeGitFile), 70 Repository: testkube.NewGitRepository("https://github.com/kubeshop/testkube-docker-action.git", "main"). 71 WithPath("LICENSE"), 72 } 73 74 path, err := f.Fetch(content) 75 assert.NoError(t, err) 76 assert.FileExists(t, path) 77 78 d, err := os.ReadFile(path) 79 assert.NoError(t, err) 80 assert.Equal(t, string(fileContent), string(d)) 81 }) 82 83 t.Run("test fetch git root dir", func(t *testing.T) { 84 t.Parallel() 85 86 content := &testkube.TestContent{ 87 Type_: string(testkube.TestContentTypeGitDir), 88 Repository: testkube.NewGitRepository("https://github.com/kubeshop/testkube-docker-action.git", "main"). 89 WithPath(""), 90 } 91 92 path, err := f.Fetch(content) 93 assert.NoError(t, err) 94 assert.FileExists(t, filepath.Join(path, "action.yaml")) 95 assert.FileExists(t, filepath.Join(path, "README.md")) 96 assert.FileExists(t, filepath.Join(path, ".github/update-major-version.yml")) 97 98 }) 99 100 t.Run("test fetch git subdir", func(t *testing.T) { 101 t.Parallel() 102 103 content := &testkube.TestContent{ 104 Type_: string(testkube.TestContentTypeGitDir), 105 Repository: testkube.NewGitRepository("https://github.com/kubeshop/testkube-docker-action.git", "main"). 106 WithPath(".github"), 107 } 108 109 path, err := f.Fetch(content) 110 assert.NoError(t, err) 111 assert.FileExists(t, filepath.Join(path, "update-major-version.yml")) 112 }) 113 114 t.Run("test fetch no content", func(t *testing.T) { 115 t.Parallel() 116 117 content := &testkube.TestContent{ 118 Type_: string(testkube.TestContentTypeEmpty), 119 } 120 121 path, err := f.Fetch(content) 122 assert.NoError(t, err) 123 assert.Equal(t, "", path) 124 }) 125 126 t.Run("test IsGitDir", func(t *testing.T) { 127 t.Parallel() 128 129 t.Run("with file", func(t *testing.T) { 130 t.Parallel() 131 132 repo := testkube.NewGitRepository("https://github.com/kubeshop/testkube-docker-action.git", "main").WithPath("action.yaml") 133 134 contentType, err := f.CalculateGitContentType(*repo) 135 assert.NoError(t, err) 136 assert.Equal(t, "git-file", contentType) 137 }) 138 139 t.Run("with dir", func(t *testing.T) { 140 t.Parallel() 141 142 repo := testkube.NewGitRepository("https://github.com/kubeshop/testkube-docker-action.git", "main").WithPath(".github") 143 144 contentType, err := f.CalculateGitContentType(*repo) 145 assert.NoError(t, err) 146 assert.Equal(t, "git-dir", contentType) 147 }) 148 }) 149 } 150 151 func TestFetcher_GetPathAndWorkingDir(t *testing.T) { 152 t.Parallel() 153 154 var tests = []struct { 155 content testkube.TestContent 156 dataDir string 157 path string 158 workingDir string 159 err error 160 }{ 161 { 162 content: testkube.TestContent{ 163 Type_: string(testkube.TestContentTypeString), 164 }, 165 dataDir: "/data", 166 path: "/data/test-content", 167 workingDir: "", 168 }, 169 { 170 content: testkube.TestContent{ 171 Type_: string(testkube.TestContentTypeFileURI), 172 }, 173 dataDir: "/data", 174 path: "/data/test-content", 175 workingDir: "", 176 }, 177 { 178 content: testkube.TestContent{ 179 Type_: string(testkube.TestContentTypeGit), 180 Repository: &testkube.Repository{ 181 Path: "path", 182 WorkingDir: "working-dir", 183 }, 184 }, 185 dataDir: "/data", 186 path: "/data/repo/path", 187 workingDir: "/data/repo/working-dir", 188 }, 189 { 190 content: testkube.TestContent{ 191 Type_: string(testkube.TestContentTypeGitDir), 192 Repository: &testkube.Repository{ 193 Path: "path", 194 WorkingDir: "working-dir", 195 }, 196 }, 197 dataDir: "/data", 198 path: "/data/repo/path", 199 workingDir: "/data/repo/working-dir", 200 }, 201 { 202 content: testkube.TestContent{ 203 Type_: string(testkube.TestContentTypeGitFile), 204 Repository: &testkube.Repository{ 205 Path: "path", 206 WorkingDir: "working-dir", 207 }, 208 }, 209 dataDir: "/data", 210 path: "/data/repo/path", 211 workingDir: "/data/repo/working-dir", 212 }, 213 { 214 dataDir: "/data", 215 path: "", 216 workingDir: "", 217 }, 218 } 219 220 for _, test := range tests { 221 path, workingDir, err := GetPathAndWorkingDir(&test.content, test.dataDir) 222 223 assert.NoError(t, err) 224 assert.Equal(t, test.path, path) 225 assert.Equal(t, test.workingDir, workingDir) 226 } 227 }