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

     1  package manifests
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/golang/mock/gomock"
    10  	"github.com/pkg/errors"
    11  	"github.com/stretchr/testify/assert"
    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  	"github.com/openshift/installer/pkg/asset"
    17  	"github.com/openshift/installer/pkg/asset/agent"
    18  	"github.com/openshift/installer/pkg/asset/agent/joiner"
    19  	"github.com/openshift/installer/pkg/asset/agent/workflow"
    20  	"github.com/openshift/installer/pkg/asset/mock"
    21  	"github.com/openshift/installer/pkg/asset/releaseimage"
    22  )
    23  
    24  func TestClusterImageSet_Generate(t *testing.T) {
    25  	currentRelease, err := releaseimage.Default()
    26  	assert.NoError(t, err)
    27  
    28  	cases := []struct {
    29  		name           string
    30  		dependencies   []asset.Asset
    31  		expectedError  string
    32  		expectedConfig *hivev1.ClusterImageSet
    33  	}{
    34  		{
    35  			name: "missing install config should still generate a ClusterImageSet with empty namespace",
    36  			dependencies: []asset.Asset{
    37  				&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
    38  				&joiner.ClusterInfo{},
    39  				&agent.OptionalInstallConfig{},
    40  				&releaseimage.Image{
    41  					PullSpec: currentRelease,
    42  				},
    43  			},
    44  			expectedConfig: &hivev1.ClusterImageSet{
    45  				TypeMeta: metav1.TypeMeta{
    46  					Kind:       "ClusterImageSet",
    47  					APIVersion: "hive.openshift.io/v1",
    48  				},
    49  				ObjectMeta: metav1.ObjectMeta{
    50  					Name: "openshift-was not built correctly",
    51  				},
    52  				Spec: hivev1.ClusterImageSetSpec{
    53  					ReleaseImage: currentRelease,
    54  				},
    55  			},
    56  		},
    57  		{
    58  			name: "invalid ClusterImageSet configuration",
    59  			dependencies: []asset.Asset{
    60  				&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
    61  				&joiner.ClusterInfo{},
    62  				getValidOptionalInstallConfig(),
    63  				&releaseimage.Image{},
    64  			},
    65  			expectedError: "invalid ClusterImageSet configuration: Spec.ReleaseImage: Forbidden: value must be equal to " + currentRelease,
    66  		},
    67  		{
    68  			name: "valid configuration",
    69  			dependencies: []asset.Asset{
    70  				&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
    71  				&joiner.ClusterInfo{},
    72  				getValidOptionalInstallConfig(),
    73  				&releaseimage.Image{
    74  					PullSpec: currentRelease,
    75  				},
    76  			},
    77  			expectedConfig: &hivev1.ClusterImageSet{
    78  				TypeMeta: metav1.TypeMeta{
    79  					Kind:       "ClusterImageSet",
    80  					APIVersion: "hive.openshift.io/v1",
    81  				},
    82  				ObjectMeta: metav1.ObjectMeta{
    83  					Name:      "openshift-was not built correctly",
    84  					Namespace: getValidOptionalInstallConfig().ClusterNamespace(),
    85  				},
    86  				Spec: hivev1.ClusterImageSetSpec{
    87  					ReleaseImage: currentRelease,
    88  				},
    89  			},
    90  		},
    91  		{
    92  			name: "add-nodes command",
    93  			dependencies: []asset.Asset{
    94  				&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeAddNodes},
    95  				&joiner.ClusterInfo{
    96  					Version:      "4.16.0",
    97  					ReleaseImage: "registry.ci.openshift.org/ocp/release@sha256:foo",
    98  					Namespace:    "agent-cluster",
    99  				},
   100  				&agent.OptionalInstallConfig{},
   101  				&releaseimage.Image{},
   102  			},
   103  			expectedConfig: &hivev1.ClusterImageSet{
   104  				TypeMeta: metav1.TypeMeta{
   105  					Kind:       "ClusterImageSet",
   106  					APIVersion: "hive.openshift.io/v1",
   107  				},
   108  				ObjectMeta: metav1.ObjectMeta{
   109  					Name:      "openshift-4.16.0",
   110  					Namespace: "agent-cluster",
   111  				},
   112  				Spec: hivev1.ClusterImageSetSpec{
   113  					ReleaseImage: "registry.ci.openshift.org/ocp/release@sha256:foo",
   114  				},
   115  			},
   116  		},
   117  	}
   118  	for _, tc := range cases {
   119  		t.Run(tc.name, func(t *testing.T) {
   120  
   121  			parents := asset.Parents{}
   122  			parents.Add(tc.dependencies...)
   123  
   124  			asset := &ClusterImageSet{}
   125  			err := asset.Generate(context.Background(), parents)
   126  
   127  			if tc.expectedError != "" {
   128  				assert.Equal(t, tc.expectedError, err.Error())
   129  			} else {
   130  				assert.NoError(t, err)
   131  				assert.Equal(t, tc.expectedConfig, asset.Config)
   132  				assert.NotEmpty(t, asset.Files())
   133  
   134  				configFile := asset.Files()[0]
   135  				assert.Equal(t, "cluster-manifests/cluster-image-set.yaml", configFile.Filename)
   136  
   137  				var actualConfig hivev1.ClusterImageSet
   138  				err = yaml.Unmarshal(configFile.Data, &actualConfig)
   139  				assert.NoError(t, err)
   140  				assert.Equal(t, *tc.expectedConfig, actualConfig)
   141  			}
   142  
   143  		})
   144  	}
   145  
   146  }
   147  
   148  func TestClusterImageSet_LoadedFromDisk(t *testing.T) {
   149  
   150  	currentRelease, err := releaseimage.Default()
   151  	assert.NoError(t, err)
   152  
   153  	cases := []struct {
   154  		name           string
   155  		data           string
   156  		fetchError     error
   157  		expectedFound  bool
   158  		expectedError  string
   159  		expectedConfig *hivev1.ClusterImageSet
   160  	}{
   161  		{
   162  			name: "valid-config-file",
   163  			data: `
   164  metadata:
   165    name: openshift-v4.10.0
   166  spec:
   167    releaseImage: ` + currentRelease,
   168  			expectedFound: true,
   169  			expectedConfig: &hivev1.ClusterImageSet{
   170  				ObjectMeta: metav1.ObjectMeta{
   171  					Name: "openshift-v4.10.0",
   172  				},
   173  				Spec: hivev1.ClusterImageSetSpec{
   174  					ReleaseImage: currentRelease,
   175  				},
   176  			},
   177  		},
   178  		{
   179  			name: "different-version-not-supported",
   180  			data: `
   181  metadata:
   182    name: openshift-v4.10.0
   183  spec:
   184    releaseImage: 99.999`,
   185  			expectedError: fmt.Sprintf("invalid ClusterImageSet configuration: Spec.ReleaseImage: Forbidden: value must be equal to %s", currentRelease),
   186  		},
   187  		{
   188  			name:          "not-yaml",
   189  			data:          `This is not a yaml file`,
   190  			expectedError: "failed to unmarshal cluster-manifests/cluster-image-set.yaml: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type v1.ClusterImageSet",
   191  		},
   192  		{
   193  			name:          "empty",
   194  			data:          "",
   195  			expectedError: fmt.Sprintf("invalid ClusterImageSet configuration: Spec.ReleaseImage: Forbidden: value must be equal to %s", currentRelease),
   196  		},
   197  		{
   198  			name:       "file-not-found",
   199  			fetchError: &os.PathError{Err: os.ErrNotExist},
   200  		},
   201  		{
   202  			name:          "error-fetching-file",
   203  			fetchError:    errors.New("fetch failed"),
   204  			expectedError: "failed to load cluster-manifests/cluster-image-set.yaml file: fetch failed",
   205  		},
   206  		{
   207  			name: "unknown-field",
   208  			data: `
   209  metadata:
   210    name: test-cluster-image-set
   211    namespace: cluster0
   212  spec:
   213    wrongField: wrongValue`,
   214  			expectedError: "failed to unmarshal cluster-manifests/cluster-image-set.yaml: error unmarshaling JSON: while decoding JSON: json: unknown field \"wrongField\"",
   215  		},
   216  	}
   217  	for _, tc := range cases {
   218  		t.Run(tc.name, func(t *testing.T) {
   219  
   220  			mockCtrl := gomock.NewController(t)
   221  			defer mockCtrl.Finish()
   222  
   223  			fileFetcher := mock.NewMockFileFetcher(mockCtrl)
   224  			fileFetcher.EXPECT().FetchByName(clusterImageSetFilename).
   225  				Return(
   226  					&asset.File{
   227  						Filename: clusterImageSetFilename,
   228  						Data:     []byte(tc.data)},
   229  					tc.fetchError,
   230  				)
   231  			fileFetcher.EXPECT().FetchByName(".agentworkflow").
   232  				Return(
   233  					&asset.File{
   234  						Filename: ".agentworkflow",
   235  						Data:     []byte("install")},
   236  					nil,
   237  				)
   238  
   239  			asset := &ClusterImageSet{}
   240  			found, err := asset.Load(fileFetcher)
   241  			assert.Equal(t, tc.expectedFound, found, "unexpected found value returned from Load")
   242  			if tc.expectedError != "" {
   243  				assert.Equal(t, tc.expectedError, err.Error())
   244  			} else {
   245  				assert.NoError(t, err)
   246  			}
   247  			if tc.expectedFound {
   248  				assert.Equal(t, tc.expectedConfig, asset.Config, "unexpected Config in ClusterImageSet")
   249  			}
   250  		})
   251  	}
   252  
   253  }