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