github.com/gophercloud/gophercloud@v1.11.0/openstack/baremetal/v1/conductors/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/gophercloud/gophercloud/openstack/baremetal/v1/conductors"
    10  	th "github.com/gophercloud/gophercloud/testhelper"
    11  	"github.com/gophercloud/gophercloud/testhelper/client"
    12  )
    13  
    14  // ConductorListBody contains the canned body of a conductor.List response, without detail.
    15  const ConductorListBody = `
    16   {
    17    "conductors": [
    18      {
    19        "hostname": "compute1.localdomain",
    20        "conductor_group": "",
    21        "links": [
    22          {
    23            "href": "http://127.0.0.1:6385/v1/conductors/compute1.localdomain",
    24            "rel": "self"
    25          },
    26          {
    27            "href": "http://127.0.0.1:6385/conductors/compute1.localdomain",
    28            "rel": "bookmark"
    29          }
    30        ],
    31        "alive": false
    32      },
    33      {
    34        "hostname": "compute2.localdomain",
    35        "conductor_group": "",
    36        "links": [
    37          {
    38            "href": "http://127.0.0.1:6385/v1/conductors/compute2.localdomain",
    39            "rel": "self"
    40          },
    41          {
    42            "href": "http://127.0.0.1:6385/conductors/compute2.localdomain",
    43            "rel": "bookmark"
    44          }
    45        ],
    46        "alive": true
    47      }
    48    ]
    49   }
    50  `
    51  
    52  // ConductorListDetailBody contains the canned body of a conductor.ListDetail response.
    53  const ConductorListDetailBody = `
    54  {
    55    "conductors": [
    56      {
    57        "links": [
    58          {
    59            "href": "http://127.0.0.1:6385/v1/conductors/compute1.localdomain",
    60            "rel": "self"
    61          },
    62          {
    63            "href": "http://127.0.0.1:6385/conductors/compute1.localdomain",
    64            "rel": "bookmark"
    65          }
    66        ],
    67        "created_at": "2018-08-07T08:39:21+00:00",
    68        "hostname": "compute1.localdomain",
    69        "conductor_group": "",
    70        "updated_at": "2018-11-30T07:07:23+00:00",
    71        "alive": false,
    72        "drivers": [
    73          "ipmi"
    74        ]
    75      },
    76      {
    77        "links": [
    78          {
    79            "href": "http://127.0.0.1:6385/v1/conductors/compute2.localdomain",
    80            "rel": "self"
    81          },
    82          {
    83            "href": "http://127.0.0.1:6385/conductors/compute2.localdomain",
    84            "rel": "bookmark"
    85          }
    86        ],
    87        "created_at": "2018-12-05T07:03:19+00:00",
    88        "hostname": "compute2.localdomain",
    89        "conductor_group": "",
    90        "updated_at": "2018-12-05T07:03:21+00:00",
    91        "alive": true,
    92        "drivers": [
    93          "ipmi"
    94        ]
    95      }
    96    ]
    97  }
    98  `
    99  
   100  // SingleConductorBody is the canned body of a Get request on an existing conductor.
   101  const SingleConductorBody = `
   102  {
   103    "links": [
   104      {
   105        "href": "http://127.0.0.1:6385/v1/conductors/compute2.localdomain",
   106        "rel": "self"
   107      },
   108      {
   109        "href": "http://127.0.0.1:6385/conductors/compute2.localdomain",
   110        "rel": "bookmark"
   111      }
   112    ],
   113    "created_at": "2018-12-05T07:03:19+00:00",
   114    "hostname": "compute2.localdomain",
   115    "conductor_group": "",
   116    "updated_at": "2018-12-05T07:03:21+00:00",
   117    "alive": true,
   118    "drivers": [
   119      "ipmi"
   120    ]
   121  }
   122  `
   123  
   124  var (
   125  	createdAtFoo, _ = time.Parse(time.RFC3339, "2018-12-05T07:03:19+00:00")
   126  	updatedAt, _    = time.Parse(time.RFC3339, "2018-12-05T07:03:21+00:00")
   127  
   128  	ConductorFoo = conductors.Conductor{
   129  		CreatedAt:      createdAtFoo,
   130  		UpdatedAt:      updatedAt,
   131  		Hostname:       "compute2.localdomain",
   132  		ConductorGroup: "",
   133  		Alive:          true,
   134  		Drivers: []string{
   135  			"ipmi",
   136  		},
   137  	}
   138  )
   139  
   140  // HandleConductorListSuccessfully sets up the test server to respond to a server List request.
   141  func HandleConductorListSuccessfully(t *testing.T) {
   142  	th.Mux.HandleFunc("/conductors", func(w http.ResponseWriter, r *http.Request) {
   143  		th.TestMethod(t, r, "GET")
   144  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   145  		w.Header().Add("Content-Type", "application/json")
   146  		r.ParseForm()
   147  
   148  		marker := r.Form.Get("marker")
   149  		switch marker {
   150  		case "":
   151  			fmt.Fprintf(w, ConductorListBody)
   152  
   153  		case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
   154  			fmt.Fprintf(w, `{ "servers": [] }`)
   155  		default:
   156  			t.Fatalf("/conductors invoked with unexpected marker=[%s]", marker)
   157  		}
   158  	})
   159  }
   160  
   161  // HandleConductorListDetailSuccessfully sets up the test server to respond to a server List request.
   162  func HandleConductorListDetailSuccessfully(t *testing.T) {
   163  	th.Mux.HandleFunc("/conductors", func(w http.ResponseWriter, r *http.Request) {
   164  		th.TestMethod(t, r, "GET")
   165  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   166  		w.Header().Add("Content-Type", "application/json")
   167  		r.ParseForm()
   168  
   169  		fmt.Fprintf(w, ConductorListDetailBody)
   170  	})
   171  }
   172  
   173  func HandleConductorGetSuccessfully(t *testing.T) {
   174  	th.Mux.HandleFunc("/conductors/1234asdf", func(w http.ResponseWriter, r *http.Request) {
   175  		th.TestMethod(t, r, "GET")
   176  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   177  		th.TestHeader(t, r, "Accept", "application/json")
   178  
   179  		fmt.Fprintf(w, SingleConductorBody)
   180  	})
   181  }