github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/metrics/sinks/monasca/init_test.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 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 15 package monasca 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "io/ioutil" 21 "net/http" 22 "net/http/httptest" 23 "net/url" 24 "os" 25 "sync" 26 "testing" 27 28 "github.com/stretchr/testify/mock" 29 ) 30 31 type keystoneClientMock struct { 32 *KeystoneClientImpl 33 mock.Mock 34 } 35 36 func (ks *keystoneClientMock) MonascaURL() (*url.URL, error) { 37 args := ks.Called() 38 return args.Get(0).(*url.URL), args.Error(1) 39 } 40 41 func (ks *keystoneClientMock) GetToken() (string, error) { 42 args := ks.Called() 43 return args.String(0), args.Error(1) 44 } 45 46 var monascaAPIStub *httptest.Server 47 var keystoneAPIStub *httptest.Server 48 49 type ksAuthRequest struct { 50 Auth ksAuth `json:"auth"` 51 } 52 53 type ksAuth struct { 54 Identity ksIdentity `json:"identity"` 55 } 56 57 type ksIdentity struct { 58 Methods []string `json:"methods"` 59 Password ksPassword `json:"password"` 60 } 61 62 type ksPassword struct { 63 User ksUser `json:"user"` 64 } 65 66 type ksUser struct { 67 ID string `json:"id"` 68 Password string `json:"password"` 69 } 70 71 // prepare before testing 72 func TestMain(m *testing.M) { 73 // monasca stub 74 monascaMutex := &sync.Mutex{} 75 defer monascaAPIStub.Close() 76 monascaAPIStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 77 monascaMutex.Lock() 78 defer monascaMutex.Unlock() 79 w.Header().Set("Content-Type", "application/json") 80 switch r.URL.Path { 81 case "/metrics": 82 defer r.Body.Close() 83 contents, err := ioutil.ReadAll(r.Body) 84 if err != nil { 85 w.WriteHeader(http.StatusInternalServerError) 86 w.Write([]byte(fmt.Sprintf("%s", err))) 87 break 88 } 89 90 // umarshal & do type checking on the fly 91 metrics := []metric{} 92 err = json.Unmarshal(contents, &metrics) 93 if err != nil { 94 w.WriteHeader(http.StatusInternalServerError) 95 w.Write([]byte(fmt.Sprintf("%s", err))) 96 break 97 } 98 99 // check for empty dimensions 100 for _, metric := range metrics { 101 for _, dimVal := range metric.Dimensions { 102 if dimVal == "" { 103 w.WriteHeader(http.StatusInternalServerError) 104 w.Write([]byte(monEmptyDimResp)) 105 break 106 } 107 } 108 } 109 110 // check token 111 token := r.Header.Get("X-Auth-Token") 112 if token != testToken { 113 w.WriteHeader(http.StatusUnauthorized) 114 w.Write([]byte(monUnauthorizedResp)) 115 break 116 } 117 w.WriteHeader(http.StatusNoContent) 118 break 119 case "/versions": 120 w.WriteHeader(http.StatusOK) 121 break 122 } 123 })) 124 125 // keystone stub 126 keystoneMutex := &sync.Mutex{} 127 defer keystoneAPIStub.Close() 128 keystoneAPIStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 129 keystoneMutex.Lock() 130 defer keystoneMutex.Unlock() 131 w.Header().Add("Content-Type", "application/json") 132 switch r.URL.Path { 133 case "/": 134 w.Write([]byte(ksVersionResp)) 135 break 136 case "/v3/auth/tokens": 137 if r.Method == "HEAD" { 138 ksToken := r.Header.Get("X-Auth-Token") 139 if ksToken != testToken { 140 w.WriteHeader(http.StatusUnauthorized) 141 break 142 } 143 token := r.Header.Get("X-Subject-Token") 144 if token == testToken { 145 // token valid 146 w.WriteHeader(http.StatusNoContent) 147 break 148 } 149 // token invalid 150 w.WriteHeader(http.StatusNotFound) 151 break 152 } 153 154 // read request 155 defer r.Body.Close() 156 contents, err := ioutil.ReadAll(r.Body) 157 if err != nil { 158 w.WriteHeader(http.StatusInternalServerError) 159 return 160 } 161 req := ksAuthRequest{} 162 err = json.Unmarshal(contents, &req) 163 if err != nil { 164 w.WriteHeader(http.StatusInternalServerError) 165 return 166 } 167 168 // authenticate 169 if req.Auth.Identity.Password.User.ID != testConfig.UserID || 170 req.Auth.Identity.Password.User.Password != testConfig.Password { 171 w.WriteHeader(http.StatusUnauthorized) 172 return 173 } 174 175 // return a token 176 w.Header().Add("X-Subject-Token", testToken) 177 w.WriteHeader(http.StatusAccepted) 178 w.Write([]byte(ksAuthResp)) 179 break 180 case "/v3/services": 181 w.Write([]byte(ksServicesResp)) 182 break 183 case "/v3/endpoints": 184 w.Write([]byte(ksEndpointsResp)) 185 default: 186 w.WriteHeader(http.StatusInternalServerError) 187 } 188 })) 189 initKeystoneRespStubs() 190 191 testConfig.Password = "bar" 192 testConfig.UserID = "0ca8f6" 193 testConfig.IdentityEndpoint = keystoneAPIStub.URL + "/v3" 194 os.Exit(m.Run()) 195 }