Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ bin/gofumpt: curl-installed bin

deps: bin bin/jq bin/golangci-lint bin/gofumpt

test:
go test -v -p 1 $(go list ./... | grep -v /validation/)
test: go-installed
./hack/run_tests.sh

lint: bin/golangci-lint
./bin/golangci-lint run ./... -c .golangci.yaml
Expand Down
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
module github.com/deckhouse/lib-dhctl

go 1.25.3

require (
github.com/deckhouse/deckhouse/pkg/log v0.1.0
github.com/gookit/color v1.5.2
github.com/name212/govalue v1.0.2
github.com/stretchr/testify v1.9.0
github.com/werf/logboek v0.5.5
k8s.io/klog/v2 v2.130.1
)

require (
github.com/DataDog/gostackparse v0.7.0 // indirect
github.com/avelino/slugify v0.0.0-20180501145920-855f152bd774 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/crypto v0.46.0 // indirect
golang.org/x/net v0.47.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/term v0.38.0 // indirect
golang.org/x/text v0.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
46 changes: 46 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
github.com/DataDog/gostackparse v0.7.0 h1:i7dLkXHvYzHV308hnkvVGDL3BR4FWl7IsXNPz/IGQh4=
github.com/DataDog/gostackparse v0.7.0/go.mod h1:lTfqcJKqS9KnXQGnyQMCugq3u1FP6UZMfWR0aitKFMM=
github.com/avelino/slugify v0.0.0-20180501145920-855f152bd774 h1:HrMVYtly2IVqg9EBooHsakQ256ueojP7QuG32K71X/U=
github.com/avelino/slugify v0.0.0-20180501145920-855f152bd774/go.mod h1:5wi5YYOpfuAKwL5XLFYopbgIl/v7NZxaJpa/4X6yFKE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/deckhouse/pkg/log v0.1.0 h1:2aPfyiHHSIJlX4x7ysyPOaIb7CLmyY+hUf9uDb8TYd8=
github.com/deckhouse/deckhouse/pkg/log v0.1.0/go.mod h1:pbAxTSDcPmwyl3wwKDcEB3qdxHnRxqTV+J0K+sha8bw=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/gookit/color v1.5.2 h1:uLnfXcaFjlrDnQDT+NCBcfhrXqYTx/rcCa6xn01Y8yI=
github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg=
github.com/name212/govalue v1.0.2 h1:NLpLfZatHyJYMohyUP8+qXtP8OriHQToxZv+DIXNrno=
github.com/name212/govalue v1.0.2/go.mod h1:3mLA4mFb82esucQHCOIAnUjN7e7AZnRYEfxeaHLKjho=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/werf/logboek v0.5.5 h1:RmtTejHJOyw0fub4pIfKsb7OTzD90ZOUyuBAXqYqJpU=
github.com/werf/logboek v0.5.5/go.mod h1:Gez5J4bxekyr6MxTmIJyId1F61rpO+0/V4vjCIEIZmk=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
40 changes: 40 additions & 0 deletions hack/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

# Copyright 2025 Flant JSC
#
# 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.

run_tests=""

if [ -n "$RUN_TEST" ]; then
echo "Found RUN_TEST env. Run only $RUN_TEST test"
run_tests="-run $RUN_TEST"
fi

run_dir="$(pwd)"
packages="$(go list ./... | grep -v /validation/)"
prefix="$(grep -oP 'module .*$' go.mod | sed 's|module ||')"

echo "Found packages: $packages in $run_dir with module $prefix"

for p in "$packages"; do
pkg_dir="${p#$prefix}"
if [ -z "$pkg_dir" ]; then
echo "Package $p cannot have dir after trim $prefix"
exit 1
fi
full_pkg_path="${run_dir}${pkg_dir}"
echo "Run tests in $full_pkg_path"
cd "$full_pkg_path"
echo "test -v -p 1 $run_tests" | xargs go
done
109 changes: 109 additions & 0 deletions pkg/log/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2025 Flant JSC
//
// 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 log

import (
"bytes"
"fmt"
"testing"

"github.com/name212/govalue"
"github.com/stretchr/testify/require"
)

func assertInBuffer(t *testing.T, buf *bytes.Buffer, msg string, inOut bool) {
out := buf.String()
assert := require.NotContains
if inOut {
assert = require.Contains
}
assert(t, out, msg)
}

func assertFollowAllInterfaces(t *testing.T, logger Logger) {
t.Run("Silent logger", func(t *testing.T) {
assertSilentLoggerProviderFollowFormatLnInterface(t, logger)
})

t.Run("Format Ln logger", func(t *testing.T) {
assertFollowFormatLnInterface(t, logger)
})

t.Run("Buffered logger", func(t *testing.T) {
assertBufferedLoggerProviderFollowFormatLnInterface(t, logger)
})
}

func assertFollowFormatLnInterface(t *testing.T, logger Logger) {
runs := []func(){
func() {
logger.InfoFLn("INFO %s", "test_info")
},
func() {
logger.WarnFLn("WARN %s", "test_warn")
},

func() {
logger.DebugFLn("DEBUG %s", "test_debug")
},

func() {
logger.ErrorFLn("ERROR %v", fmt.Errorf("test_error"))
},
}

for i, run := range runs {
t.Run(fmt.Sprintf("Does not panic FLn func %d", i), func(t *testing.T) {
require.NotPanics(t, run)
})
}
}

func assertSilentLoggerProviderFollowFormatLnInterface(t *testing.T, provider silentLoggerProvider) {
silentLogger := provider.SilentLogger()

require.NotNil(t, silentLogger)

assertFollowFormatLnInterface(t, silentLogger)
}

func assertBufferedLoggerProviderFollowFormatLnInterfaceWithoutCheckWrite(t *testing.T, provider bufferLoggerProvider) *bytes.Buffer {
buf := bytes.NewBuffer(nil)

bufferedLogger := provider.BufferLogger(buf)

require.False(t, govalue.IsNil(bufferedLogger))

assertFollowFormatLnInterface(t, bufferedLogger)

return buf
}

func assertBufferedLoggerProviderFollowFormatLnInterface(t *testing.T, provider bufferLoggerProvider) {
buf := assertBufferedLoggerProviderFollowFormatLnInterfaceWithoutCheckWrite(t, provider)

messagesInBuffer := []string{
"INFO test_info",
"WARN test_warn",
"DEBUG test_debug",
"ERROR test_error",
}

bufContent := buf.String()

for _, message := range messagesInBuffer {
require.Contains(t, bufContent, message, "expected buffer to contain", message)
}
}
124 changes: 124 additions & 0 deletions pkg/log/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2025 Flant JSC
//
// 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 log

import (
"bytes"
"fmt"
"io"
)

var (
_ baseLogger = &DummyLogger{}
_ formatWithNewLineLogger = &DummyLogger{}
_ Logger = &DummyLogger{}
_ io.Writer = &DummyLogger{}
)

type DummyLogger struct {
*formatWithNewLineLoggerWrapper

isDebug bool
}

func NewDummyLogger(isDebug bool) *DummyLogger {
l := &DummyLogger{
isDebug: isDebug,
}

l.formatWithNewLineLoggerWrapper = newFormatWithNewLineLoggerWrapper(l)

return l
}

func (d *DummyLogger) ProcessLogger() ProcessLogger {
return newWrappedProcessLogger(d)
}

func (d *DummyLogger) SilentLogger() *SilentLogger {
return NewSilentLogger()
}

func (d *DummyLogger) BufferLogger(buffer *bytes.Buffer) Logger {
return NewSimpleLogger(LoggerOptions{OutStream: buffer, IsDebug: d.isDebug})
}

func (d *DummyLogger) FlushAndClose() error {
return nil
}

func (d *DummyLogger) Process(_ Process, t string, run func() error) error {
fmt.Println(t)
err := run()
fmt.Println(t)
return err
}

func (d *DummyLogger) InfoF(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

func (d *DummyLogger) InfoLn(a ...interface{}) {
fmt.Println(a...)
}

func (d *DummyLogger) ErrorF(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

func (d *DummyLogger) ErrorLn(a ...interface{}) {
fmt.Println(a...)
}

func (d *DummyLogger) DebugF(format string, a ...interface{}) {
if d.isDebug {
fmt.Printf(format, a...)
}
}

func (d *DummyLogger) DebugLn(a ...interface{}) {
if d.isDebug {
fmt.Println(a...)
}
}

func (d *DummyLogger) Success(l string) {
fmt.Println(l)
}

func (d *DummyLogger) Fail(l string) {
fmt.Println(l)
}

func (d *DummyLogger) FailRetry(l string) {
d.Fail(l)
}

func (d *DummyLogger) WarnLn(a ...interface{}) {
fmt.Println(a...)
}

func (d *DummyLogger) WarnF(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

func (d *DummyLogger) JSON(content []byte) {
fmt.Println(string(content))
}

func (d *DummyLogger) Write(content []byte) (int, error) {
fmt.Print(string(content))
return len(content), nil
}
21 changes: 21 additions & 0 deletions pkg/log/dummy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2025 Flant JSC
//
// 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 log

import "testing"

func TestDummyLoggerFollowInterfaces(t *testing.T) {
assertFollowAllInterfaces(t, NewDummyLogger(true))
}
Loading