Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 1 | package schedulerhints |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 7 | th "github.com/rackspace/gophercloud/testhelper" |
| 8 | ) |
| 9 | |
| 10 | func TestCreateOpts(t *testing.T) { |
| 11 | base := servers.CreateOpts{ |
| 12 | Name: "createdserver", |
| 13 | ImageRef: "asdfasdfasdf", |
| 14 | FlavorRef: "performance1-1", |
| 15 | } |
| 16 | |
| 17 | schedulerHints := SchedulerHints{ |
| 18 | Group: "101aed42-22d9-4a3e-9ba1-21103b0d1aba", |
| 19 | DifferentHost: []string{ |
| 20 | "a0cf03a5-d921-4877-bb5c-86d26cf818e1", |
| 21 | "8c19174f-4220-44f0-824a-cd1eeef10287", |
| 22 | }, |
| 23 | SameHost: []string{ |
| 24 | "a0cf03a5-d921-4877-bb5c-86d26cf818e1", |
| 25 | "8c19174f-4220-44f0-824a-cd1eeef10287", |
| 26 | }, |
Joe Topjian | 7d6989c | 2015-05-05 20:38:08 +0000 | [diff] [blame^] | 27 | Query: []interface{}{">=", "$free_ram_mb", "1024"}, |
Joe Topjian | 27a6243 | 2015-04-18 01:48:50 +0000 | [diff] [blame] | 28 | TargetCell: "foobar", |
| 29 | BuildNearHostIP: "192.168.1.1/24", |
| 30 | } |
| 31 | |
| 32 | ext := CreateOptsExt{ |
| 33 | CreateOptsBuilder: base, |
| 34 | SchedulerHints: schedulerHints, |
| 35 | } |
| 36 | |
| 37 | expected := ` |
| 38 | { |
| 39 | "server": { |
| 40 | "name": "createdserver", |
| 41 | "imageRef": "asdfasdfasdf", |
| 42 | "flavorRef": "performance1-1" |
| 43 | }, |
| 44 | "os:scheduler_hints": { |
| 45 | "group": "101aed42-22d9-4a3e-9ba1-21103b0d1aba", |
| 46 | "different_host": [ |
| 47 | "a0cf03a5-d921-4877-bb5c-86d26cf818e1", |
| 48 | "8c19174f-4220-44f0-824a-cd1eeef10287" |
| 49 | ], |
| 50 | "same_host": [ |
| 51 | "a0cf03a5-d921-4877-bb5c-86d26cf818e1", |
| 52 | "8c19174f-4220-44f0-824a-cd1eeef10287" |
| 53 | ], |
| 54 | "query": [ |
| 55 | ">=", "$free_ram_mb", "1024" |
| 56 | ], |
| 57 | "target_cell": "foobar", |
| 58 | "build_near_host_ip": "192.168.1.1", |
| 59 | "cidr": "/24" |
| 60 | } |
| 61 | } |
| 62 | ` |
| 63 | actual, err := ext.ToServerCreateMap() |
| 64 | th.AssertNoErr(t, err) |
| 65 | th.CheckJSONEquals(t, expected, actual) |
| 66 | } |
Joe Topjian | 7d6989c | 2015-05-05 20:38:08 +0000 | [diff] [blame^] | 67 | |
| 68 | func TestCreateOptsWithComplexQuery(t *testing.T) { |
| 69 | base := servers.CreateOpts{ |
| 70 | Name: "createdserver", |
| 71 | ImageRef: "asdfasdfasdf", |
| 72 | FlavorRef: "performance1-1", |
| 73 | } |
| 74 | |
| 75 | schedulerHints := SchedulerHints{ |
| 76 | Group: "101aed42-22d9-4a3e-9ba1-21103b0d1aba", |
| 77 | DifferentHost: []string{ |
| 78 | "a0cf03a5-d921-4877-bb5c-86d26cf818e1", |
| 79 | "8c19174f-4220-44f0-824a-cd1eeef10287", |
| 80 | }, |
| 81 | SameHost: []string{ |
| 82 | "a0cf03a5-d921-4877-bb5c-86d26cf818e1", |
| 83 | "8c19174f-4220-44f0-824a-cd1eeef10287", |
| 84 | }, |
| 85 | Query: []interface{}{"and", []string{">=", "$free_ram_mb", "1024"}, []string{">=", "$free_disk_mb", "204800"}}, |
| 86 | TargetCell: "foobar", |
| 87 | BuildNearHostIP: "192.168.1.1/24", |
| 88 | } |
| 89 | |
| 90 | ext := CreateOptsExt{ |
| 91 | CreateOptsBuilder: base, |
| 92 | SchedulerHints: schedulerHints, |
| 93 | } |
| 94 | |
| 95 | expected := ` |
| 96 | { |
| 97 | "server": { |
| 98 | "name": "createdserver", |
| 99 | "imageRef": "asdfasdfasdf", |
| 100 | "flavorRef": "performance1-1" |
| 101 | }, |
| 102 | "os:scheduler_hints": { |
| 103 | "group": "101aed42-22d9-4a3e-9ba1-21103b0d1aba", |
| 104 | "different_host": [ |
| 105 | "a0cf03a5-d921-4877-bb5c-86d26cf818e1", |
| 106 | "8c19174f-4220-44f0-824a-cd1eeef10287" |
| 107 | ], |
| 108 | "same_host": [ |
| 109 | "a0cf03a5-d921-4877-bb5c-86d26cf818e1", |
| 110 | "8c19174f-4220-44f0-824a-cd1eeef10287" |
| 111 | ], |
| 112 | "query": [ |
| 113 | "and", |
| 114 | [">=", "$free_ram_mb", "1024"], |
| 115 | [">=", "$free_disk_mb", "204800"] |
| 116 | ], |
| 117 | "target_cell": "foobar", |
| 118 | "build_near_host_ip": "192.168.1.1", |
| 119 | "cidr": "/24" |
| 120 | } |
| 121 | } |
| 122 | ` |
| 123 | actual, err := ext.ToServerCreateMap() |
| 124 | th.AssertNoErr(t, err) |
| 125 | th.CheckJSONEquals(t, expected, actual) |
| 126 | } |