2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-02-27 19:57:10 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2023-02-07 22:42:25 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2018-02-27 19:57:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDriverInfoEquals(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2018-02-27 19:57:10 +00:00
|
|
|
require := require.New(t)
|
|
|
|
var driverInfoTest = []struct {
|
|
|
|
input []*DriverInfo
|
|
|
|
expected bool
|
|
|
|
errorMsg string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]*DriverInfo{
|
2018-02-28 17:40:54 +00:00
|
|
|
{
|
2018-02-27 19:57:10 +00:00
|
|
|
Healthy: true,
|
|
|
|
},
|
2018-02-28 17:40:54 +00:00
|
|
|
{
|
2018-02-27 19:57:10 +00:00
|
|
|
Healthy: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"Different healthy values should not be equal.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*DriverInfo{
|
2018-02-28 17:40:54 +00:00
|
|
|
{
|
2018-02-27 19:57:10 +00:00
|
|
|
HealthDescription: "not running",
|
|
|
|
},
|
2018-02-28 17:40:54 +00:00
|
|
|
{
|
2018-02-27 19:57:10 +00:00
|
|
|
HealthDescription: "running",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"Different health description values should not be equal.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*DriverInfo{
|
2018-02-28 17:40:54 +00:00
|
|
|
{
|
2018-03-20 17:25:07 +00:00
|
|
|
Detected: false,
|
2018-02-27 19:57:10 +00:00
|
|
|
Healthy: true,
|
2018-03-20 17:25:07 +00:00
|
|
|
HealthDescription: "This driver is ok",
|
2018-02-27 19:57:10 +00:00
|
|
|
},
|
2018-02-28 17:40:54 +00:00
|
|
|
{
|
2018-03-20 17:25:07 +00:00
|
|
|
Detected: true,
|
2018-02-27 19:57:10 +00:00
|
|
|
Healthy: true,
|
2018-03-20 17:25:07 +00:00
|
|
|
HealthDescription: "This driver is ok",
|
2018-02-27 19:57:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
2018-03-20 17:25:07 +00:00
|
|
|
"Same health check should be equal",
|
2018-02-27 19:57:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range driverInfoTest {
|
|
|
|
first := testCase.input[0]
|
|
|
|
second := testCase.input[1]
|
|
|
|
require.Equal(testCase.expected, first.HealthCheckEquals(second), testCase.errorMsg)
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 22:42:25 +00:00
|
|
|
|
|
|
|
func TestNodeMeta_Validate(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
input map[string]*string // only specify Meta field
|
|
|
|
contains string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Ok",
|
|
|
|
input: map[string]*string{
|
|
|
|
"foo": nil,
|
|
|
|
"bar": nil,
|
|
|
|
"eggs": nil,
|
|
|
|
"dots.are_ok-too": nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Nil",
|
|
|
|
input: nil,
|
|
|
|
contains: "missing required",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty",
|
|
|
|
input: map[string]*string{},
|
|
|
|
contains: "missing required",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "EmptyKey",
|
|
|
|
input: map[string]*string{"": nil},
|
|
|
|
contains: "not be empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Whitespace",
|
|
|
|
input: map[string]*string{
|
|
|
|
"ok": nil,
|
|
|
|
" bad": nil,
|
|
|
|
},
|
|
|
|
contains: `" bad" is invalid`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "BadChars",
|
|
|
|
input: map[string]*string{
|
|
|
|
"ok": nil,
|
|
|
|
"*bad%": nil,
|
|
|
|
},
|
|
|
|
contains: `"*bad%" is invalid`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "StartingDot",
|
|
|
|
input: map[string]*string{
|
|
|
|
"ok": nil,
|
|
|
|
".bad": nil,
|
|
|
|
},
|
|
|
|
contains: `".bad" is invalid`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "EndingDot",
|
|
|
|
input: map[string]*string{
|
|
|
|
"ok": nil,
|
|
|
|
"bad.": nil,
|
|
|
|
},
|
|
|
|
contains: `"bad." is invalid`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DottedPartsMustBeValid",
|
|
|
|
input: map[string]*string{
|
|
|
|
"ok": nil,
|
|
|
|
"bad.-part": nil,
|
|
|
|
},
|
|
|
|
contains: `"bad.-part" is invalid`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range cases {
|
|
|
|
tc := cases[i]
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
in := &NodeMetaApplyRequest{
|
|
|
|
Meta: tc.input,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := in.Validate()
|
|
|
|
|
|
|
|
switch tc.contains {
|
|
|
|
case "":
|
|
|
|
must.NoError(t, err)
|
|
|
|
default:
|
|
|
|
must.ErrorContains(t, err, tc.contains)
|
|
|
|
|
|
|
|
// Log error to make it easy to double check output.
|
|
|
|
t.Logf("Validate(%s) -> %s", tc.name, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|