github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/aggregates/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strconv"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/aggregates"
    11  	th "github.com/huaweicloud/golangsdk/testhelper"
    12  	"github.com/huaweicloud/golangsdk/testhelper/client"
    13  )
    14  
    15  // AggregateListBody is sample response to the List call
    16  const AggregateListBody = `
    17  {
    18      "aggregates": [
    19          {
    20              "name": "test-aggregate1",
    21              "availability_zone": null,
    22              "deleted": false,
    23              "created_at": "2017-12-22T10:12:06.000000",
    24              "updated_at": null,
    25              "hosts": [],
    26              "deleted_at": null,
    27              "id": 1,
    28              "metadata": {}
    29          },
    30          {
    31              "name": "test-aggregate2",
    32              "availability_zone": "test-az",
    33              "deleted": false,
    34              "created_at": "2017-12-22T10:16:07.000000",
    35              "updated_at": null,
    36              "hosts": [
    37                  "cmp0"
    38              ],
    39              "deleted_at": null,
    40              "id": 4,
    41              "metadata": {
    42                  "availability_zone": "test-az"
    43              }
    44          }
    45      ]
    46  }
    47  `
    48  
    49  const AggregateCreateBody = `
    50  {
    51      "aggregate": {
    52          "availability_zone": "london",
    53          "created_at": "2016-12-27T22:51:32.000000",
    54          "deleted": false,
    55          "deleted_at": null,
    56          "id": 32,
    57          "name": "name",
    58          "updated_at": null
    59      }
    60  }
    61  `
    62  
    63  const AggregateGetBody = `
    64  {
    65      "aggregate": {
    66              "name": "test-aggregate2",
    67              "availability_zone": "test-az",
    68              "deleted": false,
    69              "created_at": "2017-12-22T10:16:07.000000",
    70              "updated_at": null,
    71              "hosts": [
    72                  "cmp0"
    73              ],
    74              "deleted_at": null,
    75              "id": 4,
    76              "metadata": {
    77                  "availability_zone": "test-az"
    78              }
    79          }
    80  }
    81  `
    82  
    83  const AggregateUpdateBody = `
    84  {
    85      "aggregate": {
    86              "name": "test-aggregate2",
    87              "availability_zone": "nova2",
    88              "deleted": false,
    89              "created_at": "2017-12-22T10:12:06.000000",
    90              "updated_at": "2017-12-23T10:18:00.000000",
    91              "hosts": [],
    92              "deleted_at": null,
    93              "id": 1,
    94              "metadata": {
    95                  "availability_zone": "nova2"
    96              }
    97          }
    98  }
    99  `
   100  
   101  const AggregateAddHostBody = `
   102  {
   103      "aggregate": {
   104              "name": "test-aggregate2",
   105              "availability_zone": "test-az",
   106              "deleted": false,
   107              "created_at": "2017-12-22T10:16:07.000000",
   108              "updated_at": null,
   109              "hosts": [
   110                  "cmp0",
   111  				"cmp1"
   112              ],
   113              "deleted_at": null,
   114              "id": 4,
   115              "metadata": {
   116                  "availability_zone": "test-az"
   117              }
   118          }
   119  }
   120  `
   121  
   122  const AggregateRemoveHostBody = `
   123  {
   124      "aggregate": {
   125              "name": "test-aggregate2",
   126              "availability_zone": "nova2",
   127              "deleted": false,
   128              "created_at": "2017-12-22T10:12:06.000000",
   129              "updated_at": "2017-12-23T10:18:00.000000",
   130              "hosts": [],
   131              "deleted_at": null,
   132              "id": 1,
   133              "metadata": {
   134                  "availability_zone": "nova2"
   135              }
   136          }
   137  }
   138  `
   139  
   140  const AggregateSetMetadataBody = `
   141  {
   142      "aggregate": {
   143              "name": "test-aggregate2",
   144              "availability_zone": "test-az",
   145              "deleted": false,
   146              "created_at": "2017-12-22T10:16:07.000000",
   147              "updated_at": "2017-12-23T10:18:00.000000",
   148              "hosts": [
   149                  "cmp0"
   150              ],
   151              "deleted_at": null,
   152              "id": 4,
   153              "metadata": {
   154                  "availability_zone": "test-az",
   155  				"key": "value"
   156              }
   157          }
   158  }
   159  `
   160  
   161  var (
   162  	// First aggregate from the AggregateListBody
   163  	FirstFakeAggregate = aggregates.Aggregate{
   164  		AvailabilityZone: "",
   165  		Hosts:            []string{},
   166  		ID:               1,
   167  		Metadata:         map[string]string{},
   168  		Name:             "test-aggregate1",
   169  		CreatedAt:        time.Date(2017, 12, 22, 10, 12, 6, 0, time.UTC),
   170  		UpdatedAt:        time.Time{},
   171  		DeletedAt:        time.Time{},
   172  		Deleted:          false,
   173  	}
   174  
   175  	// Second aggregate from the AggregateListBody
   176  	SecondFakeAggregate = aggregates.Aggregate{
   177  		AvailabilityZone: "test-az",
   178  		Hosts:            []string{"cmp0"},
   179  		ID:               4,
   180  		Metadata:         map[string]string{"availability_zone": "test-az"},
   181  		Name:             "test-aggregate2",
   182  		CreatedAt:        time.Date(2017, 12, 22, 10, 16, 7, 0, time.UTC),
   183  		UpdatedAt:        time.Time{},
   184  		DeletedAt:        time.Time{},
   185  		Deleted:          false,
   186  	}
   187  
   188  	// Aggregate from the AggregateCreateBody
   189  	CreatedAggregate = aggregates.Aggregate{
   190  		AvailabilityZone: "london",
   191  		Hosts:            nil,
   192  		ID:               32,
   193  		Metadata:         nil,
   194  		Name:             "name",
   195  		CreatedAt:        time.Date(2016, 12, 27, 22, 51, 32, 0, time.UTC),
   196  		UpdatedAt:        time.Time{},
   197  		DeletedAt:        time.Time{},
   198  		Deleted:          false,
   199  	}
   200  
   201  	// Aggregate ID to delete
   202  	AggregateIDtoDelete = 1
   203  
   204  	// Aggregate ID to get, from the AggregateGetBody
   205  	AggregateIDtoGet = SecondFakeAggregate.ID
   206  
   207  	// Aggregate ID to update
   208  	AggregateIDtoUpdate = FirstFakeAggregate.ID
   209  
   210  	// Updated aggregate
   211  	UpdatedAggregate = aggregates.Aggregate{
   212  		AvailabilityZone: "nova2",
   213  		Hosts:            []string{},
   214  		ID:               1,
   215  		Metadata:         map[string]string{"availability_zone": "nova2"},
   216  		Name:             "test-aggregate2",
   217  		CreatedAt:        time.Date(2017, 12, 22, 10, 12, 6, 0, time.UTC),
   218  		UpdatedAt:        time.Date(2017, 12, 23, 10, 18, 0, 0, time.UTC),
   219  		DeletedAt:        time.Time{},
   220  		Deleted:          false,
   221  	}
   222  
   223  	AggregateWithAddedHost = aggregates.Aggregate{
   224  		AvailabilityZone: "test-az",
   225  		Hosts:            []string{"cmp0", "cmp1"},
   226  		ID:               4,
   227  		Metadata:         map[string]string{"availability_zone": "test-az"},
   228  		Name:             "test-aggregate2",
   229  		CreatedAt:        time.Date(2017, 12, 22, 10, 16, 7, 0, time.UTC),
   230  		UpdatedAt:        time.Time{},
   231  		DeletedAt:        time.Time{},
   232  		Deleted:          false,
   233  	}
   234  
   235  	AggregateWithRemovedHost = aggregates.Aggregate{
   236  		AvailabilityZone: "nova2",
   237  		Hosts:            []string{},
   238  		ID:               1,
   239  		Metadata:         map[string]string{"availability_zone": "nova2"},
   240  		Name:             "test-aggregate2",
   241  		CreatedAt:        time.Date(2017, 12, 22, 10, 12, 6, 0, time.UTC),
   242  		UpdatedAt:        time.Date(2017, 12, 23, 10, 18, 0, 0, time.UTC),
   243  		DeletedAt:        time.Time{},
   244  		Deleted:          false,
   245  	}
   246  
   247  	AggregateWithUpdatedMetadata = aggregates.Aggregate{
   248  		AvailabilityZone: "test-az",
   249  		Hosts:            []string{"cmp0"},
   250  		ID:               4,
   251  		Metadata:         map[string]string{"availability_zone": "test-az", "key": "value"},
   252  		Name:             "test-aggregate2",
   253  		CreatedAt:        time.Date(2017, 12, 22, 10, 16, 7, 0, time.UTC),
   254  		UpdatedAt:        time.Date(2017, 12, 23, 10, 18, 0, 0, time.UTC),
   255  		DeletedAt:        time.Time{},
   256  		Deleted:          false,
   257  	}
   258  )
   259  
   260  // HandleListSuccessfully configures the test server to respond to a List request.
   261  func HandleListSuccessfully(t *testing.T) {
   262  	th.Mux.HandleFunc("/os-aggregates", func(w http.ResponseWriter, r *http.Request) {
   263  		th.TestMethod(t, r, "GET")
   264  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   265  
   266  		w.Header().Add("Content-Type", "application/json")
   267  		fmt.Fprintf(w, AggregateListBody)
   268  	})
   269  }
   270  
   271  func HandleCreateSuccessfully(t *testing.T) {
   272  	th.Mux.HandleFunc("/os-aggregates", func(w http.ResponseWriter, r *http.Request) {
   273  		th.TestMethod(t, r, "POST")
   274  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   275  
   276  		w.Header().Add("Content-Type", "application/json")
   277  		fmt.Fprintf(w, AggregateCreateBody)
   278  	})
   279  }
   280  
   281  func HandleDeleteSuccessfully(t *testing.T) {
   282  	v := strconv.Itoa(AggregateIDtoDelete)
   283  	th.Mux.HandleFunc("/os-aggregates/"+v, func(w http.ResponseWriter, r *http.Request) {
   284  		th.TestMethod(t, r, "DELETE")
   285  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   286  
   287  		w.WriteHeader(http.StatusOK)
   288  	})
   289  }
   290  
   291  func HandleGetSuccessfully(t *testing.T) {
   292  	v := strconv.Itoa(AggregateIDtoGet)
   293  	th.Mux.HandleFunc("/os-aggregates/"+v, func(w http.ResponseWriter, r *http.Request) {
   294  		th.TestMethod(t, r, "GET")
   295  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   296  
   297  		w.Header().Add("Content-Type", "application/json")
   298  		fmt.Fprintf(w, AggregateGetBody)
   299  	})
   300  }
   301  
   302  func HandleUpdateSuccessfully(t *testing.T) {
   303  	v := strconv.Itoa(AggregateIDtoUpdate)
   304  	th.Mux.HandleFunc("/os-aggregates/"+v, func(w http.ResponseWriter, r *http.Request) {
   305  		th.TestMethod(t, r, "PUT")
   306  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   307  
   308  		w.Header().Add("Content-Type", "application/json")
   309  		fmt.Fprintf(w, AggregateUpdateBody)
   310  	})
   311  }
   312  
   313  func HandleAddHostSuccessfully(t *testing.T) {
   314  	v := strconv.Itoa(AggregateWithAddedHost.ID)
   315  	th.Mux.HandleFunc("/os-aggregates/"+v+"/action", func(w http.ResponseWriter, r *http.Request) {
   316  		th.TestMethod(t, r, "POST")
   317  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   318  
   319  		w.Header().Add("Content-Type", "application/json")
   320  		fmt.Fprintf(w, AggregateAddHostBody)
   321  	})
   322  }
   323  
   324  func HandleRemoveHostSuccessfully(t *testing.T) {
   325  	v := strconv.Itoa(AggregateWithRemovedHost.ID)
   326  	th.Mux.HandleFunc("/os-aggregates/"+v+"/action", func(w http.ResponseWriter, r *http.Request) {
   327  		th.TestMethod(t, r, "POST")
   328  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   329  
   330  		w.Header().Add("Content-Type", "application/json")
   331  		fmt.Fprintf(w, AggregateRemoveHostBody)
   332  	})
   333  }
   334  
   335  func HandleSetMetadataSuccessfully(t *testing.T) {
   336  	v := strconv.Itoa(AggregateWithUpdatedMetadata.ID)
   337  	th.Mux.HandleFunc("/os-aggregates/"+v+"/action", func(w http.ResponseWriter, r *http.Request) {
   338  		th.TestMethod(t, r, "POST")
   339  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   340  
   341  		w.Header().Add("Content-Type", "application/json")
   342  		fmt.Fprintf(w, AggregateSetMetadataBody)
   343  	})
   344  }