github.com/gophercloud/gophercloud@v1.11.0/openstack/db/v1/instances/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/gophercloud/gophercloud"
     9  	"github.com/gophercloud/gophercloud/openstack/db/v1/datastores"
    10  	"github.com/gophercloud/gophercloud/openstack/db/v1/instances"
    11  	"github.com/gophercloud/gophercloud/testhelper/fixture"
    12  )
    13  
    14  var (
    15  	timestamp  = "2015-11-12T14:22:42"
    16  	timeVal, _ = time.Parse(gophercloud.RFC3339NoZ, timestamp)
    17  )
    18  
    19  var instance = `
    20  {
    21    "created": "` + timestamp + `",
    22    "datastore": {
    23      "type": "mysql",
    24      "version": "5.6"
    25    },
    26    "flavor": {
    27      "id": "1",
    28      "links": [
    29        {
    30          "href": "https://openstack.example.com/v1.0/1234/flavors/1",
    31          "rel": "self"
    32        },
    33        {
    34          "href": "https://openstack.example.com/v1.0/1234/flavors/1",
    35          "rel": "bookmark"
    36        }
    37      ]
    38    },
    39    "links": [
    40      {
    41        "href": "https://openstack.example.com/v1.0/1234/instances/1",
    42        "rel": "self"
    43      }
    44    ],
    45    "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com",
    46    "id": "{instanceID}",
    47    "name": "json_rack_instance",
    48    "status": "BUILD",
    49    "updated": "` + timestamp + `",
    50    "volume": {
    51      "size": 2
    52    }
    53  }
    54  `
    55  
    56  var instanceGet = `
    57  {
    58    "created": "` + timestamp + `",
    59    "datastore": {
    60      "type": "mysql",
    61      "version": "5.6"
    62    },
    63    "flavor": {
    64      "id": "1",
    65      "links": [
    66        {
    67          "href": "https://openstack.example.com/v1.0/1234/flavors/1",
    68          "rel": "self"
    69        },
    70        {
    71          "href": "https://openstack.example.com/v1.0/1234/flavors/1",
    72          "rel": "bookmark"
    73        }
    74      ]
    75    },
    76    "links": [
    77      {
    78        "href": "https://openstack.example.com/v1.0/1234/instances/1",
    79        "rel": "self"
    80      }
    81    ],
    82    "id": "{instanceID}",
    83    "name": "test",
    84    "status": "ACTIVE",
    85    "operating_status": "HEALTHY",
    86    "updated": "` + timestamp + `",
    87    "volume": {
    88      "size": 1,
    89      "used": 0.12
    90    },
    91    "addresses": [
    92      {
    93        "address": "10.1.0.62",
    94        "type": "private"
    95      },
    96      {
    97        "address": "172.24.5.114",
    98        "type": "public"
    99      }
   100    ]
   101  }
   102  `
   103  
   104  var createReq = `
   105  {
   106  	"instance": {
   107  		"availability_zone": "us-east1",
   108  		"databases": [
   109  			{
   110  				"character_set": "utf8",
   111  				"collate": "utf8_general_ci",
   112  				"name": "sampledb"
   113  			},
   114  			{
   115  				"name": "nextround"
   116  			}
   117  		],
   118  		"flavorRef": "1",
   119  		"name": "json_rack_instance",
   120  		"users": [
   121  			{
   122  				"databases": [
   123  					{
   124  						"name": "sampledb"
   125  					}
   126  				],
   127  				"name": "demouser",
   128  				"password": "demopassword"
   129  			}
   130  		],
   131  		"volume": {
   132  			"size": 2,
   133  			"type": "ssd"
   134  		}
   135  	}
   136  }
   137  `
   138  
   139  var instanceWithFault = `
   140  {
   141    "created": "` + timestamp + `",
   142    "datastore": {
   143      "type": "mysql",
   144      "version": "5.6"
   145    },
   146    "flavor": {
   147      "id": "1",
   148      "links": [
   149        {
   150          "href": "https://openstack.example.com/v1.0/1234/flavors/1",
   151          "rel": "self"
   152        },
   153        {
   154          "href": "https://openstack.example.com/v1.0/1234/flavors/1",
   155          "rel": "bookmark"
   156        }
   157      ]
   158    },
   159    "links": [
   160      {
   161        "href": "https://openstack.example.com/v1.0/1234/instances/1",
   162        "rel": "self"
   163      }
   164    ],
   165    "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com",
   166    "id": "{instanceID}",
   167    "name": "json_rack_instance",
   168    "status": "BUILD",
   169    "updated": "` + timestamp + `",
   170    "volume": {
   171      "size": 2
   172    },
   173    "fault": {
   174      "message": "some error message",
   175      "created": "` + timestamp + `",
   176      "details": "some details about the error"
   177    }
   178  }
   179  `
   180  
   181  var (
   182  	instanceID    = "{instanceID}"
   183  	configGroupID = "00000000-0000-0000-0000-000000000000"
   184  	rootURL       = "/instances"
   185  	resURL        = rootURL + "/" + instanceID
   186  	uRootURL      = resURL + "/root"
   187  	aURL          = resURL + "/action"
   188  )
   189  
   190  var (
   191  	restartReq                  = `{"restart": {}}`
   192  	resizeReq                   = `{"resize": {"flavorRef": "2"}}`
   193  	resizeVolReq                = `{"resize": {"volume": {"size": 4}}}`
   194  	attachConfigurationGroupReq = `{"instance": {"configuration": "00000000-0000-0000-0000-000000000000"}}`
   195  	detachConfigurationGroupReq = `{"instance": {}}`
   196  )
   197  
   198  var (
   199  	createResp          = fmt.Sprintf(`{"instance": %s}`, instance)
   200  	createWithFaultResp = fmt.Sprintf(`{"instance": %s}`, instanceWithFault)
   201  	listInstancesResp   = fmt.Sprintf(`{"instances":[%s]}`, instance)
   202  	getInstanceResp     = fmt.Sprintf(`{"instance": %s}`, instanceGet)
   203  	enableUserResp      = `{"user":{"name":"root","password":"secretsecret"}}`
   204  	isUserEnabledResp   = `{"rootEnabled":true}`
   205  )
   206  
   207  var expectedInstance = instances.Instance{
   208  	Created: timeVal,
   209  	Updated: timeVal,
   210  	Flavor: instances.Flavor{
   211  		ID: "1",
   212  		Links: []gophercloud.Link{
   213  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
   214  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"},
   215  		},
   216  	},
   217  	Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com",
   218  	ID:       instanceID,
   219  	Links: []gophercloud.Link{
   220  		{Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"},
   221  	},
   222  	Name:   "json_rack_instance",
   223  	Status: "BUILD",
   224  	Volume: instances.Volume{Size: 2},
   225  	Datastore: datastores.DatastorePartial{
   226  		Type:    "mysql",
   227  		Version: "5.6",
   228  	},
   229  }
   230  
   231  var expectedGetInstance = instances.Instance{
   232  	Created: timeVal,
   233  	Updated: timeVal,
   234  	Flavor: instances.Flavor{
   235  		ID: "1",
   236  		Links: []gophercloud.Link{
   237  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
   238  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"},
   239  		},
   240  	},
   241  	ID: instanceID,
   242  	Links: []gophercloud.Link{
   243  		{Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"},
   244  	},
   245  	Name:   "test",
   246  	Status: "ACTIVE",
   247  	Volume: instances.Volume{Size: 1, Used: 0.12},
   248  	Datastore: datastores.DatastorePartial{
   249  		Type:    "mysql",
   250  		Version: "5.6",
   251  	},
   252  	Addresses: []instances.Address{
   253  		{Type: "private", Address: "10.1.0.62"},
   254  		{Type: "public", Address: "172.24.5.114"},
   255  	},
   256  }
   257  
   258  var expectedInstanceWithFault = instances.Instance{
   259  	Created: timeVal,
   260  	Updated: timeVal,
   261  	Flavor: instances.Flavor{
   262  		ID: "1",
   263  		Links: []gophercloud.Link{
   264  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
   265  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"},
   266  		},
   267  	},
   268  	Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com",
   269  	ID:       instanceID,
   270  	Links: []gophercloud.Link{
   271  		{Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"},
   272  	},
   273  	Name:   "json_rack_instance",
   274  	Status: "BUILD",
   275  	Volume: instances.Volume{Size: 2},
   276  	Datastore: datastores.DatastorePartial{
   277  		Type:    "mysql",
   278  		Version: "5.6",
   279  	},
   280  	Fault: &instances.Fault{
   281  		Created: timeVal,
   282  		Message: "some error message",
   283  		Details: "some details about the error",
   284  	},
   285  }
   286  
   287  func HandleCreate(t *testing.T) {
   288  	fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
   289  }
   290  
   291  func HandleCreateWithFault(t *testing.T) {
   292  	fixture.SetupHandler(t, rootURL, "POST", createReq, createWithFaultResp, 200)
   293  }
   294  
   295  func HandleList(t *testing.T) {
   296  	fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
   297  }
   298  
   299  func HandleGet(t *testing.T) {
   300  	fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
   301  }
   302  
   303  func HandleDelete(t *testing.T) {
   304  	fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
   305  }
   306  
   307  func HandleEnableRoot(t *testing.T) {
   308  	fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
   309  }
   310  
   311  func HandleIsRootEnabled(t *testing.T) {
   312  	fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
   313  }
   314  
   315  func HandleRestart(t *testing.T) {
   316  	fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
   317  }
   318  
   319  func HandleResize(t *testing.T) {
   320  	fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
   321  }
   322  
   323  func HandleResizeVol(t *testing.T) {
   324  	fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
   325  }
   326  
   327  func HandleAttachConfigurationGroup(t *testing.T) {
   328  	fixture.SetupHandler(t, resURL, "PUT", attachConfigurationGroupReq, "", 202)
   329  }
   330  
   331  func HandleDetachConfigurationGroup(t *testing.T) {
   332  	fixture.SetupHandler(t, resURL, "PUT", detachConfigurationGroupReq, "", 202)
   333  }