github.com/uber/kraken@v0.1.4/proxy/proxyserver/server_test.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 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 proxyserver 15 16 import ( 17 "fmt" 18 "io/ioutil" 19 "net/http" 20 "testing" 21 22 "github.com/uber/kraken/utils/dockerutil" 23 "github.com/uber/kraken/utils/httputil" 24 25 "bytes" 26 "encoding/json" 27 "time" 28 29 "github.com/stretchr/testify/require" 30 "github.com/uber/kraken/core" 31 "github.com/uber/kraken/utils/mockutil" 32 ) 33 34 func TestHealth(t *testing.T) { 35 require := require.New(t) 36 37 mocks, cleanup := newServerMocks(t) 38 defer cleanup() 39 40 addr := mocks.startServer() 41 42 resp, err := httputil.Get( 43 fmt.Sprintf("http://%s/health", addr)) 44 defer resp.Body.Close() 45 require.NoError(err) 46 b, err := ioutil.ReadAll(resp.Body) 47 require.NoError(err) 48 require.Equal("OK\n", string(b)) 49 } 50 51 func TestPreheatInvalidEventBody(t *testing.T) { 52 require := require.New(t) 53 54 mocks, cleanup := newServerMocks(t) 55 defer cleanup() 56 57 addr := mocks.startServer() 58 59 _, err := httputil.Post(fmt.Sprintf("http://%s/registry/notifications", addr)) 60 require.Error(err) 61 require.True(httputil.IsStatus(err, http.StatusInternalServerError)) 62 } 63 64 func TestPreheatNoPushManifestEvent(t *testing.T) { 65 require := require.New(t) 66 67 mocks, cleanup := newServerMocks(t) 68 defer cleanup() 69 70 addr := mocks.startServer() 71 72 b, _ := json.Marshal(Notification{ 73 Events: []Event{ 74 { 75 ID: "1", 76 TimeStamp: time.Now(), 77 Action: "pull", 78 Target: &Target{ 79 MediaType: "application/vnd.docker.distribution.manifest.v2+json", 80 Digest: "sha256:ce8aaa9fe90ad0dada1d2b2fed22ee9bb64bcfc1a5a4d5f7d2fe392df35050aa", 81 Repository: "kraken-test/preheat", 82 Tag: "v1.0.0", 83 }, 84 }, 85 { 86 ID: "2", 87 TimeStamp: time.Now(), 88 Action: "push", 89 Target: &Target{ 90 MediaType: "application/octet-stream", 91 Digest: "sha256:ea8dbfc366942e2c856ee1cf59982ff6e08d12af3eb0a2cbf22b8eb12b43c22a", 92 Repository: "kraken-test/preheat", 93 Tag: "v1.0.1", 94 }, 95 }, 96 }, 97 }) 98 99 _, err := httputil.Post( 100 fmt.Sprintf("http://%s/registry/notifications", addr), 101 httputil.SendBody(bytes.NewReader(b))) 102 require.NoError(err) 103 } 104 105 func TestPreheat(t *testing.T) { 106 require := require.New(t) 107 108 mocks, cleanup := newServerMocks(t) 109 defer cleanup() 110 111 addr := mocks.startServer() 112 113 repo := "kraken-test/preheat" 114 tag := "v1.0.0" 115 layers := core.DigestListFixture(3) 116 manifest, bs := dockerutil.ManifestFixture(layers[0], layers[1], layers[2]) 117 118 notification := &Notification{ 119 Events: []Event{ 120 { 121 ID: "1", 122 TimeStamp: time.Now(), 123 Action: "push", 124 Target: &Target{ 125 MediaType: "application/vnd.docker.distribution.manifest.v2+json", 126 Digest: manifest.String(), 127 Repository: repo, 128 Tag: tag, 129 }, 130 }, 131 }, 132 } 133 134 b, _ := json.Marshal(notification) 135 136 mocks.originClient.EXPECT().DownloadBlob(repo, manifest, mockutil.MatchWriter(bs)).Return(nil) 137 mocks.originClient.EXPECT().GetMetaInfo(repo, layers[0]).Return(nil, nil) 138 mocks.originClient.EXPECT().GetMetaInfo(repo, layers[1]).Return(nil, nil) 139 mocks.originClient.EXPECT().GetMetaInfo(repo, layers[2]).Return(nil, nil) 140 _, err := httputil.Post( 141 fmt.Sprintf("http://%s/registry/notifications", addr), 142 httputil.SendBody(bytes.NewReader(b))) 143 require.NoError(err) 144 }