github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/servergroups/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
     9  	th "github.com/gophercloud/gophercloud/testhelper"
    10  	"github.com/gophercloud/gophercloud/testhelper/client"
    11  )
    12  
    13  // ListOutput is a sample response to a List call.
    14  const ListOutput = `
    15  {
    16      "server_groups": [
    17          {
    18              "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
    19              "name": "test",
    20              "policies": [
    21                  "anti-affinity"
    22              ],
    23              "members": [],
    24              "metadata": {}
    25          },
    26          {
    27              "id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
    28              "name": "test2",
    29              "policies": [
    30                  "affinity"
    31              ],
    32              "members": [],
    33              "metadata": {}
    34          }
    35      ]
    36  }
    37  `
    38  
    39  // GetOutput is a sample response to a Get call.
    40  const GetOutput = `
    41  {
    42      "server_group": {
    43          "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
    44          "name": "test",
    45          "policies": [
    46              "anti-affinity"
    47          ],
    48          "members": [],
    49          "metadata": {}
    50      }
    51  }
    52  `
    53  
    54  // GetOutputMicroversion is a sample response to a Get call with microversion set to 2.64
    55  const GetOutputMicroversion = `
    56  {
    57      "server_group": {
    58          "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
    59          "name": "test",
    60          "policies": [
    61              "anti-affinity"
    62          ],
    63          "policy": "anti-affinity",
    64          "rules": {
    65            "max_server_per_host": 3
    66          },
    67          "members": [],
    68          "metadata": {}
    69      }
    70  }
    71  `
    72  
    73  // CreateOutput is a sample response to a Post call
    74  const CreateOutput = `
    75  {
    76      "server_group": {
    77          "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
    78          "name": "test",
    79          "policies": [
    80              "anti-affinity"
    81          ],
    82          "members": [],
    83          "metadata": {}
    84      }
    85  }
    86  `
    87  
    88  // CreateOutputMicroversion is a sample response to a Post call with microversion set to 2.64
    89  const CreateOutputMicroversion = `
    90  {
    91      "server_group": {
    92          "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
    93          "name": "test",
    94          "policies": [
    95              "anti-affinity"
    96          ],
    97          "policy": "anti-affinity",
    98          "rules": {
    99            "max_server_per_host": 3
   100          },
   101          "members": [],
   102          "metadata": {}
   103      }
   104  }
   105  `
   106  
   107  // FirstServerGroup is the first result in ListOutput.
   108  var FirstServerGroup = servergroups.ServerGroup{
   109  	ID:   "616fb98f-46ca-475e-917e-2563e5a8cd19",
   110  	Name: "test",
   111  	Policies: []string{
   112  		"anti-affinity",
   113  	},
   114  	Members:  []string{},
   115  	Metadata: map[string]interface{}{},
   116  }
   117  
   118  // SecondServerGroup is the second result in ListOutput.
   119  var SecondServerGroup = servergroups.ServerGroup{
   120  	ID:   "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
   121  	Name: "test2",
   122  	Policies: []string{
   123  		"affinity",
   124  	},
   125  	Members:  []string{},
   126  	Metadata: map[string]interface{}{},
   127  }
   128  
   129  // ExpectedServerGroupSlice is the slice of results that should be parsed
   130  // from ListOutput, in the expected order.
   131  var ExpectedServerGroupSlice = []servergroups.ServerGroup{FirstServerGroup, SecondServerGroup}
   132  
   133  // CreatedServerGroup is the parsed result from CreateOutput.
   134  var CreatedServerGroup = servergroups.ServerGroup{
   135  	ID:   "616fb98f-46ca-475e-917e-2563e5a8cd19",
   136  	Name: "test",
   137  	Policies: []string{
   138  		"anti-affinity",
   139  	},
   140  	Members:  []string{},
   141  	Metadata: map[string]interface{}{},
   142  }
   143  
   144  // HandleListSuccessfully configures the test server to respond to a List request.
   145  func HandleListSuccessfully(t *testing.T) {
   146  	th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) {
   147  		th.TestMethod(t, r, "GET")
   148  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   149  
   150  		w.Header().Add("Content-Type", "application/json")
   151  		fmt.Fprintf(w, ListOutput)
   152  	})
   153  }
   154  
   155  // HandleGetSuccessfully configures the test server to respond to a Get request
   156  // for an existing server group
   157  func HandleGetSuccessfully(t *testing.T) {
   158  	th.Mux.HandleFunc("/os-server-groups/4d8c3732-a248-40ed-bebc-539a6ffd25c0", func(w http.ResponseWriter, r *http.Request) {
   159  		th.TestMethod(t, r, "GET")
   160  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   161  
   162  		w.Header().Add("Content-Type", "application/json")
   163  		fmt.Fprintf(w, GetOutput)
   164  	})
   165  }
   166  
   167  // HandleGetMicroversionSuccessfully configures the test server to respond to a Get request
   168  // for an existing server group with microversion set to 2.64
   169  func HandleGetMicroversionSuccessfully(t *testing.T) {
   170  	th.Mux.HandleFunc("/os-server-groups/4d8c3732-a248-40ed-bebc-539a6ffd25c0", func(w http.ResponseWriter, r *http.Request) {
   171  		th.TestMethod(t, r, "GET")
   172  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   173  
   174  		w.Header().Add("Content-Type", "application/json")
   175  		fmt.Fprintf(w, GetOutputMicroversion)
   176  	})
   177  }
   178  
   179  // HandleCreateSuccessfully configures the test server to respond to a Create request
   180  // for a new server group
   181  func HandleCreateSuccessfully(t *testing.T) {
   182  	th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) {
   183  		th.TestMethod(t, r, "POST")
   184  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   185  		th.TestJSONRequest(t, r, `
   186  {
   187      "server_group": {
   188          "name": "test",
   189          "policies": [
   190              "anti-affinity"
   191          ]
   192      }
   193  }
   194  `)
   195  
   196  		w.Header().Add("Content-Type", "application/json")
   197  		fmt.Fprintf(w, CreateOutput)
   198  	})
   199  }
   200  
   201  // HandleCreateMicroversionSuccessfully configures the test server to respond to a Create request
   202  // for a new server group with microversion set to 2.64
   203  func HandleCreateMicroversionSuccessfully(t *testing.T) {
   204  	th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) {
   205  		th.TestMethod(t, r, "POST")
   206  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   207  		th.TestJSONRequest(t, r, `
   208  {
   209      "server_group": {
   210          "name": "test",
   211          "policies": [
   212              "anti-affinity"
   213          ],
   214          "policy": "anti-affinity",
   215          "rules": {
   216              "max_server_per_host": 3
   217          }
   218      }
   219  }
   220  `)
   221  
   222  		w.Header().Add("Content-Type", "application/json")
   223  		fmt.Fprintf(w, CreateOutputMicroversion)
   224  	})
   225  }
   226  
   227  // HandleDeleteSuccessfully configures the test server to respond to a Delete request for a
   228  // an existing server group
   229  func HandleDeleteSuccessfully(t *testing.T) {
   230  	th.Mux.HandleFunc("/os-server-groups/616fb98f-46ca-475e-917e-2563e5a8cd19", func(w http.ResponseWriter, r *http.Request) {
   231  		th.TestMethod(t, r, "DELETE")
   232  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   233  
   234  		w.WriteHeader(http.StatusAccepted)
   235  	})
   236  }