|
| 1 | +package cloudscale |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCustomImageImport_Create(t *testing.T) { |
| 12 | + setup() |
| 13 | + defer teardown() |
| 14 | + |
| 15 | + CustomImageImportRequest := &CustomImageImportRequest{ |
| 16 | + Name: "Test Image", |
| 17 | + TaggedResourceRequest: TaggedResourceRequest{ |
| 18 | + TagMap{ |
| 19 | + "tag": "one", |
| 20 | + "other": "tag", |
| 21 | + }, |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + mux.HandleFunc("/v1/custom-images/import", func(w http.ResponseWriter, r *http.Request) { |
| 26 | + expected := map[string]interface{}{ |
| 27 | + "name": "Test Image", |
| 28 | + "tags": map[string]interface{}{ |
| 29 | + "tag": "one", |
| 30 | + "other": "tag", |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + var v map[string]interface{} |
| 35 | + err := json.NewDecoder(r.Body).Decode(&v) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("decode json: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + if !reflect.DeepEqual(v, expected) { |
| 41 | + t.Errorf("Request body\n got=%#v\nwant=%#v", v, expected) |
| 42 | + } |
| 43 | + jsonStr := `{ |
| 44 | + "href": "https://api.cloudscale.ch/v1/custom-images/import/11111111-1864-4608-853a-0771b6885a3a", |
| 45 | + "uuid": "11111111-1864-4608-853a-0771b6885a3a", |
| 46 | + "custom_image": { |
| 47 | + "href": "https://api.cloudscale.ch/v1/custom-images/11111111-1864-4608-853a-0771b6885a3a", |
| 48 | + "uuid": "11111111-1864-4608-853a-0771b6885a3a", |
| 49 | + "name": "my-foo" |
| 50 | + }, |
| 51 | + "url": "https://example.com/foo.raw", |
| 52 | + "status": "in_progress", |
| 53 | + "error_message": "", |
| 54 | + "tags": {} |
| 55 | + }` |
| 56 | + fmt.Fprintf(w, jsonStr) |
| 57 | + }) |
| 58 | + |
| 59 | + customImageImport, err := client.CustomImageImports.Create(ctx, CustomImageImportRequest) |
| 60 | + if err != nil { |
| 61 | + t.Errorf("CustomImageImport.Create returned error: %v", err) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + expectedUUID := "11111111-1864-4608-853a-0771b6885a3a" |
| 66 | + if id := customImageImport.UUID; id != expectedUUID { |
| 67 | + t.Errorf("expected id '%s', received '%s'", expectedUUID, id) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestCustomImageImport_Get(t *testing.T) { |
| 72 | + setup() |
| 73 | + defer teardown() |
| 74 | + |
| 75 | + mux.HandleFunc("/v1/custom-images/import/6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15", func(w http.ResponseWriter, r *http.Request) { |
| 76 | + testHTTPMethod(t, r, http.MethodGet) |
| 77 | + fmt.Fprint(w, `{ |
| 78 | + "href": "https://api.cloudscale.ch/v1/custom-images/import/11111111-1864-4608-853a-0771b6885a3a", |
| 79 | + "uuid": "11111111-1864-4608-853a-0771b6885a3a", |
| 80 | + "custom_image": { |
| 81 | + "href": "https://api.cloudscale.ch/v1/custom-images/11111111-1864-4608-853a-0771b6885a3a", |
| 82 | + "uuid": "11111111-1864-4608-853a-0771b6885a3a", |
| 83 | + "name": "my-foo" |
| 84 | + }, |
| 85 | + "url": "https://example.com/foo.raw", |
| 86 | + "status": "in_progress", |
| 87 | + "error_message": "", |
| 88 | + "tags": {} |
| 89 | + }`) |
| 90 | + }) |
| 91 | + |
| 92 | + objectUser, err := client.CustomImageImports.Get(ctx, "6fe39134bf4178747eebc429f82cfafdd08891d4279d0d899bc4012db1db6a15") |
| 93 | + if err != nil { |
| 94 | + t.Errorf("CustomImageImport.Get returned error: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + expected := &CustomImageImport{ |
| 98 | + HREF: "https://api.cloudscale.ch/v1/custom-images/import/11111111-1864-4608-853a-0771b6885a3a", |
| 99 | + UUID: "11111111-1864-4608-853a-0771b6885a3a", |
| 100 | + CustomImage: CustomImageStub{ |
| 101 | + HREF: "https://api.cloudscale.ch/v1/custom-images/11111111-1864-4608-853a-0771b6885a3a", |
| 102 | + UUID: "11111111-1864-4608-853a-0771b6885a3a", |
| 103 | + Name: "my-foo", |
| 104 | + }, |
| 105 | + URL: "https://example.com/foo.raw", |
| 106 | + Status: "in_progress", |
| 107 | + ErrorMessage: "", |
| 108 | + } |
| 109 | + expected.Tags = TagMap{} |
| 110 | + |
| 111 | + if !reflect.DeepEqual(objectUser, expected) { |
| 112 | + t.Errorf("CustomImageImport.Get\n got=%#v\nwant=%#v", objectUser, expected) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestCustomImageImport_List(t *testing.T) { |
| 117 | + setup() |
| 118 | + defer teardown() |
| 119 | + |
| 120 | + mux.HandleFunc("/v1/custom-images/import", func(w http.ResponseWriter, r *http.Request) { |
| 121 | + testHTTPMethod(t, r, http.MethodGet) |
| 122 | + fmt.Fprint(w, `[{ |
| 123 | + "href": "https://api.cloudscale.ch/v1/custom-images/import/11111111-1864-4608-853a-0771b6885a3a", |
| 124 | + "uuid": "11111111-1864-4608-853a-0771b6885a3a", |
| 125 | + "custom_image": { |
| 126 | + "href": "https://api.cloudscale.ch/v1/custom-images/11111111-1864-4608-853a-0771b6885a3a", |
| 127 | + "uuid": "11111111-1864-4608-853a-0771b6885a3a", |
| 128 | + "name": "my-foo" |
| 129 | + }, |
| 130 | + "url": "https://example.com/foo.raw", |
| 131 | + "status": "in_progress", |
| 132 | + "error_message": "", |
| 133 | + "tags": {} |
| 134 | +}]`) |
| 135 | + }) |
| 136 | + |
| 137 | + objectUsers, err := client.CustomImageImports.List(ctx) |
| 138 | + if err != nil { |
| 139 | + t.Errorf("CustomImageImport.List returned error: %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + expectedImage := CustomImageImport{ |
| 143 | + HREF: "https://api.cloudscale.ch/v1/custom-images/import/11111111-1864-4608-853a-0771b6885a3a", |
| 144 | + UUID: "11111111-1864-4608-853a-0771b6885a3a", |
| 145 | + CustomImage: CustomImageStub{ |
| 146 | + HREF: "https://api.cloudscale.ch/v1/custom-images/11111111-1864-4608-853a-0771b6885a3a", |
| 147 | + UUID: "11111111-1864-4608-853a-0771b6885a3a", |
| 148 | + Name: "my-foo", |
| 149 | + }, |
| 150 | + URL: "https://example.com/foo.raw", |
| 151 | + Status: "in_progress", |
| 152 | + ErrorMessage: "", |
| 153 | + } |
| 154 | + expectedImage.Tags = TagMap{} |
| 155 | + expected := []CustomImageImport{ |
| 156 | + expectedImage, |
| 157 | + } |
| 158 | + if !reflect.DeepEqual(objectUsers, expected) { |
| 159 | + t.Errorf("CustomImageImport.List\n got=%#v\nwant=%#v", objectUsers, expected) |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments