github.com/openshift/installer@v1.4.17/pkg/asset/machines/worker_test.go (about)

     1  package machines
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/utils/pointer"
    10  
    11  	"github.com/openshift/installer/pkg/asset"
    12  	"github.com/openshift/installer/pkg/asset/ignition/machine"
    13  	"github.com/openshift/installer/pkg/asset/installconfig"
    14  	"github.com/openshift/installer/pkg/asset/rhcos"
    15  	"github.com/openshift/installer/pkg/types"
    16  	awstypes "github.com/openshift/installer/pkg/types/aws"
    17  )
    18  
    19  func TestWorkerGenerate(t *testing.T) {
    20  	cases := []struct {
    21  		name                  string
    22  		key                   string
    23  		hyperthreading        types.HyperthreadingMode
    24  		expectedMachineConfig []string
    25  	}{
    26  		{
    27  			name:           "no key hyperthreading enabled",
    28  			hyperthreading: types.HyperthreadingEnabled,
    29  		},
    30  		{
    31  			name:           "key present hyperthreading enabled",
    32  			key:            "ssh-rsa: dummy-key",
    33  			hyperthreading: types.HyperthreadingEnabled,
    34  			expectedMachineConfig: []string{`apiVersion: machineconfiguration.openshift.io/v1
    35  kind: MachineConfig
    36  metadata:
    37    creationTimestamp: null
    38    labels:
    39      machineconfiguration.openshift.io/role: worker
    40    name: 99-worker-ssh
    41  spec:
    42    baseOSExtensionsContainerImage: ""
    43    config:
    44      ignition:
    45        version: 3.2.0
    46      passwd:
    47        users:
    48        - name: core
    49          sshAuthorizedKeys:
    50          - 'ssh-rsa: dummy-key'
    51    extensions: null
    52    fips: false
    53    kernelArguments: null
    54    kernelType: ""
    55    osImageURL: ""
    56  `},
    57  		},
    58  		{
    59  			name:           "no key hyperthreading disabled",
    60  			hyperthreading: types.HyperthreadingDisabled,
    61  			expectedMachineConfig: []string{`apiVersion: machineconfiguration.openshift.io/v1
    62  kind: MachineConfig
    63  metadata:
    64    creationTimestamp: null
    65    labels:
    66      machineconfiguration.openshift.io/role: worker
    67    name: 99-worker-disable-hyperthreading
    68  spec:
    69    baseOSExtensionsContainerImage: ""
    70    config:
    71      ignition:
    72        version: 3.2.0
    73    extensions: null
    74    fips: false
    75    kernelArguments:
    76    - nosmt
    77    - smt-enabled=off
    78    kernelType: ""
    79    osImageURL: ""
    80  `},
    81  		},
    82  		{
    83  			name:           "key present hyperthreading disabled",
    84  			key:            "ssh-rsa: dummy-key",
    85  			hyperthreading: types.HyperthreadingDisabled,
    86  			expectedMachineConfig: []string{`apiVersion: machineconfiguration.openshift.io/v1
    87  kind: MachineConfig
    88  metadata:
    89    creationTimestamp: null
    90    labels:
    91      machineconfiguration.openshift.io/role: worker
    92    name: 99-worker-disable-hyperthreading
    93  spec:
    94    baseOSExtensionsContainerImage: ""
    95    config:
    96      ignition:
    97        version: 3.2.0
    98    extensions: null
    99    fips: false
   100    kernelArguments:
   101    - nosmt
   102    - smt-enabled=off
   103    kernelType: ""
   104    osImageURL: ""
   105  `, `apiVersion: machineconfiguration.openshift.io/v1
   106  kind: MachineConfig
   107  metadata:
   108    creationTimestamp: null
   109    labels:
   110      machineconfiguration.openshift.io/role: worker
   111    name: 99-worker-ssh
   112  spec:
   113    baseOSExtensionsContainerImage: ""
   114    config:
   115      ignition:
   116        version: 3.2.0
   117      passwd:
   118        users:
   119        - name: core
   120          sshAuthorizedKeys:
   121          - 'ssh-rsa: dummy-key'
   122    extensions: null
   123    fips: false
   124    kernelArguments: null
   125    kernelType: ""
   126    osImageURL: ""
   127  `},
   128  		},
   129  	}
   130  	for _, tc := range cases {
   131  		t.Run(tc.name, func(t *testing.T) {
   132  			parents := asset.Parents{}
   133  			parents.Add(
   134  				&installconfig.ClusterID{
   135  					UUID:    "test-uuid",
   136  					InfraID: "test-infra-id",
   137  				},
   138  				installconfig.MakeAsset(
   139  					&types.InstallConfig{
   140  						ObjectMeta: metav1.ObjectMeta{
   141  							Name: "test-cluster",
   142  						},
   143  						SSHKey:     tc.key,
   144  						BaseDomain: "test-domain",
   145  						Platform: types.Platform{
   146  							AWS: &awstypes.Platform{
   147  								Region: "us-east-1",
   148  							},
   149  						},
   150  						Compute: []types.MachinePool{
   151  							{
   152  								Replicas:       pointer.Int64Ptr(1),
   153  								Hyperthreading: tc.hyperthreading,
   154  								Platform: types.MachinePoolPlatform{
   155  									AWS: &awstypes.MachinePool{
   156  										Zones:        []string{"us-east-1a"},
   157  										InstanceType: "m5.large",
   158  									},
   159  								},
   160  							},
   161  						},
   162  					}),
   163  				rhcos.MakeAsset("test-image"),
   164  				(*rhcos.Release)(pointer.StringPtr("412.86.202208101040-0")),
   165  				&machine.Worker{
   166  					File: &asset.File{
   167  						Filename: "worker-ignition",
   168  						Data:     []byte("test-ignition"),
   169  					},
   170  				},
   171  			)
   172  			worker := &Worker{}
   173  			if err := worker.Generate(context.Background(), parents); err != nil {
   174  				t.Fatalf("failed to generate worker machines: %v", err)
   175  			}
   176  			expectedLen := len(tc.expectedMachineConfig)
   177  			if assert.Equal(t, expectedLen, len(worker.MachineConfigFiles)) {
   178  				for i := 0; i < expectedLen; i++ {
   179  					assert.Equal(t, tc.expectedMachineConfig[i], string(worker.MachineConfigFiles[i].Data), "unexepcted machine config contents")
   180  				}
   181  			} else {
   182  				assert.Equal(t, 0, len(worker.MachineConfigFiles), "expected no machine config files")
   183  			}
   184  		})
   185  	}
   186  }
   187  
   188  func TestComputeIsNotModified(t *testing.T) {
   189  	parents := asset.Parents{}
   190  	installConfig := installconfig.MakeAsset(
   191  		&types.InstallConfig{
   192  			ObjectMeta: metav1.ObjectMeta{
   193  				Name: "test-cluster",
   194  			},
   195  			SSHKey:     "ssh-rsa: dummy-key",
   196  			BaseDomain: "test-domain",
   197  			Platform: types.Platform{
   198  				AWS: &awstypes.Platform{
   199  					Region: "us-east-1",
   200  					DefaultMachinePlatform: &awstypes.MachinePool{
   201  						InstanceType: "TEST_INSTANCE_TYPE",
   202  					},
   203  				},
   204  			},
   205  			Compute: []types.MachinePool{
   206  				{
   207  					Replicas:       pointer.Int64Ptr(1),
   208  					Hyperthreading: types.HyperthreadingDisabled,
   209  					Platform: types.MachinePoolPlatform{
   210  						AWS: &awstypes.MachinePool{
   211  							Zones:        []string{"us-east-1a"},
   212  							InstanceType: "",
   213  						},
   214  					},
   215  				},
   216  			},
   217  		})
   218  
   219  	parents.Add(
   220  		&installconfig.ClusterID{
   221  			UUID:    "test-uuid",
   222  			InfraID: "test-infra-id",
   223  		},
   224  		installConfig,
   225  		rhcos.MakeAsset("test-image"),
   226  		(*rhcos.Release)(pointer.StringPtr("412.86.202208101040-0")),
   227  		&machine.Worker{
   228  			File: &asset.File{
   229  				Filename: "worker-ignition",
   230  				Data:     []byte("test-ignition"),
   231  			},
   232  		},
   233  	)
   234  	worker := &Worker{}
   235  	if err := worker.Generate(context.Background(), parents); err != nil {
   236  		t.Fatalf("failed to generate master machines: %v", err)
   237  	}
   238  
   239  	if installConfig.Config.Compute[0].Platform.AWS.Type != "" {
   240  		t.Fatalf("compute in the install config has been modified")
   241  	}
   242  }
   243  
   244  func TestDefaultAWSMachinePoolPlatform(t *testing.T) {
   245  	type testCase struct {
   246  		name                string
   247  		poolName            string
   248  		expectedMachinePool awstypes.MachinePool
   249  		assert              func(tc *testCase)
   250  	}
   251  	cases := []testCase{
   252  		{
   253  			name:     "default EBS type for compute pool",
   254  			poolName: types.MachinePoolComputeRoleName,
   255  			expectedMachinePool: awstypes.MachinePool{
   256  				EC2RootVolume: awstypes.EC2RootVolume{
   257  					Type: awstypes.VolumeTypeGp3,
   258  					Size: decimalRootVolumeSize,
   259  				},
   260  			},
   261  			assert: func(tc *testCase) {
   262  				mp := defaultAWSMachinePoolPlatform(tc.poolName)
   263  				want := tc.expectedMachinePool.EC2RootVolume.Type
   264  				got := mp.EC2RootVolume.Type
   265  				assert.Equal(t, want, got, "unexepcted EBS type")
   266  			},
   267  		},
   268  		{
   269  			name:     "default EBS type for edge pool",
   270  			poolName: types.MachinePoolEdgeRoleName,
   271  			expectedMachinePool: awstypes.MachinePool{
   272  				EC2RootVolume: awstypes.EC2RootVolume{
   273  					Type: awstypes.VolumeTypeGp2,
   274  					Size: decimalRootVolumeSize,
   275  				},
   276  			},
   277  			assert: func(tc *testCase) {
   278  				mp := defaultAWSMachinePoolPlatform(tc.poolName)
   279  				want := tc.expectedMachinePool.EC2RootVolume.Type
   280  				got := mp.EC2RootVolume.Type
   281  				assert.Equal(t, want, got, "unexepcted EBS type")
   282  			},
   283  		},
   284  	}
   285  	for i := range cases {
   286  		tc := cases[i]
   287  		t.Run(tc.name, func(t *testing.T) {
   288  			tc.assert(&tc)
   289  		})
   290  	}
   291  }