|
| 1 | +package databaseuri_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "code.cloudfoundry.org/cnbapplifecycle/pkg/databaseuri" |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDatabaseuri(t *testing.T) { |
| 12 | + RegisterFailHandler(Fail) |
| 13 | + RunSpecs(t, "Databaseuri Suite") |
| 14 | +} |
| 15 | + |
| 16 | +var _ = Describe("ParseDatabaseURI", func() { |
| 17 | + It("ignores services without credentials.uri", func() { |
| 18 | + services := `{"eg":[{}]}` |
| 19 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 20 | + Expect(err).NotTo(HaveOccurred()) |
| 21 | + Expect(uri).To(BeEmpty()) |
| 22 | + }) |
| 23 | + |
| 24 | + It("returns empty when there are non relational database services", func() { |
| 25 | + services := `{"eg":[{"credentials":{"uri":"sendgrid://foo:bar@host/db"}}]}` |
| 26 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 27 | + Expect(err).NotTo(HaveOccurred()) |
| 28 | + Expect(uri).To(BeEmpty()) |
| 29 | + }) |
| 30 | + |
| 31 | + It("returns empty when there are no services", func() { |
| 32 | + services := "{}" |
| 33 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 34 | + Expect(err).NotTo(HaveOccurred()) |
| 35 | + Expect(uri).To(BeEmpty()) |
| 36 | + }) |
| 37 | + |
| 38 | + It("returns empty when services are empty", func() { |
| 39 | + services := "" |
| 40 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 41 | + Expect(err).NotTo(HaveOccurred()) |
| 42 | + Expect(uri).To(BeEmpty()) |
| 43 | + }) |
| 44 | + |
| 45 | + Context("when there are relational database services", func() { |
| 46 | + Context("with a mysql URI", func() { |
| 47 | + It("changes the scheme to mysql2", func() { |
| 48 | + services := `{"eg":[{"credentials":{"uri":"mysql://username:password@host/db"}}]}` |
| 49 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 50 | + Expect(err).NotTo(HaveOccurred()) |
| 51 | + Expect(uri).To(Equal("mysql2://username:password@host/db")) |
| 52 | + }) |
| 53 | + }) |
| 54 | + Context("with a mysql2 URI", func() { |
| 55 | + It("returns the URI unchanged", func() { |
| 56 | + services := `{"eg":[{"credentials":{"uri":"mysql2://username:password@host/db"}}]}` |
| 57 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 58 | + Expect(err).NotTo(HaveOccurred()) |
| 59 | + Expect(uri).To(Equal("mysql2://username:password@host/db")) |
| 60 | + }) |
| 61 | + }) |
| 62 | + Context("with a postgres URI", func() { |
| 63 | + It("returns the URI unchanged", func() { |
| 64 | + services := `{"eg":[{"credentials":{"uri":"postgres://username:password@host/db"}}]}` |
| 65 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 66 | + Expect(err).NotTo(HaveOccurred()) |
| 67 | + Expect(uri).To(Equal("postgres://username:password@host/db")) |
| 68 | + }) |
| 69 | + }) |
| 70 | + Context("with a postgresql URI", func() { |
| 71 | + It("changes the scheme to postgres", func() { |
| 72 | + services := `{"eg":[{"credentials":{"uri":"postgresql://username:password@host/db"}}]}` |
| 73 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 74 | + Expect(err).NotTo(HaveOccurred()) |
| 75 | + Expect(uri).To(Equal("postgres://username:password@host/db")) |
| 76 | + }) |
| 77 | + }) |
| 78 | + Context("with multiple relational database URIs", func() { |
| 79 | + It("returns the first one found", func() { |
| 80 | + services := `{ |
| 81 | + "abc":[{"credentials":{"uri":"postgres://username:password@host/db1"}}], |
| 82 | + "def":[{"credentials":{"uri":"postgres://username:password@host/db2"}}] |
| 83 | + }` |
| 84 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 85 | + Expect(err).NotTo(HaveOccurred()) |
| 86 | + Expect(uri).To(Equal("postgres://username:password@host/db1")) |
| 87 | + }) |
| 88 | + }) |
| 89 | + Context("with an invalid URI", func() { |
| 90 | + It("returns an empty string", func() { |
| 91 | + services := `{"eg":[{"credentials":{"uri":"postgresql://invalid:password@host/%a"}}]}` |
| 92 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 93 | + Expect(err).NotTo(HaveOccurred()) |
| 94 | + Expect(uri).To(Equal("")) |
| 95 | + }) |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + It("handles multiple services correctly", func() { |
| 100 | + services := `{ |
| 101 | + "abc":[{"credentials":{"uri":"u1"}}], |
| 102 | + "def":[{"other":"data"}], |
| 103 | + "ghi":[{"credentials":{"other":"data"}}], |
| 104 | + "jkl":[{},{"credentials":{"uri":"mysql://username:password@host/db"}}] |
| 105 | + }` |
| 106 | + uri, err := databaseuri.ParseDatabaseURI(services) |
| 107 | + Expect(err).NotTo(HaveOccurred()) |
| 108 | + Expect(uri).To(Equal("mysql2://username:password@host/db")) |
| 109 | + }) |
| 110 | +}) |
0 commit comments