-
Notifications
You must be signed in to change notification settings - Fork 949
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
feature: finish the CLI logs part #1472
Merged
allencloud
merged 1 commit into
AliyunContainerService:master
from
fuweid:feature_finish_logs_client_part
Jun 12, 2018
Merged
Changes from all commits
Commits
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 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
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,94 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
) | ||
|
||
func TestContainerLogsServerError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
|
||
_, err := client.ContainerLogs(context.Background(), "nothing", types.ContainerLogsOptions{}) | ||
if err == nil || !strings.Contains(err.Error(), "Server error") { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestContainerLogsOK(t *testing.T) { | ||
expectedURL := "/containers/container_id/logs" | ||
expectedSinceTS := "1531728000.000000000" // 2018-07-16T08:00Z | ||
expectedUntilTS := "1531728300.000000000" // 2018-07-16T08:05Z | ||
|
||
opts := types.ContainerLogsOptions{ | ||
Follow: true, | ||
ShowStdout: true, | ||
ShowStderr: false, | ||
Timestamps: true, | ||
|
||
Since: "2018-07-16T08:00Z", | ||
Until: "2018-07-16T08:05Z", | ||
Tail: "10", | ||
} | ||
|
||
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) { | ||
if !strings.HasPrefix(req.URL.Path, expectedURL) { | ||
return nil, fmt.Errorf("expected URL %s, got %s", expectedURL, req.URL) | ||
} | ||
|
||
if req.Method != http.MethodGet { | ||
return nil, fmt.Errorf("expected HTTP Method = %s, got %s", http.MethodGet, req.Method) | ||
} | ||
|
||
query := req.URL.Query() | ||
if got := query.Get("follow"); got != "1" { | ||
return nil, fmt.Errorf("expected follow mode (1), got %v", got) | ||
} | ||
|
||
if got := query.Get("stdout"); got != "1" { | ||
return nil, fmt.Errorf("expected stdout mode (1), got %v", got) | ||
} | ||
|
||
if got := query.Get("stderr"); got != "" { | ||
return nil, fmt.Errorf("expected without stderr mode, got %v", got) | ||
} | ||
|
||
if got := query.Get("timestamps"); got != "1" { | ||
return nil, fmt.Errorf("expected timestamps mode, got %v", got) | ||
} | ||
|
||
if got := query.Get("tail"); got != "10" { | ||
return nil, fmt.Errorf("expected tail = %v, got %v", opts.Tail, got) | ||
} | ||
|
||
if got := query.Get("since"); got != expectedSinceTS { | ||
return nil, fmt.Errorf("expected since = %v, got %v", expectedSinceTS, got) | ||
} | ||
|
||
if got := query.Get("until"); got != expectedUntilTS { | ||
return nil, fmt.Errorf("expected since = %v, got %v", expectedUntilTS, got) | ||
} | ||
|
||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), | ||
}, nil | ||
}) | ||
|
||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
|
||
_, err := client.ContainerLogs(context.Background(), "container_id", opts) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the content don't end with
\n
like1\n2\n3\n4
,seekOffsetByTailLines(, 2)
will return the index of2
. The tail content would be2\n3\n4
. Compared with command tail,echo "1\n2\n3\n4" > a.txt; tail -n2 a.txt
is3\n4
.Does this function works as expected?😁 @fuweid