github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/cmd/ndm_daemonset/probe/probe_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 probe
    18  
    19  import (
    20  	"github.com/openebs/node-disk-manager/blockdevice"
    21  	"sync"
    22  	"testing"
    23  
    24  	"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  type fakeProbe struct {
    29  	ctrl *controller.Controller
    30  }
    31  
    32  func (p *fakeProbe) Start() {}
    33  
    34  func (p *fakeProbe) FillBlockDeviceDetails(fakeDiskInfo *blockdevice.BlockDevice) {
    35  	fakeDiskInfo.DeviceAttributes.Model = fakeModel
    36  	fakeDiskInfo.DeviceAttributes.Serial = fakeSerial
    37  	fakeDiskInfo.DeviceAttributes.Vendor = fakeVendor
    38  }
    39  
    40  func TestRegisterProbe(t *testing.T) {
    41  	expectedProbeList := make([]*controller.Probe, 0)
    42  	fakeController := &controller.Controller{
    43  		Probes: make([]*controller.Probe, 0),
    44  		Mutex:  &sync.Mutex{},
    45  	}
    46  
    47  	var i controller.ProbeInterface = &fakeProbe{}
    48  	newRegisterProbe := &registerProbe{
    49  		name:       "probe-1",
    50  		state:      true,
    51  		pi:         i,
    52  		controller: fakeController,
    53  	}
    54  	newRegisterProbe.register()
    55  	probe := &controller.Probe{
    56  		Name:      newRegisterProbe.name,
    57  		State:     newRegisterProbe.state,
    58  		Interface: newRegisterProbe.pi,
    59  	}
    60  	expectedProbeList = append(expectedProbeList, probe)
    61  	tests := map[string]struct {
    62  		actualProbeList   []*controller.Probe
    63  		expectedProbeList []*controller.Probe
    64  	}{
    65  		"add one probe and check if it is present or not": {actualProbeList: fakeController.Probes, expectedProbeList: expectedProbeList},
    66  	}
    67  	for name, test := range tests {
    68  		t.Run(name, func(t *testing.T) {
    69  			assert.Equal(t, test.expectedProbeList, test.actualProbeList)
    70  		})
    71  	}
    72  }
    73  
    74  func TestStart(t *testing.T) {
    75  	expectedProbeList := make([]*controller.Probe, 0)
    76  	fakeController := &controller.Controller{
    77  		Probes: make([]*controller.Probe, 0),
    78  		Mutex:  &sync.Mutex{},
    79  	}
    80  	go func() {
    81  		controller.ControllerBroadcastChannel <- fakeController
    82  	}()
    83  	var fakeProbeRegister = func() {
    84  		ctrl := <-controller.ControllerBroadcastChannel
    85  		if ctrl == nil {
    86  			t.Fatal("controller struct should not be nil")
    87  		}
    88  		var pi controller.ProbeInterface = &fakeProbe{ctrl: ctrl}
    89  		newRegisterProbe := &registerProbe{
    90  			name:       "fake-probe",
    91  			state:      defaultEnabled,
    92  			pi:         pi,
    93  			controller: ctrl,
    94  		}
    95  		newRegisterProbe.register()
    96  	}
    97  	var registeredProbes = []func(){fakeProbeRegister}
    98  	Start(registeredProbes)
    99  	var fi controller.ProbeInterface = &fakeProbe{ctrl: fakeController}
   100  	probe := &controller.Probe{
   101  		Name:      "fake-probe",
   102  		State:     defaultEnabled,
   103  		Interface: fi,
   104  	}
   105  	expectedProbeList = append(expectedProbeList, probe)
   106  	tests := map[string]struct {
   107  		actualProbeList   []*controller.Probe
   108  		expectedProbeList []*controller.Probe
   109  	}{
   110  		"register one probe and check if it is present or not": {actualProbeList: fakeController.Probes, expectedProbeList: expectedProbeList},
   111  	}
   112  	for name, test := range tests {
   113  		t.Run(name, func(t *testing.T) {
   114  			assert.Equal(t, test.expectedProbeList, test.actualProbeList)
   115  		})
   116  	}
   117  }