github.com/drone/drone-cache-lib@v0.0.0-20200806063744-981868645a25/cache/cache_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/drone/drone-cache-lib/storage/dummy"
    12  	"github.com/franela/goblin"
    13  )
    14  
    15  func TestCache(t *testing.T) {
    16  	g := goblin.Goblin(t)
    17  	wd, _ := os.Getwd()
    18  
    19  	g.Describe("cache package", func() {
    20  
    21  		g.Before(func() {
    22  			os.Chdir("/tmp")
    23  			createFixtures()
    24  		})
    25  
    26  		g.BeforeEach(func() {
    27  			os.Chdir("/tmp")
    28  		})
    29  
    30  		g.After(func() {
    31  			os.Chdir(wd)
    32  			cleanFixtures()
    33  		})
    34  
    35  		g.Describe("Rebuild", func() {
    36  			g.It("Should rebuild with no errors", func() {
    37  				s, err := dummy.New(dummyOpts)
    38  				g.Assert(err == nil).IsTrue("failed to create storage")
    39  
    40  				c := NewDefault(s)
    41  				g.Assert(err == nil).IsTrue("failed to create cache")
    42  
    43  				os.Chdir("/tmp/fixtures/mounts")
    44  				err = c.Rebuild([]string{"test.txt", "subdir"}, "fixtures/tarfiles/file.tar")
    45  				if err != nil {
    46  					fmt.Printf("'Should rebuild with no errors' received unexpected error: %s\n", err)
    47  				}
    48  				g.Assert(err == nil).IsTrue("failed to rebuild the cache")
    49  			})
    50  
    51  			g.It("Should return error from channel", func() {
    52  				s, err := dummy.New(dummyOpts)
    53  				g.Assert(err == nil).IsTrue("failed to create storage")
    54  
    55  				c := NewDefault(s)
    56  				g.Assert(err == nil).IsTrue("failed to create cache")
    57  
    58  				err = c.Rebuild([]string{"mount1", "mount2"}, "file.tar")
    59  				g.Assert(err != nil).IsTrue("failed to return error")
    60  				g.Assert(err.Error()).Equal("stat mount1: no such file or directory")
    61  			})
    62  		})
    63  
    64  		g.Describe("Restore", func() {
    65  			g.It("Should restore with no errors", func() {
    66  				s, err := dummy.New(dummyOpts)
    67  				g.Assert(err == nil).IsTrue("failed to create storage")
    68  
    69  				c := NewDefault(s)
    70  				g.Assert(err == nil).IsTrue("failed to create cache")
    71  
    72  				err = c.Restore("fixtures/test.tar", "")
    73  				if err != nil {
    74  					fmt.Printf("Received unexpected error: %s\n", err)
    75  				}
    76  				g.Assert(err == nil).IsTrue("failed to rebuild the cache")
    77  			})
    78  
    79  			g.It("Should restore from fallback if path does not exist", func() {
    80  				s, err := dummy.New(dummyOpts)
    81  				g.Assert(err == nil).IsTrue("failed to create storage")
    82  
    83  				c := NewDefault(s)
    84  				g.Assert(err == nil).IsTrue("failed to create cache")
    85  
    86  				err = c.Restore("fixtures/test2.tar", "fixtures/test.tar")
    87  				if err != nil {
    88  					fmt.Printf("Received unexpected error: %s\n", err)
    89  				}
    90  				g.Assert(err == nil).IsTrue("failed to rebuild the cache")
    91  			})
    92  
    93  			g.It("Should not return error on missing file", func() {
    94  				s, err := dummy.New(dummyOpts)
    95  				g.Assert(err == nil).IsTrue("failed to create storage")
    96  
    97  				c := NewDefault(s)
    98  				g.Assert(err == nil).IsTrue("failed to create cache")
    99  
   100  				err = c.Restore("fixtures/test2.tar", "")
   101  				g.Assert(err == nil).IsTrue("should not have returned error on missing file")
   102  			})
   103  		})
   104  	})
   105  }
   106  
   107  func checkFileExists(fileName string, g *goblin.G) {
   108  	_, err := os.Stat(fileName)
   109  	g.Assert(err == nil).IsTrue(fileName + " should still exist")
   110  }
   111  
   112  func checkFileRemoved(fileName string, g *goblin.G) {
   113  	_, err := os.Stat(fileName)
   114  	g.Assert(err != nil).IsTrue("Failed to clean " + fileName)
   115  }
   116  
   117  func createFixtures() {
   118  	createDirectories(cacheFixtureDirectories)
   119  	createMountContent()
   120  }
   121  
   122  func cleanFixtures() {
   123  	os.RemoveAll("/tmp/fixtures/")
   124  }
   125  
   126  func createDirectories(directories []string) {
   127  	for _, directory := range directories {
   128  		if _, err := os.Stat(directory); os.IsNotExist(err) {
   129  			os.MkdirAll(directory, os.FileMode(int(0755)))
   130  		}
   131  	}
   132  }
   133  
   134  func createMountContent() {
   135  	var err error
   136  	for _, element := range mountFiles {
   137  		err = ioutil.WriteFile("/tmp/fixtures/mounts/"+element.Path, []byte(element.Content), 0644)
   138  		if err != nil {
   139  			log.Fatalln(err)
   140  		}
   141  	}
   142  }
   143  
   144  type testFile struct {
   145  	Path    string
   146  	Content string
   147  	Time    time.Time
   148  }
   149  
   150  var (
   151  	dummyOpts = &dummy.Options{
   152  		Server:   "myserver.com",
   153  		Username: "johndoe",
   154  		Password: "supersecret",
   155  	}
   156  
   157  	mountFiles = []testFile{
   158  		{Path: "test.txt", Content: "hello\ngo\n"},
   159  		{Path: "subdir/test2.txt", Content: "hello2\ngo\n"},
   160  	}
   161  
   162  	cacheFixtureDirectories = []string{
   163  		"/tmp/fixtures/tarfiles",
   164  		"/tmp/fixtures/mounts/subdir",
   165  	}
   166  )