vitess.io/vitess@v0.16.2/go/vt/mysqlctl/tmutils/permissions_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tmutils 18 19 import ( 20 "testing" 21 22 "vitess.io/vitess/go/sqltypes" 23 querypb "vitess.io/vitess/go/vt/proto/query" 24 25 tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata" 26 ) 27 28 func mapToSQLResults(row map[string]string) ([]*querypb.Field, []sqltypes.Value) { 29 fields := make([]*querypb.Field, len(row)) 30 values := make([]sqltypes.Value, len(row)) 31 index := 0 32 for key, value := range row { 33 fields[index] = &querypb.Field{Name: key} 34 values[index] = sqltypes.NewVarBinary(value) 35 index++ 36 } 37 return fields, values 38 } 39 40 func testPermissionsDiff(t *testing.T, left, right *tabletmanagerdatapb.Permissions, leftName, rightName string, expected []string) { 41 t.Helper() 42 43 actual := DiffPermissionsToArray(leftName, left, rightName, right) 44 45 equal := false 46 if len(actual) == len(expected) { 47 equal = true 48 for i, val := range actual { 49 if val != expected[i] { 50 equal = false 51 break 52 } 53 } 54 } 55 56 if !equal { 57 t.Logf("Expected: %v", expected) 58 t.Logf("Actual : %v", actual) 59 t.Fail() 60 } 61 } 62 63 func TestPermissionsDiff(t *testing.T) { 64 65 p1 := &tabletmanagerdatapb.Permissions{} 66 p1.UserPermissions = append(p1.UserPermissions, NewUserPermission(mapToSQLResults(map[string]string{ 67 "Host": "%", 68 "User": "vt", 69 "Password": "p1", 70 "Select_priv": "Y", 71 "Insert_priv": "N", 72 // Test the next field is skipped (to avoid date drifts). 73 "password_last_changed": "2016-11-08 02:56:23", 74 }))) 75 p1.DbPermissions = append(p1.DbPermissions, NewDbPermission(mapToSQLResults(map[string]string{ 76 "Host": "%", 77 "Db": "vt_live", 78 "User": "vt", 79 "Select_priv": "N", 80 "Insert_priv": "Y", 81 }))) 82 83 if PermissionsString(p1) != 84 "User Permissions:\n"+ 85 " %:vt: UserPermission PasswordChecksum(4831957779889520640) Insert_priv(N) Select_priv(Y)\n"+ 86 "Db Permissions:\n"+ 87 " %:vt_live:vt: DbPermission Insert_priv(Y) Select_priv(N)\n" { 88 t.Logf("Actual: %v", p1.String()) 89 t.Fail() 90 } 91 92 testPermissionsDiff(t, p1, p1, "p1-1", "p1-2", []string{}) 93 94 p2 := &tabletmanagerdatapb.Permissions{} 95 testPermissionsDiff(t, p1, p2, "p1", "p2", []string{ 96 "p1 has an extra user %:vt", 97 "p1 has an extra db %:vt_live:vt", 98 }) 99 100 p2.DbPermissions = p1.DbPermissions 101 p1.DbPermissions = nil 102 testPermissionsDiff(t, p1, p2, "p1", "p2", []string{ 103 "p1 has an extra user %:vt", 104 "p2 has an extra db %:vt_live:vt", 105 }) 106 107 p2.UserPermissions = append(p2.UserPermissions, NewUserPermission(mapToSQLResults(map[string]string{ 108 "Host": "%", 109 "User": "vt", 110 "Password": "p1", 111 "Select_priv": "Y", 112 "Insert_priv": "Y", 113 }))) 114 p1.DbPermissions = append(p1.DbPermissions, NewDbPermission(mapToSQLResults(map[string]string{ 115 "Host": "%", 116 "Db": "vt_live", 117 "User": "vt", 118 "Select_priv": "Y", 119 "Insert_priv": "N", 120 }))) 121 testPermissionsDiff(t, p1, p2, "p1", "p2", []string{ 122 "permissions differ on user %:vt:\n" + 123 "p1: UserPermission PasswordChecksum(4831957779889520640) Insert_priv(N) Select_priv(Y)\n" + 124 " differs from:\n" + 125 "p2: UserPermission PasswordChecksum(4831957779889520640) Insert_priv(Y) Select_priv(Y)", 126 "permissions differ on db %:vt_live:vt:\n" + 127 "p1: DbPermission Insert_priv(N) Select_priv(Y)\n" + 128 " differs from:\n" + 129 "p2: DbPermission Insert_priv(Y) Select_priv(N)", 130 }) 131 132 p2.UserPermissions[0].Privileges["Insert_priv"] = "N" 133 p2.DbPermissions[0].Privileges["Insert_priv"] = "N" 134 p2.DbPermissions[0].Privileges["Select_priv"] = "Y" 135 testPermissionsDiff(t, p1, p2, "p1", "p2", []string{}) 136 }