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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ require (
github.com/go-openapi/runtime v0.19.16 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/goccy/go-json v0.10.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/gnostic-models v0.6.9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWe
github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ=
github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0=
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/goccy/go-json v0.10.6 h1:p8HrPJzOakx/mn/bQtjgNjdTcN+/S6FcG2CTtQOrHVU=
github.com/goccy/go-json v0.10.6/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/gofrs/uuid/v5 v5.3.2 h1:2jfO8j3XgSwlz/wHqemAEugfnTlikAYHhnqQ8Xh4fE0=
Expand Down
2 changes: 1 addition & 1 deletion pkg/debug/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package debug

import (
"encoding/json"
"fmt"
"io"
"log/slog"
Expand All @@ -19,6 +18,7 @@ import (

pkg "github.com/flant/shell-operator/pkg"
utils "github.com/flant/shell-operator/pkg/utils/file"
json "github.com/flant/shell-operator/pkg/utils/json"
structuredLogger "github.com/flant/shell-operator/pkg/utils/structured-logger"
)

Expand Down
85 changes: 85 additions & 0 deletions pkg/debug/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package debug

import (
"bytes"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func newStubRequest(path string) *http.Request {
if path == "" {
path = "/debug/test"
}
return httptest.NewRequest(http.MethodGet, path, nil)
}

func TestTransformUsingFormat_JSON(t *testing.T) {
var buf bytes.Buffer
err := transformUsingFormat(&buf, map[string]int{"count": 42}, "json")
require.NoError(t, err)
assert.Contains(t, buf.String(), `"count":42`)
}

func TestTransformUsingFormat_YAML(t *testing.T) {
var buf bytes.Buffer
err := transformUsingFormat(&buf, map[string]string{"name": "test"}, "yaml")
require.NoError(t, err)
assert.Contains(t, buf.String(), "name: test")
}

func TestTransformUsingFormat_Text_String(t *testing.T) {
var buf bytes.Buffer
err := transformUsingFormat(&buf, "hello world", "text")
require.NoError(t, err)
assert.Equal(t, "hello world", buf.String())
}

func TestTransformUsingFormat_Text_Bytes(t *testing.T) {
var buf bytes.Buffer
err := transformUsingFormat(&buf, []byte("raw bytes"), "text")
require.NoError(t, err)
assert.Equal(t, "raw bytes", buf.String())
}

func TestTransformUsingFormat_Text_Stringer(t *testing.T) {
var buf bytes.Buffer
err := transformUsingFormat(&buf, stringerVal{s: "from-stringer"}, "text")
require.NoError(t, err)
assert.Equal(t, "from-stringer", buf.String())
}

type stringerVal struct{ s string }

func (sv stringerVal) String() string { return sv.s }

func TestTransformUsingFormat_Text_NonStringFallsBackToJSON(t *testing.T) {
var buf bytes.Buffer
err := transformUsingFormat(&buf, map[string]int{"x": 1}, "text")
require.NoError(t, err)
assert.Contains(t, buf.String(), `"x":1`)
}

func TestTransformUsingFormat_UnknownFormatFallsBackToJSON(t *testing.T) {
var buf bytes.Buffer
err := transformUsingFormat(&buf, map[string]int{"y": 2}, "xml")
require.NoError(t, err)
assert.Contains(t, buf.String(), `"y":2`)
}

func TestFormatFromRequest_Default(t *testing.T) {
assert.Equal(t, "text", FormatFromRequest(newStubRequest("")))
}

func TestBadRequestError(t *testing.T) {
err := &BadRequestError{Msg: "missing param"}
assert.Equal(t, "missing param", err.Error())
}

func TestNotFoundError(t *testing.T) {
err := &NotFoundError{Msg: "not found"}
assert.Equal(t, "not found", err.Error())
}
2 changes: 1 addition & 1 deletion pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
Expand All @@ -17,6 +16,7 @@ import (
"go.opentelemetry.io/otel"

pkg "github.com/flant/shell-operator/pkg"
json "github.com/flant/shell-operator/pkg/utils/json"
utils "github.com/flant/shell-operator/pkg/utils/labels"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package executor
import (
"bytes"
"context"
"encoding/json"
json "github.com/flant/shell-operator/pkg/utils/json"
"fmt"
"io"
"math/rand/v2"
Expand Down
2 changes: 1 addition & 1 deletion pkg/filter/jq/apply.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package jq

import (
"encoding/json"
"errors"
"fmt"

"github.com/itchyny/gojq"

"github.com/flant/shell-operator/pkg/filter"
json "github.com/flant/shell-operator/pkg/utils/json"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/filter/jq/apply_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jq

import (
"encoding/json"
json "github.com/flant/shell-operator/pkg/utils/json"
"testing"

. "github.com/onsi/gomega"
Expand Down
2 changes: 1 addition & 1 deletion pkg/hook/binding_context/binding_context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bindingcontext

import (
"encoding/json"
"io"

"github.com/deckhouse/deckhouse/pkg/log"
Expand All @@ -10,6 +9,7 @@ import (

htypes "github.com/flant/shell-operator/pkg/hook/types"
kemtypes "github.com/flant/shell-operator/pkg/kube_events_manager/types"
json "github.com/flant/shell-operator/pkg/utils/json"
)

// BindingContext contains information about event for hook
Expand Down
3 changes: 2 additions & 1 deletion pkg/hook/binding_context/binding_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package bindingcontext

import (
"bytes"
"encoding/json"
"testing"

json "github.com/flant/shell-operator/pkg/utils/json"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down
3 changes: 2 additions & 1 deletion pkg/hook/config/schemas.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package config

import (
"encoding/json"
"fmt"

"github.com/go-openapi/spec"
"github.com/go-openapi/swag"

json "github.com/flant/shell-operator/pkg/utils/json"
)

var Schemas = map[string]string{
Expand Down
3 changes: 2 additions & 1 deletion pkg/hook/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package hook

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"

"github.com/hashicorp/go-multierror"

json "github.com/flant/shell-operator/pkg/utils/json"
)

type MetricOperation struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kube/object_patch/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package object_patch

import (
"bytes"
"encoding/json"
"fmt"
"io"

Expand All @@ -13,6 +12,7 @@ import (

"github.com/flant/kube-client/manifest"
"github.com/flant/shell-operator/pkg/filter"
json "github.com/flant/shell-operator/pkg/utils/json"
)

func unmarshalFromJSONOrYAML(specs []byte) ([]OperationSpec, error) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kube/object_patch/validation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package object_patch

import (
"encoding/json"
"fmt"

"github.com/go-openapi/spec"
Expand All @@ -10,6 +9,8 @@ import (
"github.com/go-openapi/validate"
"github.com/hashicorp/go-multierror"
"sigs.k8s.io/yaml"

json "github.com/flant/shell-operator/pkg/utils/json"
)

var Schemas = map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion pkg/kube_events_manager/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kubeeventsmanager

import (
"context"
"encoding/json"
"fmt"
"reflect"
"runtime"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/flant/shell-operator/pkg/filter"
kemtypes "github.com/flant/shell-operator/pkg/kube_events_manager/types"
utils_checksum "github.com/flant/shell-operator/pkg/utils/checksum"
json "github.com/flant/shell-operator/pkg/utils/json"
)

// applyFilter filters object json representation with a pre-compiled jq expression,
Expand Down
5 changes: 3 additions & 2 deletions pkg/kube_events_manager/filter_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kubeeventsmanager

import (
"encoding/json"
json "github.com/flant/shell-operator/pkg/utils/json"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,7 +15,8 @@ func TestApplyFilter(t *testing.T) {
t.Run("filter func with error", func(t *testing.T) {
uns := &unstructured.Unstructured{Object: map[string]interface{}{"foo": "bar"}}
_, err := applyFilter(nil, "", filterFuncWithError, uns)
assert.EqualError(t, err, "filterFn (github.com/flant/shell-operator/pkg/kube_events_manager.filterFuncWithError) contains an error: invalid character 'a' looking for beginning of value")
require.Error(t, err)
assert.Contains(t, err.Error(), "filterFn (github.com/flant/shell-operator/pkg/kube_events_manager.filterFuncWithError) contains an error:")
})

t.Run("nil compiledFilter computes checksum over full object", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kube_events_manager/types/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"encoding/json"
"fmt"
"log/slog"
"strings"
Expand All @@ -11,6 +10,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

pkg "github.com/flant/shell-operator/pkg"
json "github.com/flant/shell-operator/pkg/utils/json"
)

type WatchEventType string
Expand Down
2 changes: 1 addition & 1 deletion pkg/kube_events_manager/types/types_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

import (
"encoding/json"
json "github.com/flant/shell-operator/pkg/utils/json"
"sort"
"testing"

Expand Down
2 changes: 1 addition & 1 deletion pkg/task/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package dump

import (
"context"
"encoding/json"
"fmt"
"sort"
"strings"

"github.com/flant/shell-operator/pkg/task"
"github.com/flant/shell-operator/pkg/task/queue"
json "github.com/flant/shell-operator/pkg/utils/json"
)

// asQueueNames implements sort.Interface for array of queue names.
Expand Down
2 changes: 1 addition & 1 deletion pkg/task/dump/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dump

import (
"context"
"encoding/json"
json "github.com/flant/shell-operator/pkg/utils/json"
"fmt"
"sort"
"testing"
Expand Down
Loading
Loading