github.com/YousefHaggyHeroku/pack@v1.5.5/internal/registry/registry_cache_test.go (about) 1 package registry 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/url" 7 "os" 8 "path/filepath" 9 "strings" 10 "testing" 11 "time" 12 13 "github.com/heroku/color" 14 "github.com/sclevine/spec" 15 "github.com/sclevine/spec/report" 16 "gopkg.in/src-d/go-git.v4" 17 "gopkg.in/src-d/go-git.v4/plumbing/object" 18 19 ilogging "github.com/YousefHaggyHeroku/pack/internal/logging" 20 "github.com/YousefHaggyHeroku/pack/logging" 21 h "github.com/YousefHaggyHeroku/pack/testhelpers" 22 ) 23 24 func TestRegistryCache(t *testing.T) { 25 color.Disable(true) 26 spec.Run(t, "RegistryCache", testRegistryCache, spec.Parallel(), spec.Report(report.Terminal{})) 27 } 28 29 func testRegistryCache(t *testing.T, when spec.G, it spec.S) { 30 var ( 31 tmpDir string 32 err error 33 registryFixture string 34 outBuf bytes.Buffer 35 logger logging.Logger 36 ) 37 38 it.Before(func() { 39 logger = ilogging.NewLogWithWriters(&outBuf, &outBuf) 40 41 tmpDir, err = ioutil.TempDir("", "registry") 42 h.AssertNil(t, err) 43 44 registryFixture = h.CreateRegistryFixture(t, tmpDir, filepath.Join("..", "..", "testdata", "registry")) 45 }) 46 47 it.After(func() { 48 err := os.RemoveAll(tmpDir) 49 h.AssertNil(t, err) 50 }) 51 52 when("#NewDefaultRegistryCache", func() { 53 it("creates a RegistryCache with default URL", func() { 54 registryCache, err := NewDefaultRegistryCache(logger, tmpDir) 55 h.AssertNil(t, err) 56 normalizedURL, err := url.Parse("https://github.com/buildpacks/registry-index") 57 h.AssertNil(t, err) 58 59 h.AssertEq(t, registryCache.url, normalizedURL) 60 }) 61 }) 62 63 when("#NewRegistryCache", func() { 64 when("home doesn't exist", func() { 65 it("fails to create a registry cache", func() { 66 _, err := NewRegistryCache(logger, "/tmp/not-exist", "not-here") 67 h.AssertError(t, err, "finding home") 68 }) 69 }) 70 71 when("registryURL isn't a valid url", func() { 72 it("fails to create a registry cache", func() { 73 _, err := NewRegistryCache(logger, tmpDir, "://bad-uri") 74 h.AssertError(t, err, "parsing registry url") 75 }) 76 }) 77 78 it("creates a RegistryCache", func() { 79 registryCache, err := NewRegistryCache(logger, tmpDir, registryFixture) 80 h.AssertNil(t, err) 81 expectedRoot := filepath.Join(tmpDir, "registry") 82 actualRoot := strings.Split(registryCache.Root, "-")[0] 83 h.AssertEq(t, actualRoot, expectedRoot) 84 }) 85 }) 86 87 when("#LocateBuildpack", func() { 88 var ( 89 registryCache Cache 90 ) 91 92 it.Before(func() { 93 registryCache, err = NewRegistryCache(logger, tmpDir, registryFixture) 94 h.AssertNil(t, err) 95 }) 96 97 it("locates a buildpack without version (name len 3)", func() { 98 bp, err := registryCache.LocateBuildpack("example/foo") 99 h.AssertNil(t, err) 100 h.AssertNotNil(t, bp) 101 102 h.AssertEq(t, bp.Namespace, "example") 103 h.AssertEq(t, bp.Name, "foo") 104 h.AssertEq(t, bp.Version, "1.2.0") 105 }) 106 107 it("locates a buildpack without version (name len 4)", func() { 108 bp, err := registryCache.LocateBuildpack("example/java") 109 h.AssertNil(t, err) 110 h.AssertNotNil(t, bp) 111 112 h.AssertEq(t, bp.Namespace, "example") 113 h.AssertEq(t, bp.Name, "java") 114 h.AssertEq(t, bp.Version, "1.0.0") 115 }) 116 117 it("locates a buildpack with version", func() { 118 bp, err := registryCache.LocateBuildpack("example/foo@1.1.0") 119 h.AssertNil(t, err) 120 h.AssertNotNil(t, bp) 121 122 h.AssertEq(t, bp.Namespace, "example") 123 h.AssertEq(t, bp.Name, "foo") 124 h.AssertEq(t, bp.Version, "1.1.0") 125 }) 126 127 it("returns error if can't parse buildpack id", func() { 128 _, err := registryCache.LocateBuildpack("quack") 129 h.AssertError(t, err, "parsing buildpacks registry id") 130 }) 131 132 it("returns error if buildpack id is empty", func() { 133 _, err := registryCache.LocateBuildpack("example/") 134 h.AssertError(t, err, "'name' cannot be empty") 135 }) 136 137 it("returns error if can't find buildpack with requested id", func() { 138 _, err := registryCache.LocateBuildpack("example/qu") 139 h.AssertError(t, err, "reading entry") 140 }) 141 142 it("returns error if can't find buildpack with requested version", func() { 143 _, err := registryCache.LocateBuildpack("example/foo@3.5.6") 144 h.AssertError(t, err, "could not find version") 145 }) 146 }) 147 148 when("#Refresh", func() { 149 var ( 150 registryCache Cache 151 ) 152 153 it.Before(func() { 154 registryCache, err = NewRegistryCache(logger, tmpDir, registryFixture) 155 h.AssertNil(t, err) 156 }) 157 158 when("registry has new commits", func() { 159 it("pulls the latest index", func() { 160 h.AssertNil(t, registryCache.Refresh()) 161 h.AssertGitHeadEq(t, registryFixture, registryCache.Root) 162 163 r, err := git.PlainOpen(registryFixture) 164 h.AssertNil(t, err) 165 166 w, err := r.Worktree() 167 h.AssertNil(t, err) 168 169 commit, err := w.Commit("second", &git.CommitOptions{ 170 Author: &object.Signature{ 171 Name: "John Doe", 172 Email: "john@doe.org", 173 When: time.Now(), 174 }, 175 }) 176 h.AssertNil(t, err) 177 178 _, err = r.CommitObject(commit) 179 h.AssertNil(t, err) 180 181 h.AssertNil(t, registryCache.Refresh()) 182 h.AssertGitHeadEq(t, registryFixture, registryCache.Root) 183 }) 184 }) 185 186 when("Root is an empty string", func() { 187 it("fails to refresh", func() { 188 registryCache.Root = "" 189 err = registryCache.Refresh() 190 h.AssertError(t, err, "initializing") 191 }) 192 }) 193 }) 194 195 when("#Initialize", func() { 196 var ( 197 registryCache Cache 198 ) 199 200 it.Before(func() { 201 registryCache, err = NewRegistryCache(logger, tmpDir, registryFixture) 202 h.AssertNil(t, err) 203 }) 204 205 when("root is empty string", func() { 206 it.Before(func() { 207 registryCache.Root = "" 208 }) 209 210 it("fails to create registry cache", func() { 211 err = registryCache.Initialize() 212 h.AssertError(t, err, "creating registry cache") 213 }) 214 215 when("url is empty string", func() { 216 it("fails to clone cache", func() { 217 registryCache.url = &url.URL{} 218 219 err = registryCache.Initialize() 220 h.AssertError(t, err, "cloning remote registry") 221 }) 222 }) 223 }) 224 }) 225 226 when("#Commit", func() { 227 bp := Buildpack{ 228 Namespace: "example", 229 Name: "python", 230 Version: "1.0.0", 231 Yanked: false, 232 Address: "example.com", 233 } 234 235 var ( 236 registryCache Cache 237 msg = "test commit message" 238 username = "supra08" 239 ) 240 241 it.Before(func() { 242 registryCache, err = NewRegistryCache(logger, tmpDir, registryFixture) 243 h.AssertNil(t, err) 244 245 err = registryCache.CreateCache() 246 h.AssertNil(t, err) 247 }) 248 249 when("correct buildpack and commit message is passed", func() { 250 it("creates a file and a commit", func() { 251 h.AssertNil(t, registryCache.Commit(bp, username, msg)) 252 }) 253 }) 254 255 when("empty commit message is passed", func() { 256 it("fails to create commit", func() { 257 err := registryCache.Commit(bp, username, "") 258 h.AssertError(t, err, "invalid commit message") 259 }) 260 }) 261 262 when("Root is an empty string", func() { 263 it("fails to create commit", func() { 264 registryCache.Root = "" 265 err = registryCache.Commit(bp, username, msg) 266 h.AssertError(t, err, "opening registry cache: repository does not exist") 267 }) 268 }) 269 270 when("name is empty in buildpack", func() { 271 it("fails to create commit", func() { 272 bp := Buildpack{ 273 Namespace: "example", 274 Name: "", 275 Version: "1.0.0", 276 Yanked: false, 277 Address: "example.com", 278 } 279 err = registryCache.Commit(bp, username, msg) 280 h.AssertError(t, err, "'name' cannot be empty") 281 }) 282 }) 283 284 when("namespace is empty in buildpack", func() { 285 it("fails to create commit", func() { 286 bp := Buildpack{ 287 Namespace: "", 288 Name: "python", 289 Version: "1.0.0", 290 Yanked: false, 291 Address: "example.com", 292 } 293 err = registryCache.Commit(bp, username, msg) 294 h.AssertError(t, err, "'namespace' cannot be empty") 295 }) 296 }) 297 }) 298 }