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

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strconv"
     7  	"strings"
     8  	"testing"
     9  
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  	fakeclient "github.com/huaweicloud/golangsdk/testhelper/client"
    12  )
    13  
    14  type imageEntry struct {
    15  	ID   string
    16  	JSON string
    17  }
    18  
    19  // HandleImageListSuccessfully test setup
    20  func HandleImageListSuccessfully(t *testing.T) {
    21  
    22  	images := make([]imageEntry, 3)
    23  
    24  	images[0] = imageEntry{"cirros-0.3.4-x86_64-uec",
    25  		`{
    26              "status": "active",
    27              "name": "cirros-0.3.4-x86_64-uec",
    28              "tags": [],
    29              "kernel_id": "e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4",
    30              "container_format": "ami",
    31              "created_at": "2015-07-15T11:43:35Z",
    32              "ramdisk_id": "8c64f48a-45a3-4eaa-adff-a8106b6c005b",
    33              "disk_format": "ami",
    34              "updated_at": "2015-07-15T11:43:35Z",
    35              "visibility": "public",
    36              "self": "/v2/images/07aa21a9-fa1a-430e-9a33-185be5982431",
    37              "min_disk": 0,
    38              "protected": false,
    39              "id": "07aa21a9-fa1a-430e-9a33-185be5982431",
    40              "size": 25165824,
    41              "file": "/v2/images/07aa21a9-fa1a-430e-9a33-185be5982431/file",
    42              "checksum": "eb9139e4942121f22bbc2afc0400b2a4",
    43              "owner": "cba624273b8344e59dd1fd18685183b0",
    44              "virtual_size": null,
    45              "min_ram": 0,
    46              "schema": "/v2/schemas/image",
    47              "hw_disk_bus": "scsi",
    48              "hw_disk_bus_model": "virtio-scsi",
    49              "hw_scsi_model": "virtio-scsi"
    50          }`}
    51  	images[1] = imageEntry{"cirros-0.3.4-x86_64-uec-ramdisk",
    52  		`{
    53              "status": "active",
    54              "name": "cirros-0.3.4-x86_64-uec-ramdisk",
    55              "tags": [],
    56              "container_format": "ari",
    57              "created_at": "2015-07-15T11:43:32Z",
    58              "size": 3740163,
    59              "disk_format": "ari",
    60              "updated_at": "2015-07-15T11:43:32Z",
    61              "visibility": "public",
    62              "self": "/v2/images/8c64f48a-45a3-4eaa-adff-a8106b6c005b",
    63              "min_disk": 0,
    64              "protected": false,
    65              "id": "8c64f48a-45a3-4eaa-adff-a8106b6c005b",
    66              "file": "/v2/images/8c64f48a-45a3-4eaa-adff-a8106b6c005b/file",
    67              "checksum": "be575a2b939972276ef675752936977f",
    68              "owner": "cba624273b8344e59dd1fd18685183b0",
    69              "virtual_size": null,
    70              "min_ram": 0,
    71              "schema": "/v2/schemas/image",
    72              "hw_disk_bus": "scsi",
    73              "hw_disk_bus_model": "virtio-scsi",
    74              "hw_scsi_model": "virtio-scsi"
    75          }`}
    76  	images[2] = imageEntry{"cirros-0.3.4-x86_64-uec-kernel",
    77  		`{
    78              "status": "active",
    79              "name": "cirros-0.3.4-x86_64-uec-kernel",
    80              "tags": [],
    81              "container_format": "aki",
    82              "created_at": "2015-07-15T11:43:29Z",
    83              "size": 4979632,
    84              "disk_format": "aki",
    85              "updated_at": "2015-07-15T11:43:30Z",
    86              "visibility": "public",
    87              "self": "/v2/images/e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4",
    88              "min_disk": 0,
    89              "protected": false,
    90              "id": "e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4",
    91              "file": "/v2/images/e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4/file",
    92              "checksum": "8a40c862b5735975d82605c1dd395796",
    93              "owner": "cba624273b8344e59dd1fd18685183b0",
    94              "virtual_size": null,
    95              "min_ram": 0,
    96              "schema": "/v2/schemas/image",
    97              "hw_disk_bus": "scsi",
    98              "hw_disk_bus_model": "virtio-scsi",
    99              "hw_scsi_model": "virtio-scsi"
   100          }`}
   101  
   102  	th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
   103  		th.TestMethod(t, r, "GET")
   104  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
   105  
   106  		w.Header().Add("Content-Type", "application/json")
   107  
   108  		w.WriteHeader(http.StatusOK)
   109  
   110  		limit := 10
   111  		var err error
   112  		if r.FormValue("limit") != "" {
   113  			limit, err = strconv.Atoi(r.FormValue("limit"))
   114  			if err != nil {
   115  				t.Errorf("Error value for 'limit' parameter %v (error: %v)", r.FormValue("limit"), err)
   116  			}
   117  
   118  		}
   119  
   120  		marker := ""
   121  		newMarker := ""
   122  
   123  		if r.Form["marker"] != nil {
   124  			marker = r.Form["marker"][0]
   125  		}
   126  
   127  		t.Logf("limit = %v   marker = %v", limit, marker)
   128  
   129  		selected := 0
   130  		addNext := false
   131  		var imageJSON []string
   132  
   133  		fmt.Fprintf(w, `{"images": [`)
   134  
   135  		for _, i := range images {
   136  			if marker == "" || addNext {
   137  				t.Logf("Adding image %v to page", i.ID)
   138  				imageJSON = append(imageJSON, i.JSON)
   139  				newMarker = i.ID
   140  				selected++
   141  			} else {
   142  				if strings.Contains(i.JSON, marker) {
   143  					addNext = true
   144  				}
   145  			}
   146  
   147  			if selected == limit {
   148  				break
   149  			}
   150  		}
   151  		t.Logf("Writing out %v image(s)", len(imageJSON))
   152  		fmt.Fprintf(w, strings.Join(imageJSON, ","))
   153  
   154  		fmt.Fprintf(w, `],
   155  			    "next": "/images?marker=%s&limit=%v",
   156  			    "schema": "/schemas/images",
   157  			    "first": "/images?limit=%v"}`, newMarker, limit, limit)
   158  
   159  	})
   160  }
   161  
   162  // HandleImageCreationSuccessfully test setup
   163  func HandleImageCreationSuccessfully(t *testing.T) {
   164  	th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
   165  		th.TestMethod(t, r, "POST")
   166  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
   167  		th.TestJSONRequest(t, r, `{
   168  			"id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
   169  			"name": "Ubuntu 12.10",
   170  			"architecture": "x86_64",
   171  			"tags": [
   172  				"ubuntu",
   173  				"quantal"
   174  			]
   175  		}`)
   176  
   177  		w.WriteHeader(http.StatusCreated)
   178  		w.Header().Add("Content-Type", "application/json")
   179  		fmt.Fprintf(w, `{
   180  			"status": "queued",
   181  			"name": "Ubuntu 12.10",
   182  			"protected": false,
   183  			"tags": ["ubuntu","quantal"],
   184  			"container_format": "bare",
   185  			"created_at": "2014-11-11T20:47:55Z",
   186  			"disk_format": "qcow2",
   187  			"updated_at": "2014-11-11T20:47:55Z",
   188  			"visibility": "private",
   189  			"self": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
   190  			"min_disk": 0,
   191  			"protected": false,
   192  			"id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
   193  			"file": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd/file",
   194  			"owner": "b4eedccc6fb74fa8a7ad6b08382b852b",
   195  			"min_ram": 0,
   196  			"schema": "/v2/schemas/image",
   197  			"size": 0,
   198  			"checksum": "",
   199  			"virtual_size": 0,
   200  			"hw_disk_bus": "scsi",
   201  			"hw_disk_bus_model": "virtio-scsi",
   202  			"hw_scsi_model": "virtio-scsi"
   203  		}`)
   204  	})
   205  }
   206  
   207  // HandleImageCreationSuccessfullyNulls test setup
   208  // JSON null values could be also returned according to behaviour https://bugs.launchpad.net/glance/+bug/1481512
   209  func HandleImageCreationSuccessfullyNulls(t *testing.T) {
   210  	th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
   211  		th.TestMethod(t, r, "POST")
   212  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
   213  		th.TestJSONRequest(t, r, `{
   214  			"id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
   215  			"architecture": "x86_64",
   216  			"name": "Ubuntu 12.10",
   217  			"tags": [
   218  				"ubuntu",
   219  				"quantal"
   220  			]
   221  		}`)
   222  
   223  		w.WriteHeader(http.StatusCreated)
   224  		w.Header().Add("Content-Type", "application/json")
   225  		fmt.Fprintf(w, `{
   226  			"architecture": "x86_64",
   227  			"status": "queued",
   228  			"name": "Ubuntu 12.10",
   229  			"protected": false,
   230  			"tags": ["ubuntu","quantal"],
   231  			"container_format": "bare",
   232  			"created_at": "2014-11-11T20:47:55Z",
   233  			"disk_format": "qcow2",
   234  			"updated_at": "2014-11-11T20:47:55Z",
   235  			"visibility": "private",
   236  			"self": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
   237  			"min_disk": 0,
   238  			"protected": false,
   239  			"id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
   240  			"file": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd/file",
   241  			"owner": "b4eedccc6fb74fa8a7ad6b08382b852b",
   242  			"min_ram": 0,
   243  			"schema": "/v2/schemas/image",
   244  			"size": null,
   245  			"checksum": null,
   246  			"virtual_size": null
   247  		}`)
   248  	})
   249  }
   250  
   251  // HandleImageGetSuccessfully test setup
   252  func HandleImageGetSuccessfully(t *testing.T) {
   253  	th.Mux.HandleFunc("/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", func(w http.ResponseWriter, r *http.Request) {
   254  		th.TestMethod(t, r, "GET")
   255  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
   256  
   257  		w.WriteHeader(http.StatusOK)
   258  		w.Header().Add("Content-Type", "application/json")
   259  		fmt.Fprintf(w, `{
   260  			"status": "active",
   261  			"name": "cirros-0.3.2-x86_64-disk",
   262  			"tags": [],
   263  			"container_format": "bare",
   264  			"created_at": "2014-05-05T17:15:10Z",
   265  			"disk_format": "qcow2",
   266  			"updated_at": "2014-05-05T17:15:11Z",
   267  			"visibility": "public",
   268  			"self": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27",
   269  			"min_disk": 0,
   270  			"protected": false,
   271  			"id": "1bea47ed-f6a9-463b-b423-14b9cca9ad27",
   272  			"file": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27/file",
   273  			"checksum": "64d7c1cd2b6f60c92c14662941cb7913",
   274  			"owner": "5ef70662f8b34079a6eddb8da9d75fe8",
   275  			"size": 13167616,
   276  			"min_ram": 0,
   277  			"schema": "/v2/schemas/image",
   278  			"virtual_size": null,
   279  			"hw_disk_bus": "scsi",
   280  			"hw_disk_bus_model": "virtio-scsi",
   281  			"hw_scsi_model": "virtio-scsi"
   282  		}`)
   283  	})
   284  }
   285  
   286  // HandleImageDeleteSuccessfully test setup
   287  func HandleImageDeleteSuccessfully(t *testing.T) {
   288  	th.Mux.HandleFunc("/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", func(w http.ResponseWriter, r *http.Request) {
   289  		th.TestMethod(t, r, "DELETE")
   290  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
   291  
   292  		w.WriteHeader(http.StatusNoContent)
   293  	})
   294  }
   295  
   296  // HandleImageUpdateSuccessfully setup
   297  func HandleImageUpdateSuccessfully(t *testing.T) {
   298  	th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea", func(w http.ResponseWriter, r *http.Request) {
   299  		th.TestMethod(t, r, "PATCH")
   300  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
   301  
   302  		th.TestJSONRequest(t, r, `[
   303  			{
   304  				"op": "replace",
   305  				"path": "/name",
   306  				"value": "Fedora 17"
   307  			},
   308  			{
   309  				"op": "replace",
   310  				"path": "/tags",
   311  				"value": [
   312  					"fedora",
   313  					"beefy"
   314  				]
   315  			}
   316  		]`)
   317  
   318  		th.AssertEquals(t, "application/openstack-images-v2.1-json-patch", r.Header.Get("Content-Type"))
   319  
   320  		w.WriteHeader(http.StatusOK)
   321  		w.Header().Add("Content-Type", "application/json")
   322  		fmt.Fprintf(w, `{
   323  			"id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
   324  			"name": "Fedora 17",
   325  			"status": "active",
   326  			"visibility": "public",
   327  			"size": 2254249,
   328  			"checksum": "2cec138d7dae2aa59038ef8c9aec2390",
   329  			"tags": [
   330  				"fedora",
   331  				"beefy"
   332  			],
   333  			"created_at": "2012-08-10T19:23:50Z",
   334  			"updated_at": "2012-08-12T11:11:33Z",
   335  			"self": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
   336  			"file": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/file",
   337  			"schema": "/v2/schemas/image",
   338  			"owner": "",
   339  			"min_ram": 0,
   340  			"min_disk": 0,
   341  			"disk_format": "",
   342  			"virtual_size": 0,
   343  			"container_format": "",
   344  			"hw_disk_bus": "scsi",
   345  			"hw_disk_bus_model": "virtio-scsi",
   346  			"hw_scsi_model": "virtio-scsi"
   347  		}`)
   348  	})
   349  }
   350  
   351  // HandleImageListByTagsSuccessfully tests a list operation with tags.
   352  func HandleImageListByTagsSuccessfully(t *testing.T) {
   353  	th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
   354  		th.TestMethod(t, r, "GET")
   355  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
   356  
   357  		w.Header().Add("Content-Type", "application/json")
   358  
   359  		w.WriteHeader(http.StatusOK)
   360  
   361  		fmt.Fprintf(w, `{
   362      "images": [
   363          {
   364            "status": "active",
   365            "name": "cirros-0.3.2-x86_64-disk",
   366            "tags": ["foo", "bar"],
   367            "container_format": "bare",
   368            "created_at": "2014-05-05T17:15:10Z",
   369            "disk_format": "qcow2",
   370            "updated_at": "2014-05-05T17:15:11Z",
   371            "visibility": "public",
   372            "self": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27",
   373            "min_disk": 0,
   374            "protected": false,
   375            "id": "1bea47ed-f6a9-463b-b423-14b9cca9ad27",
   376            "file": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27/file",
   377            "checksum": "64d7c1cd2b6f60c92c14662941cb7913",
   378            "owner": "5ef70662f8b34079a6eddb8da9d75fe8",
   379            "size": 13167616,
   380            "min_ram": 0,
   381            "schema": "/v2/schemas/image",
   382            "virtual_size": null,
   383            "hw_disk_bus": "scsi",
   384            "hw_disk_bus_model": "virtio-scsi",
   385            "hw_scsi_model": "virtio-scsi"
   386          }
   387      ]
   388  	}`)
   389  	})
   390  }
   391  
   392  // HandleImageUpdatePropertiesSuccessfully setup
   393  func HandleImageUpdatePropertiesSuccessfully(t *testing.T) {
   394  	th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea", func(w http.ResponseWriter, r *http.Request) {
   395  		th.TestMethod(t, r, "PATCH")
   396  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
   397  
   398  		th.TestJSONRequest(t, r, `[
   399  			{
   400  				"op": "add",
   401  				"path": "/hw_disk_bus",
   402  				"value": "scsi"
   403  			},
   404  			{
   405  				"op": "add",
   406  				"path": "/hw_disk_bus_model",
   407  				"value": "virtio-scsi"
   408  			},
   409  			{
   410  				"op": "add",
   411  				"path": "/hw_scsi_model",
   412  				"value": "virtio-scsi"
   413  			}
   414  		]`)
   415  
   416  		th.AssertEquals(t, "application/openstack-images-v2.1-json-patch", r.Header.Get("Content-Type"))
   417  
   418  		w.WriteHeader(http.StatusOK)
   419  		w.Header().Add("Content-Type", "application/json")
   420  		fmt.Fprintf(w, `{
   421  			"id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
   422  			"name": "Fedora 17",
   423  			"status": "active",
   424  			"visibility": "public",
   425  			"size": 2254249,
   426  			"checksum": "2cec138d7dae2aa59038ef8c9aec2390",
   427  			"tags": [
   428  				"fedora",
   429  				"beefy"
   430  			],
   431  			"created_at": "2012-08-10T19:23:50Z",
   432  			"updated_at": "2012-08-12T11:11:33Z",
   433  			"self": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
   434  			"file": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/file",
   435  			"schema": "/v2/schemas/image",
   436  			"owner": "",
   437  			"min_ram": 0,
   438  			"min_disk": 0,
   439  			"disk_format": "",
   440  			"virtual_size": 0,
   441  			"container_format": "",
   442  			"hw_disk_bus": "scsi",
   443  			"hw_disk_bus_model": "virtio-scsi",
   444  			"hw_scsi_model": "virtio-scsi"
   445  		}`)
   446  	})
   447  }