github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/dependencymatrix/matrix_test.go (about) 1 // +build unit 2 3 package dependencymatrix_test 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/olli-ai/jx/v2/pkg/tests" 13 14 v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1" 15 "github.com/olli-ai/jx/v2/pkg/dependencymatrix" 16 17 "github.com/olli-ai/jx/v2/pkg/util" 18 19 "github.com/stretchr/testify/assert" 20 ) 21 22 func TestUpdateSimpleDependencyMatrix(t *testing.T) { 23 dir, err := ioutil.TempDir("", "") 24 assert.NoError(t, err) 25 matrixDir := filepath.Join(dir, dependencymatrix.DependencyMatrixDirName) 26 err = os.MkdirAll(matrixDir, 0700) 27 assert.NoError(t, err) 28 matrixYamlPath := filepath.Join(matrixDir, "matrix.yaml") 29 err = util.CopyFile(filepath.Join("testdata", "simple_matrix", "dependency-matrix", "matrix.yaml"), matrixYamlPath) 30 assert.NoError(t, err) 31 owner := "acme" 32 repo := "roadrunner" 33 component := "cheese" 34 toVersion := "0.0.2" 35 fromVersion := "0.0.1" 36 toTag := fmt.Sprintf("v%s", toVersion) 37 fromTag := fmt.Sprintf("v%s", fromVersion) 38 host := "fake.git" 39 update := v1.DependencyUpdate{ 40 DependencyUpdateDetails: v1.DependencyUpdateDetails{ 41 Host: host, 42 Owner: owner, 43 Repo: repo, 44 Component: component, 45 URL: fmt.Sprintf("https://%s/%s/%s.git", host, owner, repo), 46 ToReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, toTag), 47 ToVersion: toVersion, 48 ToReleaseName: toVersion, 49 FromReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, fromTag), 50 FromReleaseName: fromVersion, 51 FromVersion: fromVersion, 52 }, 53 Paths: make([]v1.DependencyUpdatePath, 0), 54 } 55 err = dependencymatrix.UpdateDependencyMatrix(dir, &update) 56 assert.NoError(t, err) 57 tests.AssertTextFileContentsEqual(t, filepath.Join("testdata", "simple_matrix", "matrix.golden.yaml"), matrixYamlPath) 58 } 59 60 func TestUpdateOneDegreeDependencyMatrix(t *testing.T) { 61 dir, err := ioutil.TempDir("", "") 62 assert.NoError(t, err) 63 matrixDir := filepath.Join(dir, dependencymatrix.DependencyMatrixDirName) 64 err = os.MkdirAll(matrixDir, 0700) 65 assert.NoError(t, err) 66 matrixYamlPath := filepath.Join(matrixDir, "matrix.yaml") 67 err = util.CopyFile(filepath.Join("testdata", "one_degree_matrix", "dependency-matrix", "matrix.yaml"), matrixYamlPath) 68 assert.NoError(t, err) 69 owner := "acme" 70 repo := "roadrunner" 71 viaRepo := "wiley" 72 toVersion := "0.0.2" 73 fromVersion := "0.0.1" 74 toTag := fmt.Sprintf("v%s", toVersion) 75 fromTag := fmt.Sprintf("v%s", fromVersion) 76 host := "fake.git" 77 update := v1.DependencyUpdate{ 78 DependencyUpdateDetails: v1.DependencyUpdateDetails{ 79 Host: host, 80 Owner: owner, 81 Repo: repo, 82 URL: fmt.Sprintf("https://%s/%s/%s.git", host, owner, repo), 83 ToReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, toTag), 84 ToVersion: toVersion, 85 ToReleaseName: toVersion, 86 FromReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, fromTag), 87 FromReleaseName: fromVersion, 88 FromVersion: fromVersion, 89 }, 90 Paths: []v1.DependencyUpdatePath{ 91 { 92 { 93 Host: host, 94 Owner: owner, 95 Repo: viaRepo, 96 URL: fmt.Sprintf("https://%s/%s/%s.git", host, owner, viaRepo), 97 ToReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, viaRepo, toTag), 98 ToVersion: toVersion, 99 ToReleaseName: toVersion, 100 FromReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, viaRepo, fromTag), 101 FromReleaseName: fromVersion, 102 FromVersion: fromVersion, 103 }, 104 }, 105 }, 106 } 107 err = dependencymatrix.UpdateDependencyMatrix(dir, &update) 108 assert.NoError(t, err) 109 tests.AssertTextFileContentsEqual(t, filepath.Join("testdata", "one_degree_matrix", "matrix.golden.yaml"), matrixYamlPath) 110 } 111 112 func TestUpdateTwoPathsDependencyMatrix(t *testing.T) { 113 dir, err := ioutil.TempDir("", "") 114 assert.NoError(t, err) 115 matrixDir := filepath.Join(dir, dependencymatrix.DependencyMatrixDirName) 116 err = os.MkdirAll(matrixDir, 0700) 117 assert.NoError(t, err) 118 matrixYamlPath := filepath.Join(matrixDir, "matrix.yaml") 119 err = util.CopyFile(filepath.Join("testdata", "two_paths_matrix", "dependency-matrix", "matrix.yaml"), matrixYamlPath) 120 assert.NoError(t, err) 121 owner := "acme" 122 repo := "roadrunner" 123 viaRepo := "wiley" 124 toVersion := "0.0.2" 125 fromVersion := "0.0.1" 126 toTag := fmt.Sprintf("v%s", toVersion) 127 fromTag := fmt.Sprintf("v%s", fromVersion) 128 host := "fake.git" 129 update := v1.DependencyUpdate{ 130 DependencyUpdateDetails: v1.DependencyUpdateDetails{ 131 Host: host, 132 Owner: owner, 133 Repo: repo, 134 URL: fmt.Sprintf("https://%s/%s/%s.git", host, owner, repo), 135 ToReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, toTag), 136 ToVersion: toVersion, 137 ToReleaseName: toVersion, 138 FromReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, fromTag), 139 FromReleaseName: fromVersion, 140 FromVersion: fromVersion, 141 }, 142 Paths: []v1.DependencyUpdatePath{ 143 { 144 { 145 Host: host, 146 Owner: owner, 147 Repo: viaRepo, 148 URL: fmt.Sprintf("https://%s/%s/%s.git", host, owner, viaRepo), 149 ToReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, viaRepo, toTag), 150 ToVersion: toVersion, 151 ToReleaseName: toVersion, 152 FromReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, viaRepo, fromTag), 153 FromReleaseName: fromVersion, 154 FromVersion: fromVersion, 155 }, 156 }, 157 }, 158 } 159 err = dependencymatrix.UpdateDependencyMatrix(dir, &update) 160 assert.NoError(t, err) 161 tests.AssertTextFileContentsEqual(t, filepath.Join("testdata", "two_paths_matrix", "matrix.golden.yaml"), matrixYamlPath) 162 } 163 164 func TestUpdateTwoDegreeDependencyMatrix(t *testing.T) { 165 dir, err := ioutil.TempDir("", "") 166 assert.NoError(t, err) 167 matrixDir := filepath.Join(dir, dependencymatrix.DependencyMatrixDirName) 168 err = os.MkdirAll(matrixDir, 0700) 169 assert.NoError(t, err) 170 matrixYamlPath := filepath.Join(matrixDir, "matrix.yaml") 171 err = util.CopyFile(filepath.Join("testdata", "two_degree_matrix", "dependency-matrix", "matrix.yaml"), matrixYamlPath) 172 assert.NoError(t, err) 173 owner := "acme" 174 repo := "roadrunner" 175 viaRepo := "wiley" 176 via2Repo := "coyote" 177 toVersion := "0.0.2" 178 fromVersion := "0.0.1" 179 toTag := fmt.Sprintf("v%s", toVersion) 180 fromTag := fmt.Sprintf("v%s", fromVersion) 181 host := "fake.git" 182 update := v1.DependencyUpdate{ 183 DependencyUpdateDetails: v1.DependencyUpdateDetails{ 184 Host: host, 185 Owner: owner, 186 Repo: repo, 187 URL: fmt.Sprintf("https://%s/%s/%s.git", host, owner, repo), 188 ToReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, toTag), 189 ToVersion: toVersion, 190 ToReleaseName: toVersion, 191 FromReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, fromTag), 192 FromReleaseName: fromVersion, 193 FromVersion: fromVersion, 194 }, 195 Paths: []v1.DependencyUpdatePath{ 196 { 197 { 198 Host: host, 199 Owner: owner, 200 Repo: viaRepo, 201 URL: fmt.Sprintf("https://%s/%s/%s.git", host, owner, viaRepo), 202 ToReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, viaRepo, toTag), 203 ToVersion: toVersion, 204 ToReleaseName: toVersion, 205 FromReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, viaRepo, fromTag), 206 FromReleaseName: fromVersion, 207 FromVersion: fromVersion, 208 }, 209 { 210 Host: host, 211 Owner: owner, 212 Repo: via2Repo, 213 URL: fmt.Sprintf("https://%s/%s/%s.git", host, owner, via2Repo), 214 ToReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, via2Repo, toTag), 215 ToVersion: toVersion, 216 ToReleaseName: toVersion, 217 FromReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, via2Repo, fromTag), 218 FromReleaseName: fromVersion, 219 FromVersion: fromVersion, 220 }, 221 }, 222 }, 223 } 224 err = dependencymatrix.UpdateDependencyMatrix(dir, &update) 225 assert.NoError(t, err) 226 tests.AssertTextFileContentsEqual(t, filepath.Join("testdata", "two_degree_matrix", "matrix.golden.yaml"), matrixYamlPath) 227 } 228 229 func TestCreateDependencyMatrix(t *testing.T) { 230 dir, err := ioutil.TempDir("", "") 231 assert.NoError(t, err) 232 matrixDir := filepath.Join(dir, dependencymatrix.DependencyMatrixDirName) 233 matrixYamlPath := filepath.Join(matrixDir, "matrix.yaml") 234 owner := "acme" 235 repo := "roadrunner" 236 component := "cheese" 237 toVersion := "0.0.2" 238 fromVersion := "0.0.1" 239 toTag := fmt.Sprintf("v%s", toVersion) 240 fromTag := fmt.Sprintf("v%s", fromVersion) 241 host := "fake.git" 242 update := v1.DependencyUpdate{ 243 DependencyUpdateDetails: v1.DependencyUpdateDetails{ 244 Host: host, 245 Owner: owner, 246 Repo: repo, 247 Component: component, 248 URL: fmt.Sprintf("https://%s/%s/%s.git", host, owner, repo), 249 ToReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, toTag), 250 ToVersion: toVersion, 251 ToReleaseName: toVersion, 252 FromReleaseHTMLURL: fmt.Sprintf("https://%s/%s/%s/releases/%s", host, owner, repo, fromTag), 253 FromReleaseName: fromVersion, 254 FromVersion: fromVersion, 255 }, 256 Paths: make([]v1.DependencyUpdatePath, 0), 257 } 258 err = dependencymatrix.UpdateDependencyMatrix(dir, &update) 259 assert.NoError(t, err) 260 tests.AssertTextFileContentsEqual(t, filepath.Join("testdata", "new_matrix", "matrix.golden.yaml"), matrixYamlPath) 261 } 262 263 func TestFindVersionForDependency(t *testing.T) { 264 dir := filepath.Join("testdata", "two_degree_matrix") 265 266 matrix, err := dependencymatrix.LoadDependencyMatrix(dir) 267 assert.NoError(t, err) 268 269 version, err := matrix.FindVersionForDependency("fake.git", "acme", "roadrunner") 270 assert.NoError(t, err) 271 assert.Equal(t, "0.0.1", version) 272 273 _, err = matrix.FindVersionForDependency("doesnotexist", "doesnotexist", "doesnotexist") 274 assert.NotNil(t, err) 275 assert.Equal(t, "could not find a dependency on host doesnotexist, owner doesnotexist, repo doesnotexist in the dependency matrix", err.Error()) 276 }