github.com/kubernetes-incubator/kube-aws@v0.16.4/flatcar/amiregistry/reliable_http_test.go (about) 1 package amiregistry 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "log" 8 "net/http" 9 "testing" 10 ) 11 12 type unreliableTransportHttpGetter struct { 13 numSimulatedErrors int 14 } 15 16 func (g *unreliableTransportHttpGetter) Get(url string) (resp *http.Response, err error) { 17 if g.numSimulatedErrors > 0 { 18 g.numSimulatedErrors-- 19 return nil, fmt.Errorf("simulated transport error: errors left=%d", g.numSimulatedErrors) 20 } 21 return &http.Response{ 22 StatusCode: 200, 23 Status: "200 OK", 24 Proto: "HTTP/1.1", 25 ProtoMajor: 1, 26 ProtoMinor: 1, 27 }, nil 28 } 29 30 type unreliableApplicationHttpGetter struct { 31 numSimulatedErrors int 32 statusCode int 33 } 34 35 func (g *unreliableApplicationHttpGetter) Get(url string) (resp *http.Response, err error) { 36 if g.statusCode <= 0 { 37 log.Panicf("unsupported status code: %d", g.statusCode) 38 } 39 40 if g.numSimulatedErrors > 0 { 41 g.numSimulatedErrors-- 42 return &http.Response{ 43 StatusCode: g.statusCode, 44 Proto: "HTTP/1.1", 45 ProtoMajor: 1, 46 ProtoMinor: 1, 47 Body: ioutil.NopCloser(bytes.NewReader([]byte(fmt.Sprintf("simulated application error: errors left=%d", g.numSimulatedErrors)))), 48 }, nil 49 } 50 return &http.Response{ 51 StatusCode: 200, 52 Status: "200 OK", 53 Proto: "HTTP/1.1", 54 ProtoMajor: 1, 55 ProtoMinor: 1, 56 }, nil 57 } 58 59 func TestAuthTokenGeneration(t *testing.T) { 60 t.Run("WithUnreliableTransport", func(t *testing.T) { 61 t.Run("SucceedAfterRetries", func(t *testing.T) { 62 twoErrors := &unreliableTransportHttpGetter{ 63 numSimulatedErrors: 2, 64 } 65 h1 := reliableHttpImpl{ 66 underlyingGet: twoErrors.Get, 67 } 68 r1, e1 := h1.Get("http://example.com/status") 69 if e1 != nil { 70 t.Errorf("expected an reliable http get to succeed after implicit retries, but it didn't: %v", e1) 71 } 72 if r1.StatusCode != 200 { 73 t.Errorf("expected an reliable http get to succeed after implicit retries, but it didn't: invalid status code: %d", r1.StatusCode) 74 } 75 }) 76 77 t.Run("FailAfterRetries", func(t *testing.T) { 78 threeErrors := &unreliableTransportHttpGetter{ 79 numSimulatedErrors: 3, 80 } 81 h2 := reliableHttpImpl{ 82 underlyingGet: threeErrors.Get, 83 } 84 _, e2 := h2.Get("http://example.com/status") 85 if e2 == nil { 86 t.Error("expected an reliable http get to fail after exceeding max retry count, but it didn't") 87 } 88 }) 89 }) 90 91 t.Run("WithUnreliableApplication", func(t *testing.T) { 92 t.Run("SucceedAfterRetries", func(t *testing.T) { 93 twoErrors := &unreliableApplicationHttpGetter{ 94 numSimulatedErrors: 2, 95 statusCode: 504, 96 } 97 h1 := reliableHttpImpl{ 98 underlyingGet: twoErrors.Get, 99 } 100 r, e := h1.Get("http://example.com/status") 101 if r == nil { 102 t.Error("expected an reliable http get to return non-nil response, but it didn't") 103 t.FailNow() 104 } 105 if r.StatusCode != 200 { 106 t.Errorf("expected an reliable http get to succeed after implicit retries, but it didn't: invalid status code \"%d\", error was \"%v\"", r.StatusCode, e) 107 } 108 }) 109 110 t.Run("FailAfterRetries", func(t *testing.T) { 111 threeErrors := &unreliableApplicationHttpGetter{ 112 numSimulatedErrors: 3, 113 statusCode: 504, 114 } 115 h := reliableHttpImpl{ 116 underlyingGet: threeErrors.Get, 117 } 118 r, e := h.Get("http://example.com/status") 119 if r == nil { 120 t.Error("expected an reliable http get to return non-nil response, but it didn't") 121 t.FailNow() 122 } 123 if r.StatusCode != 504 { 124 t.Errorf("expected an reliable http get to fail after exceeding max retry count, but it didn't: invalid status code \"%d\", error was \"%v\"", r.StatusCode, e) 125 } 126 }) 127 }) 128 }