github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snap-bootstrap/cmd_initramfs_mounts_recover_degraded_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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 main_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	main "github.com/snapcore/snapd/cmd/snap-bootstrap"
    26  )
    27  
    28  func (s *initramfsMountsSuite) TestInitramfsDegradedState(c *C) {
    29  	tt := []struct {
    30  		r         main.RecoverDegradedState
    31  		encrypted bool
    32  		degraded  bool
    33  		comment   string
    34  	}{
    35  		// unencrypted happy
    36  		{
    37  			r: main.RecoverDegradedState{
    38  				UbuntuBoot: main.PartitionState{
    39  					MountState: "mounted",
    40  				},
    41  				UbuntuData: main.PartitionState{
    42  					MountState: "mounted",
    43  				},
    44  				UbuntuSave: main.PartitionState{
    45  					MountState: "absent-but-optional",
    46  				},
    47  			},
    48  			degraded: false,
    49  			comment:  "happy unencrypted no save",
    50  		},
    51  		{
    52  			r: main.RecoverDegradedState{
    53  				UbuntuBoot: main.PartitionState{
    54  					MountState: "mounted",
    55  				},
    56  				UbuntuData: main.PartitionState{
    57  					MountState: "mounted",
    58  				},
    59  				UbuntuSave: main.PartitionState{
    60  					MountState: "mounted",
    61  				},
    62  			},
    63  			degraded: false,
    64  			comment:  "happy unencrypted save",
    65  		},
    66  		// unencrypted unhappy
    67  		{
    68  			r: main.RecoverDegradedState{
    69  				UbuntuBoot: main.PartitionState{
    70  					MountState: "error-mounting",
    71  				},
    72  				UbuntuData: main.PartitionState{
    73  					MountState: "mounted",
    74  				},
    75  				UbuntuSave: main.PartitionState{
    76  					MountState: "absent-but-optional",
    77  				},
    78  				ErrorLog: []string{
    79  					"cannot find ubuntu-boot partition on disk 259:0",
    80  				},
    81  			},
    82  			degraded: true,
    83  			comment:  "unencrypted, error mounting boot",
    84  		},
    85  		{
    86  			r: main.RecoverDegradedState{
    87  				UbuntuBoot: main.PartitionState{
    88  					MountState: "mounted",
    89  				},
    90  				UbuntuData: main.PartitionState{
    91  					MountState: "error-mounting",
    92  				},
    93  				UbuntuSave: main.PartitionState{
    94  					MountState: "absent-but-optional",
    95  				},
    96  				ErrorLog: []string{
    97  					"cannot find ubuntu-data partition on disk 259:0",
    98  				},
    99  			},
   100  			degraded: true,
   101  			comment:  "unencrypted, error mounting data",
   102  		},
   103  		{
   104  			r: main.RecoverDegradedState{
   105  				UbuntuBoot: main.PartitionState{
   106  					MountState: "mounted",
   107  				},
   108  				UbuntuData: main.PartitionState{
   109  					MountState: "mounted",
   110  				},
   111  				UbuntuSave: main.PartitionState{
   112  					MountState: "error-mounting",
   113  				},
   114  				ErrorLog: []string{
   115  					"cannot find ubuntu-save partition on disk 259:0",
   116  				},
   117  			},
   118  			degraded: true,
   119  			comment:  "unencrypted, error mounting save",
   120  		},
   121  
   122  		// encrypted happy
   123  		{
   124  			r: main.RecoverDegradedState{
   125  				UbuntuBoot: main.PartitionState{
   126  					MountState: "mounted",
   127  				},
   128  				UbuntuData: main.PartitionState{
   129  					MountState:  "mounted",
   130  					UnlockState: "unlocked",
   131  					UnlockKey:   "run",
   132  				},
   133  				UbuntuSave: main.PartitionState{
   134  					MountState:  "mounted",
   135  					UnlockState: "unlocked",
   136  					UnlockKey:   "run",
   137  				},
   138  			},
   139  			encrypted: true,
   140  			degraded:  false,
   141  			comment:   "happy encrypted",
   142  		},
   143  		// encrypted unhappy
   144  		{
   145  			r: main.RecoverDegradedState{
   146  				UbuntuBoot: main.PartitionState{
   147  					MountState: "error-mounting",
   148  				},
   149  				UbuntuData: main.PartitionState{
   150  					MountState:  "mounted",
   151  					UnlockState: "unlocked",
   152  					UnlockKey:   "fallback",
   153  				},
   154  				UbuntuSave: main.PartitionState{
   155  					MountState:  "mounted",
   156  					UnlockState: "unlocked",
   157  					UnlockKey:   "run",
   158  				},
   159  				ErrorLog: []string{
   160  					"cannot find ubuntu-boot partition on disk 259:0",
   161  				},
   162  			},
   163  			encrypted: true,
   164  			degraded:  true,
   165  			comment:   "encrypted, no boot, fallback data",
   166  		},
   167  		{
   168  			r: main.RecoverDegradedState{
   169  				UbuntuBoot: main.PartitionState{
   170  					MountState: "mounted",
   171  				},
   172  				UbuntuData: main.PartitionState{
   173  					MountState:  "mounted",
   174  					UnlockState: "unlocked",
   175  					UnlockKey:   "fallback",
   176  				},
   177  				UbuntuSave: main.PartitionState{
   178  					MountState:  "mounted",
   179  					UnlockState: "unlocked",
   180  					UnlockKey:   "run",
   181  				},
   182  				ErrorLog: []string{
   183  					"cannot unlock encrypted ubuntu-data with sealed run key: failed to unlock ubuntu-data",
   184  				},
   185  			},
   186  			encrypted: true,
   187  			degraded:  true,
   188  			comment:   "encrypted, fallback data",
   189  		},
   190  		{
   191  			r: main.RecoverDegradedState{
   192  				UbuntuBoot: main.PartitionState{
   193  					MountState: "mounted",
   194  				},
   195  				UbuntuData: main.PartitionState{
   196  					MountState:  "mounted",
   197  					UnlockState: "unlocked",
   198  					UnlockKey:   "run",
   199  				},
   200  				UbuntuSave: main.PartitionState{
   201  					MountState:  "mounted",
   202  					UnlockState: "unlocked",
   203  					UnlockKey:   "fallback",
   204  				},
   205  				ErrorLog: []string{
   206  					"cannot unlock encrypted ubuntu-save with sealed run key: failed to unlock ubuntu-save",
   207  				},
   208  			},
   209  			encrypted: true,
   210  			degraded:  true,
   211  			comment:   "encrypted, fallback save",
   212  		},
   213  		{
   214  			r: main.RecoverDegradedState{
   215  				UbuntuBoot: main.PartitionState{
   216  					MountState: "mounted",
   217  				},
   218  				UbuntuData: main.PartitionState{
   219  					MountState:  "mounted",
   220  					UnlockState: "unlocked",
   221  					UnlockKey:   "run",
   222  				},
   223  				UbuntuSave: main.PartitionState{
   224  					MountState:  "mounted",
   225  					UnlockState: "unlocked",
   226  					UnlockKey:   "recovery",
   227  				},
   228  				ErrorLog: []string{
   229  					"cannot unlock encrypted ubuntu-save with sealed run key: failed to unlock ubuntu-save",
   230  				},
   231  			},
   232  			encrypted: true,
   233  			degraded:  true,
   234  			comment:   "encrypted, recovery save",
   235  		},
   236  		{
   237  			r: main.RecoverDegradedState{
   238  				UbuntuBoot: main.PartitionState{
   239  					MountState: "mounted",
   240  				},
   241  				UbuntuData: main.PartitionState{
   242  					MountState:  "mounted",
   243  					UnlockState: "unlocked",
   244  					UnlockKey:   "fallback",
   245  				},
   246  				UbuntuSave: main.PartitionState{
   247  					MountState:  "mounted",
   248  					UnlockState: "unlocked",
   249  					UnlockKey:   "fallback",
   250  				},
   251  				ErrorLog: []string{
   252  					"cannot unlock encrypted ubuntu-data with sealed run key: failed to unlock ubuntu-data",
   253  				},
   254  			},
   255  			encrypted: true,
   256  			degraded:  true,
   257  			comment:   "encrypted, fallback data, fallback save",
   258  		},
   259  		{
   260  			r: main.RecoverDegradedState{
   261  				UbuntuBoot: main.PartitionState{
   262  					MountState: "mounted",
   263  				},
   264  				UbuntuData: main.PartitionState{
   265  					MountState:  "mounted",
   266  					UnlockState: "unlocked",
   267  					UnlockKey:   "fallback",
   268  				},
   269  				UbuntuSave: main.PartitionState{
   270  					MountState:  "not-mounted",
   271  					UnlockState: "not-unlocked",
   272  				},
   273  				ErrorLog: []string{
   274  					"cannot unlock encrypted ubuntu-save with sealed run key: failed to unlock ubuntu-save",
   275  					"cannot unlock encrypted ubuntu-save with sealed fallback key: failed to unlock ubuntu-save",
   276  				},
   277  			},
   278  			encrypted: true,
   279  			degraded:  true,
   280  			comment:   "encrypted, fallback data, no save",
   281  		},
   282  	}
   283  
   284  	for _, t := range tt {
   285  		var comment CommentInterface
   286  		if t.comment != "" {
   287  			comment = Commentf(t.comment)
   288  		}
   289  
   290  		c.Assert(t.r.Degraded(t.encrypted), Equals, t.degraded, comment)
   291  	}
   292  }