-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hardware normalization decorator (#2389)
When reading hardware data for Tinkerbell we need to apply normalizations so we can perform comparison operations. MAC Addresses are a culprit for incorrect comparisons because comparators don't take into consideration case insensitivty of a MAC. We're lower-casing MAC as it seems to be lower-case more often than not.
- Loading branch information
1 parent
d450708
commit dc30f16
Showing
6 changed files
with
147 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package hardware | ||
|
||
import "strings" | ||
|
||
// NormalizerFunc applies a normalization transformation to the Machine. | ||
type NormalizerFunc func(Machine) Machine | ||
|
||
// Normalizer is a decorator for a MachineReader that applies a set of normalization funcs | ||
// to machines. | ||
type Normalizer struct { | ||
reader MachineReader | ||
normalizers []NormalizerFunc | ||
} | ||
|
||
// NewNormalizer creates a Normalizer instance that decorates r's Read(). A set of default | ||
// normalization functions are pre-registered. | ||
func NewNormalizer(r MachineReader) *Normalizer { | ||
normalizer := NewRawNormalizer(r) | ||
RegisterDefaultNormalizations(normalizer) | ||
return normalizer | ||
} | ||
|
||
// NewRawNormalizer returns a Normalizer with default normalizations registered by | ||
// RegisterDefaultNormalizations. | ||
func NewRawNormalizer(r MachineReader) *Normalizer { | ||
return &Normalizer{reader: r} | ||
} | ||
|
||
// Read reads an Machine from the decorated MachineReader, applies all normalization funcs and | ||
// returns the machine. If the decorated MachineReader errors, it is returned. | ||
func (n Normalizer) Read() (Machine, error) { | ||
machine, err := n.reader.Read() | ||
if err != nil { | ||
return Machine{}, err | ||
} | ||
|
||
for _, fn := range n.normalizers { | ||
machine = fn(machine) | ||
} | ||
|
||
return machine, nil | ||
} | ||
|
||
// Register fn to n such that fn is run over each machine read from the wrapped MachineReader. | ||
func (n *Normalizer) Register(fn NormalizerFunc) { | ||
n.normalizers = append(n.normalizers, fn) | ||
} | ||
|
||
// LowercaseMACAddress ensures m's MACAddress field has lower chase characters. | ||
func LowercaseMACAddress(m Machine) Machine { | ||
m.MACAddress = strings.ToLower(m.MACAddress) | ||
return m | ||
} | ||
|
||
// RegisterDefaultNormalizations registers a set of default normalizations on n. | ||
func RegisterDefaultNormalizations(n *Normalizer) { | ||
for _, fn := range []NormalizerFunc{ | ||
LowercaseMACAddress, | ||
} { | ||
n.Register(fn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package hardware_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/onsi/gomega" | ||
|
||
"github.com/aws/eks-anywhere/pkg/providers/tinkerbell/hardware" | ||
"github.com/aws/eks-anywhere/pkg/providers/tinkerbell/hardware/mocks" | ||
) | ||
|
||
func TestNormalizer(t *testing.T) { | ||
g := gomega.NewWithT(t) | ||
ctrl := gomock.NewController(t) | ||
reader := mocks.NewMockMachineReader(ctrl) | ||
|
||
normalizer := hardware.NewNormalizer(reader) | ||
|
||
expect := NewValidMachine() | ||
expect.MACAddress = "AA:BB:CC:DD:EE:FF" | ||
reader.EXPECT().Read().Return(expect, (error)(nil)) | ||
|
||
machine, err := normalizer.Read() | ||
|
||
g.Expect(err).ToNot(gomega.HaveOccurred()) | ||
|
||
// Re-use the expect machine instance and lower-case the MAC. | ||
expect.MACAddress = "aa:bb:cc:dd:ee:ff" | ||
|
||
g.Expect(machine).To(gomega.Equal(expect)) | ||
} | ||
|
||
func TestRawNormalizer(t *testing.T) { | ||
g := gomega.NewWithT(t) | ||
ctrl := gomock.NewController(t) | ||
reader := mocks.NewMockMachineReader(ctrl) | ||
|
||
normalizer := hardware.NewNormalizer(reader) | ||
|
||
expect := NewValidMachine() | ||
expect.MACAddress = "AA:BB:CC:DD:EE:FF" | ||
reader.EXPECT().Read().Return(expect, (error)(nil)) | ||
|
||
machine, err := normalizer.Read() | ||
|
||
g.Expect(err).ToNot(gomega.HaveOccurred()) | ||
g.Expect(machine).To(gomega.Equal(machine)) | ||
} | ||
|
||
func TestRawNormalizerReadError(t *testing.T) { | ||
g := gomega.NewWithT(t) | ||
ctrl := gomock.NewController(t) | ||
reader := mocks.NewMockMachineReader(ctrl) | ||
|
||
normalizer := hardware.NewNormalizer(reader) | ||
|
||
expect := errors.New("foo bar") | ||
reader.EXPECT().Read().Return(hardware.Machine{}, expect) | ||
|
||
_, err := normalizer.Read() | ||
|
||
g.Expect(err).To(gomega.HaveOccurred()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters