-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed db password file endline issue (#5920)
* trimmed space in db password and added db test Signed-off-by: Yaxhveer <[email protected]> * updated changes Signed-off-by: Yaxhveer <[email protected]> * updated changelog.md Signed-off-by: Yaxhveer <[email protected]> * updated changes Signed-off-by: Yaxhveer <[email protected]> * updated test Signed-off-by: Yaxhveer <[email protected]> * corrected lint Signed-off-by: Yaxhveer <[email protected]> * updated the db test Signed-off-by: Yaxhveer <[email protected]> * corrected the db test Signed-off-by: Yaxhveer <[email protected]> * corrected test Signed-off-by: Yaxhveer <[email protected]> --------- Signed-off-by: Yaxhveer <[email protected]>
- Loading branch information
Showing
3 changed files
with
84 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package db | ||
|
||
import ( | ||
"net/url" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSetPassword(t *testing.T) { | ||
|
||
testCases := []struct { | ||
testName string | ||
url string | ||
passwordStr string | ||
isError bool | ||
expected string | ||
}{ | ||
{ | ||
testName: "Test1", | ||
url: "scheme://[email protected]", | ||
passwordStr: "\n\tpassword\n\n\t", | ||
isError: false, | ||
expected: "scheme://user:[email protected]", | ||
}, | ||
{ | ||
testName: "Test2", | ||
url: "scheme://host.com", | ||
passwordStr: "\n\tpassword\n\n\t", | ||
isError: true, | ||
expected: "--database.password-file requires username in --database.uri", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
passwordFile, err := os.CreateTemp("", "passwordFile") | ||
if err != nil { | ||
t.Fatalf("error while creating the password file: %v", err) | ||
} | ||
|
||
defer os.Remove(passwordFile.Name()) | ||
defer passwordFile.Close() | ||
|
||
_, err = passwordFile.WriteString(tc.passwordStr) | ||
if err != nil { | ||
t.Fatalf("error while writing to the password file: %v", err) | ||
} | ||
|
||
t.Run(tc.testName, func(t *testing.T) { | ||
u, _ := url.Parse(tc.url) | ||
uNew, err := setPassword(u, passwordFile.Name()) | ||
if tc.isError { | ||
assert.Error(t, err) | ||
assert.Equal(t, tc.expected, err.Error()) | ||
} else { | ||
assert.NoError(t, err) | ||
uExp, _ := url.Parse(tc.expected) | ||
assert.Equal(t, uNew, uExp) | ||
} | ||
}) | ||
} | ||
} |