-
Notifications
You must be signed in to change notification settings - Fork 749
fix: return delete-marker ObjectInfo from StatObject alongside the error #2273
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
Open
allanrogerr
wants to merge
3
commits into
minio:master
Choose a base branch
from
allanrogerr:issue-2260-statobject-delete-marker-info
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−22
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,236 @@ | ||
| /* | ||
| * MinIO Go Library for Amazon S3 Compatible Cloud Storage | ||
| * Copyright 2026 MinIO, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package minio | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
|
|
||
| "github.com/minio/minio-go/v7/pkg/credentials" | ||
| ) | ||
|
|
||
| // Tests that StatObject returns the delete-marker ObjectInfo fields | ||
| // (VersionID, IsDeleteMarker) and the MethodNotAllowed error code when a | ||
| // versioned HEAD hits a delete marker (HTTP 405). | ||
| func TestStatObjectDeleteMarker(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.Header().Set(amzDeleteMarker, "true") | ||
| w.Header().Set(amzVersionID, "test-version-id") | ||
| w.WriteHeader(http.StatusMethodNotAllowed) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| u, err := url.Parse(srv.URL) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| clnt, err := New(u.Host, &Options{ | ||
| Creds: credentials.NewStaticV4("foo", "foo12345", ""), | ||
| Region: "us-east-1", | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name", | ||
| StatObjectOptions{VersionID: "test-version-id"}) | ||
| if err == nil { | ||
| t.Fatal("expected error for delete marker, got nil") | ||
| } | ||
| errResp := ToErrorResponse(err) | ||
| if errResp.Code != MethodNotAllowed { | ||
| t.Errorf("error code = %q, want %q", errResp.Code, MethodNotAllowed) | ||
| } | ||
| if errResp.StatusCode != http.StatusMethodNotAllowed { | ||
| t.Errorf("error status = %d, want %d", errResp.StatusCode, http.StatusMethodNotAllowed) | ||
| } | ||
| if !objInfo.IsDeleteMarker { | ||
| t.Error("expected IsDeleteMarker to be true") | ||
| } | ||
| if objInfo.VersionID != "test-version-id" { | ||
| t.Errorf("VersionID = %q, want %q", objInfo.VersionID, "test-version-id") | ||
| } | ||
| } | ||
|
|
||
| // Tests that a 405 response missing either half of the delete-marker | ||
| // shape (the x-amz-delete-marker header, or a version-targeted stat) | ||
| // falls through to the generic error path with the raw status code. | ||
| func TestStatObjectMethodNotAllowedGeneric(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| deleteMarker bool | ||
| versionID string | ||
| }{ | ||
| {"no delete-marker header", false, "test-version-id"}, | ||
| {"no version id", true, ""}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| if tt.deleteMarker { | ||
| w.Header().Set(amzDeleteMarker, "true") | ||
| } | ||
| w.Header().Set(amzVersionID, "test-version-id") | ||
| w.WriteHeader(http.StatusMethodNotAllowed) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| u, err := url.Parse(srv.URL) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| clnt, err := New(u.Host, &Options{ | ||
| Creds: credentials.NewStaticV4("foo", "foo12345", ""), | ||
| Region: "us-east-1", | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name", | ||
| StatObjectOptions{VersionID: tt.versionID}) | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
| if errResp := ToErrorResponse(err); errResp.Code != "405 Method Not Allowed" { | ||
| t.Errorf("error code = %q, want %q", errResp.Code, "405 Method Not Allowed") | ||
| } | ||
| if objInfo.IsDeleteMarker != tt.deleteMarker { | ||
| t.Errorf("IsDeleteMarker = %v, want %v", objInfo.IsDeleteMarker, tt.deleteMarker) | ||
| } | ||
| if objInfo.VersionID != "test-version-id" { | ||
| t.Errorf("VersionID = %q, want %q", objInfo.VersionID, "test-version-id") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Tests that 202 and 204 responses, which executeMethod treats as | ||
| // success, are parsed like a 200 instead of being converted into errors. | ||
| func TestStatObjectNoContentSuccess(t *testing.T) { | ||
| for _, status := range []int{http.StatusAccepted, http.StatusNoContent} { | ||
| t.Run(http.StatusText(status), func(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.Header().Set("Last-Modified", "Thu, 30 Jul 2026 00:00:00 GMT") | ||
| w.Header().Set("ETag", `"deadbeef"`) | ||
| w.Header().Set(amzVersionID, "test-version-id") | ||
| w.WriteHeader(status) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| u, err := url.Parse(srv.URL) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| clnt, err := New(u.Host, &Options{ | ||
| Creds: credentials.NewStaticV4("foo", "foo12345", ""), | ||
| Region: "us-east-1", | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name", StatObjectOptions{}) | ||
| if err != nil { | ||
| t.Fatalf("expected nil error for %d, got %v", status, err) | ||
| } | ||
| if objInfo.ETag != "deadbeef" { | ||
| t.Errorf("ETag = %q, want %q", objInfo.ETag, "deadbeef") | ||
| } | ||
| if objInfo.VersionID != "test-version-id" { | ||
| t.Errorf("VersionID = %q, want %q", objInfo.VersionID, "test-version-id") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Tests that StatObject returns a zero ObjectInfo when the request fails | ||
| // before any response is received. | ||
| func TestStatObjectNoResponse(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| addr := srv.Listener.Addr().String() | ||
| srv.Close() | ||
|
|
||
| clnt, err := New(addr, &Options{ | ||
| Creds: credentials.NewStaticV4("foo", "foo12345", ""), | ||
| Region: "us-east-1", | ||
| MaxRetries: 1, | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name", StatObjectOptions{}) | ||
| if err == nil { | ||
| t.Fatal("expected error for unreachable endpoint, got nil") | ||
| } | ||
| if objInfo.IsDeleteMarker || objInfo.VersionID != "" || objInfo.ETag != "" { | ||
| t.Errorf("expected zero ObjectInfo, got %+v", objInfo) | ||
| } | ||
| } | ||
|
|
||
| // Tests that StatObject surfaces the delete-marker and replication-ready | ||
| // headers on a generic error response, e.g. HEAD on an object whose | ||
| // latest version is a delete marker (HTTP 404). | ||
| func TestStatObjectErrorHeaders(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.Header().Set(amzDeleteMarker, "true") | ||
| w.Header().Set(amzVersionID, "test-version-id") | ||
| w.Header().Set(minioTgtReplicationReady, "true") | ||
| w.WriteHeader(http.StatusNotFound) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| u, err := url.Parse(srv.URL) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| clnt, err := New(u.Host, &Options{ | ||
| Creds: credentials.NewStaticV4("foo", "foo12345", ""), | ||
| Region: "us-east-1", | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| objInfo, err := clnt.StatObject(context.Background(), "bucket-name", "object-name", StatObjectOptions{}) | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
| if errResp := ToErrorResponse(err); errResp.Code != NoSuchKey { | ||
| t.Errorf("error code = %q, want %q", errResp.Code, NoSuchKey) | ||
| } | ||
| if !objInfo.IsDeleteMarker { | ||
| t.Error("expected IsDeleteMarker to be true") | ||
| } | ||
| if objInfo.VersionID != "test-version-id" { | ||
| t.Errorf("VersionID = %q, want %q", objInfo.VersionID, "test-version-id") | ||
| } | ||
| if !objInfo.ReplicationReady { | ||
| t.Error("expected ReplicationReady to be true") | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
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.