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

     1  package manifests
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	"github.com/pkg/errors"
    10  	"github.com/stretchr/testify/assert"
    11  	corev1 "k8s.io/api/core/v1"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  	"k8s.io/apimachinery/pkg/util/yaml"
    14  
    15  	hivev1 "github.com/openshift/hive/apis/hive/v1"
    16  	hivev1agent "github.com/openshift/hive/apis/hive/v1/agent"
    17  	"github.com/openshift/installer/pkg/asset"
    18  	"github.com/openshift/installer/pkg/asset/agent"
    19  	"github.com/openshift/installer/pkg/asset/agent/workflow"
    20  	"github.com/openshift/installer/pkg/asset/mock"
    21  )
    22  
    23  func TestClusterDeployment_Generate(t *testing.T) {
    24  
    25  	cases := []struct {
    26  		name           string
    27  		dependencies   []asset.Asset
    28  		expectedError  string
    29  		expectedConfig *hivev1.ClusterDeployment
    30  	}{
    31  		{
    32  			name: "missing config",
    33  			dependencies: []asset.Asset{
    34  				&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
    35  				&agent.OptionalInstallConfig{},
    36  			},
    37  			expectedError: "missing configuration or manifest file",
    38  		},
    39  		{
    40  			name: "valid configurations",
    41  			dependencies: []asset.Asset{
    42  				&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
    43  				getValidOptionalInstallConfig(),
    44  			},
    45  			expectedConfig: &hivev1.ClusterDeployment{
    46  				TypeMeta: metav1.TypeMeta{
    47  					Kind:       "ClusterDeployment",
    48  					APIVersion: "hive.openshift.io/v1",
    49  				},
    50  				ObjectMeta: metav1.ObjectMeta{
    51  					Name:      getClusterDeploymentName(getValidOptionalInstallConfig()),
    52  					Namespace: getValidOptionalInstallConfig().ClusterNamespace(),
    53  				},
    54  				Spec: hivev1.ClusterDeploymentSpec{
    55  					ClusterName: getClusterDeploymentName(getValidOptionalInstallConfig()),
    56  					BaseDomain:  "testing.com",
    57  					PullSecretRef: &corev1.LocalObjectReference{
    58  						Name: getPullSecretName(getValidOptionalInstallConfig().ClusterName()),
    59  					},
    60  					ClusterInstallRef: &hivev1.ClusterInstallLocalReference{
    61  						Group:   "extensions.hive.openshift.io",
    62  						Version: "v1beta1",
    63  						Kind:    "AgentClusterInstall",
    64  						Name:    getAgentClusterInstallName(getValidOptionalInstallConfig()),
    65  					},
    66  				},
    67  			},
    68  		},
    69  	}
    70  	for _, tc := range cases {
    71  		t.Run(tc.name, func(t *testing.T) {
    72  
    73  			parents := asset.Parents{}
    74  			parents.Add(tc.dependencies...)
    75  
    76  			asset := &ClusterDeployment{}
    77  			err := asset.Generate(context.Background(), parents)
    78  
    79  			if tc.expectedError != "" {
    80  				assert.Equal(t, tc.expectedError, err.Error())
    81  			} else {
    82  				assert.NoError(t, err)
    83  				assert.Equal(t, tc.expectedConfig, asset.Config)
    84  				assert.NotEmpty(t, asset.Files())
    85  
    86  				configFile := asset.Files()[0]
    87  				assert.Equal(t, "cluster-manifests/cluster-deployment.yaml", configFile.Filename)
    88  
    89  				var actualConfig hivev1.ClusterDeployment
    90  				err = yaml.Unmarshal(configFile.Data, &actualConfig)
    91  				assert.NoError(t, err)
    92  				assert.Equal(t, *tc.expectedConfig, actualConfig)
    93  			}
    94  		})
    95  	}
    96  
    97  }
    98  
    99  func TestClusterDeployment_LoadedFromDisk(t *testing.T) {
   100  
   101  	cases := []struct {
   102  		name           string
   103  		data           string
   104  		fetchError     error
   105  		expectedFound  bool
   106  		expectedError  bool
   107  		expectedConfig *hivev1.ClusterDeployment
   108  	}{
   109  		{
   110  			name: "valid-config-file",
   111  			data: `
   112  metadata:
   113    name: compact-cluster
   114    namespace: cluster0
   115  spec:
   116    baseDomain: agent.example.com
   117    clusterInstallRef:
   118      group: extensions.hive.openshift.io
   119      kind: AgentClusterInstall
   120      name: test-agent-cluster-install
   121      version: v1beta1
   122    clusterName: compact-cluster
   123    controlPlaneConfig:
   124      servingCertificates: {}
   125    platform:
   126      agentBareMetal:
   127        agentSelector:
   128          matchLabels:
   129            bla: aaa
   130    pullSecretRef:
   131      name: pull-secret`,
   132  			expectedFound: true,
   133  			expectedConfig: &hivev1.ClusterDeployment{
   134  				ObjectMeta: metav1.ObjectMeta{
   135  					Name:      "compact-cluster",
   136  					Namespace: "cluster0",
   137  				},
   138  				Spec: hivev1.ClusterDeploymentSpec{
   139  					BaseDomain: "agent.example.com",
   140  					ClusterInstallRef: &hivev1.ClusterInstallLocalReference{
   141  						Group:   "extensions.hive.openshift.io",
   142  						Kind:    "AgentClusterInstall",
   143  						Name:    "test-agent-cluster-install",
   144  						Version: "v1beta1",
   145  					},
   146  					ClusterName:        "compact-cluster",
   147  					ControlPlaneConfig: hivev1.ControlPlaneConfigSpec{},
   148  					Platform: hivev1.Platform{
   149  						AgentBareMetal: &hivev1agent.BareMetalPlatform{
   150  							AgentSelector: metav1.LabelSelector{
   151  								MatchLabels: map[string]string{
   152  									"bla": "aaa",
   153  								},
   154  							},
   155  						},
   156  					},
   157  					PullSecretRef: &corev1.LocalObjectReference{
   158  						Name: "pull-secret",
   159  					},
   160  				},
   161  			},
   162  		},
   163  		{
   164  			name:          "not-yaml",
   165  			data:          `This is not a yaml file`,
   166  			expectedError: true,
   167  		},
   168  		{
   169  			name:           "empty",
   170  			data:           "",
   171  			expectedFound:  true,
   172  			expectedConfig: &hivev1.ClusterDeployment{},
   173  			expectedError:  false,
   174  		},
   175  		{
   176  			name:       "file-not-found",
   177  			fetchError: &os.PathError{Err: os.ErrNotExist},
   178  		},
   179  		{
   180  			name:          "error-fetching-file",
   181  			fetchError:    errors.New("fetch failed"),
   182  			expectedError: true,
   183  		},
   184  		{
   185  			name: "unknown-field",
   186  			data: `
   187  metadata:
   188    name: cluster-deployment-bad
   189    namespace: cluster0
   190  spec:
   191    wrongField: wrongValue`,
   192  			expectedError: true,
   193  		},
   194  	}
   195  	for _, tc := range cases {
   196  		t.Run(tc.name, func(t *testing.T) {
   197  
   198  			mockCtrl := gomock.NewController(t)
   199  			defer mockCtrl.Finish()
   200  
   201  			fileFetcher := mock.NewMockFileFetcher(mockCtrl)
   202  			fileFetcher.EXPECT().FetchByName(clusterDeploymentFilename).
   203  				Return(
   204  					&asset.File{
   205  						Filename: clusterDeploymentFilename,
   206  						Data:     []byte(tc.data)},
   207  					tc.fetchError,
   208  				)
   209  
   210  			asset := &ClusterDeployment{}
   211  			found, err := asset.Load(fileFetcher)
   212  			assert.Equal(t, tc.expectedFound, found, "unexpected found value returned from Load")
   213  			if tc.expectedError {
   214  				assert.Error(t, err, "expected error from Load")
   215  			} else {
   216  				assert.NoError(t, err, "unexpected error from Load")
   217  			}
   218  			if tc.expectedFound {
   219  				assert.Equal(t, tc.expectedConfig, asset.Config, "unexpected Config in ClusterDeployment")
   220  			}
   221  		})
   222  	}
   223  
   224  }