go.temporal.io/server@v1.23.0/common/namespace/namespace_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package namespace_test
    26  
    27  import (
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/stretchr/testify/assert"
    32  
    33  	"github.com/google/uuid"
    34  	"github.com/stretchr/testify/require"
    35  	namespacepb "go.temporal.io/api/namespace/v1"
    36  
    37  	persistencespb "go.temporal.io/server/api/persistence/v1"
    38  	"go.temporal.io/server/common/namespace"
    39  	persistence "go.temporal.io/server/common/persistence"
    40  	"go.temporal.io/server/common/primitives/timestamp"
    41  )
    42  
    43  func base(t *testing.T) *namespace.Namespace {
    44  	return namespace.FromPersistentState(&persistence.GetNamespaceResponse{
    45  		Namespace: &persistencespb.NamespaceDetail{
    46  			Info: &persistencespb.NamespaceInfo{
    47  				Id:   namespace.NewID().String(),
    48  				Name: t.Name(),
    49  				Data: make(map[string]string),
    50  			},
    51  			Config: &persistencespb.NamespaceConfig{
    52  				BadBinaries: &namespacepb.BadBinaries{
    53  					Binaries: make(map[string]*namespacepb.BadBinaryInfo),
    54  				},
    55  			},
    56  			ReplicationConfig: &persistencespb.NamespaceReplicationConfig{
    57  				ActiveClusterName: "foo",
    58  				Clusters:          []string{"foo", "bar"},
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestActiveInCluster(t *testing.T) {
    65  	base := base(t)
    66  
    67  	for _, tt := range [...]struct {
    68  		name        string
    69  		testCluster string
    70  		entry       *namespace.Namespace
    71  		want        bool
    72  	}{
    73  		{
    74  			name:        "global and cluster match",
    75  			testCluster: "foo",
    76  			entry: base.Clone(namespace.WithActiveCluster("foo"),
    77  				namespace.WithGlobalFlag(true)),
    78  			want: true,
    79  		},
    80  		{
    81  			name:        "global and cluster mismatch",
    82  			testCluster: "bar",
    83  			entry: base.Clone(namespace.WithActiveCluster("foo"),
    84  				namespace.WithGlobalFlag(true)),
    85  			want: false,
    86  		},
    87  		{
    88  			name:        "non-global and cluster mismatch",
    89  			testCluster: "bar",
    90  			entry: base.Clone(namespace.WithActiveCluster("foo"),
    91  				namespace.WithGlobalFlag(false)),
    92  			want: true,
    93  		},
    94  		{
    95  			name:        "non-global and cluster match",
    96  			testCluster: "foo",
    97  			entry: base.Clone(namespace.WithActiveCluster("foo"),
    98  				namespace.WithGlobalFlag(false)),
    99  			want: true,
   100  		},
   101  	} {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			require.Equal(t, tt.want, tt.entry.ActiveInCluster(tt.testCluster))
   104  		})
   105  	}
   106  }
   107  
   108  func Test_GetRetentionDays(t *testing.T) {
   109  	const defaultRetention = 7 * 24 * time.Hour
   110  	base := base(t).Clone(namespace.WithRetention(timestamp.DurationFromDays(7)))
   111  	for _, tt := range [...]struct {
   112  		name       string
   113  		retention  string
   114  		workflowID string
   115  		want       time.Duration
   116  	}{
   117  		{
   118  			name:       "30x0",
   119  			retention:  "30",
   120  			workflowID: uuid.NewString(),
   121  			want:       defaultRetention,
   122  		},
   123  		{
   124  			name:       "invalid retention",
   125  			retention:  "invalid-value",
   126  			workflowID: uuid.NewString(),
   127  			want:       defaultRetention,
   128  		},
   129  	} {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			ns := base.Clone()
   132  			require.Equal(t, tt.want, ns.Retention())
   133  		})
   134  	}
   135  }
   136  
   137  func TestNamespace_GetCustomData(t *testing.T) {
   138  	base := base(t)
   139  	ns := base.Clone(namespace.WithData("foo", "bar"))
   140  	data := ns.GetCustomData("foo")
   141  	assert.Equal(t, "bar", data)
   142  	data2 := ns.GetCustomData("fake")
   143  	assert.Equal(t, "", data2)
   144  }