示例#1
0
func TestMonitorsList(t *testing.T) {
	client, err := clients.NewNetworkV2Client()
	if err != nil {
		t.Fatalf("Unable to create a network client: %v", err)
	}

	allPages, err := monitors.List(client, monitors.ListOpts{}).AllPages()
	if err != nil {
		t.Fatalf("Unable to list monitors: %v", err)
	}

	allMonitors, err := monitors.ExtractMonitors(allPages)
	if err != nil {
		t.Fatalf("Unable to extract monitors: %v", err)
	}

	for _, monitor := range allMonitors {
		PrintMonitor(t, &monitor)
	}
}
示例#2
0
func TestList(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	th.Mux.HandleFunc("/v2.0/lb/health_monitors", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "GET")
		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)

		w.Header().Add("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)

		fmt.Fprintf(w, `
{
   "health_monitors":[
      {
         "admin_state_up":true,
         "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
         "delay":10,
         "max_retries":1,
         "timeout":1,
         "type":"PING",
         "id":"466c8345-28d8-4f84-a246-e04380b0461d"
      },
      {
         "admin_state_up":true,
         "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
         "delay":5,
         "expected_codes":"200",
         "max_retries":2,
         "http_method":"GET",
         "timeout":2,
         "url_path":"/",
         "type":"HTTP",
         "id":"5d4b5228-33b0-4e60-b225-9b727c1a20e7"
      }
   ]
}
			`)
	})

	count := 0

	monitors.List(fake.ServiceClient(), monitors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
		count++
		actual, err := monitors.ExtractMonitors(page)
		if err != nil {
			t.Errorf("Failed to extract monitors: %v", err)
			return false, err
		}

		expected := []monitors.Monitor{
			{
				AdminStateUp: true,
				TenantID:     "83657cfcdfe44cd5920adaf26c48ceea",
				Delay:        10,
				MaxRetries:   1,
				Timeout:      1,
				Type:         "PING",
				ID:           "466c8345-28d8-4f84-a246-e04380b0461d",
			},
			{
				AdminStateUp:  true,
				TenantID:      "83657cfcdfe44cd5920adaf26c48ceea",
				Delay:         5,
				ExpectedCodes: "200",
				MaxRetries:    2,
				Timeout:       2,
				URLPath:       "/",
				Type:          "HTTP",
				HTTPMethod:    "GET",
				ID:            "5d4b5228-33b0-4e60-b225-9b727c1a20e7",
			},
		}

		th.CheckDeepEquals(t, expected, actual)

		return true, nil
	})

	if count != 1 {
		t.Errorf("Expected 1 page, got %d", count)
	}
}