github.com/openshift/installer@v1.4.17/pkg/asset/imagebased/configimage/imagebased_config_test.go (about)

     1  package configimage
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"os/exec"
     7  	"testing"
     8  
     9  	"github.com/golang/mock/gomock"
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/openshift/installer/pkg/asset"
    13  	"github.com/openshift/installer/pkg/asset/mock"
    14  	"github.com/openshift/installer/pkg/types/imagebased"
    15  )
    16  
    17  func TestImageBasedConfig_LoadedFromDisk(t *testing.T) {
    18  	skipTestIfnmstatectlIsMissing(t)
    19  
    20  	cases := []struct {
    21  		name       string
    22  		data       string
    23  		fetchError error
    24  
    25  		expectedError  string
    26  		expectedFound  bool
    27  		expectedConfig *imagebased.Config
    28  	}{
    29  		{
    30  			name: "valid-config",
    31  			data: `
    32  apiVersion: v1beta1
    33  metadata:
    34    name: image-based-config
    35  hostname: somehostname
    36  releaseRegistry: quay.io
    37  networkConfig:
    38    interfaces:
    39      - name: eth0
    40        type: ethernet
    41        state: up
    42        mac-address: 00:00:00:00:00:00
    43        ipv4:
    44          enabled: true
    45          address:
    46            - ip: 192.168.122.2
    47              prefix-length: 23
    48          dhcp: false`,
    49  			expectedFound:  true,
    50  			expectedConfig: imageBasedConfig().Config,
    51  		},
    52  		{
    53  			name: "not-yaml",
    54  			data: `This is not a yaml file`,
    55  
    56  			expectedFound: false,
    57  			expectedError: "failed to unmarshal image-based-config.yaml: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type imagebased.Config",
    58  		},
    59  		{
    60  			name:       "file-not-found",
    61  			fetchError: &os.PathError{Err: os.ErrNotExist},
    62  
    63  			expectedFound: false,
    64  		},
    65  		{
    66  			name:       "error-fetching-file",
    67  			fetchError: errors.New("fetch failed"),
    68  
    69  			expectedFound: false,
    70  			expectedError: "failed to load image-based-config.yaml file: fetch failed",
    71  		},
    72  		{
    73  			name: "unknown-field",
    74  			data: `
    75  apiVersion: v1beta1
    76  metadata:
    77    name: image-based-config-wrong
    78  wrongField: wrongValue`,
    79  
    80  			expectedFound: false,
    81  			expectedError: "failed to unmarshal image-based-config.yaml: error unmarshaling JSON: while decoding JSON: json: unknown field \"wrongField\"",
    82  		},
    83  		{
    84  			name: "invalid-nmstate-config",
    85  			data: `
    86  apiVersion: v1beta1
    87  metadata:
    88    name: image-based-config
    89  hostname: somehostname
    90  releaseRegistry: quay.io
    91  networkConfig:
    92    interfaces:
    93      - name: eth0
    94        type: ethernet
    95        state: invalid`,
    96  
    97  			expectedFound: false,
    98  			expectedError: "networkConfig: Invalid value: interfaces:\n- name: eth0\n  state: invalid\n  type: ethernet\n: failed to execute 'nmstatectl gc', error: InvalidArgument: Invalid YAML string: interfaces: unknown variant `invalid`",
    99  		},
   100  		{
   101  			name: "invalid-additional-ntp-sources",
   102  			data: `
   103  apiVersion: v1beta1
   104  metadata:
   105    name: image-based-config
   106  hostname: somehostname
   107  releaseRegistry: quay.io
   108  additionalNTPSources:
   109    - invalid-domain@
   110  networkConfig:
   111    interfaces:
   112      - name: eth0
   113        type: ethernet
   114        state: up`,
   115  
   116  			expectedFound: false,
   117  			expectedError: "invalid Image-based Config configuration: additionalNTPSources[0]: Invalid value: \"invalid-domain@\": NTP source is not a valid domain name nor a valid IP",
   118  		},
   119  	}
   120  	for _, tc := range cases {
   121  		t.Run(tc.name, func(t *testing.T) {
   122  			mockCtrl := gomock.NewController(t)
   123  			defer mockCtrl.Finish()
   124  
   125  			fileFetcher := mock.NewMockFileFetcher(mockCtrl)
   126  			fileFetcher.EXPECT().FetchByName(imageBasedConfigFilename).
   127  				Return(
   128  					&asset.File{
   129  						Filename: imageBasedConfigFilename,
   130  						Data:     []byte(tc.data)},
   131  					tc.fetchError,
   132  				)
   133  
   134  			asset := &ImageBasedConfig{}
   135  			found, err := asset.Load(fileFetcher)
   136  			assert.Equal(t, tc.expectedFound, found)
   137  			if tc.expectedError != "" {
   138  				assert.ErrorContains(t, err, tc.expectedError)
   139  			} else {
   140  				assert.NoError(t, err)
   141  				if tc.expectedConfig != nil {
   142  					assert.Equal(t, tc.expectedConfig, asset.Config, "unexpected Config in ImageBasedConfig")
   143  				}
   144  			}
   145  		})
   146  	}
   147  }
   148  
   149  func skipTestIfnmstatectlIsMissing(t *testing.T) {
   150  	t.Helper()
   151  
   152  	_, execErr := exec.LookPath("nmstatectl")
   153  	if execErr != nil {
   154  		t.Skip("No nmstatectl binary available")
   155  	}
   156  }