github.com/openshift/installer@v1.4.17/pkg/asset/quota/gcp/gcp_test.go (about)

     1  package gcp
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	computev1 "google.golang.org/api/compute/v1"
    10  
    11  	"github.com/openshift/installer/pkg/quota"
    12  )
    13  
    14  func Test_aggregate(t *testing.T) {
    15  	cases := []struct {
    16  		input []quota.Constraint
    17  
    18  		exp []quota.Constraint
    19  	}{{
    20  		input: []quota.Constraint{
    21  			{Name: "q1", Region: "g", Count: 1},
    22  			{Name: "q1", Region: "g", Count: 1},
    23  		},
    24  		exp: []quota.Constraint{
    25  			{Name: "q1", Region: "g", Count: 2},
    26  		},
    27  	}, {
    28  		input: []quota.Constraint{
    29  			{Name: "q1", Region: "g", Count: 1},
    30  			{Name: "q2", Region: "r1", Count: 1},
    31  			{Name: "q3", Region: "r1", Count: 1},
    32  			{Name: "q2", Region: "r1", Count: 1},
    33  		},
    34  		exp: []quota.Constraint{
    35  			{Name: "q1", Region: "g", Count: 1},
    36  			{Name: "q2", Region: "r1", Count: 2},
    37  			{Name: "q3", Region: "r1", Count: 1},
    38  		},
    39  	}, {
    40  		input: []quota.Constraint{
    41  			{Name: "q1", Region: "g", Count: 1},
    42  			{Name: "q2", Region: "r1", Count: 1},
    43  			{Name: "q3", Region: "r1", Count: 1},
    44  			{Name: "q4", Region: "r1", Count: 1},
    45  		},
    46  		exp: []quota.Constraint{
    47  			{Name: "q1", Region: "g", Count: 1},
    48  			{Name: "q2", Region: "r1", Count: 1},
    49  			{Name: "q3", Region: "r1", Count: 1},
    50  			{Name: "q4", Region: "r1", Count: 1},
    51  		},
    52  	}}
    53  
    54  	for idx, test := range cases {
    55  		t.Run(fmt.Sprintf("test %d", idx), func(t *testing.T) {
    56  			got := aggregate(test.input)
    57  			assert.EqualValues(t, test.exp, got)
    58  		})
    59  	}
    60  }
    61  
    62  func Test_guessMachineCPUCount(t *testing.T) {
    63  	cases := []struct {
    64  		machineType string
    65  		expected    int64
    66  	}{{
    67  		machineType: "e2-standard-2",
    68  		expected:    2,
    69  	}, {
    70  		machineType: "e2-standard-8-4096",
    71  		expected:    8,
    72  	}, {
    73  		machineType: "e2-highmem-2",
    74  		expected:    2,
    75  	}, {
    76  		machineType: "e2-highmem-8-4096",
    77  		expected:    8,
    78  	}, {
    79  		machineType: "e2-highcpu-4",
    80  		expected:    4,
    81  	}, {
    82  		machineType: "e2-highcpu-16-4096",
    83  		expected:    16,
    84  	}, {
    85  		machineType: "n2-standard-4",
    86  		expected:    4,
    87  	}, {
    88  		machineType: "n2-standard-16-4096",
    89  		expected:    16,
    90  	}, {
    91  		machineType: "n2-standard-16.5-4096",
    92  		expected:    0,
    93  	}, {
    94  		machineType: "n2-highmem-4",
    95  		expected:    4,
    96  	}, {
    97  		machineType: "n2-highmem-32-4096",
    98  		expected:    32,
    99  	}, {
   100  		machineType: "n2-highcpu-4",
   101  		expected:    4,
   102  	}, {
   103  		machineType: "n2-highcpu-16",
   104  		expected:    16,
   105  	}, {
   106  		machineType: "n2d-standard-4",
   107  		expected:    4,
   108  	}, {
   109  		machineType: "n2d-standard-16",
   110  		expected:    16,
   111  	}, {
   112  		machineType: "n2d-highmem-4",
   113  		expected:    4,
   114  	}, {
   115  		machineType: "n2d-highmem-16",
   116  		expected:    16,
   117  	}, {
   118  		machineType: "n2d-highcpu-4",
   119  		expected:    4,
   120  	}, {
   121  		machineType: "n2d-highcpu-16",
   122  		expected:    16,
   123  	}, {
   124  		machineType: "n1-standard-2",
   125  		expected:    2,
   126  	}, {
   127  		machineType: "n1-standard-8-4096",
   128  		expected:    8,
   129  	}, {
   130  		machineType: "c2-standard-4",
   131  		expected:    4,
   132  	}, {
   133  		machineType: "c2-standard-16-4096",
   134  		expected:    16,
   135  	}, {
   136  		machineType: "e2-micro",
   137  		expected:    2,
   138  	}, {
   139  		machineType: "e2-medium",
   140  		expected:    2,
   141  	}, {
   142  		machineType: "f1-micro",
   143  		expected:    1,
   144  	}, {
   145  		machineType: "g1-small",
   146  		expected:    1,
   147  	}}
   148  	for idx, test := range cases {
   149  		t.Run(fmt.Sprintf("test %d", idx), func(t *testing.T) {
   150  			got := guessMachineCPUCount(test.machineType)
   151  			assert.EqualValues(t, test.expected, got)
   152  		})
   153  	}
   154  }
   155  
   156  func Test_machineTypeToQuota(t *testing.T) {
   157  	fake := newFakeMachineTypeGetter([]*computev1.MachineType{{
   158  		Zone:      "a",
   159  		Name:      "n1-standard-2",
   160  		GuestCpus: 2,
   161  	}, {
   162  		Zone:      "a",
   163  		Name:      "n1-custom-2-1024",
   164  		GuestCpus: 2,
   165  	}, {
   166  		Zone:      "a",
   167  		Name:      "custom-2-1024",
   168  		GuestCpus: 2,
   169  	}, {
   170  		Zone:      "a",
   171  		Name:      "n2-standard-4",
   172  		GuestCpus: 4,
   173  	}, {
   174  		Zone:      "a",
   175  		Name:      "n2-custom-4-1024",
   176  		GuestCpus: 4,
   177  	}})
   178  
   179  	tests := []struct {
   180  		zone        string
   181  		machineType string
   182  		expected    quota.Constraint
   183  	}{{
   184  		zone:        "a",
   185  		machineType: "n1-standard-2",
   186  		expected:    quota.Constraint{Name: "compute.googleapis.com/cpus", Count: 2},
   187  	}, {
   188  		zone:        "a",
   189  		machineType: "n1-custom-2-1024",
   190  		expected:    quota.Constraint{Name: "compute.googleapis.com/cpus", Count: 2},
   191  	}, {
   192  		zone:        "a",
   193  		machineType: "n1-custom-2-2048",
   194  		expected:    quota.Constraint{Name: "compute.googleapis.com/cpus", Count: 2},
   195  	}, {
   196  		zone:        "b",
   197  		machineType: "n1-standard-2",
   198  		expected:    quota.Constraint{Name: "compute.googleapis.com/cpus", Count: 2},
   199  	}, {
   200  		zone:        "a",
   201  		machineType: "custom-2-1024",
   202  		expected:    quota.Constraint{Name: "compute.googleapis.com/cpus", Count: 2},
   203  	}, {
   204  		zone:        "a",
   205  		machineType: "custom-2-2048",
   206  		expected:    quota.Constraint{Name: "compute.googleapis.com/cpus", Count: 0},
   207  	}, {
   208  		zone:        "b",
   209  		machineType: "custom-2-1024",
   210  		expected:    quota.Constraint{Name: "compute.googleapis.com/cpus", Count: 0},
   211  	}, {
   212  		zone:        "a",
   213  		machineType: "n2-standard-4",
   214  		expected:    quota.Constraint{Name: "compute.googleapis.com/n2_cpus", Count: 4},
   215  	}, {
   216  		zone:        "a",
   217  		machineType: "n2-custom-4-1024",
   218  		expected:    quota.Constraint{Name: "compute.googleapis.com/n2_cpus", Count: 4},
   219  	}, {
   220  		zone:        "b",
   221  		machineType: "n2-standard-4",
   222  		expected:    quota.Constraint{Name: "compute.googleapis.com/n2_cpus", Count: 4},
   223  	}}
   224  
   225  	for idx, test := range tests {
   226  		t.Run(fmt.Sprintf("test %d", idx), func(t *testing.T) {
   227  			got := machineTypeToQuota(fake, test.zone, test.machineType)
   228  			assert.EqualValues(t, test.expected, got)
   229  		})
   230  	}
   231  }
   232  
   233  type fakeMachineTypeGetter struct {
   234  	knownTypes map[string]*computev1.MachineType
   235  }
   236  
   237  func newFakeMachineTypeGetter(mtypes []*computev1.MachineType) *fakeMachineTypeGetter {
   238  	fake := &fakeMachineTypeGetter{
   239  		knownTypes: map[string]*computev1.MachineType{},
   240  	}
   241  
   242  	for idx, mtype := range mtypes {
   243  		fake.knownTypes[fmt.Sprintf("%s__%s", mtype.Zone, mtype.Name)] = mtypes[idx]
   244  	}
   245  
   246  	return fake
   247  }
   248  
   249  func (fake *fakeMachineTypeGetter) GetMachineType(zone string, machineType string) (*computev1.MachineType, error) {
   250  	mtype, ok := fake.knownTypes[fmt.Sprintf("%s__%s", zone, machineType)]
   251  	if !ok {
   252  		return nil, errors.New("unknwown")
   253  	}
   254  	return mtype, nil
   255  }