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

     1  package image
     2  
     3  import (
     4  	"context"
     5  	"crypto/rand"
     6  	"fmt"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"os"
    10  	"os/exec"
    11  	"testing"
    12  
    13  	"github.com/coreos/stream-metadata-go/stream"
    14  	"github.com/stretchr/testify/assert"
    15  	corev1 "k8s.io/api/core/v1"
    16  
    17  	"github.com/openshift/assisted-service/api/v1beta1"
    18  	v1 "github.com/openshift/hive/apis/hive/v1"
    19  	"github.com/openshift/installer/pkg/asset"
    20  	"github.com/openshift/installer/pkg/asset/agent/joiner"
    21  	"github.com/openshift/installer/pkg/asset/agent/manifests"
    22  	"github.com/openshift/installer/pkg/asset/agent/mirror"
    23  	"github.com/openshift/installer/pkg/asset/agent/workflow"
    24  )
    25  
    26  func TestBaseIso_Generate(t *testing.T) {
    27  	ocReleaseImage := "416.94.202402130130-0"
    28  	ocBaseIsoFilename := "openshift-4.16"
    29  
    30  	cases := []struct {
    31  		name                       string
    32  		dependencies               []asset.Asset
    33  		envVarOsImageOverrideValue string
    34  		getIsoError                error
    35  		expectedBaseIsoFilename    string
    36  		expectedError              string
    37  	}{
    38  		{
    39  			name:                       "os image override",
    40  			envVarOsImageOverrideValue: "openshift-4.15",
    41  			expectedBaseIsoFilename:    "openshift-4.15",
    42  		},
    43  		{
    44  			name: "default",
    45  			dependencies: []asset.Asset{
    46  				&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
    47  				&joiner.ClusterInfo{},
    48  				&manifests.AgentManifests{
    49  					InfraEnv: &v1beta1.InfraEnv{},
    50  					ClusterImageSet: &v1.ClusterImageSet{
    51  						Spec: v1.ClusterImageSetSpec{
    52  							ReleaseImage: ocReleaseImage,
    53  						},
    54  					},
    55  					PullSecret: &corev1.Secret{
    56  						StringData: map[string]string{
    57  							".dockerconfigjson": "supersecret",
    58  						},
    59  					},
    60  				},
    61  				&mirror.RegistriesConf{},
    62  			},
    63  			expectedBaseIsoFilename: ocBaseIsoFilename,
    64  		},
    65  		{
    66  			name: "direct download if oc is not available",
    67  			dependencies: []asset.Asset{
    68  				&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
    69  				&joiner.ClusterInfo{},
    70  				&manifests.AgentManifests{
    71  					InfraEnv: &v1beta1.InfraEnv{},
    72  					ClusterImageSet: &v1.ClusterImageSet{
    73  						Spec: v1.ClusterImageSetSpec{
    74  							ReleaseImage: ocReleaseImage,
    75  						},
    76  					},
    77  					PullSecret: &corev1.Secret{
    78  						StringData: map[string]string{
    79  							".dockerconfigjson": "supersecret",
    80  						},
    81  					},
    82  				},
    83  				&mirror.RegistriesConf{},
    84  			},
    85  			getIsoError:             &exec.Error{},
    86  			expectedBaseIsoFilename: ocReleaseImage,
    87  		},
    88  	}
    89  	for _, tc := range cases {
    90  		t.Run(tc.name, func(t *testing.T) {
    91  			dependencies := asset.Parents{}
    92  			dependencies.Add(tc.dependencies...)
    93  
    94  			// Setup a fake http server, to serve the future download request.
    95  			svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    96  				// Answer with a fixed size randomly filled buffer
    97  				buffer := make([]byte, 1024)
    98  				_, err := rand.Read(buffer)
    99  				assert.NoError(t, err)
   100  				_, err = w.Write(buffer)
   101  				assert.NoError(t, err)
   102  			}))
   103  			defer svr.Close()
   104  			// Creates a tmp folder to store the .cache downloaded images.
   105  			tmpPath, err := os.MkdirTemp("", "agent-baseiso-test")
   106  			assert.NoError(t, err)
   107  			previousXdgCacheHomeValue := os.Getenv("XDG_CACHE_HOME")
   108  			t.Setenv("XDG_CACHE_HOME", tmpPath)
   109  			// Set the image override if defined
   110  			previousOpenshiftInstallOsImageOverrideValue := os.Getenv("OPENSHIFT_INSTALL_OS_IMAGE_OVERRIDE")
   111  			if tc.envVarOsImageOverrideValue != "" {
   112  				newOsImageOverride := fmt.Sprintf("%s/%s", svr.URL, tc.envVarOsImageOverrideValue)
   113  				t.Setenv("OPENSHIFT_INSTALL_OS_IMAGE_OVERRIDE", newOsImageOverride)
   114  			}
   115  			// Cleanup on exit.
   116  			defer func() {
   117  				t.Setenv("XDG_CACHE_HOME", previousXdgCacheHomeValue)
   118  				t.Setenv("OPENSHIFT_INSTALL_OS_IMAGE_OVERRIDE", previousOpenshiftInstallOsImageOverrideValue)
   119  				err = os.RemoveAll(tmpPath)
   120  				assert.NoError(t, err)
   121  			}()
   122  
   123  			baseIso := &BaseIso{
   124  				ocRelease: &mockRelease{
   125  					isoBaseVersion:  ocReleaseImage,
   126  					baseIsoFileName: ocBaseIsoFilename,
   127  					baseIsoError:    tc.getIsoError,
   128  				},
   129  				streamGetter: func(ctx context.Context) (*stream.Stream, error) {
   130  					return &stream.Stream{
   131  						Architectures: map[string]stream.Arch{
   132  							"x86_64": {
   133  								Artifacts: map[string]stream.PlatformArtifacts{
   134  									"metal": {
   135  										Release: ocReleaseImage,
   136  										Formats: map[string]stream.ImageFormat{
   137  											"iso": {
   138  												Disk: &stream.Artifact{
   139  													Location: fmt.Sprintf("%s/%s", svr.URL, ocReleaseImage),
   140  												},
   141  											},
   142  										},
   143  									},
   144  								},
   145  							},
   146  						},
   147  					}, nil
   148  				},
   149  			}
   150  			err = baseIso.Generate(context.Background(), dependencies)
   151  
   152  			if tc.expectedError == "" {
   153  				assert.NoError(t, err)
   154  				assert.Regexp(t, tc.expectedBaseIsoFilename, baseIso.File.Filename)
   155  			} else {
   156  				assert.Equal(t, tc.expectedError, err.Error())
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  type mockRelease struct {
   163  	isoBaseVersion  string
   164  	baseIsoFileName string
   165  	baseIsoError    error
   166  }
   167  
   168  func (m *mockRelease) GetBaseIso(architecture string) (string, error) {
   169  	if m.baseIsoError != nil {
   170  		return "", m.baseIsoError
   171  	}
   172  	return m.baseIsoFileName, nil
   173  }
   174  
   175  func (m *mockRelease) GetBaseIsoVersion(architecture string) (string, error) {
   176  	return m.isoBaseVersion, nil
   177  }
   178  
   179  func (m *mockRelease) ExtractFile(image string, filename string, architecture string) ([]string, error) {
   180  	return []string{}, nil
   181  }