|
| 1 | +/* |
| 2 | +Copyright 2017 Google Inc. |
| 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 controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + "net/http/httptest" |
| 22 | + "net/url" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/GoogleCloudPlatform/k8s-stackdriver/kubelet-to-gcm/monitor/util" |
| 27 | +) |
| 28 | + |
| 29 | +func TestDoRequestAndParse_SizeLimit(t *testing.T) { |
| 30 | + // Create a mock server that returns a response larger than maxResponseBodySize. |
| 31 | + largeDataSize := maxResponseBodySize + 1024 |
| 32 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 33 | + // Write more than maxResponseBodySize bytes. |
| 34 | + data := make([]byte, largeDataSize) |
| 35 | + w.Write(data) |
| 36 | + })) |
| 37 | + defer ts.Close() |
| 38 | + |
| 39 | + u, _ := url.Parse(ts.URL) |
| 40 | + client := &Client{ |
| 41 | + client: ts.Client(), |
| 42 | + metricsURL: u, |
| 43 | + } |
| 44 | + |
| 45 | + req, _ := http.NewRequest("GET", ts.URL, nil) |
| 46 | + _, err := client.doRequestAndParse(req) |
| 47 | + |
| 48 | + if err == nil { |
| 49 | + t.Fatal("Expected error due to size limit, but got none") |
| 50 | + } |
| 51 | + |
| 52 | + if !strings.Contains(err.Error(), util.ErrBodyTooLarge.Error()) { |
| 53 | + t.Errorf("Expected error containing %q, got %v", util.ErrBodyTooLarge, err) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestDoRequestAndParse_Success(t *testing.T) { |
| 58 | + metricsData := ` |
| 59 | +# HELP node_collector_evictions_number Number of evictions |
| 60 | +# TYPE node_collector_evictions_number counter |
| 61 | +node_collector_evictions_number 10 |
| 62 | +# HELP process_start_time_seconds Start time |
| 63 | +# TYPE process_start_time_seconds gauge |
| 64 | +process_start_time_seconds 1234567890 |
| 65 | +` |
| 66 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 67 | + w.Write([]byte(metricsData)) |
| 68 | + })) |
| 69 | + defer ts.Close() |
| 70 | + |
| 71 | + u, _ := url.Parse(ts.URL) |
| 72 | + client := &Client{ |
| 73 | + client: ts.Client(), |
| 74 | + metricsURL: u, |
| 75 | + } |
| 76 | + |
| 77 | + req, _ := http.NewRequest("GET", ts.URL, nil) |
| 78 | + metrics, err := client.doRequestAndParse(req) |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("Unexpected error: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + if metrics.NodeEvictions != 10 { |
| 85 | + t.Errorf("Expected 10 evictions, got %d", metrics.NodeEvictions) |
| 86 | + } |
| 87 | + if metrics.CreateTime != 1234567890 { |
| 88 | + t.Errorf("Expected 1234567890 create time, got %d", metrics.CreateTime) |
| 89 | + } |
| 90 | +} |
0 commit comments