-
Notifications
You must be signed in to change notification settings - Fork 111
Add Unit Tests for logs Package #1226
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
ravjot07
wants to merge
2
commits into
kmesh-net:main
Choose a base branch
from
ravjot07:ctl_UT
base: main
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.
Open
Changes from 1 commit
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package logs_test | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
logs "kmesh.net/kmesh/ctl/log" | ||
) | ||
|
||
// TestGetJson_Success verifies GetJson can unmarshal valid JSON | ||
func TestGetJson_Success(t *testing.T) { | ||
mockData := []string{"logger1", "logger2"} | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
respBytes, _ := json.Marshal(mockData) | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write(respBytes) | ||
})) | ||
defer ts.Close() | ||
|
||
var result []string | ||
err := logs.GetJson(ts.URL, &result) | ||
if err != nil { | ||
t.Fatalf("GetJson failed: %v", err) | ||
} | ||
if len(result) != 2 || result[0] != "logger1" || result[1] != "logger2" { | ||
t.Errorf("Unexpected result: got %#v, want [logger1, logger2]", result) | ||
} | ||
} | ||
|
||
// TestGetJson_Failure verifies GetJson handles HTTP errors and bad JSON properly. | ||
func TestGetJson_Failure(t *testing.T) { | ||
// 1) Server returns an internal error | ||
tsError := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
http.Error(w, "something went wrong", http.StatusInternalServerError) | ||
})) | ||
defer tsError.Close() | ||
|
||
var result []string | ||
err := logs.GetJson(tsError.URL, &result) | ||
if err == nil || !strings.Contains(err.Error(), "status code 500") { | ||
t.Errorf("Expected an HTTP 500 error, got: %v", err) | ||
} | ||
|
||
// 2) Server returns invalid JSON | ||
tsBadJSON := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte("{invalid-json")) | ||
})) | ||
defer tsBadJSON.Close() | ||
|
||
err = logs.GetJson(tsBadJSON.URL, &result) | ||
if err == nil || !strings.Contains(err.Error(), "failed to unmarshal") { | ||
t.Errorf("Expected unmarshal error, got: %v", err) | ||
} | ||
} | ||
|
||
// TestSetLoggerLevel_Success checks the normal flow of SetLoggerLevel (no exit). | ||
func TestSetLoggerLevel_Success(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup i am working on this |
||
wantLoggerName := "default" | ||
wantLoggerLevel := "debug" | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Verify it was a POST | ||
if r.Method != http.MethodPost { | ||
t.Errorf("Expected POST, got %s", r.Method) | ||
} | ||
defer r.Body.Close() | ||
|
||
var info logs.LoggerInfo | ||
data, _ := io.ReadAll(r.Body) | ||
json.Unmarshal(data, &info) | ||
|
||
// Check the posted data | ||
if info.Name != wantLoggerName { | ||
t.Errorf("Expected logger name %q, got %q", wantLoggerName, info.Name) | ||
} | ||
if info.Level != wantLoggerLevel { | ||
t.Errorf("Expected logger level %q, got %q", wantLoggerLevel, info.Level) | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte("All good")) | ||
})) | ||
defer ts.Close() | ||
|
||
oldStdout := os.Stdout | ||
r, wPipe, _ := os.Pipe() | ||
os.Stdout = wPipe | ||
logs.SetLoggerLevel(ts.URL, wantLoggerName+":"+wantLoggerLevel) | ||
|
||
_ = wPipe.Close() | ||
os.Stdout = oldStdout | ||
|
||
out, _ := io.ReadAll(r) | ||
output := string(out) | ||
|
||
if !strings.Contains(output, "All good") { | ||
t.Errorf("Expected 'All good' in output, got: %s", output) | ||
} | ||
} |
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.
Need CopyRight
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.
Okiee I will add copyright text