forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rancher#44150 from nicholasSUSE/git-ssh-custom-por…
…t-v2.7 Git ssh custom port [Backport - relase/v2.7]
- Loading branch information
Showing
5 changed files
with
538 additions
and
342 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,179 @@ | ||
package git | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
assertlib "github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func Test_isGitSSH(t *testing.T) { | ||
const chartsSmallForkURL = "https://github.com/rancher/charts-small-fork" | ||
const mainBranch = "main" | ||
const lastBranch = "test-1" | ||
|
||
func TestMain(m *testing.M) { | ||
// Run all the tests | ||
exitCode := m.Run() | ||
|
||
// Cleanup after tests | ||
cleanup() | ||
|
||
// Exit with the proper code | ||
os.Exit(exitCode) | ||
} | ||
|
||
func cleanup() { | ||
// Delete the management-state directory | ||
os.RemoveAll("management-state") | ||
} | ||
|
||
func Test_Ensure(t *testing.T) { | ||
testCases := []struct { | ||
gitURL string | ||
expected bool | ||
test string | ||
secret *corev1.Secret | ||
namespace string | ||
name string | ||
gitURL string | ||
commit string | ||
insecureSkipTLS bool | ||
caBundle []byte | ||
branch string | ||
expectedError error | ||
}{ | ||
// True cases | ||
{"[email protected]:user/repo.git", true}, | ||
{"[email protected]:user/repo.git", true}, | ||
{"[email protected]:user/repo", true}, | ||
{"[email protected]:user/repo-with-dashes.git", true}, | ||
{"[email protected]:user/repo.git", true}, | ||
{"[email protected]:user/repo-with-dashes.git", true}, | ||
{"[email protected]:user/repo", true}, | ||
// False cases | ||
{"https://github.com/user/repo.git", false}, | ||
{"http://gitlab.com/user/repo.git", false}, | ||
{"http://gitlab.com/user/repo", false}, | ||
{"http://gitlab.com", false}, | ||
{"[email protected]", false}, | ||
{ | ||
test: "#1 TestCase: Success - Clone, Reset And Exit", | ||
secret: nil, | ||
namespace: "cattle-test", | ||
name: "small-fork-test", | ||
gitURL: chartsSmallForkURL, | ||
commit: "0e2b9da9ddde5c1e502bba6474119856496e5026", | ||
insecureSkipTLS: false, | ||
caBundle: []byte{}, | ||
branch: mainBranch, | ||
expectedError: nil, | ||
}, | ||
{ | ||
test: "#2 TestCase: Success - Clone, Reset And Fetch Last Branch", | ||
secret: nil, | ||
namespace: "cattle-test", | ||
name: "small-fork-test", | ||
gitURL: chartsSmallForkURL, | ||
commit: "0e2b9da9ddde5c1e502bba6474119856496e5026", | ||
insecureSkipTLS: false, | ||
caBundle: []byte{}, | ||
branch: lastBranch, | ||
expectedError: nil, | ||
}, | ||
} | ||
assert := assertlib.New(t) | ||
|
||
for _, tc := range testCases { | ||
actual, err := isGitSSH(tc.gitURL) | ||
if err != nil { | ||
t.Errorf("unexpected error: %s", err) | ||
} | ||
assert.Equalf(tc.expected, actual, "testcase: %v", tc) | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := Ensure(tc.secret, tc.namespace, tc.name, tc.gitURL, tc.commit, tc.insecureSkipTLS, tc.caBundle) | ||
// Check the error | ||
if tc.expectedError == nil && tc.expectedError != err { | ||
t.Errorf("Expected error: %v |But got: %v", tc.expectedError, err) | ||
} | ||
|
||
// Check the error | ||
if tc.expectedError == nil && tc.expectedError != err { | ||
t.Errorf("Expected error: %v |But got: %v", tc.expectedError, err) | ||
} | ||
// Only testing error in some cases | ||
if err != nil { | ||
assert.EqualError(t, tc.expectedError, err.Error()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_gitDir(t *testing.T) { | ||
assert := assertlib.New(t) | ||
func Test_Head(t *testing.T) { | ||
testCases := []struct { | ||
namespace string | ||
name string | ||
gitURL string | ||
expected string | ||
test string | ||
secret *corev1.Secret | ||
namespace string | ||
name string | ||
gitURL string | ||
insecureSkipTLS bool | ||
caBundle []byte | ||
branch string | ||
expectedCommit string | ||
expectedError error | ||
}{ | ||
{ | ||
"namespace", "name", "https://git.rancher.io/charts", | ||
"management-state/git-repo/namespace/name/4b40cac650031b74776e87c1a726b0484d0877c3ec137da0872547ff9b73a721", | ||
test: "#1 TestCase: Success - Clone, Reset And Return Commit", | ||
secret: nil, | ||
namespace: "cattle-test", | ||
name: "small-fork-test", | ||
gitURL: chartsSmallForkURL, | ||
insecureSkipTLS: false, | ||
caBundle: []byte{}, | ||
branch: mainBranch, | ||
expectedCommit: "226d544def39de56db210e96d2b0b535badf9bdd", | ||
expectedError: nil, | ||
}, | ||
// NOTE(manno): cannot test the other cases without poluting the filesystem | ||
} | ||
|
||
for _, tc := range testCases { | ||
actual := gitDir(tc.namespace, tc.name, tc.gitURL) | ||
assert.Equalf(tc.expected, actual, "testcase: %v", tc) | ||
t.Run(tc.name, func(t *testing.T) { | ||
commit, err := Head(tc.secret, tc.namespace, tc.name, tc.gitURL, tc.branch, tc.insecureSkipTLS, tc.caBundle) | ||
// Check the error | ||
if tc.expectedError == nil && tc.expectedError != err { | ||
t.Errorf("Expected error: %v |But got: %v", tc.expectedError, err) | ||
} | ||
// Only testing error in some cases | ||
if err != nil { | ||
assert.EqualError(t, tc.expectedError, err.Error()) | ||
} | ||
|
||
assert.Equal(t, len(commit), len(tc.expectedCommit)) | ||
}) | ||
} | ||
} | ||
|
||
func Test_Update(t *testing.T) { | ||
testCases := []struct { | ||
test string | ||
secret *corev1.Secret | ||
namespace string | ||
name string | ||
gitURL string | ||
insecureSkipTLS bool | ||
caBundle []byte | ||
branch string | ||
systemCatalogMode string | ||
expectedCommit string | ||
expectedError error | ||
}{ | ||
{ | ||
test: "#1 TestCase: Success ", | ||
secret: nil, | ||
namespace: "cattle-test", | ||
name: "small-fork-test", | ||
gitURL: chartsSmallForkURL, | ||
insecureSkipTLS: false, | ||
caBundle: []byte{}, | ||
branch: lastBranch, | ||
systemCatalogMode: "", | ||
expectedCommit: "226d544def39de56db210e96d2b0b535badf9bdd", | ||
expectedError: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
commit, err := Update(tc.secret, tc.namespace, tc.name, tc.gitURL, tc.branch, tc.insecureSkipTLS, tc.caBundle) | ||
// Check the error | ||
if tc.expectedError == nil && tc.expectedError != err { | ||
t.Errorf("Expected error: %v |But got: %v", tc.expectedError, err) | ||
} | ||
|
||
// Only testing error in some cases | ||
if err != nil { | ||
assert.EqualError(t, tc.expectedError, err.Error()) | ||
} | ||
|
||
assert.Equal(t, len(commit), len(tc.expectedCommit)) | ||
}) | ||
} | ||
} |
Oops, something went wrong.