Files
Packer-Cn/internal/hcp/api/errors_test.go
Anurag Sharma 543123ac17 bump golang.org/x/crypto to 0.43.0 (#13518)
* bump golang.org/x/crypto to 0.43.0
CVE-2025-47913 GO-2025-4116

* fixed go.sum

* fixed multiple warnings that prevented test runs

* make generate

* fix lint errors, update linter version

* fix go vet issues
2025-11-18 15:49:03 +05:30

58 lines
1.2 KiB
Go

package api
import (
"fmt"
"testing"
"google.golang.org/grpc/codes"
)
func TestCheckErrorCode(t *testing.T) {
tests := []struct {
name string
codeString string
expectCode codes.Code
expectSuccess bool
}{
{
"old format, code matches what is looked for",
`{Code:5,"details":[],"message":"Error: The bucket etc."}`,
codes.Code(5),
true,
},
{
"old format, code doesn't match what is looked for",
`{Code:55,"details":[],"message":"Error: The bucket etc."}`,
codes.Code(5),
false,
},
{
"new format, code matches what is looked for",
`{"code":5,"details":[],"message":"Error: The bucket etc."}`,
codes.Code(5),
true,
},
{
"new format, code doesn't match what is looked for",
`{"code":55,"details":[],"message":"Error: The bucket etc."}`,
codes.Code(5),
false,
},
{
"bad format, should always be false",
`"ceod":55`,
codes.Code(5),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
found := CheckErrorCode(fmt.Errorf("%s", tt.codeString), tt.expectCode)
if found != tt.expectSuccess {
t.Errorf("check error code returned %t, expected %t", found, tt.expectSuccess)
}
})
}
}