github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/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/vnpaycloud-console/gophercloud/v2"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/db/v1/datastores"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/db/v1/instances"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/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  		"configuration": "4a78b397-c355-4127-be45-56230b2ab74e",
   109  		"databases": [
   110  			{
   111  				"character_set": "utf8",
   112  				"collate": "utf8_general_ci",
   113  				"name": "sampledb"
   114  			},
   115  			{
   116  				"name": "nextround"
   117  			}
   118  		],
   119  		"flavorRef": "1",
   120  		"name": "json_rack_instance",
   121  		"users": [
   122  			{
   123  				"databases": [
   124  					{
   125  						"name": "sampledb"
   126  					}
   127  				],
   128  				"name": "demouser",
   129  				"password": "demopassword"
   130  			}
   131  		],
   132  		"volume": {
   133  			"size": 2,
   134  			"type": "ssd"
   135  		}
   136  	}
   137  }
   138  `
   139  
   140  var instanceWithFault = `
   141  {
   142    "created": "` + timestamp + `",
   143    "datastore": {
   144      "type": "mysql",
   145      "version": "5.6"
   146    },
   147    "flavor": {
   148      "id": "1",
   149      "links": [
   150        {
   151          "href": "https://openstack.example.com/v1.0/1234/flavors/1",
   152          "rel": "self"
   153        },
   154        {
   155          "href": "https://openstack.example.com/v1.0/1234/flavors/1",
   156          "rel": "bookmark"
   157        }
   158      ]
   159    },
   160    "links": [
   161      {
   162        "href": "https://openstack.example.com/v1.0/1234/instances/1",
   163        "rel": "self"
   164      }
   165    ],
   166    "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com",
   167    "id": "{instanceID}",
   168    "name": "json_rack_instance",
   169    "status": "BUILD",
   170    "updated": "` + timestamp + `",
   171    "volume": {
   172      "size": 2
   173    },
   174    "fault": {
   175      "message": "some error message",
   176      "created": "` + timestamp + `",
   177      "details": "some details about the error"
   178    }
   179  }
   180  `
   181  
   182  var (
   183  	instanceID    = "{instanceID}"
   184  	configGroupID = "00000000-0000-0000-0000-000000000000"
   185  	rootURL       = "/instances"
   186  	resURL        = rootURL + "/" + instanceID
   187  	uRootURL      = resURL + "/root"
   188  	aURL          = resURL + "/action"
   189  )
   190  
   191  var (
   192  	restartReq                  = `{"restart": {}}`
   193  	resizeReq                   = `{"resize": {"flavorRef": "2"}}`
   194  	resizeVolReq                = `{"resize": {"volume": {"size": 4}}}`
   195  	attachConfigurationGroupReq = `{"instance": {"configuration": "00000000-0000-0000-0000-000000000000"}}`
   196  	detachConfigurationGroupReq = `{"instance": {}}`
   197  )
   198  
   199  var (
   200  	createResp          = fmt.Sprintf(`{"instance": %s}`, instance)
   201  	createWithFaultResp = fmt.Sprintf(`{"instance": %s}`, instanceWithFault)
   202  	listInstancesResp   = fmt.Sprintf(`{"instances":[%s]}`, instance)
   203  	getInstanceResp     = fmt.Sprintf(`{"instance": %s}`, instanceGet)
   204  	enableUserResp      = `{"user":{"name":"root","password":"secretsecret"}}`
   205  	isUserEnabledResp   = `{"rootEnabled":true}`
   206  )
   207  
   208  var expectedInstance = instances.Instance{
   209  	Created: timeVal,
   210  	Updated: timeVal,
   211  	Flavor: instances.Flavor{
   212  		ID: "1",
   213  		Links: []gophercloud.Link{
   214  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
   215  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"},
   216  		},
   217  	},
   218  	Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com",
   219  	ID:       instanceID,
   220  	Links: []gophercloud.Link{
   221  		{Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"},
   222  	},
   223  	Name:   "json_rack_instance",
   224  	Status: "BUILD",
   225  	Volume: instances.Volume{Size: 2},
   226  	Datastore: datastores.DatastorePartial{
   227  		Type:    "mysql",
   228  		Version: "5.6",
   229  	},
   230  }
   231  
   232  var expectedGetInstance = instances.Instance{
   233  	Created: timeVal,
   234  	Updated: timeVal,
   235  	Flavor: instances.Flavor{
   236  		ID: "1",
   237  		Links: []gophercloud.Link{
   238  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
   239  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"},
   240  		},
   241  	},
   242  	ID: instanceID,
   243  	Links: []gophercloud.Link{
   244  		{Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"},
   245  	},
   246  	Name:   "test",
   247  	Status: "ACTIVE",
   248  	Volume: instances.Volume{Size: 1, Used: 0.12},
   249  	Datastore: datastores.DatastorePartial{
   250  		Type:    "mysql",
   251  		Version: "5.6",
   252  	},
   253  	Addresses: []instances.Address{
   254  		{Type: "private", Address: "10.1.0.62"},
   255  		{Type: "public", Address: "172.24.5.114"},
   256  	},
   257  }
   258  
   259  var expectedInstanceWithFault = instances.Instance{
   260  	Created: timeVal,
   261  	Updated: timeVal,
   262  	Flavor: instances.Flavor{
   263  		ID: "1",
   264  		Links: []gophercloud.Link{
   265  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"},
   266  			{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "bookmark"},
   267  		},
   268  	},
   269  	Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.openstack.example.com",
   270  	ID:       instanceID,
   271  	Links: []gophercloud.Link{
   272  		{Href: "https://openstack.example.com/v1.0/1234/instances/1", Rel: "self"},
   273  	},
   274  	Name:   "json_rack_instance",
   275  	Status: "BUILD",
   276  	Volume: instances.Volume{Size: 2},
   277  	Datastore: datastores.DatastorePartial{
   278  		Type:    "mysql",
   279  		Version: "5.6",
   280  	},
   281  	Fault: &instances.Fault{
   282  		Created: timeVal,
   283  		Message: "some error message",
   284  		Details: "some details about the error",
   285  	},
   286  }
   287  
   288  func HandleCreate(t *testing.T) {
   289  	fixture.SetupHandler(t, rootURL, "POST", createReq, createResp, 200)
   290  }
   291  
   292  func HandleCreateWithFault(t *testing.T) {
   293  	fixture.SetupHandler(t, rootURL, "POST", createReq, createWithFaultResp, 200)
   294  }
   295  
   296  func HandleList(t *testing.T) {
   297  	fixture.SetupHandler(t, rootURL, "GET", "", listInstancesResp, 200)
   298  }
   299  
   300  func HandleGet(t *testing.T) {
   301  	fixture.SetupHandler(t, resURL, "GET", "", getInstanceResp, 200)
   302  }
   303  
   304  func HandleDelete(t *testing.T) {
   305  	fixture.SetupHandler(t, resURL, "DELETE", "", "", 202)
   306  }
   307  
   308  func HandleEnableRoot(t *testing.T) {
   309  	fixture.SetupHandler(t, uRootURL, "POST", "", enableUserResp, 200)
   310  }
   311  
   312  func HandleIsRootEnabled(t *testing.T) {
   313  	fixture.SetupHandler(t, uRootURL, "GET", "", isUserEnabledResp, 200)
   314  }
   315  
   316  func HandleRestart(t *testing.T) {
   317  	fixture.SetupHandler(t, aURL, "POST", restartReq, "", 202)
   318  }
   319  
   320  func HandleResize(t *testing.T) {
   321  	fixture.SetupHandler(t, aURL, "POST", resizeReq, "", 202)
   322  }
   323  
   324  func HandleResizeVol(t *testing.T) {
   325  	fixture.SetupHandler(t, aURL, "POST", resizeVolReq, "", 202)
   326  }
   327  
   328  func HandleAttachConfigurationGroup(t *testing.T) {
   329  	fixture.SetupHandler(t, resURL, "PUT", attachConfigurationGroupReq, "", 202)
   330  }
   331  
   332  func HandleDetachConfigurationGroup(t *testing.T) {
   333  	fixture.SetupHandler(t, resURL, "PUT", detachConfigurationGroupReq, "", 202)
   334  }