-
Notifications
You must be signed in to change notification settings - Fork 929
feat(agent/agentcontainers): fall back to workspace folder name #18466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
398c460
feat(agent/agentcontainers): fall back to workspace folder name
DanielleMaywood 32e7320
chore: appease linter and add test case
DanielleMaywood 7704238
chore: grammar
DanielleMaywood e0ffe69
chore: move subAgentConfig.Directory assignment
DanielleMaywood a1a8957
chore: re-add removed
DanielleMaywood 52875a4
chore: flip order
DanielleMaywood ad39074
chore: improve comment
DanielleMaywood 0ea4b12
chore: filepath.Base -> filepath.ToSlash & path.Base
DanielleMaywood 4fdb580
chore: replace empty check with AgentNameRegex check
DanielleMaywood d38339a
chore: truncate name if over 64 characters long
DanielleMaywood 5647a48
chore: fix oops
DanielleMaywood ac8e241
test: scenario with very long name
DanielleMaywood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,201 @@ | ||
package agentcontainers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/coder/coder/v2/provisioner" | ||
) | ||
|
||
func TestSafeAgentName(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
folderName string | ||
expected string | ||
}{ | ||
// Basic valid names | ||
{ | ||
folderName: "simple", | ||
expected: "simple", | ||
}, | ||
{ | ||
folderName: "with-hyphens", | ||
expected: "with-hyphens", | ||
}, | ||
{ | ||
folderName: "123numbers", | ||
expected: "123numbers", | ||
}, | ||
{ | ||
folderName: "mixed123", | ||
expected: "mixed123", | ||
}, | ||
|
||
// Names that need transformation | ||
{ | ||
folderName: "With_Underscores", | ||
expected: "with-underscores", | ||
}, | ||
{ | ||
folderName: "With Spaces", | ||
expected: "with-spaces", | ||
}, | ||
{ | ||
folderName: "UPPERCASE", | ||
expected: "uppercase", | ||
}, | ||
{ | ||
folderName: "Mixed_Case-Name", | ||
expected: "mixed-case-name", | ||
}, | ||
|
||
// Names with special characters that get replaced | ||
{ | ||
folderName: "special@#$chars", | ||
expected: "special-chars", | ||
}, | ||
{ | ||
folderName: "dots.and.more", | ||
expected: "dots-and-more", | ||
}, | ||
{ | ||
folderName: "multiple___underscores", | ||
expected: "multiple-underscores", | ||
}, | ||
{ | ||
folderName: "multiple---hyphens", | ||
expected: "multiple-hyphens", | ||
}, | ||
|
||
// Edge cases with leading/trailing special chars | ||
{ | ||
folderName: "-leading-hyphen", | ||
expected: "leading-hyphen", | ||
}, | ||
{ | ||
folderName: "trailing-hyphen-", | ||
expected: "trailing-hyphen", | ||
}, | ||
{ | ||
folderName: "_leading_underscore", | ||
expected: "leading-underscore", | ||
}, | ||
{ | ||
folderName: "trailing_underscore_", | ||
expected: "trailing-underscore", | ||
}, | ||
{ | ||
folderName: "---multiple-leading", | ||
expected: "multiple-leading", | ||
}, | ||
{ | ||
folderName: "trailing-multiple---", | ||
expected: "trailing-multiple", | ||
}, | ||
|
||
// Complex transformation cases | ||
{ | ||
folderName: "___very---complex@@@name___", | ||
expected: "very-complex-name", | ||
}, | ||
{ | ||
folderName: "my.project-folder_v2", | ||
expected: "my-project-folder-v2", | ||
}, | ||
|
||
// Empty and fallback cases - now correctly uses friendlyName fallback | ||
{ | ||
folderName: "", | ||
expected: "friendly-fallback", | ||
}, | ||
{ | ||
folderName: "---", | ||
expected: "friendly-fallback", | ||
}, | ||
{ | ||
folderName: "___", | ||
expected: "friendly-fallback", | ||
}, | ||
{ | ||
folderName: "@#$", | ||
expected: "friendly-fallback", | ||
}, | ||
|
||
// Additional edge cases | ||
{ | ||
folderName: "a", | ||
expected: "a", | ||
}, | ||
{ | ||
folderName: "1", | ||
expected: "1", | ||
}, | ||
{ | ||
folderName: "a1b2c3", | ||
expected: "a1b2c3", | ||
}, | ||
{ | ||
folderName: "CamelCase", | ||
expected: "camelcase", | ||
}, | ||
{ | ||
folderName: "snake_case_name", | ||
expected: "snake-case-name", | ||
}, | ||
{ | ||
folderName: "kebab-case-name", | ||
expected: "kebab-case-name", | ||
}, | ||
{ | ||
folderName: "mix3d_C4s3-N4m3", | ||
expected: "mix3d-c4s3-n4m3", | ||
}, | ||
{ | ||
folderName: "123-456-789", | ||
expected: "123-456-789", | ||
}, | ||
{ | ||
folderName: "abc123def456", | ||
expected: "abc123def456", | ||
}, | ||
{ | ||
folderName: " spaces everywhere ", | ||
expected: "spaces-everywhere", | ||
}, | ||
{ | ||
folderName: "unicode-café-naïve", | ||
expected: "unicode-caf-na-ve", | ||
}, | ||
{ | ||
folderName: "path/with/slashes", | ||
expected: "path-with-slashes", | ||
}, | ||
{ | ||
folderName: "file.tar.gz", | ||
expected: "file-tar-gz", | ||
}, | ||
{ | ||
folderName: "version-1.2.3-alpha", | ||
expected: "version-1-2-3-alpha", | ||
}, | ||
|
||
// Truncation test for names exceeding 64 characters | ||
{ | ||
folderName: "this-is-a-very-long-folder-name-that-exceeds-sixty-four-characters-limit-and-should-be-truncated", | ||
expected: "this-is-a-very-long-folder-name-that-exceeds-sixty-four-characte", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.folderName, func(t *testing.T) { | ||
t.Parallel() | ||
name := safeAgentName(tt.folderName, "friendly-fallback") | ||
|
||
assert.Equal(t, tt.expected, name) | ||
assert.True(t, provisioner.AgentNameRegex.Match([]byte(name))) | ||
}) | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.