Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions ctl/log/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package logs_test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need CopyRight

Copy link
Contributor Author

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


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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No TestSetLoggerLevel_Failure ? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}
}
Loading