github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/interfaces/hotplug/proposed_slot_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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 hotplug
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/dirs"
    26  	"github.com/snapcore/snapd/testutil"
    27  )
    28  
    29  type proposedSlotSuite struct {
    30  	testutil.BaseTest
    31  }
    32  
    33  var _ = Suite(&proposedSlotSuite{})
    34  
    35  func (s *proposedSlotSuite) SetUpTest(c *C) {
    36  	s.BaseTest.SetUpTest(c)
    37  	dirs.SetRootDir(c.MkDir())
    38  }
    39  
    40  func (s *proposedSlotSuite) TearDownTest(c *C) {
    41  	s.BaseTest.TearDownTest(c)
    42  	dirs.SetRootDir("")
    43  }
    44  
    45  func (s *proposedSlotSuite) TestCleanHappy(c *C) {
    46  	slot := &ProposedSlot{Name: "slot1", Label: "A slot", Attrs: map[string]interface{}{"foo": "bar"}}
    47  	slot, err := slot.Clean()
    48  	c.Assert(err, IsNil)
    49  	c.Assert(slot, DeepEquals, &ProposedSlot{Name: "slot1", Label: "A slot", Attrs: map[string]interface{}{"foo": "bar"}})
    50  }
    51  
    52  func (s *proposedSlotSuite) TestNilAttrs(c *C) {
    53  	slot := &ProposedSlot{Name: "slot"}
    54  	slot, err := slot.Clean()
    55  	c.Assert(err, IsNil)
    56  	c.Assert(slot, DeepEquals, &ProposedSlot{Name: "slot", Attrs: map[string]interface{}{}})
    57  }
    58  
    59  func (s *proposedSlotSuite) TestDeepCopy(c *C) {
    60  	attrs := map[string]interface{}{"foo": "bar"}
    61  	slot := &ProposedSlot{Name: "slot1", Attrs: attrs}
    62  	slot, err := slot.Clean()
    63  	c.Assert(err, IsNil)
    64  	attrs["foo"] = "modified"
    65  	c.Assert(slot, DeepEquals, &ProposedSlot{Name: "slot1", Label: "", Attrs: map[string]interface{}{"foo": "bar"}})
    66  }
    67  
    68  func (s *proposedSlotSuite) TestEmptyName(c *C) {
    69  	slot := &ProposedSlot{Label: "A slot", Attrs: map[string]interface{}{"foo": "bar"}}
    70  	slot, err := slot.Clean()
    71  	c.Assert(err, IsNil)
    72  	c.Assert(slot, DeepEquals, &ProposedSlot{Name: "", Label: "A slot", Attrs: map[string]interface{}{"foo": "bar"}})
    73  }
    74  
    75  func (s *proposedSlotSuite) TestInvalidName(c *C) {
    76  	slot := &ProposedSlot{Name: "slot!"}
    77  	slot, err := slot.Clean()
    78  	c.Assert(err, ErrorMatches, `invalid slot name: "slot!"`)
    79  	c.Assert(slot, IsNil)
    80  }