| 1770 | } |
| 1771 | |
| 1772 | func TestSetupDbConfigCredentials(t *testing.T) { |
| 1773 | const ( |
| 1774 | expectedUsername = "alice" |
| 1775 | expectedPassword = "hunter2" |
| 1776 | expectedX509Cert = "/tmp/x509cert" |
| 1777 | expectedX509Key = "/tmp/x509key" |
| 1778 | ) |
| 1779 | var tests = []struct { |
| 1780 | name string |
| 1781 | dbConfig DbConfig |
| 1782 | bootstrapConfig BootstrapConfig |
| 1783 | credentialsConfig *base.CredentialsConfig |
| 1784 | expectX509 bool |
| 1785 | }{ |
| 1786 | { |
| 1787 | name: "bootstrap only", |
| 1788 | dbConfig: DbConfig{Name: "db"}, |
| 1789 | bootstrapConfig: BootstrapConfig{Server: "couchbase://example.org", Username: expectedUsername, Password: expectedPassword}, |
| 1790 | credentialsConfig: nil, |
| 1791 | }, |
| 1792 | { |
| 1793 | name: "db username/password override", |
| 1794 | dbConfig: DbConfig{Name: "db"}, |
| 1795 | bootstrapConfig: BootstrapConfig{Server: "couchbase://example.org", Username: "bob", Password: "foobar"}, |
| 1796 | credentialsConfig: &base.CredentialsConfig{Username: expectedUsername, Password: expectedPassword}, |
| 1797 | }, |
| 1798 | { |
| 1799 | name: "db username/password override from x509", |
| 1800 | dbConfig: DbConfig{Name: "db"}, |
| 1801 | bootstrapConfig: BootstrapConfig{Server: "couchbase://example.org", X509CertPath: "/tmp/x509cert", X509KeyPath: "/tmp/x509key"}, |
| 1802 | credentialsConfig: &base.CredentialsConfig{Username: expectedUsername, Password: expectedPassword}, |
| 1803 | }, |
| 1804 | { |
| 1805 | name: "db x509 override from username/password", |
| 1806 | dbConfig: DbConfig{Name: "db"}, |
| 1807 | bootstrapConfig: BootstrapConfig{Server: "couchbase://example.org", Username: "bob", Password: "foobar"}, |
| 1808 | credentialsConfig: &base.CredentialsConfig{CredentialsConfigX509: base.CredentialsConfigX509{X509CertPath: expectedX509Cert, X509KeyPath: expectedX509Key}}, |
| 1809 | expectX509: true, |
| 1810 | }, |
| 1811 | { |
| 1812 | name: "db x509 override", |
| 1813 | dbConfig: DbConfig{Name: "db"}, |
| 1814 | bootstrapConfig: BootstrapConfig{Server: "couchbase://example.org", X509CertPath: "/tmp/bs-x509cert", X509KeyPath: "/tmp/bs-x509key"}, |
| 1815 | credentialsConfig: &base.CredentialsConfig{CredentialsConfigX509: base.CredentialsConfigX509{X509CertPath: expectedX509Cert, X509KeyPath: expectedX509Key}}, |
| 1816 | expectX509: true, |
| 1817 | }, |
| 1818 | } |
| 1819 | for _, test := range tests { |
| 1820 | t.Run(test.name, func(t *testing.T) { |
| 1821 | err := test.dbConfig.setup(base.TestCtx(t), test.dbConfig.Name, test.bootstrapConfig, test.credentialsConfig, nil, false) |
| 1822 | require.NoError(t, err) |
| 1823 | if test.expectX509 { |
| 1824 | assert.Equal(t, "", test.dbConfig.Username) |
| 1825 | assert.Equal(t, "", test.dbConfig.Password) |
| 1826 | assert.Equal(t, expectedX509Cert, test.dbConfig.CertPath) |
| 1827 | assert.Equal(t, expectedX509Key, test.dbConfig.KeyPath) |
| 1828 | } else { |
| 1829 | assert.Equal(t, expectedUsername, test.dbConfig.Username) |