github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/mount/libmount/mount_table_test.go (about)

     1  /*
     2  Copyright 2020 The OpenEBS Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package libmount
    18  
    19  import (
    20  	"math/rand"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestMountTab_applyFilters(t *testing.T) {
    26  	type fields struct {
    27  		format       MountTabFormat
    28  		entries      []*Filesystem
    29  		allowFilters []FsFilter
    30  		denyFilters  []FsFilter
    31  	}
    32  	type args struct {
    33  		fs *Filesystem
    34  	}
    35  
    36  	allowAll := allowAllFilter()
    37  	denyAll := denyAllFilter()
    38  	tests := []struct {
    39  		name   string
    40  		fields fields
    41  		args   args
    42  		want   bool
    43  	}{
    44  		{
    45  			name: "no apply/deny filters",
    46  			want: true,
    47  		},
    48  		{
    49  			name: "allow filters only, fs allowed",
    50  			fields: fields{
    51  				allowFilters: []FsFilter{allowAll},
    52  			},
    53  			want: true,
    54  		},
    55  		{
    56  			name: "allow filters only, fs not allowed",
    57  			fields: fields{
    58  				allowFilters: []FsFilter{denyAll},
    59  			},
    60  			want: false,
    61  		},
    62  		{
    63  			name: "deny filters only, fs denied",
    64  			fields: fields{
    65  				denyFilters: []FsFilter{allowAll},
    66  			},
    67  			want: false,
    68  		},
    69  		{
    70  			name: "deny filters only, fs not denied",
    71  			fields: fields{
    72  				denyFilters: []FsFilter{denyAll},
    73  			},
    74  			want: true,
    75  		},
    76  		{
    77  			name: "both allow and deny filters, fs allowed and denied",
    78  			fields: fields{
    79  				allowFilters: []FsFilter{allowAll},
    80  				denyFilters:  []FsFilter{allowAll},
    81  			},
    82  			want: false,
    83  		},
    84  		{
    85  			name: "both allow and deny filters, fs allowed but not denied",
    86  			fields: fields{
    87  				allowFilters: []FsFilter{allowAll},
    88  				denyFilters:  []FsFilter{denyAll},
    89  			},
    90  			want: true,
    91  		},
    92  		{
    93  			name: "both allow and deny filters, fs denied but not allowed",
    94  			fields: fields{
    95  				allowFilters: []FsFilter{denyAll},
    96  				denyFilters:  []FsFilter{allowAll},
    97  			},
    98  			want: false,
    99  		},
   100  		{
   101  			name: "both allow and deny filters, fs neither allowed nor denied",
   102  			fields: fields{
   103  				allowFilters: []FsFilter{denyAll},
   104  				denyFilters:  []FsFilter{denyAll},
   105  			},
   106  			want: false,
   107  		},
   108  	}
   109  	for _, tt := range tests {
   110  		t.Run(tt.name, func(t *testing.T) {
   111  			mt := &MountTab{
   112  				format:       tt.fields.format,
   113  				entries:      tt.fields.entries,
   114  				allowFilters: tt.fields.allowFilters,
   115  				denyFilters:  tt.fields.denyFilters,
   116  			}
   117  			if got := mt.applyFilters(tt.args.fs); got != tt.want {
   118  				t.Errorf("MountTab.applyFilters() = %v, want %v", got, tt.want)
   119  			}
   120  		})
   121  	}
   122  }
   123  
   124  func TestMountTab_AddFilesystem(t *testing.T) {
   125  	type fields struct {
   126  		format       MountTabFormat
   127  		entries      []*Filesystem
   128  		allowFilters []FsFilter
   129  		denyFilters  []FsFilter
   130  	}
   131  	type args struct {
   132  		fs *Filesystem
   133  	}
   134  	tests := []struct {
   135  		name    string
   136  		fields  fields
   137  		args    args
   138  		wantErr bool
   139  	}{
   140  		{
   141  			name:    "filesystem arg nil",
   142  			args:    args{nil},
   143  			wantErr: true,
   144  		},
   145  		{
   146  			name:    "filesystem busy",
   147  			args:    args{&Filesystem{tab: &MountTab{}}},
   148  			wantErr: true,
   149  		},
   150  		{
   151  			name: "filesystem denied by filters",
   152  			fields: fields{
   153  				denyFilters: []FsFilter{allowAllFilter()},
   154  			},
   155  			args:    args{NewFilesystem()},
   156  			wantErr: true,
   157  		},
   158  		{
   159  			name:    "filesystem accepted and added",
   160  			args:    args{NewFilesystem()},
   161  			wantErr: false,
   162  		},
   163  	}
   164  	for _, tt := range tests {
   165  		t.Run(tt.name, func(t *testing.T) {
   166  			mt := &MountTab{
   167  				format:       tt.fields.format,
   168  				entries:      tt.fields.entries,
   169  				allowFilters: tt.fields.allowFilters,
   170  				denyFilters:  tt.fields.denyFilters,
   171  			}
   172  			if err := mt.AddFilesystem(tt.args.fs); (err != nil) != tt.wantErr {
   173  				t.Errorf("MountTab.AddFilesystem() error = %v, wantErr %v", err, tt.wantErr)
   174  			}
   175  		})
   176  	}
   177  }
   178  
   179  func TestMountTab_Size(t *testing.T) {
   180  	type fields struct {
   181  		format       MountTabFormat
   182  		entries      []*Filesystem
   183  		allowFilters []FsFilter
   184  		denyFilters  []FsFilter
   185  	}
   186  	// get a random length for the slice
   187  	randSize := rand.Intn(10)
   188  	tests := []struct {
   189  		name   string
   190  		fields fields
   191  		want   int
   192  	}{
   193  		{
   194  			name: "random size",
   195  			fields: fields{
   196  				entries: make([]*Filesystem, randSize),
   197  			},
   198  			want: randSize,
   199  		},
   200  	}
   201  	for _, tt := range tests {
   202  		t.Run(tt.name, func(t *testing.T) {
   203  			mt := &MountTab{
   204  				format:       tt.fields.format,
   205  				entries:      tt.fields.entries,
   206  				allowFilters: tt.fields.allowFilters,
   207  				denyFilters:  tt.fields.denyFilters,
   208  			}
   209  			if got := mt.Size(); got != tt.want {
   210  				t.Errorf("MountTab.Size() = %v, want %v", got, tt.want)
   211  			}
   212  		})
   213  	}
   214  }
   215  
   216  func TestMountTab_Find(t *testing.T) {
   217  	type fields struct {
   218  		format       MountTabFormat
   219  		entries      []*Filesystem
   220  		allowFilters []FsFilter
   221  		denyFilters  []FsFilter
   222  	}
   223  	type args struct {
   224  		filters []FsFilter
   225  	}
   226  
   227  	fs := NewFilesystem()
   228  	fsEntries := []*Filesystem{fs}
   229  	matchAll := allowAllFilter()
   230  	matchNone := denyAllFilter()
   231  	tests := []struct {
   232  		name   string
   233  		fields fields
   234  		args   args
   235  		want   *Filesystem
   236  	}{
   237  		{
   238  			name:   "not matching fs present",
   239  			fields: fields{entries: fsEntries},
   240  			args:   args{filters: []FsFilter{matchNone}},
   241  			want:   nil,
   242  		},
   243  		{
   244  			name:   "matching fs present",
   245  			fields: fields{entries: fsEntries},
   246  			args:   args{filters: []FsFilter{matchAll}},
   247  			want:   fs,
   248  		},
   249  		{
   250  			name:   "multiple filters passed, one or more filters doen't match",
   251  			fields: fields{entries: fsEntries},
   252  			args:   args{filters: []FsFilter{matchAll, matchNone, matchAll}},
   253  			want:   nil,
   254  		},
   255  		{
   256  			name: "fs entries empty",
   257  			args: args{filters: []FsFilter{matchAll}},
   258  			want: nil,
   259  		},
   260  	}
   261  	for _, tt := range tests {
   262  		t.Run(tt.name, func(t *testing.T) {
   263  			mt := &MountTab{
   264  				format:       tt.fields.format,
   265  				entries:      tt.fields.entries,
   266  				allowFilters: tt.fields.allowFilters,
   267  				denyFilters:  tt.fields.denyFilters,
   268  			}
   269  			if got := mt.Find(tt.args.filters...); !reflect.DeepEqual(got, tt.want) {
   270  				t.Errorf("MountTab.Find() = %v, want %v", got, tt.want)
   271  			}
   272  		})
   273  	}
   274  }
   275  
   276  func allowAllFilter() FsFilter {
   277  	return func(f *Filesystem) bool {
   278  		return true
   279  	}
   280  }
   281  
   282  func denyAllFilter() FsFilter {
   283  	return func(f *Filesystem) bool {
   284  		return false
   285  	}
   286  }