github.com/openshift/installer@v1.4.17/pkg/types/validation/machinepools_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"k8s.io/apimachinery/pkg/util/validation/field"
     8  	"k8s.io/utils/pointer"
     9  
    10  	"github.com/openshift/installer/pkg/types"
    11  	"github.com/openshift/installer/pkg/types/aws"
    12  	"github.com/openshift/installer/pkg/types/azure"
    13  	"github.com/openshift/installer/pkg/types/gcp"
    14  	"github.com/openshift/installer/pkg/types/ibmcloud"
    15  	"github.com/openshift/installer/pkg/types/openstack"
    16  )
    17  
    18  func validMachinePool(name string) *types.MachinePool {
    19  	return &types.MachinePool{
    20  		Name:           name,
    21  		Replicas:       pointer.Int64Ptr(1),
    22  		Hyperthreading: types.HyperthreadingDisabled,
    23  		Architecture:   types.ArchitectureAMD64,
    24  	}
    25  }
    26  
    27  func TestValidateMachinePool(t *testing.T) {
    28  	cases := []struct {
    29  		name     string
    30  		platform *types.Platform
    31  		pool     *types.MachinePool
    32  		valid    bool
    33  	}{
    34  		{
    35  			name:     "minimal",
    36  			platform: &types.Platform{AWS: &aws.Platform{Region: "us-east-1"}},
    37  			pool:     validMachinePool("test-name"),
    38  			valid:    true,
    39  		},
    40  		{
    41  			name:     "missing replicas",
    42  			platform: &types.Platform{AWS: &aws.Platform{Region: "us-east-1"}},
    43  			pool: func() *types.MachinePool {
    44  				p := validMachinePool("test-name")
    45  				p.Replicas = nil
    46  				return p
    47  			}(),
    48  			valid: false,
    49  		},
    50  		{
    51  			name:     "invalid replicas",
    52  			platform: &types.Platform{AWS: &aws.Platform{Region: "us-east-1"}},
    53  			pool: func() *types.MachinePool {
    54  				p := validMachinePool("test-name")
    55  				p.Replicas = pointer.Int64Ptr(-1)
    56  				return p
    57  			}(),
    58  			valid: false,
    59  		},
    60  		{
    61  			name:     "valid aws",
    62  			platform: &types.Platform{AWS: &aws.Platform{Region: "us-east-1"}},
    63  			pool: func() *types.MachinePool {
    64  				p := validMachinePool("test-name")
    65  				p.Platform = types.MachinePoolPlatform{
    66  					AWS: &aws.MachinePool{},
    67  				}
    68  				return p
    69  			}(),
    70  			valid: true,
    71  		},
    72  		{
    73  			name:     "invalid aws",
    74  			platform: &types.Platform{AWS: &aws.Platform{Region: "us-east-1"}},
    75  			pool: func() *types.MachinePool {
    76  				p := validMachinePool("test-name")
    77  				p.Platform = types.MachinePoolPlatform{
    78  					AWS: &aws.MachinePool{
    79  						EC2RootVolume: aws.EC2RootVolume{
    80  							Type: "io1",
    81  							Size: 128,
    82  							IOPS: -10,
    83  						},
    84  					},
    85  				}
    86  				return p
    87  			}(),
    88  			valid: false,
    89  		},
    90  		{
    91  			name:     "valid azure",
    92  			platform: &types.Platform{Azure: &azure.Platform{Region: "eastus"}},
    93  			pool: func() *types.MachinePool {
    94  				p := validMachinePool("test-name")
    95  				p.Platform = types.MachinePoolPlatform{
    96  					Azure: &azure.MachinePool{},
    97  				}
    98  				return p
    99  			}(),
   100  			valid: true,
   101  		},
   102  		{
   103  			name:     "valid openstack",
   104  			platform: &types.Platform{OpenStack: &openstack.Platform{}},
   105  			pool: func() *types.MachinePool {
   106  				p := validMachinePool("test-name")
   107  				p.Platform = types.MachinePoolPlatform{
   108  					OpenStack: &openstack.MachinePool{},
   109  				}
   110  				return p
   111  			}(),
   112  			valid: true,
   113  		},
   114  		{
   115  			name:     "mis-matched platform",
   116  			platform: &types.Platform{IBMCloud: &ibmcloud.Platform{}},
   117  			pool: func() *types.MachinePool {
   118  				p := validMachinePool("test-name")
   119  				p.Platform = types.MachinePoolPlatform{
   120  					AWS: &aws.MachinePool{},
   121  				}
   122  				return p
   123  			}(),
   124  			valid: false,
   125  		},
   126  		{
   127  			name:     "multiple platforms",
   128  			platform: &types.Platform{AWS: &aws.Platform{Region: "us-east-1"}},
   129  			pool: func() *types.MachinePool {
   130  				p := validMachinePool("test-name")
   131  				p.Platform = types.MachinePoolPlatform{
   132  					AWS:      &aws.MachinePool{},
   133  					IBMCloud: &ibmcloud.MachinePool{},
   134  				}
   135  				return p
   136  			}(),
   137  			valid: false,
   138  		},
   139  		{
   140  			name:     "valid GCP",
   141  			platform: &types.Platform{GCP: &gcp.Platform{Region: "us-east-1"}},
   142  			pool: func() *types.MachinePool {
   143  				p := validMachinePool("test-name")
   144  				p.Platform = types.MachinePoolPlatform{
   145  					GCP: &gcp.MachinePool{},
   146  				}
   147  				p.Platform.GCP.OSDisk.DiskSizeGB = 100
   148  				p.Platform.GCP.OSDisk.DiskType = "pd-standard"
   149  				return p
   150  			}(),
   151  			valid: true,
   152  		},
   153  		{
   154  			name:     "invalid GCP disk size",
   155  			platform: &types.Platform{GCP: &gcp.Platform{Region: "us-east-1"}},
   156  			pool: func() *types.MachinePool {
   157  				p := validMachinePool("test-name")
   158  				p.Platform = types.MachinePoolPlatform{
   159  					GCP: &gcp.MachinePool{},
   160  				}
   161  				p.Platform.GCP.OSDisk.DiskSizeGB = -100
   162  				p.Platform.GCP.OSDisk.DiskType = "pd-standard"
   163  				return p
   164  			}(),
   165  			valid: false,
   166  		},
   167  		{
   168  			name:     "invalid GCP disk type",
   169  			platform: &types.Platform{GCP: &gcp.Platform{Region: "us-east-1"}},
   170  			pool: func() *types.MachinePool {
   171  				p := validMachinePool("test-name")
   172  				p.Platform = types.MachinePoolPlatform{
   173  					GCP: &gcp.MachinePool{},
   174  				}
   175  				p.Platform.GCP.OSDisk.DiskSizeGB = 100
   176  				p.Platform.GCP.OSDisk.DiskType = "pd-"
   177  				return p
   178  			}(),
   179  			valid: false,
   180  		},
   181  		{
   182  			name:     "valid GCP service account use",
   183  			platform: &types.Platform{GCP: &gcp.Platform{Region: "us-east-1", NetworkProjectID: "ExampleNetworkProject"}},
   184  			pool: func() *types.MachinePool {
   185  				p := validMachinePool("master")
   186  				p.Platform = types.MachinePoolPlatform{
   187  					GCP: &gcp.MachinePool{
   188  						ServiceAccount: "ExampleServiceAccount@ExampleServiceAccount.com",
   189  					},
   190  				}
   191  				return p
   192  			}(),
   193  			valid: true,
   194  		},
   195  		{
   196  			name:     "invalid GCP service account on machine pool type",
   197  			platform: &types.Platform{GCP: &gcp.Platform{Region: "us-east-1"}},
   198  			pool: func() *types.MachinePool {
   199  				p := validMachinePool("worker")
   200  				p.Platform = types.MachinePoolPlatform{
   201  					GCP: &gcp.MachinePool{
   202  						ServiceAccount: "ExampleServiceAccount@ExampleServiceAccount.com",
   203  					},
   204  				}
   205  				return p
   206  			}(),
   207  			valid: true,
   208  		},
   209  		{
   210  			name:     "invalid GCP service account non xpn install",
   211  			platform: &types.Platform{GCP: &gcp.Platform{Region: "us-east-1"}},
   212  			pool: func() *types.MachinePool {
   213  				p := validMachinePool("master")
   214  				p.Platform = types.MachinePoolPlatform{
   215  					GCP: &gcp.MachinePool{
   216  						ServiceAccount: "ExampleServiceAccount@ExampleServiceAccount.com",
   217  					},
   218  				}
   219  				return p
   220  			}(),
   221  			valid: true,
   222  		},
   223  	}
   224  	for _, tc := range cases {
   225  		t.Run(tc.name, func(t *testing.T) {
   226  			err := ValidateMachinePool(tc.platform, tc.pool, field.NewPath("test-path")).ToAggregate()
   227  			if tc.valid {
   228  				assert.NoError(t, err)
   229  			} else {
   230  				assert.Error(t, err)
   231  			}
   232  		})
   233  	}
   234  }