github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/seed/helpers_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 seed_test
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/asserts"
    30  	"github.com/snapcore/snapd/asserts/assertstest"
    31  	"github.com/snapcore/snapd/seed"
    32  	"github.com/snapcore/snapd/seed/seedtest"
    33  	"github.com/snapcore/snapd/snap"
    34  	"github.com/snapcore/snapd/testutil"
    35  )
    36  
    37  type helpersSuite struct {
    38  	testutil.BaseTest
    39  
    40  	*seedtest.SeedSnaps
    41  
    42  	assertsDir string
    43  
    44  	devAcct *asserts.Account
    45  }
    46  
    47  var _ = Suite(&helpersSuite{})
    48  
    49  func (s *helpersSuite) SetUpTest(c *C) {
    50  	s.BaseTest.SetUpTest(c)
    51  	s.BaseTest.AddCleanup(snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {}))
    52  
    53  	s.SeedSnaps = &seedtest.SeedSnaps{}
    54  	s.SetupAssertSigning("canonical")
    55  
    56  	dir := c.MkDir()
    57  	s.assertsDir = filepath.Join(dir, "assertions")
    58  	err := os.MkdirAll(s.assertsDir, 0755)
    59  	c.Assert(err, IsNil)
    60  
    61  	s.devAcct = assertstest.NewAccount(s.StoreSigning, "developer", map[string]interface{}{
    62  		"account-id": "developerid",
    63  	}, "")
    64  
    65  }
    66  
    67  func (s *helpersSuite) writeAssertions(fn string, assertions ...asserts.Assertion) {
    68  	fn = filepath.Join(s.assertsDir, fn)
    69  	seedtest.WriteAssertions(fn, assertions...)
    70  }
    71  
    72  const fooSnap = `type: app
    73  name: foo
    74  version: 1.0
    75  `
    76  
    77  const barSnap = `type: app
    78  name: bar
    79  version: 2.0
    80  `
    81  
    82  func (s *helpersSuite) TestLoadAssertionsNoAssertions(c *C) {
    83  	os.Remove(s.assertsDir)
    84  
    85  	b, err := seed.LoadAssertions(s.assertsDir, nil)
    86  	c.Check(err, Equals, seed.ErrNoAssertions)
    87  	c.Check(b, IsNil)
    88  }
    89  
    90  func (s *helpersSuite) TestLoadAssertions(c *C) {
    91  	fooDecl, fooRev := s.MakeAssertedSnap(c, fooSnap, nil, snap.R(1), "developerid")
    92  	barDecl, barRev := s.MakeAssertedSnap(c, barSnap, nil, snap.R(2), "developerid")
    93  
    94  	s.writeAssertions("ground.asserts", s.StoreSigning.StoreAccountKey(""))
    95  	s.writeAssertions("foo.asserts", s.devAcct, fooDecl, fooRev)
    96  	s.writeAssertions("bar.asserts", barDecl, barRev)
    97  
    98  	b, err := seed.LoadAssertions(s.assertsDir, nil)
    99  	c.Assert(err, IsNil)
   100  
   101  	db, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
   102  		Backstore: asserts.NewMemoryBackstore(),
   103  		Trusted:   s.StoreSigning.Trusted,
   104  	})
   105  	c.Assert(err, IsNil)
   106  
   107  	err = b.CommitTo(db, nil)
   108  	c.Assert(err, IsNil)
   109  
   110  	_, err = db.Find(asserts.SnapRevisionType, map[string]string{
   111  		"snap-sha3-384": fooRev.SnapSHA3_384(),
   112  	})
   113  	c.Check(err, IsNil)
   114  
   115  	_, err = db.Find(asserts.SnapRevisionType, map[string]string{
   116  		"snap-sha3-384": barRev.SnapSHA3_384(),
   117  	})
   118  	c.Check(err, IsNil)
   119  }
   120  
   121  func (s *helpersSuite) TestLoadAssertionsLoadedCallback(c *C) {
   122  	fooDecl, fooRev := s.MakeAssertedSnap(c, fooSnap, nil, snap.R(1), "developerid")
   123  	barDecl, barRev := s.MakeAssertedSnap(c, barSnap, nil, snap.R(2), "developerid")
   124  
   125  	s.writeAssertions("ground.asserts", s.StoreSigning.StoreAccountKey(""))
   126  	s.writeAssertions("foo.asserts", s.devAcct, fooDecl, fooRev)
   127  	s.writeAssertions("bar.asserts", barDecl, barRev)
   128  
   129  	counts := make(map[string]int)
   130  	seen := make(map[string]bool)
   131  
   132  	loaded := func(ref *asserts.Ref) error {
   133  		if ref.Type == asserts.SnapDeclarationType {
   134  			seen[ref.PrimaryKey[1]] = true
   135  		}
   136  		counts[ref.Type.Name]++
   137  		return nil
   138  	}
   139  
   140  	_, err := seed.LoadAssertions(s.assertsDir, loaded)
   141  	c.Assert(err, IsNil)
   142  
   143  	c.Check(seen, DeepEquals, map[string]bool{
   144  		"bardidididididididididididididid": true,
   145  		"foodidididididididididididididid": true,
   146  	})
   147  
   148  	// overall
   149  	c.Check(counts, DeepEquals, map[string]int{
   150  		"account":          1,
   151  		"account-key":      1,
   152  		"snap-declaration": 2,
   153  		"snap-revision":    2,
   154  	})
   155  }
   156  
   157  func (s *helpersSuite) TestLoadAssertionsLoadedCallbackError(c *C) {
   158  	s.writeAssertions("ground.asserts", s.StoreSigning.StoreAccountKey(""))
   159  
   160  	loaded := func(ref *asserts.Ref) error {
   161  		return fmt.Errorf("boom")
   162  
   163  	}
   164  
   165  	_, err := seed.LoadAssertions(s.assertsDir, loaded)
   166  	c.Assert(err, ErrorMatches, "boom")
   167  }