Skip to content

Commit

Permalink
Merge pull request #58 from MrLuje/fix/invalid-logger-name
Browse files Browse the repository at this point in the history
fix: loggerName:logginLevel syntax
  • Loading branch information
TejaBeta authored Jul 3, 2022
2 parents b81f356 + bdf7af9 commit 8a6bb6e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/kubectl-istiolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ func handleLog(logLevel string, pod string, namespace string) error {
return fmt.Errorf("unrecognized logging level: %v", ol)
}
} else {
invalidLogName := false
invalidLogName := true
loggerLevel := regexp.MustCompile(`[:=]`).Split(ol, 2)

for _, logName := range allLoggers {
if logName == loggerLevel[0] {
invalidLogName = false
break
}
invalidLogName = true
}

if invalidLogName {
Expand Down
48 changes: 48 additions & 0 deletions internal/kubectl-istiolog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package internal

import (
"context"
"regexp"
"testing"

appv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -115,6 +116,33 @@ func TestIstioLoggerName_A001(t *testing.T) {
}
}

// We successfully parsed logger name & level and we failed on envoy call
func successfullyParsedLoggerNameAndLevel(err error) bool {
errString := err.Error()
return err != nil && regexp.MustCompile("failed to execute command on Envoy").MatchString(errString)
}

func TestIstioLoggerName_A002(t *testing.T) {
cs := testclient.NewSimpleClientset()
input := &appv1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "unit-test-pod"}}

options := options{
clientset: cs,
namespace: "unit-test-namespace",
}

_, err := cs.CoreV1().Pods(options.namespace).Create(context.TODO(), input, metav1.CreateOptions{})
if err != nil {
t.Fatal(err.Error())
}

err = options.KubectlIstioLog("unit-test-pod", "debug", false)

if !successfullyParsedLoggerNameAndLevel(err) {
t.Errorf("Error while using illegal loggerLevel")
}
}

func TestIstioLoggerLevel_A001(t *testing.T) {
cs := testclient.NewSimpleClientset()
input := &appv1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "unit-test-pod"}}
Expand All @@ -134,3 +162,23 @@ func TestIstioLoggerLevel_A001(t *testing.T) {
t.Errorf("Error while using illegal loggerLevel")
}
}

func TestIstioLoggerLevel_A002(t *testing.T) {
cs := testclient.NewSimpleClientset()
input := &appv1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "unit-test-pod"}}

options := options{
clientset: cs,
namespace: "unit-test-namespace",
}

_, err := cs.CoreV1().Pods(options.namespace).Create(context.TODO(), input, metav1.CreateOptions{})
if err != nil {
t.Fatal(err.Error())
}

err = options.KubectlIstioLog("unit-test-pod", "http:debug", false)
if !successfullyParsedLoggerNameAndLevel(err) {
t.Errorf("Error while using illegal loggerName")
}
}

0 comments on commit 8a6bb6e

Please sign in to comment.