go.temporal.io/server@v1.23.0/common/namespace/attr_validator_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
    26  
    27  import (
    28  	"testing"
    29  
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/suite"
    32  	"go.temporal.io/api/serviceerror"
    33  
    34  	persistencespb "go.temporal.io/server/api/persistence/v1"
    35  	"go.temporal.io/server/common/cluster"
    36  )
    37  
    38  type (
    39  	attrValidatorSuite struct {
    40  		suite.Suite
    41  
    42  		controller          *gomock.Controller
    43  		mockClusterMetadata *cluster.MockMetadata
    44  
    45  		validator *AttrValidatorImpl
    46  	}
    47  )
    48  
    49  func TestAttrValidatorSuite(t *testing.T) {
    50  	s := new(attrValidatorSuite)
    51  	suite.Run(t, s)
    52  }
    53  
    54  func (s *attrValidatorSuite) SetupSuite() {
    55  }
    56  
    57  func (s *attrValidatorSuite) TearDownSuite() {
    58  }
    59  
    60  func (s *attrValidatorSuite) SetupTest() {
    61  	s.controller = gomock.NewController(s.T())
    62  	s.mockClusterMetadata = cluster.NewMockMetadata(s.controller)
    63  
    64  	s.validator = NewAttrValidator(s.mockClusterMetadata)
    65  }
    66  
    67  func (s *attrValidatorSuite) TearDownTest() {
    68  	s.controller.Finish()
    69  }
    70  
    71  func (s *attrValidatorSuite) TestClusterName() {
    72  	s.mockClusterMetadata.EXPECT().GetAllClusterInfo().Return(
    73  		cluster.TestAllClusterInfo,
    74  	).AnyTimes()
    75  
    76  	err := s.validator.validateClusterName("some random foo bar")
    77  	s.IsType(&serviceerror.InvalidArgument{}, err)
    78  
    79  	err = s.validator.validateClusterName(cluster.TestCurrentClusterName)
    80  	s.NoError(err)
    81  
    82  	err = s.validator.validateClusterName(cluster.TestAlternativeClusterName)
    83  	s.NoError(err)
    84  }
    85  
    86  func (s *attrValidatorSuite) TestValidateNamespaceReplicationConfigForLocalNamespace() {
    87  	s.mockClusterMetadata.EXPECT().GetCurrentClusterName().Return(
    88  		cluster.TestCurrentClusterName,
    89  	).AnyTimes()
    90  	s.mockClusterMetadata.EXPECT().GetAllClusterInfo().Return(
    91  		cluster.TestAllClusterInfo,
    92  	).AnyTimes()
    93  
    94  	err := s.validator.ValidateNamespaceReplicationConfigForLocalNamespace(
    95  		&persistencespb.NamespaceReplicationConfig{
    96  			ActiveClusterName: cluster.TestAlternativeClusterName,
    97  			Clusters: []string{
    98  				cluster.TestAlternativeClusterName,
    99  			},
   100  		},
   101  	)
   102  	s.IsType(&serviceerror.InvalidArgument{}, err)
   103  
   104  	err = s.validator.ValidateNamespaceReplicationConfigForLocalNamespace(
   105  		&persistencespb.NamespaceReplicationConfig{
   106  			ActiveClusterName: cluster.TestAlternativeClusterName,
   107  			Clusters: []string{
   108  				cluster.TestCurrentClusterName,
   109  				cluster.TestAlternativeClusterName,
   110  			},
   111  		},
   112  	)
   113  	s.IsType(&serviceerror.InvalidArgument{}, err)
   114  
   115  	err = s.validator.ValidateNamespaceReplicationConfigForLocalNamespace(
   116  		&persistencespb.NamespaceReplicationConfig{
   117  			ActiveClusterName: cluster.TestCurrentClusterName,
   118  			Clusters: []string{
   119  				cluster.TestCurrentClusterName,
   120  				cluster.TestAlternativeClusterName,
   121  			},
   122  		},
   123  	)
   124  	s.IsType(&serviceerror.InvalidArgument{}, err)
   125  
   126  	err = s.validator.ValidateNamespaceReplicationConfigForLocalNamespace(
   127  		&persistencespb.NamespaceReplicationConfig{
   128  			ActiveClusterName: cluster.TestCurrentClusterName,
   129  			Clusters: []string{
   130  				cluster.TestCurrentClusterName,
   131  			},
   132  		},
   133  	)
   134  	s.NoError(err)
   135  }
   136  
   137  func (s *attrValidatorSuite) TestValidateNamespaceReplicationConfigForGlobalNamespace() {
   138  	s.mockClusterMetadata.EXPECT().GetCurrentClusterName().Return(
   139  		cluster.TestCurrentClusterName,
   140  	).AnyTimes()
   141  	s.mockClusterMetadata.EXPECT().GetAllClusterInfo().Return(
   142  		cluster.TestAllClusterInfo,
   143  	).AnyTimes()
   144  
   145  	err := s.validator.ValidateNamespaceReplicationConfigForGlobalNamespace(
   146  		&persistencespb.NamespaceReplicationConfig{
   147  			ActiveClusterName: cluster.TestCurrentClusterName,
   148  			Clusters: []string{
   149  				cluster.TestCurrentClusterName,
   150  			},
   151  		},
   152  	)
   153  	s.NoError(err)
   154  
   155  	err = s.validator.ValidateNamespaceReplicationConfigForGlobalNamespace(
   156  		&persistencespb.NamespaceReplicationConfig{
   157  			ActiveClusterName: cluster.TestAlternativeClusterName,
   158  			Clusters: []string{
   159  				cluster.TestAlternativeClusterName,
   160  			},
   161  		},
   162  	)
   163  	s.NoError(err)
   164  
   165  	err = s.validator.ValidateNamespaceReplicationConfigForGlobalNamespace(
   166  		&persistencespb.NamespaceReplicationConfig{
   167  			ActiveClusterName: cluster.TestAlternativeClusterName,
   168  			Clusters: []string{
   169  				cluster.TestCurrentClusterName,
   170  				cluster.TestAlternativeClusterName,
   171  			},
   172  		},
   173  	)
   174  	s.NoError(err)
   175  
   176  	err = s.validator.ValidateNamespaceReplicationConfigForGlobalNamespace(
   177  		&persistencespb.NamespaceReplicationConfig{
   178  			ActiveClusterName: cluster.TestCurrentClusterName,
   179  			Clusters: []string{
   180  				cluster.TestCurrentClusterName,
   181  				cluster.TestAlternativeClusterName,
   182  			},
   183  		},
   184  	)
   185  	s.NoError(err)
   186  }