Skip to content
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

Upgrade api modules #4881

Closed
wants to merge 4 commits into from
Closed
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 backend/apid/graphql/util/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func UnwrapResource(r interface{}) interface{} {
// WrapResource safely wraps the given resource in a type wrapper
func WrapResource(r interface{}) types.Wrapper {
switch r := r.(type) {
case corev2.Resource:
return types.WrapResource(r)
case corev3.Resource: // maybe we move this into the compat package
var tm types.TypeMeta
if getter, ok := r.(interface{ GetTypeMeta() types.TypeMeta }); ok {
Expand All @@ -121,8 +123,6 @@ func WrapResource(r interface{}) types.Wrapper {
ObjectMeta: meta,
Value: r,
}
case corev2.Resource:
return types.WrapResource(r)
case *types.Wrapper:
if r == nil {
return types.Wrapper{}
Expand Down
281 changes: 143 additions & 138 deletions cli/resource/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package resource

import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"

"github.com/go-test/deep"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/types"
"github.com/sensu/sensu-go/types/compat"
"github.com/stretchr/testify/assert"
)

func TestValidate(t *testing.T) {
Expand Down Expand Up @@ -116,151 +115,157 @@ func TestParse(t *testing.T) {
yamlError = "%$^apiVersion: core/v2\ntype: Handler\nmetadata:\n namespace: default\n name: email\nspec:\n type: pipe\n command: sensu-email-handler \n -u USERNAME -p PASSWORD\n timeout: 10\n filters:\n - is_incident\n - not_silenced\n - state_change_only\n runtime_assets:\n - email-handler\n"
)

var (
checkConfigWrapper = &types.Wrapper{
ObjectMeta: corev2.ObjectMeta{Name: "foo"},
TypeMeta: corev2.TypeMeta{
Type: "CheckConfig",
APIVersion: "core/v2",
},
Value: &corev2.CheckConfig{
ObjectMeta: corev2.ObjectMeta{Name: "foo"},
Command: "echo foo",
Interval: 100,
},
}

handlerWrapper = &types.Wrapper{
ObjectMeta: corev2.ObjectMeta{Name: "email", Namespace: "default"},
TypeMeta: corev2.TypeMeta{
Type: "Handler",
APIVersion: "core/v2",
},
Value: &corev2.Handler{
ObjectMeta: corev2.ObjectMeta{Name: "email", Namespace: "default"},
Type: "pipe",
Command: "sensu-email-handler -u USERNAME -p PASSWORD",
Timeout: 10,
Filters: []string{"is_incident", "not_silenced", "state_change_only"},
RuntimeAssets: []string{"email-handler"},
checkConfigWrapper := &types.Wrapper{
ObjectMeta: corev2.ObjectMeta{
Name: "foo",
},
TypeMeta: corev2.TypeMeta{
Type: "CheckConfig",
APIVersion: "core/v2",
},
Value: &corev2.CheckConfig{
ObjectMeta: corev2.ObjectMeta{
Name: "foo",
Labels: map[string]string{},
Annotations: map[string]string{},
},
}
Command: "echo foo",
Interval: 100,
},
}

eventFilterWrapper = &types.Wrapper{
ObjectMeta: corev2.ObjectMeta{Name: "filter_minimum", Namespace: "default"},
TypeMeta: corev2.TypeMeta{
Type: "EventFilter",
APIVersion: "core/v2",
},
Value: &corev2.EventFilter{
ObjectMeta: corev2.ObjectMeta{Name: "filter_minimum", Namespace: "default"},
Action: "allow",
Expressions: []string{"event.check.occurrences == 1"},
handlerWrapper := &types.Wrapper{
ObjectMeta: corev2.ObjectMeta{
Namespace: "default",
Name: "email",
},
TypeMeta: corev2.TypeMeta{
Type: "Handler",
APIVersion: "core/v2",
},
Value: &corev2.Handler{
ObjectMeta: corev2.ObjectMeta{
Namespace: "default",
Name: "email",
Labels: map[string]string{},
Annotations: map[string]string{},
},
}
Type: "pipe",
Command: "sensu-email-handler -u USERNAME -p PASSWORD",
Timeout: 10,
Filters: []string{"is_incident", "not_silenced", "state_change_only"},
RuntimeAssets: []string{"email-handler"},
},
}

testUnits = []struct {
name string
fileContent string
expectedWrappers []*types.Wrapper
expectedError error
}{
{
name: "should parse a single unix formatted json resource",
fileContent: jsonUnix,
expectedWrappers: []*types.Wrapper{eventFilterWrapper},
expectedError: nil,
},
{
name: "should parse a single windows formatted json resource",
fileContent: jsonWindows,
expectedWrappers: []*types.Wrapper{eventFilterWrapper},
expectedError: nil,
},
{
name: "should parse a single unix formatted yaml resource",
fileContent: yamlUnixSingle,
expectedWrappers: []*types.Wrapper{handlerWrapper},
expectedError: nil,
},
{
name: "should parse a single windows formatted yaml resource",
fileContent: yamlWindowsSingle,
expectedWrappers: []*types.Wrapper{handlerWrapper},
expectedError: nil,
},
{
name: "should parse a single unix formatted yaml resource prefixed with ---",
fileContent: yamlUnixSinglePrefixed,
expectedWrappers: []*types.Wrapper{handlerWrapper},
expectedError: nil,
},
{
name: "should parse a single windows formatted yaml resource prefixed with ---",
fileContent: yamlWindowsSinglePrefixed,
expectedWrappers: []*types.Wrapper{handlerWrapper},
expectedError: nil,
},
{
name: "should parse multiple unix formatted yaml resources",
fileContent: yamlUnixMulti,
expectedWrappers: []*types.Wrapper{checkConfigWrapper, handlerWrapper},
expectedError: nil,
},
{
name: "should parse multiple windows formatted yaml resources",
fileContent: yamlWindowsMulti,
expectedWrappers: []*types.Wrapper{checkConfigWrapper, handlerWrapper},
expectedError: nil,
},
{
name: "should parse multiple unix formatted yaml resources prefixed with ---",
fileContent: yamlUnixMultiPrefixed,
expectedWrappers: []*types.Wrapper{checkConfigWrapper, handlerWrapper},
expectedError: nil,
},
{
name: "should parse multiple windows formatted yaml resources prefixed with ---",
fileContent: yamlWindowsMultiPrefixed,
expectedWrappers: []*types.Wrapper{checkConfigWrapper, handlerWrapper},
expectedError: nil,
},
{
name: "should return an error when parsing a badly formatted json file",
fileContent: jsonError,
expectedWrappers: nil,
expectedError: fmt.Errorf("too many errors"),
},
{
name: "should return an error when parsing a badly formatted yaml file",
fileContent: yamlError,
expectedWrappers: nil,
expectedError: fmt.Errorf("error parsing resources: yaml: could not find expected directive name"),
eventFilterWrapper := &types.Wrapper{
ObjectMeta: corev2.ObjectMeta{
Namespace: "default",
Name: "filter_minimum",
},
TypeMeta: corev2.TypeMeta{
Type: "EventFilter",
APIVersion: "core/v2",
},
Value: &corev2.EventFilter{
ObjectMeta: corev2.ObjectMeta{
Namespace: "default",
Name: "filter_minimum",
Labels: map[string]string{},
Annotations: map[string]string{},
},
}
)
Action: "allow",
Expressions: []string{"event.check.occurrences == 1"},
},
}

for _, testUnit := range testUnits {
stringReader := strings.NewReader(testUnit.fileContent)
wrappers, err := Parse(stringReader)
tests := []struct {
name string
fileContent string
want []*types.Wrapper
wantErr bool
wantErrMsg string
}{
{
name: "should parse a single unix formatted json resource",
fileContent: jsonUnix,
want: []*types.Wrapper{eventFilterWrapper},
},
{
name: "should parse a single windows formatted json resource",
fileContent: jsonWindows,
want: []*types.Wrapper{eventFilterWrapper},
},
{
name: "should parse a single unix formatted yaml resource",
fileContent: yamlUnixSingle,
want: []*types.Wrapper{handlerWrapper},
},
{
name: "should parse a single windows formatted yaml resource",
fileContent: yamlWindowsSingle,
want: []*types.Wrapper{handlerWrapper},
},
{
name: "should parse a single unix formatted yaml resource prefixed with ---",
fileContent: yamlUnixSinglePrefixed,
want: []*types.Wrapper{handlerWrapper},
},
{
name: "should parse a single windows formatted yaml resource prefixed with ---",
fileContent: yamlWindowsSinglePrefixed,
want: []*types.Wrapper{handlerWrapper},
},
{
name: "should parse multiple unix formatted yaml resources",
fileContent: yamlUnixMulti,
want: []*types.Wrapper{checkConfigWrapper, handlerWrapper},
},
{
name: "should parse multiple windows formatted yaml resources",
fileContent: yamlWindowsMulti,
want: []*types.Wrapper{checkConfigWrapper, handlerWrapper},
},
{
name: "should parse multiple unix formatted yaml resources prefixed with ---",
fileContent: yamlUnixMultiPrefixed,
want: []*types.Wrapper{checkConfigWrapper, handlerWrapper},
},
{
name: "should parse multiple windows formatted yaml resources prefixed with ---",
fileContent: yamlWindowsMultiPrefixed,
want: []*types.Wrapper{checkConfigWrapper, handlerWrapper},
},
{
name: "should return an error when parsing a badly formatted json file",
fileContent: jsonError,
wantErr: true,
wantErrMsg: "too many errors",
},
{
name: "should return an error when parsing a badly formatted yaml file",
fileContent: yamlError,
wantErr: true,
wantErrMsg: "error parsing resources: yaml: could not find expected directive name",
},
}

if testUnit.expectedError != nil {
if assert.Errorf(t, err, "missing error when processing '%s'", testUnit.name) {
assert.Equal(t, testUnit.expectedError, err, "invalid error when processing %s", testUnit.name)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stringReader := strings.NewReader(tt.fileContent)
got, err := Parse(stringReader)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, 0, len(wrappers),
"there should be no wrapper when processing '%s'", testUnit.name)
continue
} else {
if !assert.Nil(t, err, "unexpected error when processing '%s': %v", testUnit.name, err) {
continue
if err != nil && tt.wantErrMsg != err.Error() {
t.Errorf("Parse() error msg = %v, wantErrMsg %v", err.Error(), tt.wantErrMsg)
return
}
}

if !assert.Equal(t, len(testUnit.expectedWrappers), len(wrappers),
"wrong number of resources parsed when processing '%s'", testUnit.name) {
continue
}
assert.Equal(t, testUnit.expectedWrappers, wrappers, "wrappers should be equal when processing '%s'", testUnit.name)
if diff := deep.Equal(got, tt.want); diff != nil {
t.Errorf("Parse() got differs from want: %v", diff)
}
})
}
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ require (
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff
github.com/robfig/cron/v3 v3.0.1
github.com/sensu/lasr v1.2.1
github.com/sensu/sensu-go/api/core/v2 v2.14.0
github.com/sensu/sensu-go/api/core/v3 v3.6.3-0.20220913191107-10ae2ae7d8cf
github.com/sensu/sensu-go/types v0.10.0
github.com/sensu/sensu-go/api/core/v2 v2.15.1-alpha2
github.com/sensu/sensu-go/api/core/v3 v3.7.1-alpha1
github.com/sensu/sensu-go/types v0.11.0
github.com/shirou/gopsutil/v3 v3.21.12
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.3
Expand Down Expand Up @@ -78,6 +78,7 @@ require (

require (
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect
github.com/go-test/deep v1.0.8
github.com/google/go-cmp v0.5.8 // indirect
github.com/kr/pty v1.1.8 // indirect
golang.org/x/text v0.3.7 // indirect
Expand Down
14 changes: 10 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU
github.com/go-resty/resty/v2 v2.5.0 h1:WFb5bD49/85PO7WgAjZ+/TJQ+Ty1XOcWEfD1zIFCM1c=
github.com/go-resty/resty/v2 v2.5.0/go.mod h1:B88+xCTEwvfD94NOuE6GS1wMlnoKNY8eEiNizfNwOwA=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
Expand Down Expand Up @@ -392,13 +394,17 @@ github.com/schollz/progressbar/v2 v2.13.2/go.mod h1:6YZjqdthH6SCZKv2rqGryrxPtfmR
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sensu/lasr v1.2.1 h1:4H1QfOrPkwYHMFE5qAI6GwKEFkcI1YRyjjWidz1MihQ=
github.com/sensu/lasr v1.2.1/go.mod h1:VIMtIK67Bcef6dTfctRCBg8EY9M9TtCY9NEFT6Zw5xQ=
github.com/sensu/sensu-go/api/core/v2 v2.14.0 h1:z4JVqy7z6iFgMDUH0uc1Ns0bqLFKTpc5bi4Iw7qweks=
github.com/sensu/sensu-go/api/core/v2 v2.14.0/go.mod h1:XCgUjY78ApTahizBz/pkc5KU17L/E5BexeZHkGDdTls=
github.com/sensu/sensu-go/api/core/v2 v2.15.0/go.mod h1:QxGKxqQv4rpweFrR4Jkp1tas3amGzAy0wO0fUwq0suU=
github.com/sensu/sensu-go/api/core/v2 v2.15.1-alpha2 h1:6hUVaCWZkzUPevOXjrSJoGdW4iF3CxdCrahv4m7bPB0=
github.com/sensu/sensu-go/api/core/v2 v2.15.1-alpha2/go.mod h1:MjM7+MCGEyTAgaZ589SiGHwYiaYF7N/58dU0J070u/0=
github.com/sensu/sensu-go/api/core/v3 v3.6.1/go.mod h1:aqNOkJxkrwRq+rPW47XtVWeb5Rk1K5adlCZGBW9nsvM=
github.com/sensu/sensu-go/api/core/v3 v3.6.3-0.20220913191107-10ae2ae7d8cf h1:K1VrKHGwQ4UpOQmy2J6IFyv0u17OKTwWrfAGILClpbw=
github.com/sensu/sensu-go/api/core/v3 v3.6.3-0.20220913191107-10ae2ae7d8cf/go.mod h1:n2dhnBTovMrzmE1P0D7gvEbUG7TPH6hdtr7qvoPf/sY=
github.com/sensu/sensu-go/types v0.10.0 h1:sm+dLuqEEECVxjW5EfXkU5weGPwrg/Jymbm28HdQpl8=
github.com/sensu/sensu-go/api/core/v3 v3.6.2/go.mod h1:aqNOkJxkrwRq+rPW47XtVWeb5Rk1K5adlCZGBW9nsvM=
github.com/sensu/sensu-go/api/core/v3 v3.7.1-alpha1 h1:nd6ib+RMRI/e7q++kmtcFt5HrJCBtmz6UGUMH132XhY=
github.com/sensu/sensu-go/api/core/v3 v3.7.1-alpha1/go.mod h1:8io5TBGBcuR9B5MiWUi3bqr2+sGe+qcH5p6NjhAlzy4=
github.com/sensu/sensu-go/types v0.10.0/go.mod h1:vFZJ9TYBAjSPYtYt+82PpS9P6m73Vzr4O23lmJonzrA=
github.com/sensu/sensu-go/types v0.11.0 h1:jsVa/apRaJJEdk0Jl7ZUksiBkuEAjCZd/gSBWlrptJA=
github.com/sensu/sensu-go/types v0.11.0/go.mod h1:fhgW3xlvkPFMZiT0IppHySeyN61ZTIKevgSPFSoaQEk=
github.com/shirou/gopsutil/v3 v3.21.12 h1:VoGxEW2hpmz0Vt3wUvHIl9fquzYLNpVpgNNB7pGJimA=
github.com/shirou/gopsutil/v3 v3.21.12/go.mod h1:BToYZVTlSVlfazpDDYFnsVZLaoRG+g8ufT6fPQLdJzA=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down