github.com/openshift/installer@v1.4.17/pkg/asset/agent/agentconfig/agent_config_test.go (about)

     1  package agentconfig
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	"github.com/stretchr/testify/assert"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  
    12  	"github.com/openshift/installer/pkg/asset"
    13  	"github.com/openshift/installer/pkg/asset/mock"
    14  	"github.com/openshift/installer/pkg/types/agent"
    15  )
    16  
    17  func TestAgentConfig_LoadedFromDisk(t *testing.T) {
    18  	cases := []struct {
    19  		name       string
    20  		data       string
    21  		fetchError error
    22  
    23  		expectedError  string
    24  		expectedFound  bool
    25  		expectedConfig *AgentConfigBuilder
    26  	}{
    27  		{
    28  			name: "valid-config-single-node",
    29  			data: `
    30  apiVersion: v1beta1
    31  metadata:
    32    name: agent-config-cluster0
    33  rendezvousIP: 192.168.111.80
    34  bootArtifactsBaseURL: http://user-specified-pxe-infra.com`,
    35  			expectedFound:  true,
    36  			expectedConfig: agentConfig().bootArtifactsBaseURL("http://user-specified-pxe-infra.com"),
    37  		},
    38  		{
    39  			name: "not-yaml",
    40  			data: `This is not a yaml file`,
    41  
    42  			expectedFound: false,
    43  			expectedError: "failed to unmarshal agent-config.yaml: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type agent.Config",
    44  		},
    45  		{
    46  			name:       "file-not-found",
    47  			fetchError: &os.PathError{Err: os.ErrNotExist},
    48  
    49  			expectedFound: false,
    50  		},
    51  		{
    52  			name:       "error-fetching-file",
    53  			fetchError: errors.New("fetch failed"),
    54  
    55  			expectedFound: false,
    56  			expectedError: "failed to load agent-config.yaml file: fetch failed",
    57  		},
    58  		{
    59  			name: "unknown-field",
    60  			data: `
    61  apiVersion: v1beta1
    62  metadata:
    63    name: agent-config-wrong
    64  wrongField: wrongValue`,
    65  
    66  			expectedFound: false,
    67  			expectedError: "failed to unmarshal agent-config.yaml: error unmarshaling JSON: while decoding JSON: json: unknown field \"wrongField\"",
    68  		},
    69  		{
    70  			name: "empty-rendezvousIP",
    71  			data: `
    72  apiVersion: v1beta1
    73  metadata:
    74    name: agent-config-cluster0`,
    75  
    76  			expectedFound:  true,
    77  			expectedConfig: agentConfig().rendezvousIP(""),
    78  		},
    79  		{
    80  			name: "invalid-rendezvousIP",
    81  			data: `
    82  apiVersion: v1beta1
    83  metadata:
    84    name: agent-config-cluster0
    85  rendezvousIP: not-a-valid-ip`,
    86  
    87  			expectedFound: false,
    88  			expectedError: "invalid Agent Config configuration: rendezvousIP: Invalid value: \"not-a-valid-ip\": \"not-a-valid-ip\" is not a valid IP",
    89  		},
    90  		{
    91  			name: "empty-bootArtifactsBaseURL",
    92  			data: `
    93  apiVersion: v1alpha1
    94  metadata:
    95    name: agent-config-cluster0
    96  rendezvousIP: 192.168.111.80`,
    97  
    98  			expectedFound:  true,
    99  			expectedConfig: agentConfig(),
   100  		},
   101  		{
   102  			name: "invalid-bootArtifactsBaseURL",
   103  			data: `
   104  apiVersion: v1alpha1
   105  metadata:
   106    name: agent-config-cluster0
   107  bootArtifactsBaseURL: not-a-valid-url`,
   108  
   109  			expectedFound: false,
   110  			expectedError: "invalid Agent Config configuration: bootArtifactsBaseURL: Invalid value: \"not-a-valid-url\": invalid URI \"not-a-valid-url\" (no scheme)",
   111  		},
   112  		{
   113  			name: "invalid-additionalNTPSourceDomain",
   114  			data: `
   115  apiVersion: v1beta1
   116  metadata:
   117    name: agent-config-cluster0
   118  additionalNTPSources:
   119    - 0.fedora.pool.ntp.org
   120    - 1.fedora.pool.ntp.org
   121    - 192.168.111.14
   122    - fd10:39:192:1::1337
   123    - invalid_pool.ntp.org
   124  rendezvousIP: 192.168.111.80`,
   125  			expectedFound: false,
   126  			expectedError: "invalid Agent Config configuration: AdditionalNTPSources[4]: Invalid value: \"invalid_pool.ntp.org\": NTP source is not a valid domain name nor a valid IP",
   127  		},
   128  	}
   129  	for _, tc := range cases {
   130  		t.Run(tc.name, func(t *testing.T) {
   131  			mockCtrl := gomock.NewController(t)
   132  			defer mockCtrl.Finish()
   133  
   134  			fileFetcher := mock.NewMockFileFetcher(mockCtrl)
   135  			fileFetcher.EXPECT().FetchByName(agentConfigFilename).
   136  				Return(
   137  					&asset.File{
   138  						Filename: agentConfigFilename,
   139  						Data:     []byte(tc.data)},
   140  					tc.fetchError,
   141  				)
   142  
   143  			asset := &AgentConfig{}
   144  			found, err := asset.Load(fileFetcher)
   145  			assert.Equal(t, tc.expectedFound, found)
   146  			if tc.expectedError != "" {
   147  				assert.Equal(t, tc.expectedError, err.Error())
   148  			} else {
   149  				assert.NoError(t, err)
   150  				if tc.expectedConfig != nil {
   151  					assert.Equal(t, tc.expectedConfig.build(), asset.Config, "unexpected Config in AgentConfig")
   152  				}
   153  			}
   154  		})
   155  	}
   156  }
   157  
   158  // AgentConfigBuilder it's a builder class to make it easier creating agent.Config instance
   159  // used in the test cases.
   160  type AgentConfigBuilder struct {
   161  	agent.Config
   162  }
   163  
   164  func agentConfig() *AgentConfigBuilder {
   165  	return &AgentConfigBuilder{
   166  		Config: agent.Config{
   167  			ObjectMeta: metav1.ObjectMeta{
   168  				Name: "agent-config-cluster0",
   169  			},
   170  			TypeMeta: metav1.TypeMeta{
   171  				APIVersion: agent.AgentConfigVersion,
   172  			},
   173  			RendezvousIP: "192.168.111.80",
   174  		},
   175  	}
   176  }
   177  
   178  func (acb *AgentConfigBuilder) build() *agent.Config {
   179  	return &acb.Config
   180  }
   181  
   182  func (acb *AgentConfigBuilder) rendezvousIP(ip string) *AgentConfigBuilder {
   183  	acb.Config.RendezvousIP = ip
   184  	return acb
   185  }
   186  
   187  func (acb *AgentConfigBuilder) bootArtifactsBaseURL(url string) *AgentConfigBuilder {
   188  	acb.Config.BootArtifactsBaseURL = url
   189  	return acb
   190  }