diff_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2015 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 raft
  15. import (
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "os"
  20. "os/exec"
  21. "strings"
  22. )
  23. func diffu(a, b string) string {
  24. if a == b {
  25. return ""
  26. }
  27. aname, bname := mustTemp("base", a), mustTemp("other", b)
  28. defer os.Remove(aname)
  29. defer os.Remove(bname)
  30. cmd := exec.Command("diff", "-u", aname, bname)
  31. buf, err := cmd.CombinedOutput()
  32. if err != nil {
  33. if _, ok := err.(*exec.ExitError); ok {
  34. // do nothing
  35. return string(buf)
  36. }
  37. panic(err)
  38. }
  39. return string(buf)
  40. }
  41. func mustTemp(pre, body string) string {
  42. f, err := ioutil.TempFile("", pre)
  43. if err != nil {
  44. panic(err)
  45. }
  46. _, err = io.Copy(f, strings.NewReader(body))
  47. if err != nil {
  48. panic(err)
  49. }
  50. f.Close()
  51. return f.Name()
  52. }
  53. func ltoa(l *raftLog) string {
  54. s := fmt.Sprintf("committed: %d\n", l.committed)
  55. s += fmt.Sprintf("applied: %d\n", l.applied)
  56. for i, e := range l.allEntries() {
  57. s += fmt.Sprintf("#%d: %+v\n", i, e)
  58. }
  59. return s
  60. }