github.com/hernad/nomad@v1.6.112/nomad/structs/node_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package structs
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/hernad/nomad/ci"
    10  	"github.com/shoenig/test/must"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestDriverInfoEquals(t *testing.T) {
    15  	ci.Parallel(t)
    16  
    17  	require := require.New(t)
    18  	var driverInfoTest = []struct {
    19  		input    []*DriverInfo
    20  		expected bool
    21  		errorMsg string
    22  	}{
    23  		{
    24  			[]*DriverInfo{
    25  				{
    26  					Healthy: true,
    27  				},
    28  				{
    29  					Healthy: false,
    30  				},
    31  			},
    32  			false,
    33  			"Different healthy values should not be equal.",
    34  		},
    35  		{
    36  			[]*DriverInfo{
    37  				{
    38  					HealthDescription: "not running",
    39  				},
    40  				{
    41  					HealthDescription: "running",
    42  				},
    43  			},
    44  			false,
    45  			"Different health description values should not be equal.",
    46  		},
    47  		{
    48  			[]*DriverInfo{
    49  				{
    50  					Detected:          false,
    51  					Healthy:           true,
    52  					HealthDescription: "This driver is ok",
    53  				},
    54  				{
    55  					Detected:          true,
    56  					Healthy:           true,
    57  					HealthDescription: "This driver is ok",
    58  				},
    59  			},
    60  			true,
    61  			"Same health check should be equal",
    62  		},
    63  	}
    64  	for _, testCase := range driverInfoTest {
    65  		first := testCase.input[0]
    66  		second := testCase.input[1]
    67  		require.Equal(testCase.expected, first.HealthCheckEquals(second), testCase.errorMsg)
    68  	}
    69  }
    70  
    71  func TestNodeMeta_Validate(t *testing.T) {
    72  	ci.Parallel(t)
    73  
    74  	cases := []struct {
    75  		name     string
    76  		input    map[string]*string // only specify Meta field
    77  		contains string
    78  	}{
    79  		{
    80  			name: "Ok",
    81  			input: map[string]*string{
    82  				"foo":             nil,
    83  				"bar":             nil,
    84  				"eggs":            nil,
    85  				"dots.are_ok-too": nil,
    86  			},
    87  		},
    88  		{
    89  			name:     "Nil",
    90  			input:    nil,
    91  			contains: "missing required",
    92  		},
    93  		{
    94  			name:     "Empty",
    95  			input:    map[string]*string{},
    96  			contains: "missing required",
    97  		},
    98  		{
    99  			name:     "EmptyKey",
   100  			input:    map[string]*string{"": nil},
   101  			contains: "not be empty",
   102  		},
   103  		{
   104  			name: "Whitespace",
   105  			input: map[string]*string{
   106  				"ok":   nil,
   107  				" bad": nil,
   108  			},
   109  			contains: `" bad" is invalid`,
   110  		},
   111  		{
   112  			name: "BadChars",
   113  			input: map[string]*string{
   114  				"ok":    nil,
   115  				"*bad%": nil,
   116  			},
   117  			contains: `"*bad%" is invalid`,
   118  		},
   119  		{
   120  			name: "StartingDot",
   121  			input: map[string]*string{
   122  				"ok":   nil,
   123  				".bad": nil,
   124  			},
   125  			contains: `".bad" is invalid`,
   126  		},
   127  		{
   128  			name: "EndingDot",
   129  			input: map[string]*string{
   130  				"ok":   nil,
   131  				"bad.": nil,
   132  			},
   133  			contains: `"bad." is invalid`,
   134  		},
   135  		{
   136  			name: "DottedPartsMustBeValid",
   137  			input: map[string]*string{
   138  				"ok":        nil,
   139  				"bad.-part": nil,
   140  			},
   141  			contains: `"bad.-part" is invalid`,
   142  		},
   143  	}
   144  
   145  	for i := range cases {
   146  		tc := cases[i]
   147  		t.Run(tc.name, func(t *testing.T) {
   148  			in := &NodeMetaApplyRequest{
   149  				Meta: tc.input,
   150  			}
   151  
   152  			err := in.Validate()
   153  
   154  			switch tc.contains {
   155  			case "":
   156  				must.NoError(t, err)
   157  			default:
   158  				must.ErrorContains(t, err, tc.contains)
   159  
   160  				// Log error to make it easy to double check output.
   161  				t.Logf("Validate(%s) -> %s", tc.name, err)
   162  			}
   163  		})
   164  	}
   165  }