github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/loader/package_loader_test.go (about)

     1  package loader
     2  
     3  import (
     4  	"github.com/onsi/gomega"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2"
    11  )
    12  
    13  func TestLoader_Load_Package(t *testing.T) {
    14  	expected := []string{
    15  		"structure/swagger.json",
    16  		"structure/docs/README.md",
    17  	}
    18  
    19  	for testName, testCase := range map[string]struct {
    20  		path string
    21  	}{
    22  		"ZipArchive": {
    23  			path: "./testdata/structure.zip",
    24  		},
    25  		"TarGzArchive": {
    26  			path: "./testdata/structure.tar.gz",
    27  		},
    28  		"TarArchive": {
    29  			path: "./testdata/structure.tar.gz",
    30  		},
    31  		"TgzArchive": {
    32  			path: "./testdata/structure.tgz",
    33  		},
    34  	} {
    35  		t.Run(testName, func(t *testing.T) {
    36  			// Given
    37  			g := gomega.NewGomegaWithT(t)
    38  
    39  			tmpDir := "../../tmp"
    40  			err := os.MkdirAll(tmpDir, os.ModePerm)
    41  			g.Expect(err).NotTo(gomega.HaveOccurred())
    42  			defer os.RemoveAll(tmpDir)
    43  
    44  			loader := &loader{
    45  				temporaryDir:    tmpDir,
    46  				osRemoveAllFunc: os.RemoveAll,
    47  				osCreateFunc:    os.Create,
    48  				httpGetFunc:     getFile(testCase.path),
    49  				ioutilTempDir:   ioutil.TempDir,
    50  			}
    51  
    52  			// When
    53  			basePath, files, err := loader.Load(testCase.path, "asset", v1alpha2.AssetPackage, "")
    54  			defer loader.Clean(basePath)
    55  
    56  			// Then
    57  			g.Expect(err).NotTo(gomega.HaveOccurred())
    58  			g.Expect(files).To(gomega.HaveLen(2))
    59  			g.Expect(files).To(gomega.ConsistOf(expected))
    60  		})
    61  	}
    62  }
    63  
    64  func TestLoader_Load_WithFilter(t *testing.T) {
    65  	for testName, testCase := range map[string]struct {
    66  		filter   string
    67  		expected []string
    68  	}{
    69  		"AllFiles": {
    70  			filter: ".*",
    71  			expected: []string{
    72  				"test/README.md",
    73  				"test/nested/nested.md",
    74  				"swagger.json",
    75  				"docs/README.md",
    76  			},
    77  		},
    78  		"MarkdownFiles": {
    79  			filter: ".*\\.md$",
    80  			expected: []string{
    81  				"test/README.md",
    82  				"test/nested/nested.md",
    83  				"docs/README.md",
    84  			},
    85  		},
    86  		"ReadmeFiles": {
    87  			filter: "(^|/)README\\.md$",
    88  			expected: []string{
    89  				"test/README.md",
    90  				"docs/README.md",
    91  			},
    92  		},
    93  		"FilesFromTestDirectory": {
    94  			filter: "^test/.*",
    95  			expected: []string{
    96  				"test/README.md",
    97  				"test/nested/nested.md",
    98  			},
    99  		},
   100  		"NoFiles": {
   101  			filter:   "^nomatch$",
   102  			expected: []string{},
   103  		},
   104  	} {
   105  		t.Run(testName, func(t *testing.T) {
   106  			// Given
   107  			g := gomega.NewGomegaWithT(t)
   108  
   109  			tmpDir := "../../tmp"
   110  			testPath := "./testdata/complex.zip"
   111  			err := os.MkdirAll(tmpDir, os.ModePerm)
   112  			g.Expect(err).NotTo(gomega.HaveOccurred())
   113  			defer os.RemoveAll(tmpDir)
   114  
   115  			loader := &loader{
   116  				temporaryDir:    tmpDir,
   117  				osRemoveAllFunc: os.RemoveAll,
   118  				osCreateFunc:    os.Create,
   119  				httpGetFunc:     getFile(testPath),
   120  				ioutilTempDir:   ioutil.TempDir,
   121  			}
   122  
   123  			// When
   124  			basePath, files, err := loader.Load(testPath, "asset", v1alpha2.AssetPackage, testCase.filter)
   125  			defer loader.Clean(basePath)
   126  
   127  			// Then
   128  			g.Expect(err).NotTo(gomega.HaveOccurred())
   129  			g.Expect(files).To(gomega.HaveLen(len(testCase.expected)))
   130  			g.Expect(files).To(gomega.ConsistOf(testCase.expected))
   131  		})
   132  	}
   133  }
   134  
   135  func getFile(path string) func(url string) (*http.Response, error) {
   136  	file, err := os.Open(path)
   137  	if err != nil {
   138  		return func(url string) (*http.Response, error) {
   139  			return nil, err
   140  		}
   141  	}
   142  
   143  	get := func(url string) (*http.Response, error) {
   144  		return &http.Response{
   145  			StatusCode: http.StatusOK,
   146  			Body:       file,
   147  		}, nil
   148  	}
   149  
   150  	return get
   151  }