etcd-dump-log_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "io/ioutil"
  18. "os"
  19. "os/exec"
  20. "path"
  21. "path/filepath"
  22. "strings"
  23. "testing"
  24. "github.com/coreos/etcd/auth/authpb"
  25. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  26. "github.com/coreos/etcd/pkg/fileutil"
  27. "github.com/coreos/etcd/pkg/pbutil"
  28. "github.com/coreos/etcd/raft/raftpb"
  29. "github.com/coreos/etcd/wal"
  30. "go.uber.org/zap"
  31. )
  32. func TestEtcdDumpLogEntryType(t *testing.T) {
  33. // directory where the command is
  34. binDir, err := os.Getwd()
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. dumpLogsBinary := path.Join(binDir + "/etcd-dump-logs")
  39. if !fileutil.Exist(dumpLogsBinary) {
  40. t.Skipf("%q does not exist", dumpLogsBinary)
  41. }
  42. p, err := ioutil.TempDir(os.TempDir(), "etcddumplogstest")
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. defer os.RemoveAll(p)
  47. memberdir := filepath.Join(p, "member")
  48. err = os.Mkdir(memberdir, 0744)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. waldir := walDir(p)
  53. snapdir := snapDir(p)
  54. w, err := wal.Create(zap.NewExample(), waldir, nil)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. err = os.Mkdir(snapdir, 0744)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. ents := make([]raftpb.Entry, 0)
  63. // append entries into wal log
  64. appendConfigChangeEnts(&ents)
  65. appendNormalRequestEnts(&ents)
  66. appendNormalIRREnts(&ents)
  67. appendUnknownNormalEnts(&ents)
  68. // force commit newly appended entries
  69. err = w.Save(raftpb.HardState{}, ents)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. w.Close()
  74. argtests := []struct {
  75. name string
  76. args []string
  77. fileExpected string
  78. }{
  79. {"no entry-type", []string{p}, "expectedoutput/listAll.output"},
  80. {"confchange entry-type", []string{"-entry-type", "ConfigChange", p}, "expectedoutput/listConfigChange.output"},
  81. {"normal entry-type", []string{"-entry-type", "Normal", p}, "expectedoutput/listNormal.output"},
  82. {"request entry-type", []string{"-entry-type", "Request", p}, "expectedoutput/listRequest.output"},
  83. {"internalRaftRequest entry-type", []string{"-entry-type", "InternalRaftRequest", p}, "expectedoutput/listInternalRaftRequest.output"},
  84. {"range entry-type", []string{"-entry-type", "IRRRange", p}, "expectedoutput/listIRRRange.output"},
  85. {"put entry-type", []string{"-entry-type", "IRRPut", p}, "expectedoutput/listIRRPut.output"},
  86. {"del entry-type", []string{"-entry-type", "IRRDeleteRange", p}, "expectedoutput/listIRRDeleteRange.output"},
  87. {"txn entry-type", []string{"-entry-type", "IRRTxn", p}, "expectedoutput/listIRRTxn.output"},
  88. {"compaction entry-type", []string{"-entry-type", "IRRCompaction", p}, "expectedoutput/listIRRCompaction.output"},
  89. {"lease grant entry-type", []string{"-entry-type", "IRRLeaseGrant", p}, "expectedoutput/listIRRLeaseGrant.output"},
  90. {"lease revoke entry-type", []string{"-entry-type", "IRRLeaseRevoke", p}, "expectedoutput/listIRRLeaseRevoke.output"},
  91. {"confchange and txn entry-type", []string{"-entry-type", "ConfigChange,IRRCompaction", p}, "expectedoutput/listConfigChangeIRRCompaction.output"},
  92. }
  93. for _, argtest := range argtests {
  94. t.Run(argtest.name, func(t *testing.T) {
  95. cmd := exec.Command(dumpLogsBinary, argtest.args...)
  96. actual, err := cmd.CombinedOutput()
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. expected, err := ioutil.ReadFile(path.Join(binDir, argtest.fileExpected))
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. if !bytes.Equal(actual, expected) {
  105. t.Errorf(`Got input of length %d, wanted input of length %d
  106. ==== BEGIN RECEIVED FILE ====
  107. %s
  108. ==== END RECEIVED FILE ====
  109. ==== BEGIN EXPECTED FILE ====
  110. %s
  111. ==== END EXPECTED FILE ====
  112. `, len(actual), len(expected), actual, expected)
  113. }
  114. })
  115. }
  116. }
  117. func appendConfigChangeEnts(ents *[]raftpb.Entry) {
  118. configChangeData := []raftpb.ConfChange{
  119. {ID: 1, Type: raftpb.ConfChangeAddNode, NodeID: 2, Context: []byte("")},
  120. {ID: 2, Type: raftpb.ConfChangeRemoveNode, NodeID: 2, Context: []byte("")},
  121. {ID: 3, Type: raftpb.ConfChangeUpdateNode, NodeID: 2, Context: []byte("")},
  122. {ID: 4, Type: raftpb.ConfChangeAddLearnerNode, NodeID: 3, Context: []byte("")},
  123. }
  124. configChangeEntries := []raftpb.Entry{
  125. {Term: 1, Index: 1, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(&configChangeData[0])},
  126. {Term: 2, Index: 2, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(&configChangeData[1])},
  127. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(&configChangeData[2])},
  128. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(&configChangeData[3])},
  129. }
  130. *ents = append(*ents, configChangeEntries...)
  131. }
  132. func appendNormalRequestEnts(ents *[]raftpb.Entry) {
  133. a := true
  134. b := false
  135. requests := []etcdserverpb.Request{
  136. {ID: 0, Method: "", Path: "/path0", Val: "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", Dir: true, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 9, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
  137. {ID: 1, Method: "QGET", Path: "/path1", Val: "{\"0\":\"1\",\"2\":[\"3\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 9, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
  138. {ID: 2, Method: "SYNC", Path: "/path2", Val: "{\"0\":\"1\",\"2\":[\"3\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 2, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
  139. {ID: 3, Method: "DELETE", Path: "/path3", Val: "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &a, Expiration: 2, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
  140. {ID: 4, Method: "RANDOM", Path: "/path4/superlong" + strings.Repeat("/path", 30), Val: "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", Dir: false, PrevValue: "", PrevIndex: 0, PrevExist: &b, Expiration: 2, Wait: false, Since: 1, Recursive: false, Sorted: false, Quorum: false, Time: 1, Stream: false, Refresh: &b},
  141. }
  142. for i, request := range requests {
  143. var currentry raftpb.Entry
  144. currentry.Term = 3
  145. currentry.Index = uint64(i + 5)
  146. currentry.Type = raftpb.EntryNormal
  147. currentry.Data = pbutil.MustMarshal(&request)
  148. *ents = append(*ents, currentry)
  149. }
  150. }
  151. func appendNormalIRREnts(ents *[]raftpb.Entry) {
  152. irrrange := &etcdserverpb.RangeRequest{Key: []byte("1"), RangeEnd: []byte("hi"), Limit: 6, Revision: 1, SortOrder: 1, SortTarget: 0, Serializable: false, KeysOnly: false, CountOnly: false, MinModRevision: 0, MaxModRevision: 20000, MinCreateRevision: 0, MaxCreateRevision: 20000}
  153. irrput := &etcdserverpb.PutRequest{Key: []byte("foo1"), Value: []byte("bar1"), Lease: 1, PrevKv: false, IgnoreValue: false, IgnoreLease: true}
  154. irrdeleterange := &etcdserverpb.DeleteRangeRequest{Key: []byte("0"), RangeEnd: []byte("9"), PrevKv: true}
  155. delInRangeReq := &etcdserverpb.RequestOp{Request: &etcdserverpb.RequestOp_RequestDeleteRange{
  156. RequestDeleteRange: &etcdserverpb.DeleteRangeRequest{
  157. Key: []byte("a"), RangeEnd: []byte("b"),
  158. },
  159. },
  160. }
  161. irrtxn := &etcdserverpb.TxnRequest{Success: []*etcdserverpb.RequestOp{delInRangeReq}, Failure: []*etcdserverpb.RequestOp{delInRangeReq}}
  162. irrcompaction := &etcdserverpb.CompactionRequest{Revision: 0, Physical: true}
  163. irrleasegrant := &etcdserverpb.LeaseGrantRequest{TTL: 1, ID: 1}
  164. irrleaserevoke := &etcdserverpb.LeaseRevokeRequest{ID: 2}
  165. irralarm := &etcdserverpb.AlarmRequest{Action: 3, MemberID: 4, Alarm: 5}
  166. irrauthenable := &etcdserverpb.AuthEnableRequest{}
  167. irrauthdisable := &etcdserverpb.AuthDisableRequest{}
  168. irrauthenticate := &etcdserverpb.InternalAuthenticateRequest{Name: "myname", Password: "password", SimpleToken: "token"}
  169. irrauthuseradd := &etcdserverpb.AuthUserAddRequest{Name: "name1", Password: "pass1"}
  170. irrauthuserdelete := &etcdserverpb.AuthUserDeleteRequest{Name: "name1"}
  171. irrauthuserget := &etcdserverpb.AuthUserGetRequest{Name: "name1"}
  172. irrauthuserchangepassword := &etcdserverpb.AuthUserChangePasswordRequest{Name: "name1", Password: "pass2"}
  173. irrauthusergrantrole := &etcdserverpb.AuthUserGrantRoleRequest{User: "user1", Role: "role1"}
  174. irrauthuserrevokerole := &etcdserverpb.AuthUserRevokeRoleRequest{Name: "user2", Role: "role2"}
  175. irrauthuserlist := &etcdserverpb.AuthUserListRequest{}
  176. irrauthrolelist := &etcdserverpb.AuthRoleListRequest{}
  177. irrauthroleadd := &etcdserverpb.AuthRoleAddRequest{Name: "role2"}
  178. irrauthroledelete := &etcdserverpb.AuthRoleDeleteRequest{Role: "role1"}
  179. irrauthroleget := &etcdserverpb.AuthRoleGetRequest{Role: "role3"}
  180. perm := &authpb.Permission{
  181. PermType: authpb.WRITE,
  182. Key: []byte("Keys"),
  183. RangeEnd: []byte("RangeEnd"),
  184. }
  185. irrauthrolegrantpermission := &etcdserverpb.AuthRoleGrantPermissionRequest{Name: "role3", Perm: perm}
  186. irrauthrolerevokepermission := &etcdserverpb.AuthRoleRevokePermissionRequest{Role: "role3", Key: []byte("key"), RangeEnd: []byte("rangeend")}
  187. irrs := []etcdserverpb.InternalRaftRequest{
  188. {ID: 5, Range: irrrange},
  189. {ID: 6, Put: irrput},
  190. {ID: 7, DeleteRange: irrdeleterange},
  191. {ID: 8, Txn: irrtxn},
  192. {ID: 9, Compaction: irrcompaction},
  193. {ID: 10, LeaseGrant: irrleasegrant},
  194. {ID: 11, LeaseRevoke: irrleaserevoke},
  195. {ID: 12, Alarm: irralarm},
  196. {ID: 13, AuthEnable: irrauthenable},
  197. {ID: 14, AuthDisable: irrauthdisable},
  198. {ID: 15, Authenticate: irrauthenticate},
  199. {ID: 16, AuthUserAdd: irrauthuseradd},
  200. {ID: 17, AuthUserDelete: irrauthuserdelete},
  201. {ID: 18, AuthUserGet: irrauthuserget},
  202. {ID: 19, AuthUserChangePassword: irrauthuserchangepassword},
  203. {ID: 20, AuthUserGrantRole: irrauthusergrantrole},
  204. {ID: 21, AuthUserRevokeRole: irrauthuserrevokerole},
  205. {ID: 22, AuthUserList: irrauthuserlist},
  206. {ID: 23, AuthRoleList: irrauthrolelist},
  207. {ID: 24, AuthRoleAdd: irrauthroleadd},
  208. {ID: 25, AuthRoleDelete: irrauthroledelete},
  209. {ID: 26, AuthRoleGet: irrauthroleget},
  210. {ID: 27, AuthRoleGrantPermission: irrauthrolegrantpermission},
  211. {ID: 28, AuthRoleRevokePermission: irrauthrolerevokepermission},
  212. }
  213. for i, irr := range irrs {
  214. var currentry raftpb.Entry
  215. currentry.Term = uint64(i + 4)
  216. currentry.Index = uint64(i + 10)
  217. currentry.Type = raftpb.EntryNormal
  218. currentry.Data = pbutil.MustMarshal(&irr)
  219. *ents = append(*ents, currentry)
  220. }
  221. }
  222. func appendUnknownNormalEnts(ents *[]raftpb.Entry) {
  223. var currentry raftpb.Entry
  224. currentry.Term = 27
  225. currentry.Index = 34
  226. currentry.Type = raftpb.EntryNormal
  227. currentry.Data = []byte("?")
  228. *ents = append(*ents, currentry)
  229. }