github.com/mailgun/holster/v4@v4.20.0/mongoutil/uri_test.go (about)

     1  package mongoutil_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/mailgun/holster/v4/mongoutil"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"gopkg.in/yaml.v3"
    11  )
    12  
    13  func TestMongoConfig_URIWithOptions(t *testing.T) {
    14  	for _, tt := range []struct {
    15  		cfg  mongoutil.Config
    16  		name string
    17  		uri  string
    18  	}{{
    19  		name: "Bare minimum config",
    20  		cfg: mongoutil.Config{
    21  			URI: "mongodb://127.0.0.1:27017/foo",
    22  		},
    23  		uri: "mongodb://127.0.0.1:27017/foo",
    24  	}, {
    25  		name: "URI parameters appended after URI encoded parameters",
    26  		cfg: mongoutil.Config{
    27  			URI: "mongodb://127.0.0.1:27017/foo?replicaSet=bazz&blah=wow",
    28  			Options: []map[string]interface{}{
    29  				{"replica_set": "bar"},
    30  				{"max_pool_size": 5},
    31  			},
    32  		},
    33  		uri: "mongodb://127.0.0.1:27017/foo?replicaSet=bazz&blah=wow&replicaSet=bar&maxPoolSize=5",
    34  	}, {
    35  		name: "Read preference provided",
    36  		cfg: mongoutil.Config{
    37  			URI: "mongodb://127.0.0.1:27017/foo",
    38  			Options: []map[string]interface{}{
    39  				{"read_preference": "secondaryPreferred"},
    40  			},
    41  		},
    42  		uri: "mongodb://127.0.0.1:27017/foo?readPreference=secondaryPreferred",
    43  	}, {
    44  		name: "Servers and Database provided",
    45  		cfg: mongoutil.Config{
    46  			Servers: []string{
    47  				"mongodb-n01:27017",
    48  				"mongodb-n02:28017",
    49  			},
    50  			Database: "foo",
    51  			Options: []map[string]interface{}{
    52  				{"read_preference": "secondaryPreferred"},
    53  			},
    54  		},
    55  		uri: "mongodb://mongodb-n01:27017,mongodb-n02:28017/foo?readPreference=secondaryPreferred",
    56  	}, {
    57  		name: "Servers and Database provided",
    58  		cfg: mongoutil.Config{
    59  			Servers: []string{
    60  				"mongodb-n01:27017",
    61  				"mongodb-n02:28017",
    62  			},
    63  			Options: []map[string]interface{}{
    64  				{"read_preference": "secondaryPreferred"},
    65  			},
    66  		},
    67  		uri: "mongodb://mongodb-n01:27017,mongodb-n02:28017/?readPreference=secondaryPreferred",
    68  	}, {
    69  		name: "URI provided with no database specified",
    70  		cfg: mongoutil.Config{
    71  			URI: "mongodb://127.0.0.1:27017",
    72  			Options: []map[string]interface{}{
    73  				{"read_preference": "secondaryPreferred"},
    74  			},
    75  		},
    76  		uri: "mongodb://127.0.0.1:27017/?readPreference=secondaryPreferred",
    77  	}, {
    78  		name: "Servers override provided URI",
    79  		cfg: mongoutil.Config{
    80  			URI: "mongodb://127.0.0.1:27017",
    81  			Servers: []string{
    82  				"mongodb-n01:27017",
    83  				"mongodb-n02:28017",
    84  			},
    85  			Options: []map[string]interface{}{
    86  				{"read_preference": "secondaryPreferred"},
    87  			},
    88  		},
    89  		uri: "mongodb://mongodb-n01:27017,mongodb-n02:28017/?readPreference=secondaryPreferred",
    90  	},
    91  	} {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			uri := tt.cfg.URIWithOptions()
    94  			assert.Equal(t, tt.uri, uri)
    95  		})
    96  	}
    97  }
    98  
    99  func TestMongoURIFromJSON(t *testing.T) {
   100  	cfgJSON := []byte(`{
   101  		"uri": "mongodb://127.0.0.1:27017/foo",
   102  		"options": [
   103  			{"compressors": "snappy,zlib"},
   104  			{"replica_set": "v34_queue"},
   105  			{"read_preference": "secondaryPreferred"},
   106  			{"max_pool_size": 5},
   107  			{"ssl": true}
   108  		]
   109  	}`)
   110  	var conf mongoutil.Config
   111  	// When
   112  	err := json.Unmarshal(cfgJSON, &conf)
   113  	// Then
   114  	require.NoError(t, err)
   115  	require.Equal(t,
   116  		"mongodb://127.0.0.1:27017/foo?compressors=snappy,zlib&replicaSet=v34_queue&"+
   117  			"readPreference=secondaryPreferred&maxPoolSize=5&ssl=true", conf.URIWithOptions())
   118  }
   119  
   120  func TestMongoURIFromYAML(t *testing.T) {
   121  	cfgYAML := []byte(`servers:
   122      - mongo-routes-n01-us-east-1.postgun.com:27017
   123      - mongo-routes-n02-us-east-1.postgun.com:27017
   124      - mongo-routes-n03-us-east-1.postgun.com:27017
   125  database: mg_prod
   126  options:
   127      - ssl: true
   128      - tlsCertificateKeyFile: /etc/mailgun/ssl/mongo.pem
   129      - tlsCAFile: /etc/mailgun/ssl/mongo-ca.crt
   130      - replicaSet: routes
   131      - readPreferenceTags: "dc:use1"
   132      - readPreferenceTags: "dc:usw2"
   133  `)
   134  	var conf mongoutil.Config
   135  	// When
   136  	err := yaml.Unmarshal(cfgYAML, &conf)
   137  	// Then
   138  	require.NoError(t, err)
   139  	require.Equal(t, "mongodb://mongo-routes-n01-us-east-1.postgun.com:27017,"+
   140  		"mongo-routes-n02-us-east-1.postgun.com:27017,mongo-routes-n03-us-east-1.postgun.com:27017/mg_prod?"+
   141  		"ssl=true&tlsCertificateKeyFile=/etc/mailgun/ssl/mongo.pem&tlsCAFile=/etc/mailgun/ssl/mongo-ca.crt&"+
   142  		"replicaSet=routes&readPreferenceTags=dc:use1&readPreferenceTags=dc:usw2", conf.URIWithOptions())
   143  }