gitee.com/mysnapcore/mysnapd@v0.1.0/image/helpers_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package image_test
    21  
    22  import (
    23  	"os"
    24  	"os/exec"
    25  	"path/filepath"
    26  	"sort"
    27  	"strings"
    28  
    29  	"gopkg.in/check.v1"
    30  
    31  	"gitee.com/mysnapcore/mysnapd/gadget"
    32  	"gitee.com/mysnapcore/mysnapd/image"
    33  	"gitee.com/mysnapcore/mysnapd/snap/snaptest"
    34  )
    35  
    36  var validGadgetYaml = `
    37  volumes:
    38    vol1:
    39      bootloader: grub
    40      structure:
    41        - name: non-fs
    42          type: bare
    43          size: 512
    44          offset: 0
    45          content:
    46          - image: non-fs.img
    47        - name: ubuntu-seed
    48          role: system-seed
    49          type: 83,0FC63DAF-8483-4772-8E79-3D69D8477DE4
    50          size: 100M
    51          filesystem: ext4
    52          content:
    53           - source: system-seed.efi
    54             target: EFI/boot/system-seed.efi
    55        - name: structure-name
    56          role: system-boot
    57          type: 83,0FC63DAF-8483-4772-8E79-3D69D8477DE4
    58          size: 100M
    59          filesystem: ext4
    60          content:
    61           - source: grubx64.efi
    62             target: EFI/boot/grubx64.efi
    63        - name: ubuntu-data
    64          role: system-data
    65          type: 83,0FC63DAF-8483-4772-8E79-3D69D8477DE4
    66          size: 100M
    67    vol2:
    68      structure:
    69        - name: struct2
    70          type: 83,0FC63DAF-8483-4772-8E79-3D69D8477DE4
    71          size: 100M
    72          filesystem: ext4
    73          content:
    74           - source: foo
    75             target: subdir/foo
    76  `
    77  
    78  func (s *imageSuite) TestWriteResolvedContent(c *check.C) {
    79  	prepareImageDir := c.MkDir()
    80  
    81  	s.testWriteResolvedContent(c, prepareImageDir)
    82  }
    83  
    84  func (s *imageSuite) TestWriteResolvedContentRelativePath(c *check.C) {
    85  	prepareImageDir := c.MkDir()
    86  
    87  	// chdir to prepareImage dir and run writeResolvedContent from
    88  	// this relative dir
    89  	cwd, err := os.Getwd()
    90  	c.Assert(err, check.IsNil)
    91  	err = os.Chdir(prepareImageDir)
    92  	c.Assert(err, check.IsNil)
    93  	defer func() { os.Chdir(cwd) }()
    94  
    95  	s.testWriteResolvedContent(c, ".")
    96  }
    97  
    98  // treeLines is used to sort the output from find
    99  type treeLines []string
   100  
   101  func (t treeLines) Len() int {
   102  	return len(t)
   103  }
   104  func (t treeLines) Less(i, j int) bool {
   105  	// strip off the first character of the two strings (assuming the strings
   106  	// are at least 1 character long)
   107  	s1 := t[i]
   108  	if len(s1) > 1 {
   109  		s1 = s1[1:]
   110  	}
   111  	s2 := t[j]
   112  	if len(s2) > 1 {
   113  		s2 = s2[1:]
   114  	}
   115  	return s1 < s2
   116  }
   117  func (t treeLines) Swap(i, j int) {
   118  	t[i], t[j] = t[j], t[i]
   119  }
   120  
   121  func (s *imageSuite) testWriteResolvedContent(c *check.C, prepareImageDir string) {
   122  	// on uc20 there is a "system-seed" under the <PrepareImageDir>
   123  	uc20systemSeed, err := filepath.Abs(filepath.Join(prepareImageDir, "system-seed"))
   124  	c.Assert(err, check.IsNil)
   125  	err = os.MkdirAll(uc20systemSeed, 0755)
   126  	c.Assert(err, check.IsNil)
   127  
   128  	// the resolved content is written here
   129  	gadgetRoot := c.MkDir()
   130  	snaptest.PopulateDir(gadgetRoot, [][]string{
   131  		{"meta/snap.yaml", packageGadget},
   132  		{"meta/gadget.yaml", validGadgetYaml},
   133  		{"system-seed.efi", "content of system-seed.efi"},
   134  		{"grubx64.efi", "content of grubx64.efi"},
   135  		{"foo", "content of foo"},
   136  		{"non-fs.img", "content of non-fs.img"},
   137  	})
   138  	kernelRoot := c.MkDir()
   139  
   140  	model := s.makeUC20Model(nil)
   141  	gadgetInfo, err := gadget.ReadInfoAndValidate(gadgetRoot, model, nil)
   142  	c.Assert(err, check.IsNil)
   143  
   144  	err = image.WriteResolvedContent(prepareImageDir, gadgetInfo, gadgetRoot, kernelRoot)
   145  	c.Assert(err, check.IsNil)
   146  
   147  	// XXX: add testutil.DirEquals([][]string)
   148  	cmd := exec.Command("find", ".", "-printf", "%y %P\n")
   149  	cmd.Dir = prepareImageDir
   150  	tree, err := cmd.CombinedOutput()
   151  	c.Assert(err, check.IsNil)
   152  	// sort the tree output
   153  	lines := strings.Split(string(tree), "\n")
   154  	sort.Sort(treeLines(lines))
   155  	c.Check(strings.Join(lines, "\n"), check.Equals, `
   156  d 
   157  d resolved-content
   158  d resolved-content/vol1
   159  l resolved-content/vol1/part1
   160  d resolved-content/vol1/part2
   161  d resolved-content/vol1/part2/EFI
   162  d resolved-content/vol1/part2/EFI/boot
   163  f resolved-content/vol1/part2/EFI/boot/grubx64.efi
   164  d resolved-content/vol2
   165  d resolved-content/vol2/part0
   166  d resolved-content/vol2/part0/subdir
   167  f resolved-content/vol2/part0/subdir/foo
   168  d system-seed
   169  d system-seed/EFI
   170  d system-seed/EFI/boot
   171  f system-seed/EFI/boot/system-seed.efi`)
   172  
   173  	// check symlink target for "ubuntu-seed" -> <prepareImageDir>/system-seed
   174  	t, err := os.Readlink(filepath.Join(prepareImageDir, "resolved-content/vol1/part1"))
   175  	c.Assert(err, check.IsNil)
   176  	c.Check(t, check.Equals, uc20systemSeed)
   177  }