github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/policy/helpers_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 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 policy_test
    21  
    22  import (
    23  	"github.com/snapcore/snapd/interfaces"
    24  	"github.com/snapcore/snapd/interfaces/policy"
    25  	"github.com/snapcore/snapd/snap"
    26  	"github.com/snapcore/snapd/snap/snaptest"
    27  
    28  	. "gopkg.in/check.v1"
    29  )
    30  
    31  type helpersSuite struct{}
    32  
    33  var _ = Suite(&helpersSuite{})
    34  
    35  func (s *helpersSuite) TestNestedGet(c *C) {
    36  	consumer := snaptest.MockInfo(c, `
    37  name: consumer
    38  version: 0
    39  apps:
    40      app:
    41  plugs:
    42      plug:
    43          interface: interface
    44  `, nil)
    45  	plugInfo := consumer.Plugs["plug"]
    46  	plug := interfaces.NewConnectedPlug(plugInfo, nil, map[string]interface{}{
    47  		"a": "123",
    48  	})
    49  
    50  	producer := snaptest.MockInfo(c, `
    51  name: producer
    52  version: 0
    53  apps:
    54      app:
    55  slots:
    56      slot:
    57          interface: interface
    58  `, nil)
    59  	slotInfo := producer.Slots["slot"]
    60  	slot := interfaces.NewConnectedSlot(slotInfo, nil, map[string]interface{}{
    61  		"a": "123",
    62  	})
    63  
    64  	_, err := policy.NestedGet("slot", slot, "b")
    65  	c.Check(err, ErrorMatches, `slot attribute "b" not found`)
    66  
    67  	_, err = policy.NestedGet("plug", plug, "a.b")
    68  	c.Check(err, ErrorMatches, `plug attribute "a\.b" not found`)
    69  
    70  	v, err := policy.NestedGet("slot", slot, "a")
    71  	c.Check(err, IsNil)
    72  	c.Check(v, Equals, "123")
    73  
    74  	slot = interfaces.NewConnectedSlot(slotInfo, nil, map[string]interface{}{
    75  		"a": map[string]interface{}{
    76  			"b": []interface{}{"1", "2", "3"},
    77  		},
    78  	})
    79  
    80  	v, err = policy.NestedGet("slot", slot, "a.b")
    81  	c.Check(err, IsNil)
    82  	c.Check(v, DeepEquals, []interface{}{"1", "2", "3"})
    83  }
    84  
    85  func (s *helpersSuite) TestSnapdTypeCheck(c *C) {
    86  	// Type checking the snapd snap is done in a special way.
    87  	// It appears to be of type "core" while in reality it is of type "app".
    88  	sideInfo := &snap.SideInfo{
    89  		SnapID: "PMrrV4ml8uWuEUDBT8dSGnKUYbevVhc4",
    90  	}
    91  	snapInfo := snaptest.MockInfo(c, `
    92  name: snapd
    93  version: 1
    94  type: app
    95  slots:
    96      network:
    97  `, sideInfo)
    98  	err := policy.CheckSnapType(snapInfo, []string{"core"})
    99  	c.Assert(err, IsNil)
   100  }