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

     1  package store
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/openshift/installer/pkg/asset"
    11  )
    12  
    13  func TestFetchByName(t *testing.T) {
    14  	tests := []struct {
    15  		name       string
    16  		files      map[string][]byte
    17  		input      string
    18  		expectFile *asset.File
    19  	}{
    20  		{
    21  			name:       "input doesn't match",
    22  			files:      map[string][]byte{"foo.bar": []byte("some data")},
    23  			input:      "bar.foo",
    24  			expectFile: nil,
    25  		},
    26  		{
    27  			name:  "with contents",
    28  			files: map[string][]byte{"foo.bar": []byte("some data")},
    29  			input: "foo.bar",
    30  			expectFile: &asset.File{
    31  				Filename: "foo.bar",
    32  				Data:     []byte("some data"),
    33  			},
    34  		},
    35  		{
    36  			name:  "match one file",
    37  			files: map[string][]byte{"foo.bar": []byte("some data")},
    38  			input: "foo.bar",
    39  			expectFile: &asset.File{
    40  				Filename: "foo.bar",
    41  				Data:     []byte("some data"),
    42  			},
    43  		},
    44  	}
    45  
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			tempDir := t.TempDir()
    49  
    50  			for filename, data := range tt.files {
    51  				err := os.WriteFile(filepath.Join(tempDir, filename), data, 0o666) //nolint:gosec // no sensitive data
    52  				if err != nil {
    53  					t.Fatal(err)
    54  				}
    55  			}
    56  
    57  			f := &fileFetcher{directory: tempDir}
    58  			file, err := f.FetchByName(tt.input)
    59  			if err != nil {
    60  				if os.IsNotExist(err) && tt.expectFile == nil {
    61  					return
    62  				}
    63  				t.Fatal(err)
    64  			}
    65  
    66  			assert.Equal(t, tt.expectFile, file)
    67  		})
    68  	}
    69  }
    70  
    71  func TestFetchByPattern(t *testing.T) {
    72  	tempDir := t.TempDir()
    73  
    74  	files := map[string][]byte{
    75  		"master-0.ign":   []byte("some data 0"),
    76  		"master-1.ign":   []byte("some data 1"),
    77  		"master-2.ign":   []byte("some data 2"),
    78  		"master-10.ign":  []byte("some data 3"),
    79  		"master-20.ign":  []byte("some data 4"),
    80  		"master-00.ign":  []byte("some data 5"),
    81  		"master-01.ign":  []byte("some data 6"),
    82  		"amaster-0.ign":  []byte("some data 7"),
    83  		"master-.ign":    []byte("some data 8"),
    84  		"master-.igni":   []byte("some data 9"),
    85  		"master-.ignign": []byte("some data 10"),
    86  		"manifests/0":    []byte("some data 11"),
    87  		"manifests/some": []byte("some data 12"),
    88  		"amanifests/a":   []byte("some data 13"),
    89  	}
    90  
    91  	for path, data := range files {
    92  		dir := filepath.Dir(path)
    93  		if dir != "." {
    94  			err := os.MkdirAll(filepath.Join(tempDir, dir), 0777)
    95  			if err != nil {
    96  				t.Fatal(err)
    97  			}
    98  		}
    99  		err := os.WriteFile(filepath.Join(tempDir, path), data, 0o666) //nolint:gosec // no sensitive data
   100  		if err != nil {
   101  			t.Fatal(err)
   102  		}
   103  	}
   104  	tests := []struct {
   105  		input       string
   106  		expectFiles []*asset.File
   107  	}{
   108  		{
   109  			input: "master-[0-9]*.ign",
   110  			expectFiles: []*asset.File{
   111  				{
   112  					Filename: "master-0.ign",
   113  					Data:     []byte("some data 0"),
   114  				},
   115  				{
   116  					Filename: "master-00.ign",
   117  					Data:     []byte("some data 5"),
   118  				},
   119  				{
   120  					Filename: "master-01.ign",
   121  					Data:     []byte("some data 6"),
   122  				},
   123  				{
   124  					Filename: "master-1.ign",
   125  					Data:     []byte("some data 1"),
   126  				},
   127  				{
   128  					Filename: "master-10.ign",
   129  					Data:     []byte("some data 3"),
   130  				},
   131  				{
   132  					Filename: "master-2.ign",
   133  					Data:     []byte("some data 2"),
   134  				},
   135  				{
   136  					Filename: "master-20.ign",
   137  					Data:     []byte("some data 4"),
   138  				},
   139  			},
   140  		},
   141  		{
   142  			input: filepath.Join("manifests", "*"),
   143  			expectFiles: []*asset.File{
   144  				{
   145  					Filename: "manifests/0",
   146  					Data:     []byte("some data 11"),
   147  				},
   148  				{
   149  					Filename: "manifests/some",
   150  					Data:     []byte("some data 12"),
   151  				},
   152  			},
   153  		},
   154  	}
   155  
   156  	for _, tt := range tests {
   157  		t.Run(tt.input, func(t *testing.T) {
   158  			f := &fileFetcher{directory: tempDir}
   159  			files, err := f.FetchByPattern(tt.input)
   160  			if err != nil {
   161  				t.Fatal(err)
   162  			}
   163  
   164  			assert.Equal(t, tt.expectFiles, files)
   165  		})
   166  	}
   167  }