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

     1  package loader
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/onsi/gomega"
    12  )
    13  
    14  func TestLoader_Clean(t *testing.T) {
    15  	t.Run("Success", func(t *testing.T) {
    16  		// Given
    17  		g := gomega.NewGomegaWithT(t)
    18  		loader := &loader{
    19  			temporaryDir:    "/tmp",
    20  			osRemoveAllFunc: removeAll,
    21  		}
    22  
    23  		// When
    24  		err := loader.Clean("test")
    25  
    26  		// Then
    27  		g.Expect(err).NotTo(gomega.HaveOccurred())
    28  	})
    29  
    30  	t.Run("Fail", func(t *testing.T) {
    31  		// Given
    32  		g := gomega.NewGomegaWithT(t)
    33  		loader := &loader{
    34  			temporaryDir:    "/tmp",
    35  			osRemoveAllFunc: removeAll,
    36  		}
    37  
    38  		// When
    39  		err := loader.Clean("error1")
    40  
    41  		// Then
    42  		g.Expect(err).To(gomega.HaveOccurred())
    43  	})
    44  }
    45  
    46  func TestLoader_Load_NotSupported(t *testing.T) {
    47  	// Given
    48  	g := gomega.NewGomegaWithT(t)
    49  	loader := &loader{
    50  		temporaryDir:    "/tmp",
    51  		osRemoveAllFunc: os.RemoveAll,
    52  		osCreateFunc:    os.Create,
    53  		httpGetFunc:     get,
    54  		ioutilTempDir:   ioutil.TempDir,
    55  	}
    56  
    57  	// When
    58  	_, files, err := loader.Load("test", "asset", "other", "")
    59  
    60  	// Then
    61  	g.Expect(err).To(gomega.HaveOccurred())
    62  	g.Expect(files).To(gomega.HaveLen(0))
    63  }
    64  
    65  func removeAll(s string) error {
    66  	if s == "error1" {
    67  		return fmt.Errorf("nope")
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func createError(name string) (*os.File, error) {
    74  	return nil, fmt.Errorf("nope")
    75  
    76  }
    77  
    78  func get(url string) (*http.Response, error) {
    79  	if url == "error3" {
    80  		return nil, fmt.Errorf("nope")
    81  	}
    82  
    83  	response := &http.Response{
    84  		StatusCode: http.StatusOK,
    85  		Body:       ioutil.NopCloser(bytes.NewReader([]byte("ala ma kota"))),
    86  	}
    87  
    88  	return response, nil
    89  }
    90  
    91  func tempDirError(dir, prefix string) (string, error) {
    92  	return "", fmt.Errorf("nope")
    93  }