|
| 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 api |
| 16 | + |
| 17 | +import ( |
| 18 | + "reflect" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/google/cadvisor/events" |
| 22 | +) |
| 23 | + |
| 24 | +func TestGetEventRequestBasicRequest(t *testing.T) { |
| 25 | + var request = []string{"allTime=true&TypeOom=true&MaxEventsReturned=10"} |
| 26 | + expectedQuery := &events.Request{ |
| 27 | + EventType: map[events.EventType]bool{ |
| 28 | + events.TypeOom: true, |
| 29 | + events.TypeContainerCreation: false, |
| 30 | + events.TypeContainerDeletion: false, |
| 31 | + }, |
| 32 | + MaxEventsReturned: 10, |
| 33 | + } |
| 34 | + receivedQuery, shouldGetAllEvents, err := getEventRequest(request) |
| 35 | + if !reflect.DeepEqual(expectedQuery, receivedQuery) { |
| 36 | + t.Errorf("expected %v but received %v", expectedQuery, receivedQuery) |
| 37 | + } |
| 38 | + if !shouldGetAllEvents { |
| 39 | + t.Errorf("expected to call GetEvents rather than WatchEvents") |
| 40 | + } |
| 41 | + if err != nil { |
| 42 | + t.Errorf("got error when parsing event request url: %v", err) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestGetEventEmptyRequest(t *testing.T) { |
| 47 | + var request = []string{} |
| 48 | + expectedQuery := events.NewAllEventsRequest() |
| 49 | + receivedQuery, shouldGetAllEvents, err := getEventRequest(request) |
| 50 | + if !reflect.DeepEqual(expectedQuery, receivedQuery) { |
| 51 | + t.Errorf("expected %v but received %v", expectedQuery, receivedQuery) |
| 52 | + } |
| 53 | + if !shouldGetAllEvents { |
| 54 | + t.Errorf("expected to call GetEvents rather than WatchEvents") |
| 55 | + } |
| 56 | + if err != nil { |
| 57 | + t.Errorf("got error when parsing event request url: %v", err) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestGetEventEmptyStringRequest(t *testing.T) { |
| 62 | + var request = []string{""} |
| 63 | + expectedQuery := events.NewAllEventsRequest() |
| 64 | + receivedQuery, shouldGetAllEvents, err := getEventRequest(request) |
| 65 | + if !reflect.DeepEqual(expectedQuery, receivedQuery) { |
| 66 | + t.Errorf("expected %v but received %v", expectedQuery, receivedQuery) |
| 67 | + } |
| 68 | + if !shouldGetAllEvents { |
| 69 | + t.Errorf("expected to call GetEvents rather than WatchEvents") |
| 70 | + } |
| 71 | + if err != nil { |
| 72 | + t.Errorf("got error when parsing event request url: %v", err) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestGetEventRequestDoubleArgument(t *testing.T) { |
| 77 | + var request = []string{"allTime=true&TypeOom=true&TypeOom=false"} |
| 78 | + expectedQuery := &events.Request{ |
| 79 | + EventType: map[events.EventType]bool{ |
| 80 | + events.TypeOom: true, |
| 81 | + events.TypeContainerCreation: false, |
| 82 | + events.TypeContainerDeletion: false, |
| 83 | + }, |
| 84 | + } |
| 85 | + receivedQuery, shouldGetAllEvents, err := getEventRequest(request) |
| 86 | + if !reflect.DeepEqual(expectedQuery, receivedQuery) { |
| 87 | + t.Errorf("expected %v but received %v", expectedQuery, receivedQuery) |
| 88 | + } |
| 89 | + if !shouldGetAllEvents { |
| 90 | + t.Errorf("expected to call GetEvents rather than WatchEvents") |
| 91 | + } |
| 92 | + if err != nil { |
| 93 | + t.Errorf("got error when parsing event request url: %v", err) |
| 94 | + } |
| 95 | +} |
0 commit comments