k8s.io/client-go@v0.22.2/discovery/cached/disk/round_tripper_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package disk 18 19 import ( 20 "bytes" 21 "io/ioutil" 22 "net/http" 23 "net/url" 24 "os" 25 "path/filepath" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 // copied from k8s.io/client-go/transport/round_trippers_test.go 32 type testRoundTripper struct { 33 Request *http.Request 34 Response *http.Response 35 Err error 36 } 37 38 func (rt *testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { 39 rt.Request = req 40 return rt.Response, rt.Err 41 } 42 43 func TestCacheRoundTripper(t *testing.T) { 44 rt := &testRoundTripper{} 45 cacheDir, err := ioutil.TempDir("", "cache-rt") 46 defer os.RemoveAll(cacheDir) 47 if err != nil { 48 t.Fatal(err) 49 } 50 cache := newCacheRoundTripper(cacheDir, rt) 51 52 // First call, caches the response 53 req := &http.Request{ 54 Method: http.MethodGet, 55 URL: &url.URL{Host: "localhost"}, 56 } 57 rt.Response = &http.Response{ 58 Header: http.Header{"ETag": []string{`"123456"`}}, 59 Body: ioutil.NopCloser(bytes.NewReader([]byte("Content"))), 60 StatusCode: http.StatusOK, 61 } 62 resp, err := cache.RoundTrip(req) 63 if err != nil { 64 t.Fatal(err) 65 } 66 content, err := ioutil.ReadAll(resp.Body) 67 if err != nil { 68 t.Fatal(err) 69 } 70 if string(content) != "Content" { 71 t.Errorf(`Expected Body to be "Content", got %q`, string(content)) 72 } 73 74 // Second call, returns cached response 75 req = &http.Request{ 76 Method: http.MethodGet, 77 URL: &url.URL{Host: "localhost"}, 78 } 79 rt.Response = &http.Response{ 80 StatusCode: http.StatusNotModified, 81 Body: ioutil.NopCloser(bytes.NewReader([]byte("Other Content"))), 82 } 83 84 resp, err = cache.RoundTrip(req) 85 if err != nil { 86 t.Fatal(err) 87 } 88 89 // Read body and make sure we have the initial content 90 content, err = ioutil.ReadAll(resp.Body) 91 resp.Body.Close() 92 if err != nil { 93 t.Fatal(err) 94 } 95 if string(content) != "Content" { 96 t.Errorf("Invalid content read from cache %q", string(content)) 97 } 98 } 99 100 func TestCacheRoundTripperPathPerm(t *testing.T) { 101 assert := assert.New(t) 102 103 rt := &testRoundTripper{} 104 cacheDir, err := ioutil.TempDir("", "cache-rt") 105 os.RemoveAll(cacheDir) 106 defer os.RemoveAll(cacheDir) 107 108 if err != nil { 109 t.Fatal(err) 110 } 111 cache := newCacheRoundTripper(cacheDir, rt) 112 113 // First call, caches the response 114 req := &http.Request{ 115 Method: http.MethodGet, 116 URL: &url.URL{Host: "localhost"}, 117 } 118 rt.Response = &http.Response{ 119 Header: http.Header{"ETag": []string{`"123456"`}}, 120 Body: ioutil.NopCloser(bytes.NewReader([]byte("Content"))), 121 StatusCode: http.StatusOK, 122 } 123 resp, err := cache.RoundTrip(req) 124 if err != nil { 125 t.Fatal(err) 126 } 127 content, err := ioutil.ReadAll(resp.Body) 128 if err != nil { 129 t.Fatal(err) 130 } 131 if string(content) != "Content" { 132 t.Errorf(`Expected Body to be "Content", got %q`, string(content)) 133 } 134 135 err = filepath.Walk(cacheDir, func(path string, info os.FileInfo, err error) error { 136 if err != nil { 137 return err 138 } 139 if info.IsDir() { 140 assert.Equal(os.FileMode(0750), info.Mode().Perm()) 141 } else { 142 assert.Equal(os.FileMode(0660), info.Mode().Perm()) 143 } 144 return nil 145 }) 146 assert.NoError(err) 147 }