From 7b56539b87e7b7762978b121996f7958330c6c96 Mon Sep 17 00:00:00 2001 From: Christian Kruse Date: Thu, 29 Sep 2022 11:15:22 -0700 Subject: [PATCH 1/4] bump sensu go core and types modules Signed-off-by: Christian Kruse --- go.mod | 6 +++--- go.sum | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index c3e7401330..ba6a5bcb8b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 941162f484..717b719add 100644 --- a/go.sum +++ b/go.sum @@ -394,11 +394,19 @@ 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 h1:TdH4Iq2OfpVqoY3yVXpUVe2lMmC0Ksp9Dq3wpIELXRM= +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/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 h1:sm+dLuqEEECVxjW5EfXkU5weGPwrg/Jymbm28HdQpl8= 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= From a8af40674540739db071b96a4912e386c6ed2887 Mon Sep 17 00:00:00 2001 From: Christian Kruse Date: Thu, 29 Sep 2022 11:19:45 -0700 Subject: [PATCH 2/4] Patch graphql api util for api/core/v2 compat Signed-off-by: Christian Kruse --- backend/apid/graphql/util/api/api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/apid/graphql/util/api/api.go b/backend/apid/graphql/util/api/api.go index a71f28df10..40a505d883 100644 --- a/backend/apid/graphql/util/api/api.go +++ b/backend/apid/graphql/util/api/api.go @@ -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 { @@ -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{} From 90e4e9788628558f5ef959b67c280901aa036954 Mon Sep 17 00:00:00 2001 From: Christian Kruse Date: Thu, 29 Sep 2022 11:23:05 -0700 Subject: [PATCH 3/4] add go-test deep dependency Signed-off-by: Christian Kruse --- go.mod | 1 + go.sum | 8 +++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index ba6a5bcb8b..8cbc17d7f1 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 717b719add..ca2e6846ad 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -392,18 +394,14 @@ 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 h1:TdH4Iq2OfpVqoY3yVXpUVe2lMmC0Ksp9Dq3wpIELXRM= 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/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 h1:sm+dLuqEEECVxjW5EfXkU5weGPwrg/Jymbm28HdQpl8= 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= From 7ea8204c7b89dd470ae49990966eab3d7ea55989 Mon Sep 17 00:00:00 2001 From: Justin Kolberg Date: Mon, 25 Jul 2022 13:54:36 -0700 Subject: [PATCH 4/4] fix cli/resource/TestParse tests Signed-off-by: Justin Kolberg (cherry picked from commit ffbe551112adddb49453051711636187da60348d) --- cli/resource/parse_test.go | 281 +++++++++++++++++++------------------ 1 file changed, 143 insertions(+), 138 deletions(-) diff --git a/cli/resource/parse_test.go b/cli/resource/parse_test.go index e456ecae50..70fe808dc6 100644 --- a/cli/resource/parse_test.go +++ b/cli/resource/parse_test.go @@ -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) { @@ -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) + } + }) } }