github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/integration/ns_admin_integration_test.go (about)

     1  // +build integration
     2  
     3  // Copyright (c) 2019 Uber Technologies, Inc.
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  //
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  //
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  package integration
    24  
    25  import (
    26  	"testing"
    27  
    28  	"github.com/m3db/m3/src/cluster/integration/etcd"
    29  	"github.com/m3db/m3/src/cluster/kv"
    30  	"github.com/m3db/m3/src/dbnode/namespace"
    31  	"github.com/m3db/m3/src/dbnode/namespace/kvadmin"
    32  	"github.com/m3db/m3/src/x/ident"
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  const (
    37  	mainProtoStr = `syntax = "proto3";
    38  
    39  package mainpkg;
    40  
    41  import "mainpkg/imported.proto";
    42  
    43  message TestMessage {
    44    double latitude = 1;
    45    double longitude = 2;
    46    int64 epoch = 3;
    47    bytes deliveryID = 4;
    48    map<string, string> attributes = 5;
    49    ImportedMessage an_imported_message = 6;
    50  }
    51  `
    52  	importedProtoStr = `
    53  syntax = "proto3";
    54  
    55  package mainpkg;
    56  
    57  message ImportedMessage {
    58    double latitude = 1;
    59    double longitude = 2;
    60    int64 epoch = 3;
    61    bytes deliveryID = 4;
    62  }
    63  `
    64  )
    65  
    66  func deployNamespace(t *testing.T) (kv.Store, kvadmin.NamespaceMetadataAdminService, string, func()) {
    67  	opts := etcd.NewOptions()
    68  	t.Logf("etcd service: %v, env: %v, zone: %v", opts.ServiceID(), opts.Environment(), opts.Zone())
    69  	kv, err := etcd.New(opts)
    70  	t.Logf("etcd endpoints: %v", kv.Endpoints())
    71  	require.NoError(t, err)
    72  	// Must start the embedded server before closing.
    73  	require.NoError(t, kv.Start())
    74  	cleanup := func() {require.NoError(t, kv.Close())}
    75  
    76  	c, err := kv.ConfigServiceClient()
    77  	require.NoError(t, err)
    78  	kvStore, err := c.KV()
    79  	require.NoError(t, err)
    80  	require.NotNil(t, kvStore)
    81  	_, err = c.Services(nil)
    82  	require.NoError(t, err)
    83  
    84  	as := kvadmin.NewAdminService(kvStore, "", nil)
    85  
    86  	_, err = as.Get("ns1")
    87  	if err == kvadmin.ErrNamespaceNotFound {
    88  		optsProto, err := namespace.OptionsToProto(namespace.NewOptions())
    89  		require.NoError(t, err)
    90  		require.NoError(t, as.Add("ns1", optsProto))
    91  	}
    92  
    93  	protoFile := "mainpkg/test.proto"
    94  	protoMsg := "mainpkg.TestMessage"
    95  	protoMap := map[string]string{protoFile: mainProtoStr, "mainpkg/imported.proto": importedProtoStr}
    96  	deployID, err := as.DeploySchema("ns1", protoFile, protoMsg, protoMap)
    97  	require.NoError(t, err)
    98  
    99  	return kvStore, as, deployID, cleanup
   100  }
   101  
   102  func TestNamespaceAdmin_DeploySchemaToEtcd(t *testing.T) {
   103  	_, as, _, cleanup := deployNamespace(t)
   104  	defer cleanup()
   105  
   106  	protoFile := "mainpkg/test.proto"
   107  	protoMsg := "mainpkg.TestMessage"
   108  	protoMap := map[string]string{protoFile: mainProtoStr, "mainpkg/imported.proto": importedProtoStr}
   109  	invalidMsg := "TestMessage"
   110  	_, err := as.DeploySchema("ns1", protoFile, invalidMsg, protoMap)
   111  	require.Error(t, err)
   112  	invalidMap := map[string]string{protoFile: mainProtoStr}
   113  	_, err = as.DeploySchema("ns1", protoFile, protoMsg, invalidMap)
   114  	require.Error(t, err)
   115  }
   116  
   117  func TestNamespaceAdmin_LoadSchemaFromEtcd(t *testing.T) {
   118  	kvStore, _, deployID, cleanup := deployNamespace(t)
   119  	defer cleanup()
   120  
   121  	schemaReg := namespace.NewSchemaRegistry(true, nil)
   122  	err := kvadmin.LoadSchemaRegistryFromKVStore(schemaReg, kvStore)
   123  	require.NoError(t, err)
   124  
   125  	actualDesc, err := schemaReg.GetLatestSchema(ident.StringID("ns1"))
   126  	require.NoError(t, err)
   127  	require.EqualValues(t, deployID, actualDesc.DeployId())
   128  }
   129