Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame^] | 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | func TestExtractContainerMetadata(t *testing.T) { |
| 14 | getResult := &http.Response{} |
| 15 | |
| 16 | expected := map[string]string{} |
| 17 | |
| 18 | actual := ExtractMetadata(getResult) |
| 19 | |
| 20 | if !reflect.DeepEqual(expected, actual) { |
| 21 | t.Errorf("Expected: %+v\nActual:%+v", expected, actual) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestExtractContainerInfo(t *testing.T) { |
| 26 | responseBody := ` |
| 27 | [ |
| 28 | { |
| 29 | "count": 3, |
| 30 | "bytes": 2000, |
| 31 | "name": "artemis" |
| 32 | }, |
| 33 | { |
| 34 | "count": 1, |
| 35 | "bytes": 450, |
| 36 | "name": "diana" |
| 37 | } |
| 38 | ] |
| 39 | ` |
| 40 | |
| 41 | listResult := &http.Response{ |
| 42 | Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), |
| 43 | } |
| 44 | |
| 45 | var expected []Container |
| 46 | err := json.Unmarshal([]byte(responseBody), &expected) |
| 47 | if err != nil { |
| 48 | t.Errorf("Error unmarshaling JSON: %s", err) |
| 49 | } |
| 50 | |
| 51 | actual, err := ExtractInfo(listResult) |
| 52 | if err != nil { |
| 53 | t.Errorf("Error extracting containers info: %s", err) |
| 54 | } |
| 55 | |
| 56 | if !reflect.DeepEqual(expected, actual) { |
| 57 | t.Errorf("\nExpected: %+v\nActual: %+v", expected, actual) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestExtractConatinerNames(t *testing.T) { |
| 62 | responseBody := "artemis\ndiana\n" |
| 63 | |
| 64 | listResult := &http.Response{ |
| 65 | Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), |
| 66 | } |
| 67 | |
| 68 | expected := []string{"artemis", "diana"} |
| 69 | |
| 70 | actual, err := ExtractNames(listResult) |
| 71 | if err != nil { |
| 72 | t.Errorf("Error extracting container names: %s", err) |
| 73 | } |
| 74 | |
| 75 | if !reflect.DeepEqual(expected, actual) { |
| 76 | t.Errorf("Expected: %+v\nActual:%+v", expected, actual) |
| 77 | } |
| 78 | } |