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

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