github.com/mad-app/mattermost-server@v5.11.1+incompatible/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 9 "time" 10 11 "github.com/mattermost/mattermost-server/model" 12 "github.com/mattermost/mattermost-server/store" 13 ) 14 15 func TestClusterDiscoveryStore(t *testing.T, ss store.Store) { 16 t.Run("", func(t *testing.T) { testClusterDiscoveryStore(t, ss) }) 17 t.Run("Delete", func(t *testing.T) { testClusterDiscoveryStoreDelete(t, ss) }) 18 t.Run("LastPing", func(t *testing.T) { testClusterDiscoveryStoreLastPing(t, ss) }) 19 t.Run("Exists", func(t *testing.T) { testClusterDiscoveryStoreExists(t, ss) }) 20 t.Run("ClusterDiscoveryGetStore", func(t *testing.T) { testClusterDiscoveryGetStore(t, ss) }) 21 } 22 23 func testClusterDiscoveryStore(t *testing.T, ss store.Store) { 24 discovery := &model.ClusterDiscovery{ 25 ClusterName: "cluster_name", 26 Hostname: "hostname" + model.NewId(), 27 Type: "test_test", 28 } 29 30 if result := <-ss.ClusterDiscovery().Save(discovery); result.Err != nil { 31 t.Fatal(result.Err) 32 } 33 34 if result := <-ss.ClusterDiscovery().Cleanup(); result.Err != nil { 35 t.Fatal(result.Err) 36 } 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 if result := <-ss.ClusterDiscovery().Save(discovery); result.Err != nil { 47 t.Fatal(result.Err) 48 } 49 50 if result := <-ss.ClusterDiscovery().Delete(discovery); result.Err != nil { 51 t.Fatal(result.Err) 52 } 53 } 54 55 func testClusterDiscoveryStoreLastPing(t *testing.T, ss store.Store) { 56 discovery := &model.ClusterDiscovery{ 57 ClusterName: "cluster_name_lastPing", 58 Hostname: "hostname" + model.NewId(), 59 Type: "test_test_lastPing" + model.NewId(), 60 } 61 62 if result := <-ss.ClusterDiscovery().Save(discovery); result.Err != nil { 63 t.Fatal(result.Err) 64 } 65 66 if result := <-ss.ClusterDiscovery().SetLastPingAt(discovery); result.Err != nil { 67 t.Fatal(result.Err) 68 } 69 70 ttime := model.GetMillis() 71 72 time.Sleep(1 * time.Second) 73 74 if result := <-ss.ClusterDiscovery().SetLastPingAt(discovery); result.Err != nil { 75 t.Fatal(result.Err) 76 } 77 78 if result := <-ss.ClusterDiscovery().GetAll(discovery.Type, "cluster_name_lastPing"); result.Err != nil { 79 t.Fatal(result.Err) 80 } else { 81 list := result.Data.([]*model.ClusterDiscovery) 82 83 if len(list) != 1 { 84 t.Fatal("should only be 1 items") 85 return 86 } 87 88 if list[0].LastPingAt-ttime < 500 { 89 t.Fatal("failed to set time") 90 } 91 } 92 93 discovery2 := &model.ClusterDiscovery{ 94 ClusterName: "cluster_name_missing", 95 Hostname: "hostname" + model.NewId(), 96 Type: "test_test_missing", 97 } 98 99 if result := <-ss.ClusterDiscovery().SetLastPingAt(discovery2); result.Err != nil { 100 t.Fatal(result.Err) 101 } 102 } 103 104 func testClusterDiscoveryStoreExists(t *testing.T, ss store.Store) { 105 discovery := &model.ClusterDiscovery{ 106 ClusterName: "cluster_name_Exists", 107 Hostname: "hostname" + model.NewId(), 108 Type: "test_test_Exists" + model.NewId(), 109 } 110 111 if result := <-ss.ClusterDiscovery().Save(discovery); result.Err != nil { 112 t.Fatal(result.Err) 113 } 114 115 if result := <-ss.ClusterDiscovery().Exists(discovery); result.Err != nil { 116 t.Fatal(result.Err) 117 } else { 118 val := result.Data.(bool) 119 if !val { 120 t.Fatal("should be true") 121 } 122 } 123 124 discovery.ClusterName = "cluster_name_Exists2" 125 126 if result := <-ss.ClusterDiscovery().Exists(discovery); result.Err != nil { 127 t.Fatal(result.Err) 128 } else { 129 val := result.Data.(bool) 130 if val { 131 t.Fatal("should be true") 132 } 133 } 134 } 135 136 func testClusterDiscoveryGetStore(t *testing.T, ss store.Store) { 137 testType1 := model.NewId() 138 139 discovery1 := &model.ClusterDiscovery{ 140 ClusterName: "cluster_name", 141 Hostname: "hostname1", 142 Type: testType1, 143 } 144 store.Must(ss.ClusterDiscovery().Save(discovery1)) 145 146 discovery2 := &model.ClusterDiscovery{ 147 ClusterName: "cluster_name", 148 Hostname: "hostname2", 149 Type: testType1, 150 } 151 store.Must(ss.ClusterDiscovery().Save(discovery2)) 152 153 discovery3 := &model.ClusterDiscovery{ 154 ClusterName: "cluster_name", 155 Hostname: "hostname3", 156 Type: testType1, 157 CreateAt: 1, 158 LastPingAt: 1, 159 } 160 store.Must(ss.ClusterDiscovery().Save(discovery3)) 161 162 testType2 := model.NewId() 163 164 discovery4 := &model.ClusterDiscovery{ 165 ClusterName: "cluster_name", 166 Hostname: "hostname1", 167 Type: testType2, 168 } 169 store.Must(ss.ClusterDiscovery().Save(discovery4)) 170 171 if result := <-ss.ClusterDiscovery().GetAll(testType1, "cluster_name"); result.Err != nil { 172 t.Fatal(result.Err) 173 } else { 174 list := result.Data.([]*model.ClusterDiscovery) 175 176 if len(list) != 2 { 177 t.Fatal("Should only have returned 2") 178 } 179 } 180 181 if result := <-ss.ClusterDiscovery().GetAll(testType2, "cluster_name"); result.Err != nil { 182 t.Fatal(result.Err) 183 } else { 184 list := result.Data.([]*model.ClusterDiscovery) 185 186 if len(list) != 1 { 187 t.Fatal("Should only have returned 1") 188 } 189 } 190 191 if result := <-ss.ClusterDiscovery().GetAll(model.NewId(), "cluster_name"); result.Err != nil { 192 t.Fatal(result.Err) 193 } else { 194 list := result.Data.([]*model.ClusterDiscovery) 195 196 if len(list) != 0 { 197 t.Fatal("shouldn't be any") 198 } 199 } 200 }