Skip to content
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

Merged
merged 5 commits into from
Feb 22, 2024

Conversation

rboyer
Copy link
Member

@rboyer rboyer commented Feb 21, 2024

Description

This adds a bunch of coverage of the topology.Compile method. It is not complete, but it is a start.

  • A few panics and miscellany were fixed.
  • The testing/deployer tests are now also run in CI.

}

func compile(logger hclog.Logger, raw *Config, prev *Topology) (*Topology, error) {
func compile(logger hclog.Logger, raw *Config, prev *Topology, testingID string) (*Topology, error) {
Copy link
Member Author

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) {
Copy link
Member Author

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{}{}
Copy link
Member Author

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" {
Copy link
Member Author

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 {
Copy link
Member Author

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.
Copy link
Member Author

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 {
Copy link
Member Author

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 {
Copy link
Member Author

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")
Copy link
Member Author

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) {
Copy link
Member Author

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{
Copy link
Member Author

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.

@rboyer rboyer added pr/no-changelog PR does not need a corresponding .changelog entry pr/no-metrics-test pr/no-backport labels Feb 21, 2024
@rboyer rboyer requested review from huikang and NiniOak February 21, 2024 20:42
@rboyer rboyer self-assigned this Feb 21, 2024
@rboyer rboyer added the theme/testing Testing, and related enhancements label Feb 21, 2024
@rboyer rboyer marked this pull request as ready for review February 21, 2024 20:42
"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) {
Copy link
Contributor

@NiniOak NiniOak Feb 21, 2024

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?

Copy link
Member Author

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, "")
Copy link
Contributor

@NiniOak NiniOak Feb 21, 2024

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?

Copy link
Member Author

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.

Copy link
Contributor

@NiniOak NiniOak left a 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

@rboyer rboyer merged commit 235747d into main Feb 22, 2024
91 checks passed
@rboyer rboyer deleted the rboyer/deployer-unit-tests branch February 22, 2024 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/no-backport pr/no-changelog PR does not need a corresponding .changelog entry pr/no-metrics-test theme/testing Testing, and related enhancements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants