github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/cmd/termination/termination_test.go (about)

     1  package termination
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/authzed/spicedb/pkg/spiceerrors"
    11  
    12  	"github.com/spf13/cobra"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestPublishError(t *testing.T) {
    17  	cmd := cobra.Command{}
    18  	RegisterFlags(cmd.Flags())
    19  
    20  	f, err := os.CreateTemp("", "")
    21  	require.NoError(t, err)
    22  	filePath := f.Name()
    23  	cmd.Flag(terminationLogFlagName).Value = newStringValue(filePath, &filePath)
    24  	publishedError := spiceerrors.NewTerminationErrorBuilder(errors.New("hi")).Metadata("k", "v").Component("test").Error()
    25  	err = PublishError(func(cmd *cobra.Command, args []string) error {
    26  		return publishedError
    27  	})(&cmd, nil)
    28  	var termErr spiceerrors.TerminationError
    29  	require.ErrorAs(t, err, &termErr)
    30  
    31  	jsonBytes, err := os.ReadFile(f.Name())
    32  	require.NoError(t, err)
    33  	var readErr spiceerrors.TerminationError
    34  	err = json.Unmarshal(jsonBytes, &readErr)
    35  	require.NoError(t, err)
    36  
    37  	require.Equal(t, "hi", readErr.ErrorString)
    38  	require.Equal(t, "test", readErr.Component)
    39  	require.NotEmpty(t, readErr.Timestamp)
    40  	require.Equal(t, publishedError.Timestamp, readErr.Timestamp)
    41  	require.Len(t, readErr.Metadata, 1)
    42  	require.Equal(t, readErr.Metadata["k"], "v")
    43  
    44  	// test error metadata is truncated when too large
    45  	var builder strings.Builder
    46  	for i := 0; i < kubeTerminationLogLimit; i++ {
    47  		builder.WriteString("a")
    48  	}
    49  	publishedError.Metadata["large_value"] = builder.String()
    50  	_ = PublishError(func(cmd *cobra.Command, args []string) error {
    51  		return publishedError
    52  	})(&cmd, nil)
    53  
    54  	jsonBytes, err = os.ReadFile(f.Name())
    55  	require.NoError(t, err)
    56  	err = json.Unmarshal(jsonBytes, &readErr)
    57  	require.NoError(t, err)
    58  	require.Len(t, readErr.Metadata, 0)
    59  }
    60  
    61  func TestPublishDisabled(t *testing.T) {
    62  	cmd := cobra.Command{}
    63  	RegisterFlags(cmd.Flags())
    64  
    65  	filePath := ""
    66  	cmd.Flag(terminationLogFlagName).Value = newStringValue(filePath, &filePath)
    67  	publishedError := spiceerrors.NewTerminationErrorBuilder(errors.New("hi")).Metadata("k", "v").Component("test").Error()
    68  	err := PublishError(func(cmd *cobra.Command, args []string) error {
    69  		return publishedError
    70  	})(&cmd, nil)
    71  	var termErr spiceerrors.TerminationError
    72  	require.ErrorAs(t, err, &termErr)
    73  }
    74  
    75  type testValue string
    76  
    77  func newStringValue(val string, p *string) *testValue {
    78  	*p = val
    79  	return (*testValue)(p)
    80  }
    81  
    82  func (s *testValue) Set(val string) error {
    83  	*s = testValue(val)
    84  	return nil
    85  }
    86  
    87  func (s *testValue) Type() string {
    88  	return "string"
    89  }
    90  
    91  func (s *testValue) String() string {
    92  	return string(*s)
    93  }