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

     1  package testing
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/huaweicloud/golangsdk/openstack/compute/v2/images"
    11  	"github.com/huaweicloud/golangsdk/pagination"
    12  	th "github.com/huaweicloud/golangsdk/testhelper"
    13  	fake "github.com/huaweicloud/golangsdk/testhelper/client"
    14  )
    15  
    16  func TestListImages(t *testing.T) {
    17  	th.SetupHTTP()
    18  	defer th.TeardownHTTP()
    19  
    20  	th.Mux.HandleFunc("/images/detail", func(w http.ResponseWriter, r *http.Request) {
    21  		th.TestMethod(t, r, "GET")
    22  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    23  
    24  		w.Header().Add("Content-Type", "application/json")
    25  		r.ParseForm()
    26  		marker := r.Form.Get("marker")
    27  		switch marker {
    28  		case "":
    29  			fmt.Fprintf(w, `
    30  				{
    31  					"images": [
    32  						{
    33  							"status": "ACTIVE",
    34  							"updated": "2014-09-23T12:54:56Z",
    35  							"id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
    36  							"OS-EXT-IMG-SIZE:size": 476704768,
    37  							"name": "F17-x86_64-cfntools",
    38  							"created": "2014-09-23T12:54:52Z",
    39  							"minDisk": 0,
    40  							"progress": 100,
    41  							"minRam": 0,
    42  							"metadata": {
    43  								"architecture": "x86_64",
    44  								"block_device_mapping": {
    45  									"guest_format": null,
    46  									"boot_index": 0,
    47  									"device_name": "/dev/vda",
    48  									"delete_on_termination": false
    49  								}
    50                }
    51  						},
    52  						{
    53  							"status": "ACTIVE",
    54  							"updated": "2014-09-23T12:51:43Z",
    55  							"id": "f90f6034-2570-4974-8351-6b49732ef2eb",
    56  							"OS-EXT-IMG-SIZE:size": 13167616,
    57  							"name": "cirros-0.3.2-x86_64-disk",
    58  							"created": "2014-09-23T12:51:42Z",
    59  							"minDisk": 0,
    60  							"progress": 100,
    61  							"minRam": 0
    62  						}
    63  					]
    64  				}
    65  			`)
    66  		case "2":
    67  			fmt.Fprintf(w, `{ "images": [] }`)
    68  		default:
    69  			t.Fatalf("Unexpected marker: [%s]", marker)
    70  		}
    71  	})
    72  
    73  	pages := 0
    74  	options := &images.ListOpts{Limit: 2}
    75  	err := images.ListDetail(fake.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) {
    76  		pages++
    77  
    78  		actual, err := images.ExtractImages(page)
    79  		if err != nil {
    80  			return false, err
    81  		}
    82  
    83  		expected := []images.Image{
    84  			{
    85  				ID:       "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
    86  				Name:     "F17-x86_64-cfntools",
    87  				Created:  "2014-09-23T12:54:52Z",
    88  				Updated:  "2014-09-23T12:54:56Z",
    89  				MinDisk:  0,
    90  				MinRAM:   0,
    91  				Progress: 100,
    92  				Status:   "ACTIVE",
    93  				Metadata: map[string]interface{}{
    94  					"architecture": "x86_64",
    95  					"block_device_mapping": map[string]interface{}{
    96  						"guest_format":          interface{}(nil),
    97  						"boot_index":            float64(0),
    98  						"device_name":           "/dev/vda",
    99  						"delete_on_termination": false,
   100  					},
   101  				},
   102  			},
   103  			{
   104  				ID:       "f90f6034-2570-4974-8351-6b49732ef2eb",
   105  				Name:     "cirros-0.3.2-x86_64-disk",
   106  				Created:  "2014-09-23T12:51:42Z",
   107  				Updated:  "2014-09-23T12:51:43Z",
   108  				MinDisk:  0,
   109  				MinRAM:   0,
   110  				Progress: 100,
   111  				Status:   "ACTIVE",
   112  			},
   113  		}
   114  
   115  		if !reflect.DeepEqual(expected, actual) {
   116  			t.Errorf("Unexpected page contents: expected %#v, got %#v", expected, actual)
   117  		}
   118  
   119  		return false, nil
   120  	})
   121  
   122  	if err != nil {
   123  		t.Fatalf("EachPage error: %v", err)
   124  	}
   125  	if pages != 1 {
   126  		t.Errorf("Expected one page, got %d", pages)
   127  	}
   128  }
   129  
   130  func TestGetImage(t *testing.T) {
   131  	th.SetupHTTP()
   132  	defer th.TeardownHTTP()
   133  
   134  	th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
   135  		th.TestMethod(t, r, "GET")
   136  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   137  
   138  		w.Header().Add("Content-Type", "application/json")
   139  		fmt.Fprintf(w, `
   140  			{
   141  				"image": {
   142  					"status": "ACTIVE",
   143  					"updated": "2014-09-23T12:54:56Z",
   144  					"id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
   145  					"OS-EXT-IMG-SIZE:size": 476704768,
   146  					"name": "F17-x86_64-cfntools",
   147  					"created": "2014-09-23T12:54:52Z",
   148  					"minDisk": 0,
   149  					"progress": 100,
   150  					"minRam": 0,
   151  					"metadata": {
   152  						"architecture": "x86_64",
   153  						"block_device_mapping": {
   154  							"guest_format": null,
   155  							"boot_index": 0,
   156  							"device_name": "/dev/vda",
   157  							"delete_on_termination": false
   158  						}
   159  					}
   160  				}
   161  			}
   162  		`)
   163  	})
   164  
   165  	actual, err := images.Get(fake.ServiceClient(), "12345678").Extract()
   166  	if err != nil {
   167  		t.Fatalf("Unexpected error from Get: %v", err)
   168  	}
   169  
   170  	expected := &images.Image{
   171  		Status:   "ACTIVE",
   172  		Updated:  "2014-09-23T12:54:56Z",
   173  		ID:       "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
   174  		Name:     "F17-x86_64-cfntools",
   175  		Created:  "2014-09-23T12:54:52Z",
   176  		MinDisk:  0,
   177  		Progress: 100,
   178  		MinRAM:   0,
   179  		Metadata: map[string]interface{}{
   180  			"architecture": "x86_64",
   181  			"block_device_mapping": map[string]interface{}{
   182  				"guest_format":          interface{}(nil),
   183  				"boot_index":            float64(0),
   184  				"device_name":           "/dev/vda",
   185  				"delete_on_termination": false,
   186  			},
   187  		},
   188  	}
   189  
   190  	if !reflect.DeepEqual(expected, actual) {
   191  		t.Errorf("Expected %#v, but got %#v", expected, actual)
   192  	}
   193  }
   194  
   195  func TestNextPageURL(t *testing.T) {
   196  	var page images.ImagePage
   197  	var body map[string]interface{}
   198  	bodyString := []byte(`{"images":{"links":[{"href":"http://192.154.23.87/12345/images/image3","rel":"bookmark"}]}, "images_links":[{"href":"http://192.154.23.87/12345/images/image4","rel":"next"}]}`)
   199  	err := json.Unmarshal(bodyString, &body)
   200  	if err != nil {
   201  		t.Fatalf("Error unmarshaling data into page body: %v", err)
   202  	}
   203  	page.Body = body
   204  
   205  	expected := "http://192.154.23.87/12345/images/image4"
   206  	actual, err := page.NextPageURL()
   207  	th.AssertNoErr(t, err)
   208  	th.CheckEquals(t, expected, actual)
   209  }
   210  
   211  // Test Image delete
   212  func TestDeleteImage(t *testing.T) {
   213  	th.SetupHTTP()
   214  	defer th.TeardownHTTP()
   215  
   216  	th.Mux.HandleFunc("/images/12345678", func(w http.ResponseWriter, r *http.Request) {
   217  		th.TestMethod(t, r, "DELETE")
   218  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   219  
   220  		w.WriteHeader(http.StatusNoContent)
   221  	})
   222  
   223  	res := images.Delete(fake.ServiceClient(), "12345678")
   224  	th.AssertNoErr(t, res.Err)
   225  }