github.com/openshift/installer@v1.4.17/pkg/asset/imagebased/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  	"testing"
    11  
    12  	"github.com/coreos/stream-metadata-go/stream"
    13  	"github.com/stretchr/testify/assert"
    14  
    15  	"github.com/openshift/installer/pkg/asset"
    16  )
    17  
    18  func TestBaseIso_Generate(t *testing.T) {
    19  	ocReleaseImage := "417.94.202407011030-0"
    20  
    21  	cases := []struct {
    22  		name                       string
    23  		envVarOsImageOverrideValue string
    24  		expectedBaseIsoFilename    string
    25  	}{
    26  		{
    27  			name:                       "os image override",
    28  			envVarOsImageOverrideValue: "openshift-4.18",
    29  			expectedBaseIsoFilename:    "openshift-4.18",
    30  		},
    31  		{
    32  			name:                    "default",
    33  			expectedBaseIsoFilename: ocReleaseImage,
    34  		},
    35  	}
    36  	for _, tc := range cases {
    37  		t.Run(tc.name, func(t *testing.T) {
    38  			// Setup an HTTP server, to serve the dummy base ISO payload.
    39  			svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    40  				// Respond with a fixed size, randomly filled buffer.
    41  				buffer := make([]byte, 1024)
    42  				_, err := rand.Read(buffer)
    43  				assert.NoError(t, err)
    44  				_, err = w.Write(buffer)
    45  				assert.NoError(t, err)
    46  			}))
    47  			defer svr.Close()
    48  			// Creates a temp folder to store the .cache downloaded ISO images.
    49  			tmpPath, err := os.MkdirTemp("", "imagebased-baseiso-test")
    50  			assert.NoError(t, err)
    51  			previousXdgCacheHomeValue := os.Getenv("XDG_CACHE_HOME")
    52  			t.Setenv("XDG_CACHE_HOME", tmpPath)
    53  			// Set the OS image override if defined.
    54  			previousOpenshiftInstallOsImageOverrideValue := os.Getenv("OPENSHIFT_INSTALL_OS_IMAGE_OVERRIDE")
    55  			if tc.envVarOsImageOverrideValue != "" {
    56  				newOsImageOverride := fmt.Sprintf("%s/%s", svr.URL, tc.envVarOsImageOverrideValue)
    57  				t.Setenv("OPENSHIFT_INSTALL_OS_IMAGE_OVERRIDE", newOsImageOverride)
    58  			}
    59  			// Cleanup on exit.
    60  			defer func() {
    61  				t.Setenv("XDG_CACHE_HOME", previousXdgCacheHomeValue)
    62  				t.Setenv("OPENSHIFT_INSTALL_OS_IMAGE_OVERRIDE", previousOpenshiftInstallOsImageOverrideValue)
    63  				err = os.RemoveAll(tmpPath)
    64  				assert.NoError(t, err)
    65  			}()
    66  
    67  			baseIso := &BaseIso{
    68  				streamGetter: func(ctx context.Context) (*stream.Stream, error) {
    69  					return &stream.Stream{
    70  						Architectures: map[string]stream.Arch{
    71  							"x86_64": {
    72  								Artifacts: map[string]stream.PlatformArtifacts{
    73  									"metal": {
    74  										Release: ocReleaseImage,
    75  										Formats: map[string]stream.ImageFormat{
    76  											"iso": {
    77  												Disk: &stream.Artifact{
    78  													Location: fmt.Sprintf("%s/%s", svr.URL, ocReleaseImage),
    79  												},
    80  											},
    81  										},
    82  									},
    83  								},
    84  							},
    85  						},
    86  					}, nil
    87  				},
    88  			}
    89  			err = baseIso.Generate(context.TODO(), asset.Parents{})
    90  
    91  			assert.NoError(t, err)
    92  			assert.Regexp(t, tc.expectedBaseIsoFilename, baseIso.File.Filename)
    93  		})
    94  	}
    95  }