github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/mount/libmount/mount_table_diff_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  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestMountTabDiff_getMountEntry(t *testing.T) {
    25  	type args struct {
    26  		source string
    27  		id     int
    28  	}
    29  
    30  	requiredArgs := args{"src1", 1}
    31  	requiredFs := &Filesystem{
    32  		id:     requiredArgs.id,
    33  		source: requiredArgs.source,
    34  	}
    35  	requiredMTDEntry := &MountTabDiffEntry{
    36  		action: MountActionMount,
    37  		newFs:  requiredFs,
    38  	}
    39  
    40  	tests := []struct {
    41  		name string
    42  		md   MountTabDiff
    43  		want *MountTabDiffEntry
    44  	}{
    45  		{
    46  			name: "no mount action entry present",
    47  			md: []*MountTabDiffEntry{
    48  				{
    49  					action: MountActionMove,
    50  					newFs:  requiredFs,
    51  				},
    52  				{
    53  					action: MountActionRemount,
    54  					newFs:  requiredFs,
    55  				},
    56  				{
    57  					action: MountActionUmount,
    58  					newFs:  requiredFs,
    59  				},
    60  			},
    61  			want: nil,
    62  		},
    63  		{
    64  			name: "no matching id",
    65  			md: []*MountTabDiffEntry{
    66  				{
    67  					action: MountActionMount,
    68  					newFs: &Filesystem{
    69  						id:     2,
    70  						source: "src1",
    71  					},
    72  				},
    73  			},
    74  			want: nil,
    75  		},
    76  		{
    77  			name: "no matching source",
    78  			md: []*MountTabDiffEntry{
    79  				{
    80  					action: MountActionMount,
    81  					newFs: &Filesystem{
    82  						id:     1,
    83  						source: "src2",
    84  					},
    85  				},
    86  			},
    87  			want: nil,
    88  		},
    89  		{
    90  			name: "action, source and id match",
    91  			md: []*MountTabDiffEntry{
    92  				{
    93  					action: MountActionMount,
    94  					newFs: &Filesystem{
    95  						id:     1,
    96  						source: "src2",
    97  					},
    98  				},
    99  				requiredMTDEntry,
   100  			},
   101  			want: requiredMTDEntry,
   102  		},
   103  	}
   104  	for _, tt := range tests {
   105  		t.Run(tt.name, func(t *testing.T) {
   106  			if got := tt.md.getMountEntry(requiredArgs.source, requiredArgs.id); !reflect.DeepEqual(got, tt.want) {
   107  				t.Errorf("MountTabDiff.getMountEntry() = %v, want %v", got, tt.want)
   108  			}
   109  		})
   110  	}
   111  }
   112  
   113  func TestGenerateDiff(t *testing.T) {
   114  	type args struct {
   115  		oldTab *MountTab
   116  		newTab *MountTab
   117  	}
   118  
   119  	dummyFs := NewFilesystem()
   120  	dummyMountTab := &MountTab{}
   121  	tests := []struct {
   122  		name string
   123  		args args
   124  		want MountTabDiff
   125  	}{
   126  		{
   127  			name: "both args nil",
   128  			args: args{nil, nil},
   129  			want: NewMountTabDiff(),
   130  		},
   131  		{
   132  			name: "both tabs empty",
   133  			args: args{&MountTab{}, &MountTab{}},
   134  			want: NewMountTabDiff(),
   135  		},
   136  		{
   137  			name: "oldTab nil",
   138  			args: args{nil, &MountTab{entries: []*Filesystem{dummyFs}}},
   139  			want: MountTabDiff{{action: MountActionMount, oldFs: nil, newFs: dummyFs}},
   140  		},
   141  		{
   142  			name: "new mounts only",
   143  			args: args{dummyMountTab, &MountTab{entries: []*Filesystem{dummyFs}}},
   144  			want: MountTabDiff{{action: MountActionMount, oldFs: nil, newFs: dummyFs}},
   145  		},
   146  		{
   147  			name: "newFs nil",
   148  			args: args{&MountTab{entries: []*Filesystem{dummyFs}}, nil},
   149  			want: MountTabDiff{{action: MountActionUmount, oldFs: dummyFs, newFs: nil}},
   150  		},
   151  		{
   152  			name: "all fs unmounted",
   153  			args: args{&MountTab{entries: []*Filesystem{dummyFs}}, dummyMountTab},
   154  			want: MountTabDiff{{action: MountActionUmount, oldFs: dummyFs, newFs: nil}},
   155  		},
   156  
   157  		// TODO: Add test for other types of changes
   158  	}
   159  	for _, tt := range tests {
   160  		t.Run(tt.name, func(t *testing.T) {
   161  			if got := GenerateDiff(tt.args.oldTab, tt.args.newTab); !reflect.DeepEqual(got, tt.want) {
   162  				t.Errorf("GenerateDiff() = %v, want %v", got, tt.want)
   163  			}
   164  		})
   165  	}
   166  }
   167  
   168  func TestMountTabDiffEntry_GetAction(t *testing.T) {
   169  	type fields struct {
   170  		action MountAction
   171  	}
   172  	tests := []struct {
   173  		name   string
   174  		fields fields
   175  		want   MountAction
   176  	}{
   177  		{
   178  			name:   "mount",
   179  			fields: fields{action: MountActionMount},
   180  			want:   MountActionMount,
   181  		},
   182  		{
   183  			name:   "umount",
   184  			fields: fields{action: MountActionUmount},
   185  			want:   MountActionUmount,
   186  		},
   187  		{
   188  			name:   "move",
   189  			fields: fields{action: MountActionMove},
   190  			want:   MountActionMove,
   191  		},
   192  		{
   193  			name:   "remount",
   194  			fields: fields{action: MountActionRemount},
   195  			want:   MountActionRemount,
   196  		},
   197  	}
   198  	for _, tt := range tests {
   199  		t.Run(tt.name, func(t *testing.T) {
   200  			mde := &MountTabDiffEntry{
   201  				action: tt.fields.action,
   202  			}
   203  			if got := mde.GetAction(); got != tt.want {
   204  				t.Errorf("MountTabDiffEntry.GetAction() = %v, want %v", got, tt.want)
   205  			}
   206  		})
   207  	}
   208  }
   209  
   210  func TestMountTabDiffEntry_GetOldFs(t *testing.T) {
   211  	type fields struct {
   212  		oldFs *Filesystem
   213  	}
   214  
   215  	tests := []struct {
   216  		name   string
   217  		fields fields
   218  		want   *Filesystem
   219  	}{
   220  		{
   221  			name:   "oldFs nil",
   222  			fields: fields{nil},
   223  			want:   nil,
   224  		},
   225  		{
   226  			name:   "sanity check",
   227  			fields: fields{&Filesystem{id: 1}},
   228  			want:   &Filesystem{id: 1},
   229  		},
   230  	}
   231  	for _, tt := range tests {
   232  		t.Run(tt.name, func(t *testing.T) {
   233  			mde := &MountTabDiffEntry{
   234  				oldFs: tt.fields.oldFs,
   235  			}
   236  			if got := mde.GetOldFs(); !reflect.DeepEqual(got, tt.want) {
   237  				t.Errorf("MountTabDiffEntry.GetOldFs() = %v, want %v", got, tt.want)
   238  			}
   239  		})
   240  	}
   241  }
   242  
   243  func TestMountTabDiffEntry_GetNewFs(t *testing.T) {
   244  	type fields struct {
   245  		newFs *Filesystem
   246  	}
   247  	tests := []struct {
   248  		name   string
   249  		fields fields
   250  		want   *Filesystem
   251  	}{
   252  		{
   253  			name:   "newFs nil",
   254  			fields: fields{nil},
   255  			want:   nil,
   256  		},
   257  		{
   258  			name:   "sanity check",
   259  			fields: fields{&Filesystem{id: 1}},
   260  			want:   &Filesystem{id: 1},
   261  		},
   262  	}
   263  	for _, tt := range tests {
   264  		t.Run(tt.name, func(t *testing.T) {
   265  			mde := &MountTabDiffEntry{
   266  				newFs: tt.fields.newFs,
   267  			}
   268  			if got := mde.GetNewFs(); !reflect.DeepEqual(got, tt.want) {
   269  				t.Errorf("MountTabDiffEntry.GetNewFs() = %v, want %v", got, tt.want)
   270  			}
   271  		})
   272  	}
   273  }
   274  
   275  func TestMountTabDiff_ListSources(t *testing.T) {
   276  	fs1 := NewFilesystem()
   277  	fs2 := NewFilesystem()
   278  	fs3 := NewFilesystem()
   279  
   280  	fs1.SetSource("src1")
   281  	fs2.SetSource("src2")
   282  	fs3.SetSource("src3")
   283  
   284  	tests := []struct {
   285  		name string
   286  		md   MountTabDiff
   287  		want []string
   288  	}{
   289  		{
   290  			name: "list sources",
   291  			md: MountTabDiff{
   292  				{
   293  					action: MountActionMount,
   294  					newFs:  fs1,
   295  				},
   296  				{
   297  					action: MountActionUmount,
   298  					oldFs:  fs1,
   299  				},
   300  				{
   301  					action: MountActionMove,
   302  					newFs:  fs2,
   303  					oldFs:  fs2,
   304  				},
   305  				{
   306  					action: MountActionRemount,
   307  					newFs:  fs3,
   308  					oldFs:  fs3,
   309  				},
   310  			},
   311  			want: []string{"src1", "src2", "src3"},
   312  		},
   313  	}
   314  	for _, tt := range tests {
   315  		t.Run(tt.name, func(t *testing.T) {
   316  			if got := tt.md.ListSources(); !reflect.DeepEqual(got, tt.want) {
   317  				t.Errorf("MountTabDiff.ListSources() = %v, want %v", got, tt.want)
   318  			}
   319  		})
   320  	}
   321  }