...

Source file src/edge-infra.dev/pkg/sds/devices/wait/wait_test.go

Documentation: edge-infra.dev/pkg/sds/devices/wait

     1  package wait
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  	"gotest.tools/v3/fs"
    14  )
    15  
    16  func TestMain(m *testing.M) {
    17  	os.Exit(m.Run())
    18  }
    19  
    20  func TestCheckDevices(t *testing.T) {
    21  	testCases := map[string]struct {
    22  		expectedCount  int
    23  		actualCount    int
    24  		expectedResult bool
    25  	}{
    26  		"BelowExpected": {
    27  			expectedCount:  3,
    28  			actualCount:    2,
    29  			expectedResult: false,
    30  		},
    31  		"EqualExpected": {
    32  			expectedCount:  3,
    33  			actualCount:    3,
    34  			expectedResult: true,
    35  		},
    36  		"AboveExpected": {
    37  			expectedCount:  3,
    38  			actualCount:    4,
    39  			expectedResult: true,
    40  		},
    41  	}
    42  
    43  	for name, tc := range testCases {
    44  		t.Run(name, func(t *testing.T) {
    45  			dir := fs.NewDir(t, name)
    46  			defer dir.Remove()
    47  			dirPath := dir.Path()
    48  
    49  			w := waiter{
    50  				deviceDir: dirPath,
    51  			}
    52  
    53  			for i := range tc.actualCount {
    54  				filename := strings.Join([]string{"ds-", strconv.Itoa(i), ".sock"}, "")
    55  				fp := filepath.Join(dirPath, filename)
    56  				require.NoError(t, os.WriteFile(fp, []byte{}, 0644))
    57  			}
    58  
    59  			res, err := w.checkDevices(context.Background(), tc.expectedCount)
    60  			require.NoError(t, err)
    61  			assert.Equal(t, tc.expectedResult, res)
    62  		})
    63  	}
    64  }
    65  

View as plain text