github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/cluster_discovery_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/mattermost/mattermost-server/v5/model"
    14  	"github.com/mattermost/mattermost-server/v5/store"
    15  )
    16  
    17  func TestClusterDiscoveryStore(t *testing.T, ss store.Store) {
    18  	t.Run("", func(t *testing.T) { testClusterDiscoveryStore(t, ss) })
    19  	t.Run("Delete", func(t *testing.T) { testClusterDiscoveryStoreDelete(t, ss) })
    20  	t.Run("LastPing", func(t *testing.T) { testClusterDiscoveryStoreLastPing(t, ss) })
    21  	t.Run("Exists", func(t *testing.T) { testClusterDiscoveryStoreExists(t, ss) })
    22  	t.Run("ClusterDiscoveryGetStore", func(t *testing.T) { testClusterDiscoveryGetStore(t, ss) })
    23  }
    24  
    25  func testClusterDiscoveryStore(t *testing.T, ss store.Store) {
    26  	discovery := &model.ClusterDiscovery{
    27  		ClusterName: "cluster_name",
    28  		Hostname:    "hostname" + model.NewId(),
    29  		Type:        "test_test",
    30  	}
    31  
    32  	err := ss.ClusterDiscovery().Save(discovery)
    33  	require.NoError(t, err)
    34  
    35  	err = ss.ClusterDiscovery().Cleanup()
    36  	require.NoError(t, err)
    37  }
    38  
    39  func testClusterDiscoveryStoreDelete(t *testing.T, ss store.Store) {
    40  	discovery := &model.ClusterDiscovery{
    41  		ClusterName: "cluster_name",
    42  		Hostname:    "hostname" + model.NewId(),
    43  		Type:        "test_test",
    44  	}
    45  
    46  	err := ss.ClusterDiscovery().Save(discovery)
    47  	require.NoError(t, err)
    48  
    49  	_, err = ss.ClusterDiscovery().Delete(discovery)
    50  	require.NoError(t, err)
    51  }
    52  
    53  func testClusterDiscoveryStoreLastPing(t *testing.T, ss store.Store) {
    54  	discovery := &model.ClusterDiscovery{
    55  		ClusterName: "cluster_name_lastPing",
    56  		Hostname:    "hostname" + model.NewId(),
    57  		Type:        "test_test_lastPing" + model.NewId(),
    58  	}
    59  
    60  	err := ss.ClusterDiscovery().Save(discovery)
    61  	require.NoError(t, err)
    62  
    63  	err = ss.ClusterDiscovery().SetLastPingAt(discovery)
    64  	require.NoError(t, err)
    65  
    66  	ttime := model.GetMillis()
    67  
    68  	time.Sleep(1 * time.Second)
    69  
    70  	err = ss.ClusterDiscovery().SetLastPingAt(discovery)
    71  	require.NoError(t, err)
    72  
    73  	list, err := ss.ClusterDiscovery().GetAll(discovery.Type, "cluster_name_lastPing")
    74  	require.NoError(t, err)
    75  	assert.Len(t, list, 1)
    76  
    77  	require.Less(t, int64(500), list[0].LastPingAt-ttime)
    78  
    79  	discovery2 := &model.ClusterDiscovery{
    80  		ClusterName: "cluster_name_missing",
    81  		Hostname:    "hostname" + model.NewId(),
    82  		Type:        "test_test_missing",
    83  	}
    84  
    85  	err = ss.ClusterDiscovery().SetLastPingAt(discovery2)
    86  	require.NoError(t, err)
    87  }
    88  
    89  func testClusterDiscoveryStoreExists(t *testing.T, ss store.Store) {
    90  	discovery := &model.ClusterDiscovery{
    91  		ClusterName: "cluster_name_Exists",
    92  		Hostname:    "hostname" + model.NewId(),
    93  		Type:        "test_test_Exists" + model.NewId(),
    94  	}
    95  
    96  	err := ss.ClusterDiscovery().Save(discovery)
    97  	require.NoError(t, err)
    98  
    99  	val, err := ss.ClusterDiscovery().Exists(discovery)
   100  	require.NoError(t, err)
   101  	assert.True(t, val)
   102  
   103  	discovery.ClusterName = "cluster_name_Exists2"
   104  
   105  	val, err = ss.ClusterDiscovery().Exists(discovery)
   106  	require.NoError(t, err)
   107  	assert.False(t, val)
   108  }
   109  
   110  func testClusterDiscoveryGetStore(t *testing.T, ss store.Store) {
   111  	testType1 := model.NewId()
   112  
   113  	discovery1 := &model.ClusterDiscovery{
   114  		ClusterName: "cluster_name",
   115  		Hostname:    "hostname1",
   116  		Type:        testType1,
   117  	}
   118  	require.NoError(t, ss.ClusterDiscovery().Save(discovery1))
   119  
   120  	discovery2 := &model.ClusterDiscovery{
   121  		ClusterName: "cluster_name",
   122  		Hostname:    "hostname2",
   123  		Type:        testType1,
   124  	}
   125  	require.NoError(t, ss.ClusterDiscovery().Save(discovery2))
   126  
   127  	discovery3 := &model.ClusterDiscovery{
   128  		ClusterName: "cluster_name",
   129  		Hostname:    "hostname3",
   130  		Type:        testType1,
   131  		CreateAt:    1,
   132  		LastPingAt:  1,
   133  	}
   134  	require.NoError(t, ss.ClusterDiscovery().Save(discovery3))
   135  
   136  	testType2 := model.NewId()
   137  
   138  	discovery4 := &model.ClusterDiscovery{
   139  		ClusterName: "cluster_name",
   140  		Hostname:    "hostname1",
   141  		Type:        testType2,
   142  	}
   143  	require.NoError(t, ss.ClusterDiscovery().Save(discovery4))
   144  
   145  	list, err := ss.ClusterDiscovery().GetAll(testType1, "cluster_name")
   146  	require.NoError(t, err)
   147  	assert.Len(t, list, 2)
   148  
   149  	list, err = ss.ClusterDiscovery().GetAll(testType2, "cluster_name")
   150  	require.NoError(t, err)
   151  	assert.Len(t, list, 1)
   152  
   153  	list, err = ss.ClusterDiscovery().GetAll(model.NewId(), "cluster_name")
   154  	require.NoError(t, err)
   155  	assert.Empty(t, list)
   156  }