Files
tasks/common/headers/caller_info_test.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

170 lines
4.2 KiB
Go

package headers
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc/metadata"
)
type (
callerInfoSuite struct {
*require.Assertions
suite.Suite
}
)
func TestCallerInfoSuite(t *testing.T) {
suite.Run(t, &callerInfoSuite{})
}
func (s *callerInfoSuite) SetupTest() {
s.Assertions = require.New(s.T())
}
func (s *callerInfoSuite) TestSetCallerName() {
ctx := context.Background()
info := GetCallerInfo(ctx)
s.Empty(info.CallerName)
ctx = SetCallerName(ctx, CallerNameSystem)
info = GetCallerInfo(ctx)
s.Equal(CallerNameSystem, info.CallerName)
ctx = SetCallerName(ctx, "")
info = GetCallerInfo(ctx)
s.Equal(CallerNameSystem, info.CallerName)
newCallerName := "new caller name"
ctx = SetCallerName(ctx, newCallerName)
info = GetCallerInfo(ctx)
s.Equal(newCallerName, info.CallerName)
}
func (s *callerInfoSuite) TestSetCallerType() {
ctx := context.Background()
info := GetCallerInfo(ctx)
s.Empty(info.CallerType)
ctx = SetCallerType(ctx, CallerTypeBackground)
info = GetCallerInfo(ctx)
s.Equal(CallerTypeBackground, info.CallerType)
ctx = SetCallerName(ctx, "")
info = GetCallerInfo(ctx)
s.Equal(CallerTypeBackground, info.CallerType)
ctx = SetCallerType(ctx, CallerTypeAPI)
info = GetCallerInfo(ctx)
s.Equal(CallerTypeAPI, info.CallerType)
ctx = SetCallerType(ctx, CallerTypePreemptable)
info = GetCallerInfo(ctx)
s.Equal(CallerTypePreemptable, info.CallerType)
}
func (s *callerInfoSuite) TestSetCallOrigin() {
ctx := context.Background()
info := GetCallerInfo(ctx)
s.Empty(info.CallOrigin)
initiation := "method name"
ctx = SetOrigin(ctx, initiation)
info = GetCallerInfo(ctx)
s.Equal(initiation, info.CallOrigin)
ctx = SetOrigin(ctx, "")
info = GetCallerInfo(ctx)
s.Equal(initiation, info.CallOrigin)
newCallOrigin := "another method name"
ctx = SetOrigin(ctx, newCallOrigin)
info = GetCallerInfo(ctx)
s.Equal(newCallOrigin, info.CallOrigin)
}
func (s *callerInfoSuite) TestSetCallerInfo_PreserveOtherValues() {
existingKey := "key"
existingValue := "value"
callerName := "callerName"
callerType := CallerTypeAPI
callOrigin := "methodName"
ctx := metadata.NewIncomingContext(
context.Background(),
metadata.Pairs(existingKey, existingValue),
)
ctx = SetCallerInfo(ctx, NewCallerInfo(callerName, callerType, callOrigin))
md, ok := metadata.FromIncomingContext(ctx)
s.True(ok)
s.Equal(existingValue, md.Get(existingKey)[0])
s.Equal(callerName, md.Get(CallerNameHeaderName)[0])
s.Equal(callerType, md.Get(CallerTypeHeaderName)[0])
s.Equal(callOrigin, md.Get(CallOriginHeaderName)[0])
s.Len(md, 4)
}
func (s *callerInfoSuite) TestSetCallerInfo_NoExistingCallerInfo() {
callerName := CallerNameSystem
callerType := CallerTypeAPI
callOrigin := "methodName"
ctx := SetCallerInfo(context.Background(), CallerInfo{
CallerName: callerName,
CallerType: callerType,
CallOrigin: callOrigin,
})
md, ok := metadata.FromIncomingContext(ctx)
s.True(ok)
s.Equal(callerName, md.Get(CallerNameHeaderName)[0])
s.Equal(callerType, md.Get(CallerTypeHeaderName)[0])
s.Equal(callOrigin, md.Get(CallOriginHeaderName)[0])
s.Len(md, 3)
}
func (s *callerInfoSuite) TestSetCallerInfo_WithExistingCallerInfo() {
callerName := CallerNameSystem
callerType := CallerTypeBackground
callOrigin := "methodName"
ctx := SetCallerName(context.Background(), callerName)
ctx = SetCallerType(ctx, CallerTypeAPI)
ctx = SetOrigin(ctx, callOrigin)
ctx = SetCallerInfo(ctx, CallerInfo{
CallerName: "",
CallerType: callerType,
CallOrigin: "",
})
md, ok := metadata.FromIncomingContext(ctx)
s.True(ok)
s.Equal(callerName, md.Get(CallerNameHeaderName)[0])
s.Equal(callerType, md.Get(CallerTypeHeaderName)[0])
s.Equal(callOrigin, md.Get(CallOriginHeaderName)[0])
s.Len(md, 3)
}
func (s *callerInfoSuite) TestSetCallerInfo_WithPartialCallerInfo() {
callerName := CallerNameSystem
callerType := CallerTypeAPI
ctx := SetCallerType(context.Background(), callerType)
ctx = SetCallerInfo(ctx, CallerInfo{
CallerName: callerName,
})
md, ok := metadata.FromIncomingContext(ctx)
s.True(ok)
s.Equal(callerName, md.Get(CallerNameHeaderName)[0])
s.Equal(callerType, md.Get(CallerTypeHeaderName)[0])
s.Empty(md.Get(CallOriginHeaderName))
s.Len(md, 2)
}