-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add AsGoGetterURL() to connection (#303)
* feat: add AsGoGetterURL() to connection * feat: return file content on AsEnv() * added tests as well * feat: env prep * make envprep context aware * chore: use lowercase for connection types * fix: use new CmdEnv * chore: fix test
- Loading branch information
1 parent
1e214bd
commit 748be3f
Showing
2 changed files
with
324 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package models | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func Test_Connection_AsGoGetterURL(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
connection Connection | ||
expectedURL string | ||
expectedError error | ||
}{ | ||
{ | ||
name: "HTTP Connection", | ||
connection: Connection{ | ||
Type: ConnectionTypeHTTP, | ||
URL: "http://example.com", | ||
Username: "testuser", | ||
Password: "testpassword", | ||
}, | ||
expectedURL: "http://testuser:testpassword@example.com", | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "Git Connection", | ||
connection: Connection{ | ||
Type: ConnectionTypeGit, | ||
URL: "https://github.com/repo.git", | ||
Certificate: "cert123", | ||
Properties: map[string]string{"ref": "main"}, | ||
}, | ||
expectedURL: "https://github.com/repo.git?ref=main&sshkey=Y2VydDEyMw%3D%3D", | ||
expectedError: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
resultURL, err := tc.connection.AsGoGetterURL() | ||
|
||
if resultURL != tc.expectedURL { | ||
t.Errorf("Expected URL: %s, but got: %s", tc.expectedURL, resultURL) | ||
} | ||
|
||
if err != tc.expectedError { | ||
t.Errorf("Expected error: %v, but got: %v", tc.expectedError, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_Connection_AsEnv(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
connection Connection | ||
expectedEnv []string | ||
expectedFileContent string | ||
}{ | ||
{ | ||
name: "AWS Connection", | ||
connection: Connection{ | ||
Type: ConnectionTypeAWS, | ||
Username: "awsuser", | ||
Password: "awssecret", | ||
Properties: map[string]string{"profile": "awsprofile", "region": "us-east-1"}, | ||
}, | ||
expectedEnv: []string{ | ||
"AWS_ACCESS_KEY_ID=awsuser", | ||
"AWS_SECRET_ACCESS_KEY=awssecret", | ||
"AWS_DEFAULT_PROFILE=awsprofile", | ||
"AWS_DEFAULT_REGION=us-east-1", | ||
}, | ||
expectedFileContent: "[default]\naws_access_key_id = awsuser\naws_secret_access_key = awssecret\nregion = us-east-1\n", | ||
}, | ||
{ | ||
name: "GCP Connection", | ||
connection: Connection{ | ||
Type: ConnectionTypeGCP, | ||
Username: "gcpuser", | ||
Certificate: `{"account": "gcpuser"}`, | ||
}, | ||
expectedEnv: []string{}, | ||
expectedFileContent: `{"account": "gcpuser"}`, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
envPrep := tc.connection.AsEnv(context.Background()) | ||
|
||
for i, expected := range tc.expectedEnv { | ||
if envPrep.Env[i] != expected { | ||
t.Errorf("Expected environment variable: %s, but got: %s", expected, envPrep.Env[i]) | ||
} | ||
} | ||
|
||
for _, content := range envPrep.Files { | ||
if content.String() != tc.expectedFileContent { | ||
t.Errorf("Expected file content: %s, but got: %s", tc.expectedFileContent, content.String()) | ||
} | ||
} | ||
}) | ||
} | ||
} |