Skip to content

Commit

Permalink
feat(blueprint-test): add ParseKubectlJSONResult
Browse files Browse the repository at this point in the history
  • Loading branch information
apeabody committed Nov 8, 2024
1 parent f7d39ea commit 5190f3f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
16 changes: 16 additions & 0 deletions infra/blueprint-test/pkg/utils/jsonresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package utils

import (
"os"
"regexp"

"github.com/mitchellh/go-testing-interface"
"github.com/tidwall/gjson"
Expand All @@ -43,3 +44,18 @@ func ParseJSONResult(t testing.TB, j string) gjson.Result {
}
return gjson.Parse(j)
}

// Kubectl transient errors
var (
KubectlTransientErrors = []string{
"E022[23] .* the server is currently unable to handle the request",
}
)

// Filter transient errors from kubectl output
func ParseKubectlJSONResult(t testing.TB, str string) gjson.Result {
for _, error := range KubectlTransientErrors {
str = regexp.MustCompile(error).ReplaceAllString(str, "")
}
return ParseJSONResult(t, str)
}
60 changes: 60 additions & 0 deletions infra/blueprint-test/pkg/utils/jsonresult_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2024 Google LLC
*
* 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 utils

import (
"testing"

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

func TestKubectlJSONResult(t *testing.T) {
tests := []struct {
name string
input string
output map[string]interface{}
}{
{
name: "one error",
input: `E0223 error the server is currently unable to handle the request
{
"apiVersion": "v1"
}`,
output: map[string]interface{}{
"apiVersion": "v1",
},
},
{
name: "two error",
input: `E0223 error the server is currently unable to handle the request
E0222 some other error so the server is currently unable to handle the request
{
"apiVersion": "v1"
}`,
output: map[string]interface{}{
"apiVersion": "v1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
funcOut := ParseKubectlJSONResult(t, tt.input)
assert.Equal(tt.output, funcOut.Value().(map[string]interface{}))
})
}
}

0 comments on commit 5190f3f

Please sign in to comment.