ctl_v3_lock_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2016 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 e2e
  15. import (
  16. "os"
  17. "runtime"
  18. "strings"
  19. "syscall"
  20. "testing"
  21. "time"
  22. "github.com/coreos/etcd/pkg/expect"
  23. )
  24. // debugLockSignal forces SIGQUIT to debug etcdctl elect and lock failures
  25. var debugLockSignal os.Signal
  26. func init() {
  27. // hacks to ignore SIGQUIT debugging for some builds
  28. switch {
  29. case os.Getenv("COVERDIR") != "":
  30. // SIGQUIT interferes with coverage collection
  31. debugLockSignal = syscall.SIGTERM
  32. case runtime.GOARCH == "ppc64le":
  33. // ppc64le's signal handling won't kill processes with SIGQUIT
  34. // in the same way as amd64/i386, so processes won't terminate
  35. // as expected. Since this debugging code for CI, just ignore
  36. // ppc64le.
  37. debugLockSignal = syscall.SIGKILL
  38. default:
  39. // stack dumping OK
  40. debugLockSignal = syscall.SIGQUIT
  41. }
  42. }
  43. func TestCtlV3Lock(t *testing.T) {
  44. oldenv := os.Getenv("EXPECT_DEBUG")
  45. defer os.Setenv("EXPECT_DEBUG", oldenv)
  46. os.Setenv("EXPECT_DEBUG", "1")
  47. testCtl(t, testLock)
  48. }
  49. func testLock(cx ctlCtx) {
  50. // debugging for #6464
  51. sig := cx.epc.withStopSignal(debugLockSignal)
  52. defer cx.epc.withStopSignal(sig)
  53. name := "a"
  54. holder, ch, err := ctlV3Lock(cx, name)
  55. if err != nil {
  56. cx.t.Fatal(err)
  57. }
  58. l1 := ""
  59. select {
  60. case <-time.After(2 * time.Second):
  61. cx.t.Fatalf("timed out locking")
  62. case l1 = <-ch:
  63. if !strings.HasPrefix(l1, name) {
  64. cx.t.Errorf("got %q, expected %q prefix", l1, name)
  65. }
  66. }
  67. // blocked process that won't acquire the lock
  68. blocked, ch, err := ctlV3Lock(cx, name)
  69. if err != nil {
  70. cx.t.Fatal(err)
  71. }
  72. select {
  73. case <-time.After(100 * time.Millisecond):
  74. case <-ch:
  75. cx.t.Fatalf("should block")
  76. }
  77. // overlap with a blocker that will acquire the lock
  78. blockAcquire, ch, err := ctlV3Lock(cx, name)
  79. if err != nil {
  80. cx.t.Fatal(err)
  81. }
  82. defer blockAcquire.Stop()
  83. select {
  84. case <-time.After(100 * time.Millisecond):
  85. case <-ch:
  86. cx.t.Fatalf("should block")
  87. }
  88. // kill blocked process with clean shutdown
  89. if err = blocked.Signal(os.Interrupt); err != nil {
  90. cx.t.Fatal(err)
  91. }
  92. if err = closeWithTimeout(blocked, time.Second); err != nil {
  93. cx.t.Fatal(err)
  94. }
  95. // kill the holder with clean shutdown
  96. if err = holder.Signal(os.Interrupt); err != nil {
  97. cx.t.Fatal(err)
  98. }
  99. if err = closeWithTimeout(holder, time.Second); err != nil {
  100. cx.t.Fatal(err)
  101. }
  102. // blockAcquire should acquire the lock
  103. select {
  104. case <-time.After(time.Second):
  105. cx.t.Fatalf("timed out from waiting to holding")
  106. case l2 := <-ch:
  107. if l1 == l2 || !strings.HasPrefix(l2, name) {
  108. cx.t.Fatalf("expected different lock name, got l1=%q, l2=%q", l1, l2)
  109. }
  110. }
  111. }
  112. // ctlV3Lock creates a lock process with a channel listening for when it acquires the lock.
  113. func ctlV3Lock(cx ctlCtx, name string) (*expect.ExpectProcess, <-chan string, error) {
  114. cmdArgs := append(cx.PrefixArgs(), "lock", name)
  115. proc, err := spawnCmd(cmdArgs)
  116. outc := make(chan string, 1)
  117. if err != nil {
  118. close(outc)
  119. return proc, outc, err
  120. }
  121. proc.StopSignal = debugLockSignal
  122. go func() {
  123. s, xerr := proc.ExpectFunc(func(string) bool { return true })
  124. if xerr != nil {
  125. cx.t.Errorf("expect failed (%v)", xerr)
  126. }
  127. outc <- s
  128. }()
  129. return proc, outc, err
  130. }