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

     1  package defaults
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/openshift/installer/pkg/types"
     9  )
    10  
    11  func defaultMachinePool(name string) *types.MachinePool {
    12  	repCount := int64(3)
    13  	return &types.MachinePool{
    14  		Name:           name,
    15  		Replicas:       &repCount,
    16  		Hyperthreading: types.HyperthreadingEnabled,
    17  		Architecture:   types.ArchitectureAMD64,
    18  	}
    19  }
    20  
    21  func defaultEdgeMachinePool(name string) *types.MachinePool {
    22  	pool := defaultMachinePool(name)
    23  	defaultEdgeReplicaCount := int64(0)
    24  	pool.Replicas = &defaultEdgeReplicaCount
    25  	return pool
    26  }
    27  
    28  func TestSetMahcinePoolDefaults(t *testing.T) {
    29  	defaultEdgeReplicaCount := int64(0)
    30  	cases := []struct {
    31  		name     string
    32  		pool     *types.MachinePool
    33  		platform string
    34  		expected *types.MachinePool
    35  	}{
    36  		{
    37  			name:     "empty",
    38  			pool:     &types.MachinePool{},
    39  			expected: defaultMachinePool(""),
    40  		},
    41  		{
    42  			name:     "empty",
    43  			pool:     &types.MachinePool{Replicas: &defaultEdgeReplicaCount},
    44  			expected: defaultEdgeMachinePool(""),
    45  		},
    46  		{
    47  			name:     "default",
    48  			pool:     defaultMachinePool("test-name"),
    49  			expected: defaultMachinePool("test-name"),
    50  		},
    51  		{
    52  			name:     "default",
    53  			pool:     defaultEdgeMachinePool("test-name"),
    54  			expected: defaultEdgeMachinePool("test-name"),
    55  		},
    56  		{
    57  			name: "non-default replicas",
    58  			pool: func() *types.MachinePool {
    59  				p := defaultMachinePool("test-name")
    60  				repCount := int64(5)
    61  				p.Replicas = &repCount
    62  				return p
    63  			}(),
    64  			expected: func() *types.MachinePool {
    65  				p := defaultMachinePool("test-name")
    66  				repCount := int64(5)
    67  				p.Replicas = &repCount
    68  				return p
    69  			}(),
    70  		},
    71  		{
    72  			name: "non-default replicas",
    73  			pool: func() *types.MachinePool {
    74  				p := defaultEdgeMachinePool("test-name")
    75  				repCount := int64(5)
    76  				p.Replicas = &repCount
    77  				return p
    78  			}(),
    79  			expected: func() *types.MachinePool {
    80  				p := defaultEdgeMachinePool("test-name")
    81  				repCount := int64(5)
    82  				p.Replicas = &repCount
    83  				return p
    84  			}(),
    85  		},
    86  		{
    87  			name: "non-default hyperthreading",
    88  			pool: func() *types.MachinePool {
    89  				p := defaultMachinePool("test-name")
    90  				p.Hyperthreading = types.HyperthreadingMode("test-hyperthreading")
    91  				return p
    92  			}(),
    93  			expected: func() *types.MachinePool {
    94  				p := defaultMachinePool("test-name")
    95  				p.Hyperthreading = types.HyperthreadingMode("test-hyperthreading")
    96  				return p
    97  			}(),
    98  		},
    99  		{
   100  			name: "non-default hyperthreading",
   101  			pool: func() *types.MachinePool {
   102  				p := defaultEdgeMachinePool("test-name")
   103  				p.Hyperthreading = types.HyperthreadingMode("test-hyperthreading")
   104  				return p
   105  			}(),
   106  			expected: func() *types.MachinePool {
   107  				p := defaultEdgeMachinePool("test-name")
   108  				p.Hyperthreading = types.HyperthreadingMode("test-hyperthreading")
   109  				return p
   110  			}(),
   111  		},
   112  	}
   113  	for _, tc := range cases {
   114  		t.Run(tc.name, func(t *testing.T) {
   115  			SetMachinePoolDefaults(tc.pool, tc.platform)
   116  			assert.Equal(t, tc.expected, tc.pool, "unexpected machine pool")
   117  		})
   118  	}
   119  }
   120  
   121  func TestHasEdgePoolConfig(t *testing.T) {
   122  	cases := []struct {
   123  		name     string
   124  		pool     []types.MachinePool
   125  		expected bool
   126  	}{
   127  		{
   128  			name:     "empty",
   129  			pool:     []types.MachinePool{*defaultMachinePool("non-edge")},
   130  			expected: false,
   131  		}, {
   132  			name:     "worker",
   133  			pool:     []types.MachinePool{*defaultMachinePool("worker")},
   134  			expected: false,
   135  		}, {
   136  			name:     "edge",
   137  			pool:     []types.MachinePool{*defaultEdgeMachinePool("edge")},
   138  			expected: true,
   139  		}, {
   140  			name:     "edge",
   141  			pool:     []types.MachinePool{*defaultEdgeMachinePool("edge"), *defaultMachinePool("non-edge")},
   142  			expected: true,
   143  		}, {
   144  			name:     "edge",
   145  			pool:     []types.MachinePool{*defaultEdgeMachinePool("edge"), *defaultMachinePool("worker")},
   146  			expected: true,
   147  		},
   148  	}
   149  
   150  	for _, tc := range cases {
   151  		t.Run(tc.name, func(t *testing.T) {
   152  			res := hasEdgePoolConfig(tc.pool)
   153  			assert.Equal(t, tc.expected, res, "unexpected machine pool")
   154  		})
   155  	}
   156  }