-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deployer: add a bunch of test coverage and fix a few panics #20694
Conversation
} | ||
|
||
func compile(logger hclog.Logger, raw *Config, prev *Topology) (*Topology, error) { | ||
func compile(logger hclog.Logger, raw *Config, prev *Topology, testingID string) (*Topology, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this so that we can control the id assignment during testing.
@@ -90,22 +100,17 @@ func compile(logger hclog.Logger, raw *Config, prev *Topology) (*Topology, error | |||
nextIndex int // use a global index so any shared networks work properly with assignments | |||
) | |||
|
|||
foundPeerNames := make(map[string]map[string]struct{}) | |||
for _, c := range raw.Clusters { | |||
compileCluster := func(c *Cluster) (map[string]struct{}, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A follow-on PR can extract these anonymous methods as real methods. I did it this way to make it more obvious where the bugfixes were, and to add error logging context.
@@ -158,6 +162,7 @@ func compile(logger hclog.Logger, raw *Config, prev *Topology) (*Topology, error | |||
m = make(map[string]struct{}) | |||
tenancies[partition] = m | |||
} | |||
m["default"] = struct{}{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the ways to introduce a tenancy would inject the default, but this one did not. Correcting this omission. The system will automatically create this anyway so this just accurately reflects the tenancies that will exist.
@@ -238,10 +242,9 @@ func compile(logger hclog.Logger, raw *Config, prev *Topology) (*Topology, error | |||
} | |||
addTenancy(n.Partition, "default") | |||
|
|||
if _, exists := seenNodes[n.ID()]; exists { | |||
return nil, fmt.Errorf("cannot have two nodes in the same cluster %q with the same name %q", c.Name, n.ID()) | |||
if n.IsServer() && n.Partition != "default" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this restriction. Practically this would have failed in consul itself, but this makes it fail up higher where it's more actionable.
@@ -476,6 +482,15 @@ func compile(logger hclog.Logger, raw *Config, prev *Topology) (*Topology, error | |||
Protocol: cfg.ActualProtocol, | |||
}) | |||
} | |||
sort.Slice(svcPorts, func(i, j int) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes the output deterministic to aid in unit test comparisons.
maps.Copy(foundPeerNames, peerNames) | ||
} | ||
|
||
// Default anything in the toplevel services map. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Omission: many of the pathways to implicitly create this v2 services map will default the protocol, but the BYO map does not.
@@ -898,6 +902,9 @@ func (w *Workload) ports() []int { | |||
if len(w.Ports) > 0 { | |||
seen := make(map[int]struct{}) | |||
for _, port := range w.Ports { | |||
if port == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Panic if no ports were defined.
Number: w.Port, | ||
Protocol: "tcp", | ||
}, | ||
} | ||
w.Port = 0 | ||
} | ||
if w.Ports == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another panic.
@@ -990,7 +1002,7 @@ func (w *Workload) Validate() error { | |||
} | |||
} else { | |||
if len(w.Ports) > 0 { | |||
return fmt.Errorf("cannot specify mulitport on service in v1") | |||
return fmt.Errorf("cannot specify multiport on service in v1") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo
Workload{}, | ||
} | ||
|
||
func assertDeepEqual[V any](t testingT, exp, got V, msgAndArgs ...any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an inlined, modified version of prototest.AssertDeepEqual
for use in this module.
} | ||
} | ||
|
||
cases := map[string]testcase{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cases covered here test some happy paths, and most of the unhappy paths.
"github.com/hashicorp/consul/testing/deployer/util" | ||
) | ||
|
||
const DockerPrefix = "cslc" // ConSuLCluster | ||
|
||
func Compile(logger hclog.Logger, raw *Config) (*Topology, error) { | ||
return compile(logger, raw, nil) | ||
return compile(logger, raw, nil, "") | ||
} | ||
|
||
func Recompile(logger hclog.Logger, raw *Config, prev *Topology) (*Topology, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is testingID
not added to the parameters required for Recompile
function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not want to expose the testingID
parameter in an exported function yet, as it is just there for the package tests to use.
Possibly if deployer-meta-tests get written for stuff outside of the topology
package we might revisit this. It seemed easiest to keep this testing detail internal to this package to start.
"github.com/hashicorp/consul/testing/deployer/util" | ||
) | ||
|
||
const DockerPrefix = "cslc" // ConSuLCluster | ||
|
||
func Compile(logger hclog.Logger, raw *Config) (*Topology, error) { | ||
return compile(logger, raw, nil) | ||
return compile(logger, raw, nil, "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the testingID
value be set at a high level? Like wherever compile is called?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests in compile_test.go
call the function this way:
got, err := compile(logger, tc.in, nil, clusterID)
I'm not calling Compile
, but rather the unexported compile
and using the testingID
field which is otherwise inaccessible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few questions but non blockers, thanks for these updates
Description
This adds a bunch of coverage of the
topology.Compile
method. It is not complete, but it is a start.testing/deployer
tests are now also run in CI.