gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/snap/naming/snapref_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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 naming_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/snap/naming"
    26  )
    27  
    28  type snapRefSuite struct{}
    29  
    30  var _ = Suite(&snapRefSuite{})
    31  
    32  func (s *snapRefSuite) TestNewSnapRef(c *C) {
    33  	fooRef := naming.NewSnapRef("foo", "foo-id")
    34  	c.Check(fooRef.SnapName(), Equals, "foo")
    35  	c.Check(fooRef.ID(), Equals, "foo-id")
    36  
    37  	fooNameOnlyRef := naming.NewSnapRef("foo", "")
    38  	c.Check(fooNameOnlyRef.SnapName(), Equals, "foo")
    39  	c.Check(fooNameOnlyRef.ID(), Equals, "")
    40  }
    41  
    42  func (s *snapRefSuite) TestSnap(c *C) {
    43  	fooNameOnlyRef := naming.Snap("foo")
    44  	c.Check(fooNameOnlyRef.SnapName(), Equals, "foo")
    45  	c.Check(fooNameOnlyRef.ID(), Equals, "")
    46  }
    47  
    48  func (s *snapRefSuite) TestSameSnap(c *C) {
    49  	fooRef := naming.NewSnapRef("foo", "foo-id")
    50  	fooNameOnlyRef := naming.NewSnapRef("foo", "")
    51  	altFooRef := naming.NewSnapRef("foo-proj", "foo-id")
    52  	barNameOnylRef := naming.NewSnapRef("bar", "")
    53  	unrelFooRef := naming.NewSnapRef("foo", "unrel-id")
    54  
    55  	c.Check(naming.SameSnap(fooRef, altFooRef), Equals, true)
    56  	c.Check(naming.SameSnap(fooRef, fooNameOnlyRef), Equals, true)
    57  	c.Check(naming.SameSnap(altFooRef, fooNameOnlyRef), Equals, false)
    58  	c.Check(naming.SameSnap(unrelFooRef, fooRef), Equals, false)
    59  	c.Check(naming.SameSnap(fooRef, barNameOnylRef), Equals, false)
    60  	// weakness but expected
    61  	c.Check(naming.SameSnap(unrelFooRef, fooNameOnlyRef), Equals, true)
    62  }
    63  
    64  func (s *snapRefSuite) TestSnapSet(c *C) {
    65  	ss := naming.NewSnapSet(nil)
    66  	c.Check(ss.Empty(), Equals, true)
    67  	c.Check(ss.Size(), Equals, 0)
    68  
    69  	fooRef := naming.NewSnapRef("foo", "foo-id")
    70  	fooNameOnlyRef := naming.Snap("foo")
    71  
    72  	ss.Add(fooRef)
    73  	c.Check(ss.Empty(), Equals, false)
    74  	c.Check(ss.Size(), Equals, 1)
    75  	ss.Add(fooNameOnlyRef)
    76  	c.Check(ss.Size(), Equals, 1)
    77  
    78  	altFooRef := naming.NewSnapRef("foo-proj", "foo-id")
    79  	c.Check(ss.Lookup(fooRef), Equals, fooRef)
    80  	c.Check(ss.Lookup(fooNameOnlyRef), Equals, fooRef)
    81  	c.Check(ss.Lookup(altFooRef), Equals, fooRef)
    82  
    83  	barNameOnylRef := naming.NewSnapRef("bar", "")
    84  	unrelFooRef := naming.NewSnapRef("foo", "unrel-id")
    85  	c.Check(ss.Lookup(barNameOnylRef), Equals, nil)
    86  	c.Check(ss.Lookup(unrelFooRef), Equals, nil)
    87  
    88  	// weaker behavior but expected
    89  	ss1 := naming.NewSnapSet([]naming.SnapRef{fooNameOnlyRef})
    90  	c.Check(ss.Empty(), Equals, false)
    91  	c.Check(ss.Size(), Equals, 1)
    92  	ss1.Add(fooRef)
    93  	c.Check(ss.Size(), Equals, 1)
    94  	c.Check(ss1.Lookup(fooRef), Equals, fooNameOnlyRef)
    95  	c.Check(ss1.Lookup(altFooRef), Equals, nil)
    96  	c.Check(ss1.Lookup(barNameOnylRef), Equals, nil)
    97  	c.Check(ss1.Lookup(unrelFooRef), Equals, fooNameOnlyRef)
    98  }