github.com/openshift/installer@v1.4.17/pkg/asset/ignition/machine/worker_ignition_customizations_test.go (about)

     1  package machine
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/openshift/installer/pkg/asset"
    10  	"github.com/openshift/installer/pkg/asset/ignition"
    11  	"github.com/openshift/installer/pkg/asset/installconfig"
    12  	"github.com/openshift/installer/pkg/asset/tls"
    13  	"github.com/openshift/installer/pkg/ipnet"
    14  	"github.com/openshift/installer/pkg/types"
    15  	"github.com/openshift/installer/pkg/types/aws"
    16  )
    17  
    18  // TestWorkerIgnitionCustomizationsGenerate tests generating the worker ignition check asset.
    19  func TestWorkerIgnitionCustomizationsGenerate(t *testing.T) {
    20  	cases := []struct {
    21  		name          string
    22  		customize     bool
    23  		assetExpected bool
    24  	}{
    25  		{
    26  			name:          "not customized",
    27  			customize:     false,
    28  			assetExpected: false,
    29  		},
    30  		{
    31  			name:          "pointer customized",
    32  			customize:     true,
    33  			assetExpected: true,
    34  		},
    35  	}
    36  
    37  	for _, tc := range cases {
    38  		t.Run(tc.name, func(t *testing.T) {
    39  			installConfig := installconfig.MakeAsset(
    40  				&types.InstallConfig{
    41  					Networking: &types.Networking{
    42  						ServiceNetwork: []ipnet.IPNet{*ipnet.MustParseCIDR("10.0.1.0/24")},
    43  					},
    44  					Platform: types.Platform{
    45  						AWS: &aws.Platform{
    46  							Region: "us-east",
    47  						},
    48  					},
    49  				})
    50  
    51  			rootCA := &tls.RootCA{}
    52  			err := rootCA.Generate(context.Background(), nil)
    53  			assert.NoError(t, err, "unexpected error generating root CA")
    54  
    55  			parents := asset.Parents{}
    56  			parents.Add(installConfig, rootCA)
    57  
    58  			worker := &Worker{}
    59  			err = worker.Generate(context.Background(), parents)
    60  			assert.NoError(t, err, "unexpected error generating worker asset")
    61  
    62  			if tc.customize == true {
    63  				// Modify the worker config, emulating a customization to the pointer
    64  				worker.Config.Storage.Files = append(worker.Config.Storage.Files,
    65  					ignition.FileFromString("/etc/foo", "root", 0644, "foo"))
    66  			}
    67  
    68  			parents.Add(worker)
    69  			workerIgnCheck := &WorkerIgnitionCustomizations{}
    70  			err = workerIgnCheck.Generate(context.Background(), parents)
    71  			assert.NoError(t, err, "unexpected error generating worker ignition check asset")
    72  
    73  			actualFiles := workerIgnCheck.Files()
    74  			if tc.assetExpected == true {
    75  				assert.Equal(t, 1, len(actualFiles), "unexpected number of files in worker state")
    76  				assert.Equal(t, workerMachineConfigFileName, actualFiles[0].Filename, "unexpected name for worker ignition config")
    77  			} else {
    78  				assert.Equal(t, 0, len(actualFiles), "unexpected number of files in worker state")
    79  			}
    80  		})
    81  	}
    82  }