| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package mongodocstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"net/url" |
| 20 |
"os" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"gocloud.dev/docstore" |
| 24 |
) |
| 25 |
|
| 26 |
func fakeConnectionStringInEnv() func() { |
| 27 |
oldURLVal := os.Getenv("MONGO_SERVER_URL") |
| 28 |
os.Setenv("MONGO_SERVER_URL", "mongodb://localhost") |
| 29 |
return func() { |
| 30 |
os.Setenv("MONGO_SERVER_URL", oldURLVal) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestOpenCollectionURL(t *testing.T) { |
| 35 |
cleanup := fakeConnectionStringInEnv() |
| 36 |
defer cleanup() |
| 37 |
|
| 38 |
tests := []struct { |
| 39 |
URL string |
| 40 |
WantErr bool |
| 41 |
}{ |
| 42 |
// OK. |
| 43 |
{"mongo://mydb/mycollection", false}, |
| 44 |
// Missing database name. |
| 45 |
{"mongo:///mycollection", true}, |
| 46 |
// Missing collection name. |
| 47 |
{"mongo://mydb/", true}, |
| 48 |
// Passing id_field parameter. |
| 49 |
{"mongo://mydb/mycollection?id_field=foo", false}, |
| 50 |
// Passing revision field. |
| 51 |
{"mongo://mydb/mycollection?id_field=foo&revision_field=123", false}, |
| 52 |
// Invalid parameter. |
| 53 |
{"mongo://mydb/mycollection?param=value", true}, |
| 54 |
} |
| 55 |
|
| 56 |
ctx := context.Background() |
| 57 |
for _, test := range tests { |
| 58 |
d, err := docstore.OpenCollection(ctx, test.URL) |
| 59 |
if d != nil { |
| 60 |
defer d.Close() |
| 61 |
} |
| 62 |
if (err != nil) != test.WantErr { |
| 63 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestDefaultDialerOpenCollectionURL(t *testing.T) { |
| 69 |
// Defer cleanup |
| 70 |
oldURLVal := os.Getenv("MONGO_SERVER_URL") |
| 71 |
defer os.Setenv("MONGO_SERVER_URL", oldURLVal) |
| 72 |
|
| 73 |
tests := []struct { |
| 74 |
name string |
| 75 |
currentMongoServerURL string |
| 76 |
currentWantErr bool |
| 77 |
newMongoServerURL string |
| 78 |
newWantErr bool |
| 79 |
}{ |
| 80 |
{ |
| 81 |
name: "fail when MONGO_SERVER_URL is empty / unset", |
| 82 |
currentMongoServerURL: "", |
| 83 |
currentWantErr: true, |
| 84 |
newMongoServerURL: "", |
| 85 |
newWantErr: true, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "fail when updated MONGO_SERVER_URL is empty / unset", |
| 89 |
currentMongoServerURL: "mongodb://localhost", |
| 90 |
currentWantErr: false, |
| 91 |
newMongoServerURL: "", |
| 92 |
newWantErr: true, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "pass when MONGO_SERVER_URL is updated to new value", |
| 96 |
currentMongoServerURL: "mongodb://localhost", |
| 97 |
currentWantErr: false, |
| 98 |
newMongoServerURL: "mongodb://localhost:27017", |
| 99 |
newWantErr: false, |
| 100 |
}, |
| 101 |
} |
| 102 |
|
| 103 |
// Set starting conditions |
| 104 |
d := new(defaultDialer) |
| 105 |
ctx := context.Background() |
| 106 |
mongoURLString := "mongo://mydb/mycollection" |
| 107 |
u, err := url.Parse(mongoURLString) |
| 108 |
if err != nil { |
| 109 |
t.Error(err) |
| 110 |
} |
| 111 |
|
| 112 |
for _, test := range tests { |
| 113 |
t.Run(test.name, func(t *testing.T) { |
| 114 |
// Set MONGO_SERVER_URL |
| 115 |
os.Setenv("MONGO_SERVER_URL", test.currentMongoServerURL) |
| 116 |
_, err = d.OpenCollectionURL(ctx, u) |
| 117 |
if err != nil && !test.currentWantErr { |
| 118 |
t.Error(err) |
| 119 |
} |
| 120 |
|
| 121 |
// Update MONGO_SERVER_URL |
| 122 |
os.Setenv("MONGO_SERVER_URL", test.newMongoServerURL) |
| 123 |
_, err = d.OpenCollectionURL(ctx, u) |
| 124 |
if err != nil && !test.newWantErr { |
| 125 |
t.Error(err) |
| 126 |
} |
| 127 |
|
| 128 |
// Check if the MONGO_SERVER_URL was updated after rotation |
| 129 |
if !test.newWantErr { |
| 130 |
if d.mongoServerURL != test.newMongoServerURL { |
| 131 |
t.Errorf("expected updated MONGO_SERVER_URL to be set to: %s, but got: %s", test.newMongoServerURL, d.mongoServerURL) |
| 132 |
} |
| 133 |
} |
| 134 |
}) |
| 135 |
} |
| 136 |
} |