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

     1  /*
     2  Copyright 2018 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 udevevent
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestNewMonitor(t *testing.T) {
    25  	monitor, err := newMonitor()
    26  	if err != nil {
    27  		t.Error(err)
    28  	}
    29  	defer monitor.free()
    30  	if monitor.udev == nil {
    31  		t.Errorf("udev should not be nil")
    32  	}
    33  	if monitor.udevMonitor == nil {
    34  		t.Errorf("udevMonitor should not be nil")
    35  	}
    36  }
    37  
    38  func TestSetup(t *testing.T) {
    39  	monitor, err := newMonitor()
    40  	if err != nil {
    41  		t.Error(err)
    42  	}
    43  	defer monitor.free()
    44  	fd, err := monitor.setup()
    45  	if err != nil {
    46  		t.Error(err)
    47  	}
    48  	if fd < 3 {
    49  		t.Errorf("fd value should be greater than 2")
    50  	}
    51  }
    52  
    53  func TestSubscribe(t *testing.T) {
    54  	type args struct {
    55  		eventTypes []UdevEventType
    56  	}
    57  	tests := []struct {
    58  		name string
    59  		args args
    60  		want *Subscription
    61  	}{
    62  		{
    63  			name: "no event types passed",
    64  			want: &Subscription{
    65  				subscribedTypes: []UdevEventType{EventTypeAdd,
    66  					EventTypeRemove, EventTypeChange},
    67  			},
    68  		},
    69  		{
    70  			name: "event type passed",
    71  			args: args{
    72  				eventTypes: []UdevEventType{EventTypeChange},
    73  			},
    74  			want: &Subscription{
    75  				subscribedTypes: []UdevEventType{EventTypeChange},
    76  			},
    77  		},
    78  	}
    79  	for _, tt := range tests {
    80  		t.Run(tt.name, func(t *testing.T) {
    81  			got := Subscribe(tt.args.eventTypes...)
    82  			if !reflect.DeepEqual(got.subscribedTypes,
    83  				tt.want.subscribedTypes) {
    84  				t.Errorf("Subscribe() = %v, want %v", got, tt.want)
    85  			}
    86  			if len(subscriptions) != 1 {
    87  				t.Error("subscriptions array not updated")
    88  			}
    89  			subscriptions = nil
    90  		})
    91  	}
    92  }
    93  
    94  func TestUnsubscribe(t *testing.T) {
    95  	type args struct {
    96  		sub *Subscription
    97  	}
    98  
    99  	dummySubscription := Subscription{
   100  		targetChannel:   make(chan UdevEvent),
   101  		subscribedTypes: []UdevEventType{EventTypeAdd},
   102  	}
   103  
   104  	tests := []struct {
   105  		name          string
   106  		args          args
   107  		subscriptions []*Subscription
   108  		wantErr       bool
   109  	}{
   110  		{
   111  			name:          "valid subscription",
   112  			subscriptions: []*Subscription{&dummySubscription},
   113  			args:          args{&dummySubscription},
   114  			wantErr:       false,
   115  		},
   116  		{
   117  			name:    "invalid subscription, argument nil",
   118  			wantErr: true,
   119  		},
   120  		{
   121  			name: "invalid subscription, target channel nil",
   122  			args: args{&Subscription{
   123  				subscribedTypes: []UdevEventType{EventTypeAdd},
   124  			}},
   125  			wantErr: true,
   126  		},
   127  		{
   128  			name: "invalid subscription, subscribed types nil",
   129  			args: args{&Subscription{
   130  				targetChannel: make(chan UdevEvent),
   131  			}},
   132  			wantErr: true,
   133  		},
   134  		{
   135  			name:    "invalid subscription, all fields nil",
   136  			args:    args{&Subscription{}},
   137  			wantErr: true,
   138  		},
   139  	}
   140  	for _, tt := range tests {
   141  		t.Run(tt.name, func(t *testing.T) {
   142  			subscriptions = tt.subscriptions
   143  			if err := Unsubscribe(tt.args.sub); (err != nil) != tt.wantErr {
   144  				t.Errorf("Unsubscribe() error = %v, wantErr %v", err, tt.wantErr)
   145  			}
   146  			subscriptions = nil
   147  		})
   148  	}
   149  }