open-nomad/plugins/shared/structs/attribute_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

697 lines
11 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2018-10-12 06:18:26 +00:00
package structs
import (
2018-10-12 22:25:34 +00:00
"fmt"
2018-10-12 06:18:26 +00:00
"testing"
"github.com/hashicorp/nomad/helper/pointer"
2018-10-12 06:18:26 +00:00
"github.com/stretchr/testify/require"
)
2018-10-12 22:25:34 +00:00
func TestAttribute_Validate(t *testing.T) {
cases := []struct {
Input *Attribute
Fail bool
}{
{
Input: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
},
{
Input: &Attribute{
String: pointer.Of("foo"),
2018-10-12 22:25:34 +00:00
},
},
{
Input: &Attribute{
Int: pointer.Of(int64(123)),
2018-10-12 22:25:34 +00:00
},
},
{
Input: &Attribute{
Float: pointer.Of(float64(123.2)),
2018-10-12 22:25:34 +00:00
},
},
{
Input: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Fail: true,
},
{
Input: &Attribute{
String: pointer.Of("foo"),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Fail: true,
},
{
Input: &Attribute{
Int: pointer.Of(int64(123)),
2018-10-12 22:25:34 +00:00
Unit: "lolNO",
},
Fail: true,
},
{
Input: &Attribute{
Float: pointer.Of(float64(123.2)),
2018-10-12 22:25:34 +00:00
Unit: "lolNO",
},
Fail: true,
},
{
Input: &Attribute{
Int: pointer.Of(int64(123)),
Float: pointer.Of(float64(123.2)),
2018-10-12 22:25:34 +00:00
Unit: "mW",
},
Fail: true,
},
}
for _, c := range cases {
t.Run(c.Input.GoString(), func(t *testing.T) {
if err := c.Input.Validate(); err != nil && !c.Fail {
require.NoError(t, err)
}
})
}
}
type compareTestCase struct {
A *Attribute
B *Attribute
Expected int
NotComparable bool
}
func TestAttribute_Compare_Bool(t *testing.T) {
cases := []*compareTestCase{
{
A: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
Expected: 0,
},
{
A: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Bool: pointer.Of(false),
2018-10-12 22:25:34 +00:00
},
Expected: 1,
},
{
A: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
String: pointer.Of("foo"),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
{
A: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Int: pointer.Of(int64(123)),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
{
A: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(123.2)),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
}
testComparison(t, cases)
}
func TestAttribute_Compare_String(t *testing.T) {
cases := []*compareTestCase{
{
A: &Attribute{
String: pointer.Of("a"),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
String: pointer.Of("b"),
2018-10-12 22:25:34 +00:00
},
Expected: -1,
},
{
A: &Attribute{
String: pointer.Of("hello"),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
String: pointer.Of("hello"),
2018-10-12 22:25:34 +00:00
},
Expected: 0,
},
{
A: &Attribute{
String: pointer.Of("b"),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
String: pointer.Of("a"),
2018-10-12 22:25:34 +00:00
},
Expected: 1,
},
{
A: &Attribute{
String: pointer.Of("hello"),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
{
A: &Attribute{
String: pointer.Of("hello"),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Int: pointer.Of(int64(123)),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
{
A: &Attribute{
String: pointer.Of("hello"),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(123.2)),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
}
testComparison(t, cases)
}
func TestAttribute_Compare_Float(t *testing.T) {
cases := []*compareTestCase{
{
A: &Attribute{
Float: pointer.Of(float64(101.5)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(100001.5)),
2018-10-12 22:25:34 +00:00
},
Expected: -1,
},
{
A: &Attribute{
Float: pointer.Of(float64(100001.5)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(100001.5)),
2018-10-12 22:25:34 +00:00
},
Expected: 0,
},
{
A: &Attribute{
Float: pointer.Of(float64(999999999.5)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(101.5)),
2018-10-12 22:25:34 +00:00
},
Expected: 1,
},
{
A: &Attribute{
Float: pointer.Of(float64(101.5)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
{
A: &Attribute{
Float: pointer.Of(float64(101.5)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
String: pointer.Of("hello"),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
}
testComparison(t, cases)
}
func TestAttribute_Compare_Int(t *testing.T) {
cases := []*compareTestCase{
{
A: &Attribute{
Int: pointer.Of(int64(3)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
},
Expected: -1,
},
{
A: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
},
Expected: 0,
},
{
A: &Attribute{
Int: pointer.Of(int64(100)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
},
Expected: 1,
},
{
A: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Bool: pointer.Of(true),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
{
A: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
String: pointer.Of("hello"),
2018-10-12 22:25:34 +00:00
},
NotComparable: true,
},
}
testComparison(t, cases)
}
func TestAttribute_Compare_Int_With_Units(t *testing.T) {
cases := []*compareTestCase{
{
A: &Attribute{
Int: pointer.Of(int64(3)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
B: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Expected: -1,
},
{
A: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
B: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Expected: 0,
},
{
A: &Attribute{
Int: pointer.Of(int64(100)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
B: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Expected: 1,
},
{
A: &Attribute{
Int: pointer.Of(int64(3)),
2018-10-12 22:25:34 +00:00
Unit: "GB",
},
B: &Attribute{
Int: pointer.Of(int64(3)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Expected: 1,
},
{
A: &Attribute{
Int: pointer.Of(int64(1)),
2018-10-12 22:25:34 +00:00
Unit: "GiB",
},
B: &Attribute{
Int: pointer.Of(int64(1024)),
2018-10-12 22:25:34 +00:00
Unit: "MiB",
},
Expected: 0,
},
{
A: &Attribute{
Int: pointer.Of(int64(1)),
2018-10-12 22:25:34 +00:00
Unit: "GiB",
},
B: &Attribute{
Int: pointer.Of(int64(1025)),
2018-10-12 22:25:34 +00:00
Unit: "MiB",
},
Expected: -1,
},
{
A: &Attribute{
Int: pointer.Of(int64(1000)),
2018-10-12 22:25:34 +00:00
Unit: "mW",
},
B: &Attribute{
Int: pointer.Of(int64(1)),
2018-10-12 22:25:34 +00:00
Unit: "W",
},
Expected: 0,
},
}
testComparison(t, cases)
}
func TestAttribute_Compare_Float_With_Units(t *testing.T) {
cases := []*compareTestCase{
{
A: &Attribute{
Float: pointer.Of(float64(3.0)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
B: &Attribute{
Float: pointer.Of(float64(10.0)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Expected: -1,
},
{
A: &Attribute{
Float: pointer.Of(float64(10.0)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
B: &Attribute{
Float: pointer.Of(float64(10.0)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Expected: 0,
},
{
A: &Attribute{
Float: pointer.Of(float64(100.0)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
B: &Attribute{
Float: pointer.Of(float64(10.0)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Expected: 1,
},
{
A: &Attribute{
Float: pointer.Of(float64(3.0)),
2018-10-12 22:25:34 +00:00
Unit: "GB",
},
B: &Attribute{
Float: pointer.Of(float64(3.0)),
2018-10-12 22:25:34 +00:00
Unit: "MB",
},
Expected: 1,
},
{
A: &Attribute{
Float: pointer.Of(float64(1.0)),
2018-10-12 22:25:34 +00:00
Unit: "GiB",
},
B: &Attribute{
Float: pointer.Of(float64(1024.0)),
2018-10-12 22:25:34 +00:00
Unit: "MiB",
},
Expected: 0,
},
{
A: &Attribute{
Float: pointer.Of(float64(1.0)),
2018-10-12 22:25:34 +00:00
Unit: "GiB",
},
B: &Attribute{
Float: pointer.Of(float64(1025.0)),
2018-10-12 22:25:34 +00:00
Unit: "MiB",
},
Expected: -1,
},
{
A: &Attribute{
Float: pointer.Of(float64(1000.0)),
2018-10-12 22:25:34 +00:00
Unit: "mW",
},
B: &Attribute{
Float: pointer.Of(float64(1.0)),
2018-10-12 22:25:34 +00:00
Unit: "W",
},
Expected: 0,
},
{
A: &Attribute{
Float: pointer.Of(float64(1.5)),
2018-10-12 22:25:34 +00:00
Unit: "GiB",
},
B: &Attribute{
Float: pointer.Of(float64(1400.0)),
2018-10-12 22:25:34 +00:00
Unit: "MiB",
},
Expected: 1,
},
}
testComparison(t, cases)
}
func TestAttribute_Compare_IntToFloat(t *testing.T) {
cases := []*compareTestCase{
{
A: &Attribute{
Int: pointer.Of(int64(3)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(10.0)),
2018-10-12 22:25:34 +00:00
},
Expected: -1,
},
{
A: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(10.0)),
2018-10-12 22:25:34 +00:00
},
Expected: 0,
},
{
A: &Attribute{
Int: pointer.Of(int64(10)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(10.1)),
2018-10-12 22:25:34 +00:00
},
Expected: -1,
},
{
A: &Attribute{
Int: pointer.Of(int64(100)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(10.0)),
2018-10-12 22:25:34 +00:00
},
Expected: 1,
},
{
A: &Attribute{
Int: pointer.Of(int64(100)),
2018-10-12 22:25:34 +00:00
},
B: &Attribute{
Float: pointer.Of(float64(100.00001)),
2018-10-12 22:25:34 +00:00
},
Expected: -1,
},
}
testComparison(t, cases)
}
func testComparison(t *testing.T, cases []*compareTestCase) {
for _, c := range cases {
t.Run(fmt.Sprintf("%#v vs %#v", c.A, c.B), func(t *testing.T) {
v, ok := c.A.Compare(c.B)
if !ok && !c.NotComparable {
t.Fatal("should be comparable")
} else if ok {
require.Equal(t, c.Expected, v)
}
})
}
}
2018-10-12 06:18:26 +00:00
func TestAttribute_ParseAndValidate(t *testing.T) {
cases := []struct {
Input string
Expected *Attribute
}{
{
Input: "true",
Expected: &Attribute{
Bool: pointer.Of(true),
2018-10-12 06:18:26 +00:00
},
},
{
Input: "false",
Expected: &Attribute{
Bool: pointer.Of(false),
2018-10-12 06:18:26 +00:00
},
},
2018-10-14 01:38:08 +00:00
{
Input: "1",
Expected: &Attribute{
Int: pointer.Of(int64(1)),
2018-10-14 01:38:08 +00:00
},
},
2018-10-12 06:18:26 +00:00
{
Input: "100",
Expected: &Attribute{
Int: pointer.Of(int64(100)),
2018-10-12 06:18:26 +00:00
},
},
{
Input: "-100",
Expected: &Attribute{
Int: pointer.Of(int64(-100)),
2018-10-12 06:18:26 +00:00
},
},
{
Input: "-1.0",
Expected: &Attribute{
Float: pointer.Of(float64(-1.0)),
2018-10-12 06:18:26 +00:00
},
},
{
Input: "-100.25",
Expected: &Attribute{
Float: pointer.Of(float64(-100.25)),
2018-10-12 06:18:26 +00:00
},
},
{
Input: "1.01",
Expected: &Attribute{
Float: pointer.Of(float64(1.01)),
2018-10-12 06:18:26 +00:00
},
},
{
Input: "100.25",
Expected: &Attribute{
Float: pointer.Of(float64(100.25)),
2018-10-12 06:18:26 +00:00
},
},
{
Input: "foobar",
Expected: &Attribute{
String: pointer.Of("foobar"),
2018-10-12 06:18:26 +00:00
},
},
{
Input: "foo123bar",
Expected: &Attribute{
String: pointer.Of("foo123bar"),
2018-10-12 06:18:26 +00:00
},
},
{
Input: "100MB",
Expected: &Attribute{
Int: pointer.Of(int64(100)),
2018-10-12 06:18:26 +00:00
Unit: "MB",
},
},
{
Input: "-100MHz",
Expected: &Attribute{
Int: pointer.Of(int64(-100)),
2018-10-12 06:18:26 +00:00
Unit: "MHz",
},
},
{
Input: "-1.0MB/s",
Expected: &Attribute{
Float: pointer.Of(float64(-1.0)),
2018-10-12 06:18:26 +00:00
Unit: "MB/s",
},
},
{
Input: "-100.25GiB/s",
Expected: &Attribute{
Float: pointer.Of(float64(-100.25)),
2018-10-12 06:18:26 +00:00
Unit: "GiB/s",
},
},
{
Input: "1.01TB",
Expected: &Attribute{
Float: pointer.Of(float64(1.01)),
2018-10-12 06:18:26 +00:00
Unit: "TB",
},
},
{
Input: "100.25mW",
Expected: &Attribute{
Float: pointer.Of(float64(100.25)),
2018-10-12 06:18:26 +00:00
Unit: "mW",
},
},
}
for _, c := range cases {
t.Run(c.Input, func(t *testing.T) {
a := ParseAttribute(c.Input)
require.Equal(t, c.Expected, a)
require.NoError(t, a.Validate())
})
}
}
2018-10-12 22:25:34 +00:00
func BenchmarkParse(b *testing.B) {
cases := []string{
"true",
"false",
"100",
"-100",
"-1.0",
"-100.25",
"1.01",
"100.25",
"foobar",
"foo123bar",
"100MB",
"-100MHz",
"-1.0MB/s",
"-100.25GiB/s",
"1.01TB",
"100.25mW",
}
for n := 0; n < b.N; n++ {
for _, c := range cases {
ParseAttribute(c)
}
}
}