-
Notifications
You must be signed in to change notification settings - Fork 55
Default registry #358
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
Default registry #358
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
33ad3e4
feat(registry): add support for custom default and insecure registry …
ilopezluna 2913a86
feat(registry): refactor default registry options to use cached value…
ilopezluna cc2339c
Merge branch 'main' into default-registry
ilopezluna a85e2a9
test(registry): add unit tests for GetDefaultRegistryOptions function
ilopezluna 100d3ae
test(registry): add tests for default registry behavior and HTTPS scheme
ilopezluna 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
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,133 @@ | ||
| package registry | ||
|
|
||
| import ( | ||
| "os" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-containerregistry/pkg/name" | ||
| ) | ||
|
|
||
| func TestGetDefaultRegistryOptions_NoEnvVars(t *testing.T) { | ||
| // Reset the sync.Once for this test | ||
| resetOnceForTest() | ||
|
|
||
| // Ensure no env vars are set | ||
| os.Unsetenv("DEFAULT_REGISTRY") | ||
| os.Unsetenv("INSECURE_REGISTRY") | ||
|
|
||
| opts := GetDefaultRegistryOptions() | ||
|
|
||
| if len(opts) != 0 { | ||
| t.Errorf("Expected empty options slice, got %d options", len(opts)) | ||
| } | ||
|
|
||
| // Verify that the default registry (index.docker.io) is used when no options are set | ||
| ref, err := name.ParseReference("myrepo/myimage:tag", opts...) | ||
| if err != nil { | ||
| t.Fatalf("Failed to parse reference: %v", err) | ||
| } | ||
|
|
||
| // When no DEFAULT_REGISTRY is set, the default should be index.docker.io | ||
| expectedRegistry := "index.docker.io" | ||
| if ref.Context().Registry.Name() != expectedRegistry { | ||
| t.Errorf("Expected default registry to be '%s', got '%s'", expectedRegistry, ref.Context().Registry.Name()) | ||
| } | ||
|
|
||
| // Verify it uses HTTPS (secure by default) | ||
| if ref.Context().Registry.Scheme() != "https" { | ||
| t.Errorf("Expected scheme to be 'https', got '%s'", ref.Context().Registry.Scheme()) | ||
| } | ||
| } | ||
|
|
||
| func TestGetDefaultRegistryOptions_OnlyDefaultRegistry(t *testing.T) { | ||
| // Reset the sync.Once for this test | ||
| resetOnceForTest() | ||
|
|
||
| t.Setenv("DEFAULT_REGISTRY", "custom.registry.io") | ||
| os.Unsetenv("INSECURE_REGISTRY") | ||
|
|
||
| opts := GetDefaultRegistryOptions() | ||
|
|
||
| if len(opts) != 1 { | ||
| t.Fatalf("Expected 1 option, got %d", len(opts)) | ||
| } | ||
|
|
||
| // Verify the option sets the default registry by parsing a reference without explicit registry | ||
| ref, err := name.ParseReference("myrepo/myimage:tag", opts...) | ||
| if err != nil { | ||
| t.Fatalf("Failed to parse reference: %v", err) | ||
| } | ||
|
|
||
| if ref.Context().Registry.Name() != "custom.registry.io" { | ||
| t.Errorf("Expected registry to be 'custom.registry.io', got '%s'", ref.Context().Registry.Name()) | ||
| } | ||
|
|
||
| // Verify it's not insecure (should use https) | ||
| if ref.Context().Registry.Scheme() != "https" { | ||
| t.Errorf("Expected scheme to be 'https', got '%s'", ref.Context().Registry.Scheme()) | ||
| } | ||
| } | ||
|
|
||
| func TestGetDefaultRegistryOptions_OnlyInsecureRegistry(t *testing.T) { | ||
| // Reset the sync.Once for this test | ||
| resetOnceForTest() | ||
|
|
||
| os.Unsetenv("DEFAULT_REGISTRY") | ||
| t.Setenv("INSECURE_REGISTRY", "true") | ||
|
|
||
| opts := GetDefaultRegistryOptions() | ||
|
|
||
| if len(opts) != 1 { | ||
| t.Fatalf("Expected 1 option, got %d", len(opts)) | ||
| } | ||
|
|
||
| // Verify the option makes the registry insecure by parsing a reference | ||
| ref, err := name.ParseReference("myregistry.io/myrepo/myimage:tag", opts...) | ||
| if err != nil { | ||
| t.Fatalf("Failed to parse reference: %v", err) | ||
| } | ||
|
|
||
| // Insecure registries should use http | ||
| if ref.Context().Registry.Scheme() != "http" { | ||
| t.Errorf("Expected scheme to be 'http', got '%s'", ref.Context().Registry.Scheme()) | ||
| } | ||
| } | ||
|
|
||
| func TestGetDefaultRegistryOptions_BothEnvVars(t *testing.T) { | ||
| // Reset the sync.Once for this test | ||
| resetOnceForTest() | ||
|
|
||
| t.Setenv("DEFAULT_REGISTRY", "custom.registry.io") | ||
| t.Setenv("INSECURE_REGISTRY", "true") | ||
|
|
||
| opts := GetDefaultRegistryOptions() | ||
|
|
||
| if len(opts) != 2 { | ||
| t.Fatalf("Expected 2 options, got %d", len(opts)) | ||
| } | ||
|
|
||
| // Verify both options are applied | ||
| ref, err := name.ParseReference("myrepo/myimage:tag", opts...) | ||
| if err != nil { | ||
| t.Fatalf("Failed to parse reference: %v", err) | ||
| } | ||
|
|
||
| // Check custom registry is used | ||
| if ref.Context().Registry.Name() != "custom.registry.io" { | ||
| t.Errorf("Expected registry to be 'custom.registry.io', got '%s'", ref.Context().Registry.Name()) | ||
| } | ||
|
|
||
| // Check insecure is applied (http scheme) | ||
| if ref.Context().Registry.Scheme() != "http" { | ||
| t.Errorf("Expected scheme to be 'http', got '%s'", ref.Context().Registry.Scheme()) | ||
| } | ||
| } | ||
|
|
||
| // Helper function to reset the sync.Once for testing | ||
| // Note: This is a workaround for testing. In production code, sync.Once ensures | ||
| // the initialization only happens once for the lifetime of the program. | ||
| func resetOnceForTest() { | ||
| once = sync.Once{} | ||
| defaultRegistryOpts = nil | ||
| } |
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.