github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/kernel/validate_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020Canonical 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 kernel_test
    21  
    22  import (
    23  	"os"
    24  	"path/filepath"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/kernel"
    29  )
    30  
    31  type validateKernelSuite struct {
    32  	dir string
    33  }
    34  
    35  var _ = Suite(&validateKernelSuite{})
    36  
    37  func (s *validateKernelSuite) SetUpTest(c *C) {
    38  	s.dir = c.MkDir()
    39  }
    40  
    41  func (s *validateKernelSuite) TestValidateMissingContentFile(c *C) {
    42  	var kernelYaml = `
    43  assets:
    44    dtbs:
    45      edition: 1
    46      content:
    47        - foo
    48  `
    49  	mockKernelRoot := makeMockKernel(c, kernelYaml, nil)
    50  
    51  	err := kernel.Validate(mockKernelRoot)
    52  	c.Assert(err, ErrorMatches, `asset "dtbs": content "foo" source path does not exist`)
    53  }
    54  
    55  func (s *validateKernelSuite) TestValidateMissingContentDir(c *C) {
    56  	var kernelYaml = `
    57  assets:
    58    dtbs:
    59      edition: 1
    60      content:
    61        - dir/
    62  `
    63  	mockKernelRoot := makeMockKernel(c, kernelYaml, map[string]string{"dir": ""})
    64  
    65  	err := kernel.Validate(mockKernelRoot)
    66  	c.Assert(err, ErrorMatches, `asset "dtbs": content "dir/" is not a directory`)
    67  }
    68  
    69  func (s *validateKernelSuite) TestValidateHappy(c *C) {
    70  	var kernelYaml = `
    71  assets:
    72    dtbs:
    73      edition: 1
    74      content:
    75        - foo
    76        - dir/
    77  `
    78  	mockKernelRoot := makeMockKernel(c, kernelYaml, map[string]string{
    79  		"foo": "",
    80  	})
    81  	err := os.MkdirAll(filepath.Join(mockKernelRoot, "dir"), 0755)
    82  	c.Assert(err, IsNil)
    83  
    84  	err = kernel.Validate(mockKernelRoot)
    85  	c.Assert(err, IsNil)
    86  }
    87  
    88  func (s *validateKernelSuite) TestValidateHappyNoKernelYaml(c *C) {
    89  	emptyDir := c.MkDir()
    90  	err := kernel.Validate(emptyDir)
    91  	c.Assert(err, IsNil)
    92  }