github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/compilers/mirage/mirage_test.go (about) 1 package mirage 2 3 import ( 4 "io" 5 "os" 6 "path/filepath" 7 8 "github.com/docker/docker/pkg/ioutils" 9 . "github.com/onsi/ginkgo" 10 11 . "github.com/onsi/gomega" 12 13 "strings" 14 ) 15 16 var xlFile = ` 17 # Generated by www (Tue, 23 Aug 2016 19:44:16 GMT). 18 19 name = '@NAME@' 20 kernel = '@KERNEL@' 21 builder = 'linux' 22 memory = @MEMORY@ 23 on_crash = 'preserve' 24 25 disk = [ 'format=raw, vdev=xvdb, access=rw, target=@BLOCK:tar_block1.img@', 'format=raw, vdev=xvdc, access=rw, target=@BLOCK:fat_block1.img@' ] 26 27 # if your system uses openvswitch then either edit /etc/xen/xl.conf and set 28 # vif.default.script="vif-openvswitch" 29 # or add "script=vif-openvswitch," before the "bridge=" below: 30 vif = [ 'bridge=@NETWORK:tap0@' ] 31 ` 32 33 var mirageDescribe = ` 34 Name www 35 Root /home/opam/mirage-skeleton/static_website 36 Keys dhcp=false (default), gateways=10.0.0.1 (default), interfaces=0.0.0.0 (default), ip=10.0.0.2 (default), 37 kv_ro=crunch (default), logs= (default), net=direct (default), netmask=255.255.255.0 (default), network=tap0 (default), 38 ocaml_version_check=false, port=8080 (default), socket= (default), target=unix (default), unix=false, warn_error=false (default), 39 xen=false 40 41 ` 42 43 var _ = Describe("Mirage", func() { 44 var sourcesDir string 45 var reader io.Reader 46 BeforeEach(func() { 47 sourcesDir, _ = ioutils.TempDir("", "") 48 reader = strings.NewReader(xlFile) 49 50 }) 51 AfterEach(func() { 52 os.RemoveAll(sourcesDir) 53 sourcesDir = "" 54 }) 55 56 Describe("Reading XL file", func() { 57 It("should get correct matches", func() { 58 matches, err := getDiskMatchesFromReader(reader) 59 60 Expect(err).NotTo(HaveOccurred()) 61 Expect(len(matches)).To(Equal(2)) 62 Expect(matches[0][1]).To(Equal("xvdb")) 63 Expect(matches[0][2]).To(Equal("tar_block1.img")) 64 Expect(matches[1][1]).To(Equal("xvdc")) 65 Expect(matches[1][2]).To(Equal("fat_block1.img")) 66 }) 67 It("should get correct disks where no volumes exist", func() { 68 matches, err := getDisksFromReader(sourcesDir, reader) 69 70 Expect(err).NotTo(HaveOccurred()) 71 Expect(len(matches)).To(Equal(0)) 72 }) 73 It("should get correct disks with fat", func() { 74 75 f, _ := os.Create(filepath.Join(sourcesDir, "fat_block1.img")) 76 f.Close() 77 78 matches, err := getDisksFromReader(sourcesDir, reader) 79 80 Expect(err).NotTo(HaveOccurred()) 81 Expect(len(matches)).To(Equal(1)) 82 Expect(matches[0]).To(Equal("xvdc")) 83 }) 84 It("should get correct disks with tar", func() { 85 86 f, _ := os.Create(filepath.Join(sourcesDir, "tar_block1.img")) 87 f.Close() 88 89 matches, err := getDisksFromReader(sourcesDir, reader) 90 91 Expect(err).NotTo(HaveOccurred()) 92 Expect(len(matches)).To(Equal(1)) 93 Expect(matches[0]).To(Equal("xvdb")) 94 }) 95 }) 96 97 Describe("Reading mirage describe", func() { 98 It("should non zero keys section", func() { 99 res := getKeyStringFromDescribe(mirageDescribe) 100 Expect(len(res)).NotTo(Equal(0)) 101 }) 102 It("should getall keys", func() { 103 res := getKeys(getKeyStringFromDescribe(mirageDescribe)) 104 Expect(len(res)).To(Equal(16)) 105 }) 106 It("should get kv_ro key", func() { 107 res := getKeys(getKeyStringFromDescribe(mirageDescribe)) 108 Expect(len(res)).NotTo(Equal(0)) 109 Expect(res).To(HaveKey("kv_ro")) 110 Expect(res["kv_ro"]).To(Equal("crunch (default)")) 111 }) 112 }) 113 Describe("Packaging raw image", func() { 114 It("should find unikernel file", func() { 115 116 f, _ := os.Create(filepath.Join(sourcesDir, "file.xen")) 117 f.Close() 118 119 res, err := getUnikernelFile(sourcesDir) 120 Expect(err).NotTo(HaveOccurred()) 121 Expect(res).To(Equal(filepath.Join(sourcesDir, "file.xen"))) 122 }) 123 }) 124 })