|
| 1 | +package cassandra |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/url" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/gocql/gocql" |
| 10 | + cas "github.com/golang-migrate/migrate/v4/database/cassandra" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAstra(t *testing.T) { |
| 14 | + type mockResult struct { |
| 15 | + timeout time.Duration |
| 16 | + |
| 17 | + // NewClusterFromBundle |
| 18 | + path string |
| 19 | + username string |
| 20 | + password string |
| 21 | + |
| 22 | + // NewClusterFromURL |
| 23 | + apiUrl string |
| 24 | + databaseID string |
| 25 | + token string |
| 26 | + } |
| 27 | + |
| 28 | + var ( |
| 29 | + errNewClusterFromBundle = errors.New("NewClusterFromBundle") |
| 30 | + errNewClusterFromURL = errors.New("NewClusterFromURL") |
| 31 | + ) |
| 32 | + |
| 33 | + test := func(t *testing.T, url string) (mockResult, error) { |
| 34 | + t.Helper() |
| 35 | + |
| 36 | + var mr mockResult |
| 37 | + |
| 38 | + // Since we can't actually call the Astra API, we mock the calls and return an error so we never dial. |
| 39 | + cas.GocqlastraNewClusterFromBundle = func(path string, username string, password string, timeout time.Duration) (*gocql.ClusterConfig, error) { |
| 40 | + mr.path = path |
| 41 | + mr.username = username |
| 42 | + mr.password = password |
| 43 | + mr.timeout = timeout |
| 44 | + return nil, errNewClusterFromBundle |
| 45 | + } |
| 46 | + |
| 47 | + cas.GocqlastraNewClusterFromURL = func(apiUrl string, databaseID string, token string, timeout time.Duration) (*gocql.ClusterConfig, error) { |
| 48 | + mr.apiUrl = apiUrl |
| 49 | + mr.databaseID = databaseID |
| 50 | + mr.token = token |
| 51 | + mr.timeout = timeout |
| 52 | + return nil, errNewClusterFromURL |
| 53 | + } |
| 54 | + |
| 55 | + astra := &Astra{} |
| 56 | + |
| 57 | + _, err := astra.Open(url) |
| 58 | + return mr, err |
| 59 | + } |
| 60 | + |
| 61 | + t.Run("Token", func(t *testing.T) { |
| 62 | + mr, err := test(t, "astra:///testks?token=token&database_id=database_id") |
| 63 | + if err != errNewClusterFromURL { |
| 64 | + t.Error("Expected", errNewClusterFromURL, "but got", err) |
| 65 | + } |
| 66 | + if mr.token != "token" { |
| 67 | + t.Error("Expected token to be 'token' but got", mr.token) |
| 68 | + } |
| 69 | + if mr.databaseID != "database_id" { |
| 70 | + t.Error("Expected database_id to be 'database_id' but got", mr.databaseID) |
| 71 | + } |
| 72 | + }) |
| 73 | + t.Run("Bundle", func(t *testing.T) { |
| 74 | + mr, err := test(t, "astra:///testks?bundle=bundle.zip&token=AstraCS:password") |
| 75 | + if err != errNewClusterFromBundle { |
| 76 | + t.Error("Expected", errNewClusterFromBundle, "but got", err) |
| 77 | + } |
| 78 | + if mr.path != "bundle.zip" { |
| 79 | + t.Error("Expected path to be 'bundle.zip' but got", mr.path) |
| 80 | + } |
| 81 | + if mr.username != "token" { |
| 82 | + t.Error("Expected username to be 'token' but got", mr.username) |
| 83 | + } |
| 84 | + if mr.password != "AstraCS:password" { |
| 85 | + t.Error("Expected password to be 'AstraCS:password' but got", mr.password) |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("No Keyspace", func(t *testing.T) { |
| 90 | + astra := &Astra{} |
| 91 | + _, err := astra.Open("astra://") |
| 92 | + if err != cas.ErrNoKeyspace { |
| 93 | + t.Error("Expected", cas.ErrNoKeyspace, "but got", err) |
| 94 | + } |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("AstraMissing", func(t *testing.T) { |
| 98 | + astra := &Astra{} |
| 99 | + _, err := astra.Open("astra:///testks") |
| 100 | + if err != cas.ErrAstraMissing { |
| 101 | + t.Error("Expected", cas.ErrAstraMissing, "but got", err) |
| 102 | + } |
| 103 | + }) |
| 104 | + t.Run("No Token", func(t *testing.T) { |
| 105 | + astra := &Astra{} |
| 106 | + _, err := astra.Open("astra:///testks?database_id=database_id") |
| 107 | + if err != cas.ErrAstraMissing { |
| 108 | + t.Error("Expected", cas.ErrAstraMissing, "but got", err) |
| 109 | + } |
| 110 | + }) |
| 111 | + t.Run("No DatabaseID", func(t *testing.T) { |
| 112 | + astra := &Astra{} |
| 113 | + _, err := astra.Open("astra:///testks?token=AstraCS:password") |
| 114 | + if err != cas.ErrAstraMissing { |
| 115 | + t.Error("Expected", cas.ErrAstraMissing, "but got", err) |
| 116 | + } |
| 117 | + }) |
| 118 | + t.Run("No Bundle", func(t *testing.T) { |
| 119 | + astra := &Astra{} |
| 120 | + _, err := astra.Open("astra:///testks?token=AstraCS:password") |
| 121 | + if err != cas.ErrAstraMissing { |
| 122 | + t.Error("Expected", cas.ErrAstraMissing, "but got", err) |
| 123 | + } |
| 124 | + }) |
| 125 | + t.Run("Custom API URL", func(t *testing.T) { |
| 126 | + mr, err := test(t, "astra:///testks?token=token&database_id=database_id&api_url=api_url") |
| 127 | + if err != errNewClusterFromURL { |
| 128 | + t.Error("Expected", errNewClusterFromURL, "but got", err) |
| 129 | + } |
| 130 | + if mr.apiUrl != "api_url" { |
| 131 | + t.Error("Expected api_url to be 'api_url' but got", mr.apiUrl) |
| 132 | + } |
| 133 | + }) |
| 134 | +} |
| 135 | + |
| 136 | +func TestTripleSlashInURLMeansNoHost(t *testing.T) { |
| 137 | + const str = "astra:///testks?token=token&database_id=database_id" |
| 138 | + u, err := url.Parse(str) |
| 139 | + if err != nil { |
| 140 | + t.Fatal(err) |
| 141 | + } |
| 142 | + if u.Host != "" { |
| 143 | + t.Error("Expected host to be empty but got", u.Host) |
| 144 | + } |
| 145 | + if u.Path != "/testks" { |
| 146 | + t.Error("Expected path to be '/testks' but got", u.Path) |
| 147 | + } |
| 148 | +} |
0 commit comments