http_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 leasehttp
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "os"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/lease"
  22. "github.com/coreos/etcd/mvcc/backend"
  23. "golang.org/x/net/context"
  24. )
  25. func TestRenewHTTP(t *testing.T) {
  26. be, tmpPath := backend.NewTmpBackend(time.Hour, 10000)
  27. defer os.Remove(tmpPath)
  28. defer be.Close()
  29. le := lease.NewLessor(be, int64(5))
  30. le.Promote(time.Second)
  31. l, err := le.Grant(1, int64(5))
  32. if err != nil {
  33. t.Fatalf("failed to create lease: %v", err)
  34. }
  35. ts := httptest.NewServer(NewHandler(le))
  36. defer ts.Close()
  37. ttl, err := RenewHTTP(l.ID, ts.URL+LeasePrefix, http.DefaultTransport, time.Second)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if ttl != 5 {
  42. t.Fatalf("ttl expected 5, got %d", ttl)
  43. }
  44. }
  45. func TestTimeToLiveHTTP(t *testing.T) {
  46. be, tmpPath := backend.NewTmpBackend(time.Hour, 10000)
  47. defer os.Remove(tmpPath)
  48. defer be.Close()
  49. le := lease.NewLessor(be, int64(5))
  50. le.Promote(time.Second)
  51. l, err := le.Grant(1, int64(5))
  52. if err != nil {
  53. t.Fatalf("failed to create lease: %v", err)
  54. }
  55. ts := httptest.NewServer(NewHandler(le))
  56. defer ts.Close()
  57. resp, err := TimeToLiveHTTP(context.TODO(), l.ID, true, ts.URL+LeaseInternalPrefix, http.DefaultTransport)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if resp.LeaseTimeToLiveResponse.ID != 1 {
  62. t.Fatalf("lease id expected 1, got %d", resp.LeaseTimeToLiveResponse.ID)
  63. }
  64. if resp.LeaseTimeToLiveResponse.GrantedTTL != 5 {
  65. t.Fatalf("granted TTL expected 5, got %d", resp.LeaseTimeToLiveResponse.GrantedTTL)
  66. }
  67. }