gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/runsc/boot/gofer_conf_test.go (about)

     1  // Copyright 2023 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package boot
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestGoferConf(t *testing.T) {
    22  	tcs := []struct {
    23  		cfg          GoferMountConf
    24  		wantOverlay  bool
    25  		wantHostFile bool
    26  		wantLisafs   bool
    27  		wantTmpfs    bool
    28  		wantErofs    bool
    29  		wantValid    bool
    30  	}{{
    31  		cfg: GoferMountConf{Lower: NoneLower, Upper: NoOverlay},
    32  		// This is not a valid config.
    33  		wantValid: false,
    34  	}, {
    35  		cfg:          GoferMountConf{Lower: NoneLower, Upper: MemoryOverlay},
    36  		wantOverlay:  false,
    37  		wantHostFile: false,
    38  		wantLisafs:   false,
    39  		wantTmpfs:    true,
    40  		wantErofs:    false,
    41  		wantValid:    true,
    42  	}, {
    43  		cfg:          GoferMountConf{Lower: NoneLower, Upper: SelfOverlay},
    44  		wantOverlay:  false,
    45  		wantHostFile: true,
    46  		wantLisafs:   false,
    47  		wantTmpfs:    true,
    48  		wantErofs:    false,
    49  		wantValid:    true,
    50  	}, {
    51  		cfg:          GoferMountConf{Lower: NoneLower, Upper: AnonOverlay},
    52  		wantOverlay:  false,
    53  		wantHostFile: true,
    54  		wantLisafs:   false,
    55  		wantTmpfs:    true,
    56  		wantErofs:    false,
    57  		wantValid:    true,
    58  	}, {
    59  		cfg:          GoferMountConf{Lower: Lisafs, Upper: NoOverlay},
    60  		wantOverlay:  false,
    61  		wantHostFile: false,
    62  		wantLisafs:   true,
    63  		wantTmpfs:    false,
    64  		wantErofs:    false,
    65  		wantValid:    true,
    66  	}, {
    67  		cfg:          GoferMountConf{Lower: Lisafs, Upper: MemoryOverlay},
    68  		wantOverlay:  true,
    69  		wantHostFile: false,
    70  		wantLisafs:   true,
    71  		wantTmpfs:    false,
    72  		wantErofs:    false,
    73  		wantValid:    true,
    74  	}, {
    75  		cfg:          GoferMountConf{Lower: Lisafs, Upper: SelfOverlay},
    76  		wantOverlay:  true,
    77  		wantHostFile: true,
    78  		wantLisafs:   true,
    79  		wantTmpfs:    false,
    80  		wantErofs:    false,
    81  		wantValid:    true,
    82  	}, {
    83  		cfg:          GoferMountConf{Lower: Lisafs, Upper: AnonOverlay},
    84  		wantOverlay:  true,
    85  		wantHostFile: true,
    86  		wantLisafs:   true,
    87  		wantTmpfs:    false,
    88  		wantErofs:    false,
    89  		wantValid:    true,
    90  	}, {
    91  		cfg:          GoferMountConf{Lower: Erofs, Upper: NoOverlay},
    92  		wantOverlay:  false,
    93  		wantHostFile: false,
    94  		wantLisafs:   false,
    95  		wantTmpfs:    false,
    96  		wantErofs:    true,
    97  		wantValid:    true,
    98  	}, {
    99  		cfg:          GoferMountConf{Lower: Erofs, Upper: MemoryOverlay},
   100  		wantOverlay:  true,
   101  		wantHostFile: false,
   102  		wantLisafs:   false,
   103  		wantTmpfs:    false,
   104  		wantErofs:    true,
   105  		wantValid:    true,
   106  	}, {
   107  		cfg:          GoferMountConf{Lower: Erofs, Upper: SelfOverlay},
   108  		wantOverlay:  true,
   109  		wantHostFile: true,
   110  		wantLisafs:   false,
   111  		wantTmpfs:    false,
   112  		wantErofs:    true,
   113  		wantValid:    true,
   114  	}, {
   115  		cfg:          GoferMountConf{Lower: Erofs, Upper: AnonOverlay},
   116  		wantOverlay:  true,
   117  		wantHostFile: true,
   118  		wantLisafs:   false,
   119  		wantTmpfs:    false,
   120  		wantErofs:    true,
   121  		wantValid:    true,
   122  	}, {
   123  		cfg: GoferMountConf{Lower: LowerMax, Upper: UpperMax},
   124  		// This is not a valid config.
   125  		wantValid: false,
   126  	}}
   127  	for _, tc := range tcs {
   128  		if got := tc.cfg.valid(); got != tc.wantValid {
   129  			t.Errorf("gofer conf = %+v, valid() = %t, want = %t", tc.cfg, got, tc.wantValid)
   130  		}
   131  		if !tc.wantValid {
   132  			// Skip the following tests, if this is not a valid config.
   133  			continue
   134  		}
   135  		if got := tc.cfg.ShouldUseOverlayfs(); got != tc.wantOverlay {
   136  			t.Errorf("gofer conf = %+v, ShouldUseOverlayfs() = %t, want = %t", tc.cfg, got, tc.wantOverlay)
   137  		}
   138  		if got := tc.cfg.IsFilestorePresent(); got != tc.wantHostFile {
   139  			t.Errorf("gofer conf = %+v, IsFilestorePresent() = %t, want = %t", tc.cfg, got, tc.wantHostFile)
   140  		}
   141  		if got := tc.cfg.ShouldUseLisafs(); got != tc.wantLisafs {
   142  			t.Errorf("gofer conf = %+v, ShouldUseLisafs() = %t, want = %t", tc.cfg, got, tc.wantLisafs)
   143  		}
   144  		if got := tc.cfg.ShouldUseTmpfs(); got != tc.wantTmpfs {
   145  			t.Errorf("gofer conf = %+v, ShouldUseTmpfs() = %t, want = %t", tc.cfg, got, tc.wantTmpfs)
   146  		}
   147  		if got := tc.cfg.ShouldUseErofs(); got != tc.wantErofs {
   148  			t.Errorf("gofer conf = %+v, ShouldUseErofs() = %t, want = %t", tc.cfg, got, tc.wantErofs)
   149  		}
   150  	}
   151  }
   152  
   153  func TestGoferConfFlags(t *testing.T) {
   154  	want := GoferMountConfFlags{
   155  		{Lower: NoneLower, Upper: MemoryOverlay},
   156  		{Lower: NoneLower, Upper: SelfOverlay},
   157  		{Lower: NoneLower, Upper: AnonOverlay},
   158  		{Lower: Lisafs, Upper: NoOverlay},
   159  		{Lower: Lisafs, Upper: MemoryOverlay},
   160  		{Lower: Lisafs, Upper: SelfOverlay},
   161  		{Lower: Lisafs, Upper: AnonOverlay},
   162  		{Lower: Erofs, Upper: NoOverlay},
   163  		{Lower: Erofs, Upper: MemoryOverlay},
   164  		{Lower: Erofs, Upper: SelfOverlay},
   165  		{Lower: Erofs, Upper: AnonOverlay},
   166  	}
   167  	var got GoferMountConfFlags
   168  	got.Set(want.String())
   169  	if len(got) != len(want) {
   170  		t.Fatalf("gofer conf flags is incorrect length: want = %d, got = %d", len(want), len(got))
   171  	}
   172  	for i := range want {
   173  		if want[i] != got[i] {
   174  			t.Errorf("gofer conf is incorrect: want = %d, got = %d", want[i], got[i])
   175  		}
   176  	}
   177  }