github.com/gophercloud/gophercloud@v1.11.0/openstack/clustering/v1/policies/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/clustering/v1/policies"
    10  	th "github.com/gophercloud/gophercloud/testhelper"
    11  	fake "github.com/gophercloud/gophercloud/testhelper/client"
    12  )
    13  
    14  const PolicyListBody1 = `
    15  {
    16    "policies": [
    17      {
    18        "created_at": "2018-04-02T21:43:30.000000",
    19        "data": {},
    20        "domain": null,
    21        "id": "PolicyListBodyID1",
    22        "name": "delpol",
    23        "project": "018cd0909fb44cd5bc9b7a3cd664920e",
    24        "spec": {
    25          "description": "A policy for choosing victim node(s) from a cluster for deletion.",
    26          "properties": {
    27            "criteria": "OLDEST_FIRST",
    28            "destroy_after_deletion": true,
    29            "grace_period": 60,
    30            "reduce_desired_capacity": false
    31          },
    32          "type": "senlin.policy.deletion",
    33          "version": 1
    34        },
    35        "type": "senlin.policy.deletion-1.0",
    36        "updated_at": "2018-04-02T00:19:12Z",
    37        "user": "fe43e41739154b72818565e0d2580819"
    38      }
    39    ]
    40  }
    41  `
    42  
    43  const PolicyListBody2 = `
    44  {
    45    "policies": [
    46      {
    47        "created_at": "2018-04-02T22:29:36.000000",
    48        "data": {},
    49        "domain": null,
    50        "id": "PolicyListBodyID2",
    51        "name": "delpol2",
    52        "project": "018cd0909fb44cd5bc9b7a3cd664920e",
    53        "spec": {
    54          "description": "A policy for choosing victim node(s) from a cluster for deletion.",
    55          "properties": {
    56            "criteria": "OLDEST_FIRST",
    57            "destroy_after_deletion": true,
    58            "grace_period": 60,
    59            "reduce_desired_capacity": false
    60          },
    61          "type": "senlin.policy.deletion",
    62          "version": "1.0"
    63        },
    64        "type": "senlin.policy.deletion-1.0",
    65        "updated_at": "2018-04-02T23:15:11.000000",
    66        "user": "fe43e41739154b72818565e0d2580819"
    67      }
    68    ]
    69  }
    70  `
    71  
    72  const PolicyCreateBody = `
    73  {
    74    "policy": {
    75      "created_at": "2018-04-04T00:18:36Z",
    76      "data": {},
    77      "domain": null,
    78      "id": "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
    79      "name": "delpol4",
    80      "project": "018cd0909fb44cd5bc9b7a3cd664920e",
    81      "spec": {
    82        "description": "A policy for choosing victim node(s) from a cluster for deletion.",
    83        "properties": {
    84          "hooks": {
    85            "params": {
    86              "queue": "zaqar_queue_name"
    87            },
    88            "timeout": 180,
    89            "type": "zaqar"
    90          }
    91        },
    92        "type": "senlin.policy.deletion",
    93        "version": 1.1
    94      },
    95      "type": "senlin.policy.deletion-1.1",
    96      "updated_at": null,
    97      "user": "fe43e41739154b72818565e0d2580819"
    98    }
    99  }
   100  `
   101  
   102  const PolicyGetBody = `
   103  {
   104    "policy": {
   105        "created_at": "2018-04-02T21:43:30.000000",
   106        "data": {},
   107        "domain": null,
   108        "id": "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
   109        "name": "delpol",
   110        "project": "018cd0909fb44cd5bc9b7a3cd664920e",
   111        "spec": {
   112          "description": "A policy for choosing victim node(s) from a cluster for deletion.",
   113          "properties": {
   114            "criteria": "OLDEST_FIRST",
   115            "destroy_after_deletion": true,
   116            "grace_period": 60,
   117            "reduce_desired_capacity": false
   118          },
   119          "type": "senlin.policy.deletion",
   120          "version": 1
   121        },
   122        "type": "senlin.policy.deletion-1.0",
   123        "updated_at": "2018-04-02T00:19:12Z",
   124        "user": "fe43e41739154b72818565e0d2580819"
   125    }
   126  }
   127  `
   128  
   129  const PolicyUpdateBody = `
   130  {
   131    "policy": {
   132      "created_at": "2018-04-02T21:43:30.000000",
   133      "data": {},
   134      "domain": null,
   135      "id": "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
   136      "name": "delpol4",
   137      "project": "018cd0909fb44cd5bc9b7a3cd664920e",
   138      "spec": {
   139        "description": "A policy for choosing victim node(s) from a cluster for deletion.",
   140        "properties": {
   141          "hooks": {
   142            "params": {
   143              "queue": "zaqar_queue_name"
   144            },
   145            "timeout": 180,
   146            "type": "zaqar"
   147          }
   148        },
   149        "type": "senlin.policy.deletion",
   150        "version": 1.1
   151      },
   152      "type": "senlin.policy.deletion-1.1",
   153      "updated_at": null,
   154      "user": "fe43e41739154b72818565e0d2580819"
   155    }
   156  }
   157  `
   158  
   159  const PolicyBadUpdateBody = `
   160  {
   161    "policy": {
   162      "created_at": "invalid",
   163      "data": {},
   164      "domain": null,
   165      "id": "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
   166      "name": "delpol4",
   167      "project": "018cd0909fb44cd5bc9b7a3cd664920e",
   168      "spec": {
   169        "description": "A policy for choosing victim node(s) from a cluster for deletion.",
   170        "properties": {
   171          "hooks": {
   172            "params": {
   173              "queue": "zaqar_queue_name"
   174            },
   175            "timeout": 180,
   176            "type": "zaqar"
   177          }
   178        },
   179        "type": "senlin.policy.deletion",
   180        "version": 1.1
   181      },
   182      "type": "invalid",
   183      "updated_at": null,
   184      "user": "fe43e41739154b72818565e0d2580819"
   185    }
   186  }
   187  `
   188  
   189  const PolicyValidateBody = `
   190  {
   191    "policy": {
   192      "created_at": "2018-04-02T21:43:30.000000",
   193      "data": {},
   194      "domain": null,
   195      "id": "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
   196      "name": "delpol4",
   197      "project": "018cd0909fb44cd5bc9b7a3cd664920e",
   198      "spec": {
   199        "description": "A policy for choosing victim node(s) from a cluster for deletion.",
   200        "properties": {
   201          "hooks": {
   202            "params": {
   203              "queue": "zaqar_queue_name"
   204            },
   205            "timeout": 180,
   206            "type": "zaqar"
   207          }
   208        },
   209        "type": "senlin.policy.deletion",
   210        "version": 1.1
   211      },
   212      "type": "senlin.policy.deletion-1.1",
   213      "updated_at": null,
   214      "user": "fe43e41739154b72818565e0d2580819"
   215    }
   216  }
   217  `
   218  
   219  const PolicyBadValidateBody = `
   220  {
   221    "policy": {
   222      "created_at": "invalid",
   223      "data": {},
   224      "domain": null,
   225      "id": "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
   226      "name": "delpol4",
   227      "project": "018cd0909fb44cd5bc9b7a3cd664920e",
   228      "spec": {
   229        "description": "A policy for choosing victim node(s) from a cluster for deletion.",
   230        "properties": {
   231          "hooks": {
   232            "params": {
   233              "queue": "zaqar_queue_name"
   234            },
   235            "timeout": 180,
   236            "type": "zaqar"
   237          }
   238        },
   239        "type": "senlin.policy.deletion",
   240        "version": 1.1
   241      },
   242      "type": "invalid",
   243      "updated_at": null,
   244      "user": "fe43e41739154b72818565e0d2580819"
   245    }
   246  }
   247  `
   248  
   249  const PolicyDeleteRequestID = "req-7328d1b0-9945-456f-b2cd-5166b77d14a8"
   250  const PolicyIDtoUpdate = "b99b3ab4-3aa6-4fba-b827-69b88b9c544a"
   251  const PolicyIDtoGet = "b99b3ab4-3aa6-4fba-b827-69b88b9c544a"
   252  const PolicyIDtoDelete = "1"
   253  
   254  var ExpectedPolicy1 = policies.Policy{
   255  	CreatedAt: time.Date(2018, 4, 2, 21, 43, 30, 0, time.UTC),
   256  	Data:      map[string]interface{}{},
   257  	Domain:    "",
   258  	ID:        "PolicyListBodyID1",
   259  	Name:      "delpol",
   260  	Project:   "018cd0909fb44cd5bc9b7a3cd664920e",
   261  
   262  	Spec: policies.Spec{
   263  		Description: "A policy for choosing victim node(s) from a cluster for deletion.",
   264  		Properties: map[string]interface{}{
   265  			"criteria":                "OLDEST_FIRST",
   266  			"destroy_after_deletion":  true,
   267  			"grace_period":            float64(60),
   268  			"reduce_desired_capacity": false,
   269  		},
   270  		Type:    "senlin.policy.deletion",
   271  		Version: "1.0",
   272  	},
   273  	Type:      "senlin.policy.deletion-1.0",
   274  	User:      "fe43e41739154b72818565e0d2580819",
   275  	UpdatedAt: time.Date(2018, 4, 2, 0, 19, 12, 0, time.UTC),
   276  }
   277  
   278  var ExpectedPolicy2 = policies.Policy{
   279  	CreatedAt: time.Date(2018, 4, 2, 22, 29, 36, 0, time.UTC),
   280  	Data:      map[string]interface{}{},
   281  	Domain:    "",
   282  	ID:        "PolicyListBodyID2",
   283  	Name:      "delpol2",
   284  	Project:   "018cd0909fb44cd5bc9b7a3cd664920e",
   285  
   286  	Spec: policies.Spec{
   287  		Description: "A policy for choosing victim node(s) from a cluster for deletion.",
   288  		Properties: map[string]interface{}{
   289  			"criteria":                "OLDEST_FIRST",
   290  			"destroy_after_deletion":  true,
   291  			"grace_period":            float64(60),
   292  			"reduce_desired_capacity": false,
   293  		},
   294  		Type:    "senlin.policy.deletion",
   295  		Version: "1.0",
   296  	},
   297  	Type:      "senlin.policy.deletion-1.0",
   298  	User:      "fe43e41739154b72818565e0d2580819",
   299  	UpdatedAt: time.Date(2018, 4, 2, 23, 15, 11, 0, time.UTC),
   300  }
   301  
   302  var ExpectedPolicies = [][]policies.Policy{
   303  	{ExpectedPolicy1},
   304  	{ExpectedPolicy2},
   305  }
   306  
   307  var ExpectedCreatePolicy = policies.Policy{
   308  	CreatedAt: time.Date(2018, 4, 4, 0, 18, 36, 0, time.UTC),
   309  	Data:      map[string]interface{}{},
   310  	Domain:    "",
   311  	ID:        "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
   312  	Name:      "delpol4",
   313  	Project:   "018cd0909fb44cd5bc9b7a3cd664920e",
   314  
   315  	Spec: policies.Spec{
   316  		Description: "A policy for choosing victim node(s) from a cluster for deletion.",
   317  		Properties: map[string]interface{}{
   318  			"hooks": map[string]interface{}{
   319  				"params": map[string]interface{}{
   320  					"queue": "zaqar_queue_name",
   321  				},
   322  				"timeout": float64(180),
   323  				"type":    "zaqar",
   324  			},
   325  		},
   326  		Type:    "senlin.policy.deletion",
   327  		Version: "1.1",
   328  	},
   329  	Type: "senlin.policy.deletion-1.1",
   330  	User: "fe43e41739154b72818565e0d2580819",
   331  }
   332  
   333  var ExpectedGetPolicy = policies.Policy{
   334  	CreatedAt: time.Date(2018, 4, 2, 21, 43, 30, 0, time.UTC),
   335  	Data:      map[string]interface{}{},
   336  	Domain:    "",
   337  	ID:        "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
   338  	Name:      "delpol",
   339  	Project:   "018cd0909fb44cd5bc9b7a3cd664920e",
   340  
   341  	Spec: policies.Spec{
   342  		Description: "A policy for choosing victim node(s) from a cluster for deletion.",
   343  		Properties: map[string]interface{}{
   344  			"criteria":                "OLDEST_FIRST",
   345  			"destroy_after_deletion":  true,
   346  			"grace_period":            float64(60),
   347  			"reduce_desired_capacity": false,
   348  		},
   349  		Type:    "senlin.policy.deletion",
   350  		Version: "1.0",
   351  	},
   352  	Type:      "senlin.policy.deletion-1.0",
   353  	User:      "fe43e41739154b72818565e0d2580819",
   354  	UpdatedAt: time.Date(2018, 4, 2, 0, 19, 12, 0, time.UTC),
   355  }
   356  
   357  var ExpectedUpdatePolicy = policies.Policy{
   358  	CreatedAt: time.Date(2018, 4, 2, 21, 43, 30, 0, time.UTC),
   359  	Data:      map[string]interface{}{},
   360  	Domain:    "",
   361  	ID:        "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
   362  	Name:      "delpol4",
   363  	Project:   "018cd0909fb44cd5bc9b7a3cd664920e",
   364  
   365  	Spec: policies.Spec{
   366  		Description: "A policy for choosing victim node(s) from a cluster for deletion.",
   367  		Properties: map[string]interface{}{
   368  			"hooks": map[string]interface{}{
   369  				"params": map[string]interface{}{
   370  					"queue": "zaqar_queue_name",
   371  				},
   372  				"timeout": float64(180),
   373  				"type":    "zaqar",
   374  			},
   375  		},
   376  		Type:    "senlin.policy.deletion",
   377  		Version: "1.1",
   378  	},
   379  	Type: "senlin.policy.deletion-1.1",
   380  	User: "fe43e41739154b72818565e0d2580819",
   381  }
   382  
   383  var ExpectedValidatePolicy = policies.Policy{
   384  	CreatedAt: time.Date(2018, 4, 2, 21, 43, 30, 0, time.UTC),
   385  	Data:      map[string]interface{}{},
   386  	Domain:    "",
   387  	ID:        "b99b3ab4-3aa6-4fba-b827-69b88b9c544a",
   388  	Name:      "delpol4",
   389  	Project:   "018cd0909fb44cd5bc9b7a3cd664920e",
   390  
   391  	Spec: policies.Spec{
   392  		Description: "A policy for choosing victim node(s) from a cluster for deletion.",
   393  		Properties: map[string]interface{}{
   394  			"hooks": map[string]interface{}{
   395  				"params": map[string]interface{}{
   396  					"queue": "zaqar_queue_name",
   397  				},
   398  				"timeout": float64(180),
   399  				"type":    "zaqar",
   400  			},
   401  		},
   402  		Type:    "senlin.policy.deletion",
   403  		Version: "1.1",
   404  	},
   405  	Type: "senlin.policy.deletion-1.1",
   406  	User: "fe43e41739154b72818565e0d2580819",
   407  }
   408  
   409  func HandlePolicyList(t *testing.T) {
   410  	th.Mux.HandleFunc("/v1/policies", func(w http.ResponseWriter, r *http.Request) {
   411  		th.TestMethod(t, r, "GET")
   412  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   413  
   414  		w.Header().Add("Content-Type", "application/json")
   415  		w.WriteHeader(http.StatusOK)
   416  
   417  		r.ParseForm()
   418  		marker := r.Form.Get("marker")
   419  		switch marker {
   420  		case "":
   421  			fmt.Fprintf(w, PolicyListBody1)
   422  		case "PolicyListBodyID1":
   423  			fmt.Fprintf(w, PolicyListBody2)
   424  		case "PolicyListBodyID2":
   425  			fmt.Fprintf(w, `{"policies":[]}`)
   426  		default:
   427  			t.Fatalf("Unexpected marker: [%s]", marker)
   428  		}
   429  	})
   430  }
   431  
   432  func HandlePolicyCreate(t *testing.T) {
   433  	th.Mux.HandleFunc("/v1/policies", func(w http.ResponseWriter, r *http.Request) {
   434  		th.TestMethod(t, r, "POST")
   435  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   436  
   437  		w.Header().Add("Content-Type", "application/json")
   438  		w.WriteHeader(http.StatusCreated)
   439  
   440  		fmt.Fprintf(w, PolicyCreateBody)
   441  	})
   442  }
   443  
   444  func HandlePolicyDelete(t *testing.T) {
   445  	th.Mux.HandleFunc("/v1/policies/"+PolicyIDtoDelete, func(w http.ResponseWriter, r *http.Request) {
   446  		th.TestMethod(t, r, "DELETE")
   447  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   448  
   449  		w.Header().Add("X-OpenStack-Request-Id", PolicyDeleteRequestID)
   450  		w.WriteHeader(http.StatusNoContent)
   451  	})
   452  }
   453  
   454  func HandlePolicyGet(t *testing.T) {
   455  	th.Mux.HandleFunc("/v1/policies/"+PolicyIDtoGet,
   456  		func(w http.ResponseWriter, r *http.Request) {
   457  			th.TestMethod(t, r, "GET")
   458  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   459  
   460  			w.Header().Add("Content-Type", "application/json")
   461  			w.WriteHeader(http.StatusOK)
   462  
   463  			fmt.Fprintf(w, PolicyGetBody)
   464  		})
   465  }
   466  
   467  func HandlePolicyUpdate(t *testing.T) {
   468  	th.Mux.HandleFunc("/v1/policies/"+PolicyIDtoUpdate, func(w http.ResponseWriter, r *http.Request) {
   469  		th.TestMethod(t, r, "PATCH")
   470  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   471  
   472  		w.Header().Add("Content-Type", "application/json")
   473  		w.WriteHeader(http.StatusOK)
   474  
   475  		fmt.Fprintf(w, PolicyUpdateBody)
   476  	})
   477  }
   478  
   479  func HandleBadPolicyUpdate(t *testing.T) {
   480  	th.Mux.HandleFunc("/v1/policies/"+PolicyIDtoUpdate, func(w http.ResponseWriter, r *http.Request) {
   481  		th.TestMethod(t, r, "PATCH")
   482  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   483  
   484  		w.Header().Add("Content-Type", "application/json")
   485  		w.WriteHeader(http.StatusOK)
   486  
   487  		fmt.Fprintf(w, PolicyBadUpdateBody)
   488  	})
   489  }
   490  
   491  func HandlePolicyValidate(t *testing.T) {
   492  	th.Mux.HandleFunc("/v1/policies/validate", func(w http.ResponseWriter, r *http.Request) {
   493  		th.TestMethod(t, r, "POST")
   494  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   495  
   496  		w.Header().Add("Content-Type", "application/json")
   497  		w.WriteHeader(http.StatusOK)
   498  
   499  		fmt.Fprintf(w, PolicyValidateBody)
   500  	})
   501  }
   502  
   503  func HandleBadPolicyValidate(t *testing.T) {
   504  	th.Mux.HandleFunc("/v1/policies/validate", func(w http.ResponseWriter, r *http.Request) {
   505  		th.TestMethod(t, r, "POST")
   506  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   507  
   508  		w.Header().Add("Content-Type", "application/json")
   509  		w.WriteHeader(http.StatusOK)
   510  
   511  		fmt.Fprintf(w, PolicyBadValidateBody)
   512  	})
   513  }