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

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/huaweicloud/golangsdk/openstack/imageservice/v2/tasks"
    10  	"github.com/huaweicloud/golangsdk/pagination"
    11  	th "github.com/huaweicloud/golangsdk/testhelper"
    12  	fakeclient "github.com/huaweicloud/golangsdk/testhelper/client"
    13  )
    14  
    15  func TestList(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/tasks", func(w http.ResponseWriter, r *http.Request) {
    20  		th.TestMethod(t, r, "GET")
    21  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
    22  
    23  		w.Header().Add("Content-Type", "application/json")
    24  		w.WriteHeader(http.StatusOK)
    25  
    26  		fmt.Fprintf(w, TasksListResult)
    27  	})
    28  
    29  	count := 0
    30  
    31  	tasks.List(fakeclient.ServiceClient(), tasks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    32  		count++
    33  		actual, err := tasks.ExtractTasks(page)
    34  		if err != nil {
    35  			t.Errorf("Failed to extract tasks: %v", err)
    36  			return false, nil
    37  		}
    38  
    39  		expected := []tasks.Task{
    40  			Task1,
    41  			Task2,
    42  		}
    43  
    44  		th.CheckDeepEquals(t, expected, actual)
    45  
    46  		return true, nil
    47  	})
    48  
    49  	if count != 1 {
    50  		t.Errorf("Expected 1 page, got %d", count)
    51  	}
    52  }
    53  
    54  func TestGet(t *testing.T) {
    55  	th.SetupHTTP()
    56  	defer th.TeardownHTTP()
    57  
    58  	th.Mux.HandleFunc("/tasks/1252f636-1246-4319-bfba-c47cde0efbe0", func(w http.ResponseWriter, r *http.Request) {
    59  		th.TestMethod(t, r, "GET")
    60  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
    61  
    62  		w.Header().Add("Content-Type", "application/json")
    63  		w.WriteHeader(http.StatusOK)
    64  
    65  		fmt.Fprintf(w, TasksGetResult)
    66  	})
    67  
    68  	s, err := tasks.Get(fakeclient.ServiceClient(), "1252f636-1246-4319-bfba-c47cde0efbe0").Extract()
    69  	th.AssertNoErr(t, err)
    70  
    71  	th.AssertEquals(t, s.Status, string(tasks.TaskStatusPending))
    72  	th.AssertEquals(t, s.CreatedAt, time.Date(2018, 7, 25, 8, 59, 13, 0, time.UTC))
    73  	th.AssertEquals(t, s.UpdatedAt, time.Date(2018, 7, 25, 8, 59, 14, 0, time.UTC))
    74  	th.AssertEquals(t, s.Self, "/v2/tasks/1252f636-1246-4319-bfba-c47cde0efbe0")
    75  	th.AssertEquals(t, s.Owner, "424e7cf0243c468ca61732ba45973b3e")
    76  	th.AssertEquals(t, s.Message, "")
    77  	th.AssertEquals(t, s.Type, "import")
    78  	th.AssertEquals(t, s.ID, "1252f636-1246-4319-bfba-c47cde0efbe0")
    79  	th.AssertEquals(t, s.Schema, "/v2/schemas/task")
    80  	th.AssertDeepEquals(t, s.Result, map[string]interface{}(nil))
    81  	th.AssertDeepEquals(t, s.Input, map[string]interface{}{
    82  		"import_from":        "http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img",
    83  		"import_from_format": "raw",
    84  		"image_properties": map[string]interface{}{
    85  			"container_format": "bare",
    86  			"disk_format":      "raw",
    87  		},
    88  	})
    89  }
    90  
    91  func TestCreate(t *testing.T) {
    92  	th.SetupHTTP()
    93  	defer th.TeardownHTTP()
    94  
    95  	th.Mux.HandleFunc("/tasks", func(w http.ResponseWriter, r *http.Request) {
    96  		th.TestMethod(t, r, "POST")
    97  		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
    98  		th.TestJSONRequest(t, r, TaskCreateRequest)
    99  
   100  		w.Header().Add("Content-Type", "application/json")
   101  		w.WriteHeader(http.StatusCreated)
   102  
   103  		fmt.Fprintf(w, TaskCreateResult)
   104  	})
   105  
   106  	opts := tasks.CreateOpts{
   107  		Type: "import",
   108  		Input: map[string]interface{}{
   109  			"image_properties": map[string]interface{}{
   110  				"container_format": "bare",
   111  				"disk_format":      "raw",
   112  			},
   113  			"import_from_format": "raw",
   114  			"import_from":        "https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img",
   115  		},
   116  	}
   117  	s, err := tasks.Create(fakeclient.ServiceClient(), opts).Extract()
   118  	th.AssertNoErr(t, err)
   119  
   120  	th.AssertEquals(t, s.Status, string(tasks.TaskStatusPending))
   121  	th.AssertEquals(t, s.CreatedAt, time.Date(2018, 7, 25, 11, 7, 54, 0, time.UTC))
   122  	th.AssertEquals(t, s.UpdatedAt, time.Date(2018, 7, 25, 11, 7, 54, 0, time.UTC))
   123  	th.AssertEquals(t, s.Self, "/v2/tasks/d550c87d-86ed-430a-9895-c7a1f5ce87e9")
   124  	th.AssertEquals(t, s.Owner, "fb57277ef2f84a0e85b9018ec2dedbf7")
   125  	th.AssertEquals(t, s.Message, "")
   126  	th.AssertEquals(t, s.Type, "import")
   127  	th.AssertEquals(t, s.ID, "d550c87d-86ed-430a-9895-c7a1f5ce87e9")
   128  	th.AssertEquals(t, s.Schema, "/v2/schemas/task")
   129  	th.AssertDeepEquals(t, s.Result, map[string]interface{}(nil))
   130  	th.AssertDeepEquals(t, s.Input, map[string]interface{}{
   131  		"import_from":        "https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img",
   132  		"import_from_format": "raw",
   133  		"image_properties": map[string]interface{}{
   134  			"container_format": "bare",
   135  			"disk_format":      "raw",
   136  		},
   137  	})
   138  }