github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/patch/patch6_1_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 patch_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/overlord/patch"
    31  	"github.com/snapcore/snapd/overlord/state"
    32  	"github.com/snapcore/snapd/snap"
    33  	"github.com/snapcore/snapd/snap/snaptest"
    34  )
    35  
    36  type patch61Suite struct{}
    37  
    38  var _ = Suite(&patch61Suite{})
    39  
    40  var statePatch6_1JSON = []byte(`
    41  {
    42  	"last-task-id": 999,
    43  	"last-change-id": 99,
    44  
    45  	"data": {
    46  		"patch-level": 6,
    47  		"snaps": {
    48  			"core": {
    49  				"sequence": [{"name": "core", "revision": "2"}],
    50          "flags": 1,
    51  				"current": "2"}
    52  			},
    53  	  "conns": {
    54  	    "gnome-calculator:icon-themes gtk-common-themes:icon-themes": {
    55  				"auto": true,
    56  				"interface": "content",
    57  				"plug-static": {
    58  					"content": "icon-themes",
    59  					"default-provider": "gtk-common-themes",
    60  					"target": "$SNAP/data-dir/icons"
    61  				},
    62  				"slot-static": {
    63  					"content": "icon-themes",
    64  					"source": {
    65  						"read": ["$SNAP/share/icons/Adwaita"]
    66  					}
    67  				}
    68  			},
    69  			"foobar:fooplug gtk-common-themes:icon-themes": {
    70  				"auto": true,
    71  				"interface": "content"
    72  			},
    73  			"gnome-calculator:network core:network": {
    74  				"auto": true,
    75  				"interface": "network"
    76  			},
    77  			"other-snap:icon-themes gtk-common-themes:icon-themes": {
    78  				"auto": true,
    79  				"interface": "content",
    80  				"undesired":true
    81  			}
    82  		}
    83  	},
    84  		"changes": {
    85  			"1": {
    86  				"id": "1",
    87  				"kind": "install-snap",
    88  				"summary": "install a snap",
    89  				"status": 0,
    90  				"data": {"snap-names": ["foobar"]},
    91  				"task-ids": ["11"]
    92  			}
    93  	},
    94  	"tasks": {
    95  		"11": {
    96  				"id": "11",
    97  				"change": "1",
    98  				"kind": "discard-conns",
    99  				"summary": "",
   100  				"status": 0,
   101  				"data": {"snap-setup": {
   102  								"channel": "edge",
   103  								"flags": 1
   104  				},
   105  				"removed": {
   106  				    "foobar:fooplug gtk-common-themes:icon-themes": {
   107  				      "auto": true,
   108  				      "interface": "content"
   109  				    }
   110  				}
   111  			},
   112    		"halt-tasks": []
   113      }
   114  	}
   115  }`)
   116  
   117  var mockSnap1Yaml = `
   118  name: gnome-calculator
   119  version: 3.28.2
   120  summary: GNOME Calculator
   121  grade: stable
   122  plugs:
   123    icon-themes:
   124      default-provider: gtk-common-themes
   125      interface: content
   126      target: $SNAP/data-dir/icons
   127    sound-themes:
   128      default-provider: gtk-common-themes
   129      interface: content
   130      target: $SNAP/data-dir/sounds
   131  slots:
   132    gnome-calculator:
   133      bus: session
   134      interface: dbus
   135      name: org.gnome.Calculator
   136  apps:
   137    gnome-calculator:
   138      command: command-gnome-calculator.wrapper
   139  `
   140  
   141  var mockSnap2Yaml = `
   142  name: foobar
   143  version: 1
   144  summary: FooBar snap
   145  plugs:
   146    fooplug:
   147      interface: content
   148      target: $SNAP/foo-dir/icons
   149  slots:
   150    fooslot:
   151      bus: session
   152      interface: dbus
   153      name: org.gnome.Calculator
   154  apps:
   155    foo:
   156      command: bar
   157  `
   158  
   159  var mockSnap3Yaml = `
   160  name: gtk-common-themes
   161  version: 1
   162  summary: gtk common themes snap
   163  slots:
   164    icon-themes:
   165      bus: session
   166      interface: dbus
   167      name: org.gnome.Calculator
   168  apps:
   169    foo:
   170      command: bar
   171  `
   172  
   173  var mockSnap4Yaml = `
   174  name: other-snap
   175  version: 1.1
   176  plugs:
   177    icon-themes:
   178      default-provider: gtk-common-themes
   179      interface: content
   180      target: $SNAP/data-dir/icons
   181  apps:
   182    foo:
   183      command: bar
   184  `
   185  
   186  func (s *patch61Suite) SetUpTest(c *C) {
   187  	dirs.SetRootDir(c.MkDir())
   188  
   189  	err := os.MkdirAll(filepath.Dir(dirs.SnapStateFile), 0755)
   190  	c.Assert(err, IsNil)
   191  	err = ioutil.WriteFile(dirs.SnapStateFile, statePatch6_1JSON, 0644)
   192  	c.Assert(err, IsNil)
   193  
   194  	snap.MockSanitizePlugsSlots(func(*snap.Info) {})
   195  
   196  	snaptest.MockSnapCurrent(c, mockSnap1Yaml, &snap.SideInfo{Revision: snap.R("x1")})
   197  	snaptest.MockSnapCurrent(c, mockSnap2Yaml, &snap.SideInfo{Revision: snap.R("x1")})
   198  	snaptest.MockSnapCurrent(c, mockSnap3Yaml, &snap.SideInfo{Revision: snap.R("x1")})
   199  	snaptest.MockSnapCurrent(c, mockSnap4Yaml, &snap.SideInfo{Revision: snap.R("x1")})
   200  }
   201  
   202  func (s *patch61Suite) TestPatch61(c *C) {
   203  	restore := patch.MockLevel(6, 1)
   204  	defer restore()
   205  
   206  	r, err := os.Open(dirs.SnapStateFile)
   207  	c.Assert(err, IsNil)
   208  	defer r.Close()
   209  	st, err := state.ReadState(nil, r)
   210  	c.Assert(err, IsNil)
   211  
   212  	// repeat the patch a few times to ensure it's idempotent
   213  	for i := 0; i < 3; i++ {
   214  		st.Lock()
   215  		sublevel := 0
   216  		st.Set("patch-sublevel", sublevel)
   217  		st.Unlock()
   218  
   219  		c.Assert(patch.Apply(st), IsNil)
   220  		st.Lock()
   221  
   222  		st.Get("patch-sublevel", &sublevel)
   223  		c.Assert(sublevel, Equals, 1)
   224  
   225  		var conns map[string]interface{}
   226  		c.Assert(st.Get("conns", &conns), IsNil)
   227  		c.Assert(conns, DeepEquals, map[string]interface{}{
   228  			"gnome-calculator:icon-themes gtk-common-themes:icon-themes": map[string]interface{}{
   229  				"auto":      true,
   230  				"interface": "content",
   231  				"plug-static": map[string]interface{}{
   232  					"content":          "icon-themes",
   233  					"default-provider": "gtk-common-themes",
   234  					"target":           "$SNAP/data-dir/icons",
   235  				},
   236  				"slot-static": map[string]interface{}{
   237  					"content": "icon-themes",
   238  					"source": map[string]interface{}{
   239  						"read": []interface{}{"$SNAP/share/icons/Adwaita"},
   240  					},
   241  				},
   242  			},
   243  			"foobar:fooplug gtk-common-themes:icon-themes": map[string]interface{}{
   244  				"auto":      true,
   245  				"interface": "content",
   246  				"plug-static": map[string]interface{}{
   247  					"target": "$SNAP/foo-dir/icons",
   248  				},
   249  				"slot-static": map[string]interface{}{
   250  					"bus":  "session",
   251  					"name": "org.gnome.Calculator",
   252  				},
   253  			},
   254  			"gnome-calculator:network core:network": map[string]interface{}{
   255  				"auto":      true,
   256  				"interface": "network",
   257  			},
   258  			"other-snap:icon-themes gtk-common-themes:icon-themes": map[string]interface{}{
   259  				"undesired": true,
   260  				"auto":      true,
   261  				"interface": "content",
   262  			},
   263  		})
   264  
   265  		var removed map[string]interface{}
   266  		task := st.Task("11")
   267  		c.Assert(task, NotNil)
   268  		c.Assert(task.Get("removed", &removed), IsNil)
   269  		c.Assert(removed, DeepEquals, map[string]interface{}{
   270  			"foobar:fooplug gtk-common-themes:icon-themes": map[string]interface{}{
   271  				"auto":      true,
   272  				"interface": "content",
   273  				"plug-static": map[string]interface{}{
   274  					"target": "$SNAP/foo-dir/icons",
   275  				},
   276  				"slot-static": map[string]interface{}{
   277  					"bus":  "session",
   278  					"name": "org.gnome.Calculator",
   279  				},
   280  			},
   281  		})
   282  		st.Unlock()
   283  	}
   284  }