|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/alibaba/pouch/apis/types" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func TestSystemInfoError(t *testing.T) { |
| 19 | + client := &APIClient{ |
| 20 | + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), |
| 21 | + } |
| 22 | + _, err := client.SystemInfo(context.Background()) |
| 23 | + if err == nil || !strings.Contains(err.Error(), "Server error") { |
| 24 | + t.Fatalf("expected a Server Error, got %v", err) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestSystemInfo(t *testing.T) { |
| 29 | + expectedURL := "/info" |
| 30 | + |
| 31 | + httpClient := newMockClient(func(req *http.Request) (*http.Response, error) { |
| 32 | + if !strings.HasPrefix(req.URL.Path, expectedURL) { |
| 33 | + return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) |
| 34 | + } |
| 35 | + info := types.SystemInfo{ |
| 36 | + ContainersRunning: 2, |
| 37 | + ContainersStopped: 3, |
| 38 | + Debug: true, |
| 39 | + Name: "my_host", |
| 40 | + PouchRootDir: "/var/lib/pouch", |
| 41 | + } |
| 42 | + b, err := json.Marshal(info) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + return &http.Response{ |
| 48 | + StatusCode: http.StatusOK, |
| 49 | + Body: ioutil.NopCloser(bytes.NewReader([]byte(b))), |
| 50 | + }, nil |
| 51 | + }) |
| 52 | + |
| 53 | + client := &APIClient{ |
| 54 | + HTTPCli: httpClient, |
| 55 | + } |
| 56 | + |
| 57 | + info, err := client.SystemInfo(context.Background()) |
| 58 | + if err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + assert.Equal(t, info.PouchRootDir, "/var/lib/pouch") |
| 62 | + assert.Equal(t, info.Name, "my_host") |
| 63 | + assert.Equal(t, info.Debug, true) |
| 64 | + assert.Equal(t, info.ContainersStopped, int64(3)) |
| 65 | + assert.Equal(t, info.ContainersRunning, int64(2)) |
| 66 | +} |
0 commit comments