github.com/openshift/installer@v1.4.17/pkg/asset/asset_test.go (about)

     1  package asset
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  type persistAsset struct{}
    14  
    15  func (a *persistAsset) Name() string {
    16  	return "persist-asset"
    17  }
    18  
    19  func (a *persistAsset) Dependencies() []Asset {
    20  	return []Asset{}
    21  }
    22  
    23  func (a *persistAsset) Generate(context.Context, Parents) error {
    24  	return nil
    25  }
    26  
    27  type writablePersistAsset struct {
    28  	persistAsset
    29  	FileList []*File
    30  }
    31  
    32  func (a *writablePersistAsset) Files() []*File {
    33  	return a.FileList
    34  }
    35  
    36  func (a *writablePersistAsset) Load(FileFetcher) (bool, error) {
    37  	return false, nil
    38  }
    39  
    40  func TestPersistToFile(t *testing.T) {
    41  	cases := []struct {
    42  		name      string
    43  		filenames []string
    44  	}{
    45  		{
    46  			name:      "no files",
    47  			filenames: []string{},
    48  		},
    49  		{
    50  			name:      "single file",
    51  			filenames: []string{"file1"},
    52  		},
    53  		{
    54  			name:      "multiple files",
    55  			filenames: []string{"file1", "file2"},
    56  		},
    57  		{
    58  			name:      "new directory",
    59  			filenames: []string{"dir1/file1"},
    60  		},
    61  	}
    62  	for _, tc := range cases {
    63  		t.Run(tc.name, func(t *testing.T) {
    64  			dir := t.TempDir()
    65  
    66  			asset := &writablePersistAsset{
    67  				FileList: make([]*File, len(tc.filenames)),
    68  			}
    69  			expectedFiles := map[string][]byte{}
    70  			for i, filename := range tc.filenames {
    71  				data := []byte(fmt.Sprintf("data%d", i))
    72  				asset.FileList[i] = &File{
    73  					Filename: filename,
    74  					Data:     data,
    75  				}
    76  				expectedFiles[filepath.Join(dir, filename)] = data
    77  			}
    78  			err := PersistToFile(asset, dir)
    79  			assert.NoError(t, err, "unexpected error persisting state to file")
    80  			verifyFilesCreated(t, dir, expectedFiles)
    81  		})
    82  	}
    83  }
    84  
    85  func verifyFilesCreated(t *testing.T, dir string, expectedFiles map[string][]byte) {
    86  	dirContents, err := os.ReadDir(dir)
    87  	assert.NoError(t, err, "could not read contents of directory %q", dir)
    88  	for _, fileinfo := range dirContents {
    89  		fullPath := filepath.Join(dir, fileinfo.Name())
    90  		if fileinfo.IsDir() {
    91  			verifyFilesCreated(t, fullPath, expectedFiles)
    92  		} else {
    93  			expectedData, fileExpected := expectedFiles[fullPath]
    94  			if !fileExpected {
    95  				t.Errorf("Unexpected file created: %v", fullPath)
    96  				continue
    97  			}
    98  			actualData, err := os.ReadFile(fullPath)
    99  			assert.NoError(t, err, "unexpected error reading created file %q", fullPath)
   100  			assert.Equal(t, expectedData, actualData, "unexpected data in created file %q", fullPath)
   101  			delete(expectedFiles, fullPath)
   102  		}
   103  	}
   104  	for f := range expectedFiles {
   105  		t.Errorf("Expected file %q not created", f)
   106  	}
   107  }