Skip to content

Commit

Permalink
Add DeepCopy method for E2EConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer buringerst@vmware.com
  • Loading branch information
sbueringer committed Jan 11, 2024
1 parent 11620ba commit f6ab43c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/framework/clusterctl/e2e_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,31 @@ type Files struct {
TargetName string `json:"targetName,omitempty"`
}

func (c *E2EConfig) DeepCopy() *E2EConfig {
if c == nil {
return nil
}
out := new(E2EConfig)
out.ManagementClusterName = c.ManagementClusterName
out.Images = make([]ContainerImage, len(c.Images))
for i, image := range c.Images {

Check failure on line 258 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 258 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 258 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 258 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 258 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 258 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)
out.Images[i] = image
}
out.Providers = make([]ProviderConfig, len(c.Providers))
for i, image := range c.Providers {

Check failure on line 262 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 262 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 262 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 262 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 262 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)

Check failure on line 262 in test/framework/clusterctl/e2e_config.go

View workflow job for this annotation

GitHub Actions / lint (test)

S1001: should use copy(to, from) instead of a loop (gosimple)
out.Providers[i] = image
}
out.Variables = make(map[string]string, len(c.Variables))
for key, val := range c.Variables {
out.Variables[key] = val
}
out.Intervals = make(map[string][]string, len(c.Intervals))
for key, val := range c.Intervals {
out.Intervals[key] = val
}
return out
}

// ResolveReleases converts release markers to release version.
func (c *E2EConfig) ResolveReleases(ctx context.Context) error {
for i := range c.Providers {
Expand Down
71 changes: 71 additions & 0 deletions test/framework/clusterctl/e2e_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,74 @@ func Test_resolveReleaseMarker(t *testing.T) {
})
}
}

func Test_E2EConfig_DeepCopy(t *testing.T) {
g := NewWithT(t)

config1 := E2EConfig{
ManagementClusterName: "config1-cluster-name",
Images: []ContainerImage{
{
Name: "config1-image",
},
},
Providers: []ProviderConfig{
{
Name: "config1-provider",
},
},
Variables: map[string]string{
"config1-variable": "config1-variable-value",
},
Intervals: map[string][]string{
"config1-interval": {"2m", "10s"},
},
}

config2 := config1.DeepCopy()

// Verify everything was copied from config1
g.Expect(config2.ManagementClusterName).To(Equal("config1-cluster-name"))
g.Expect(config2.Images).To(Equal([]ContainerImage{
{
Name: "config1-image",
},
}))
g.Expect(config2.Providers).To(Equal([]ProviderConfig{
{
Name: "config1-provider",
},
}))
g.Expect(config2.Variables).To(Equal(map[string]string{
"config1-variable": "config1-variable-value",
}))
g.Expect(config2.Intervals).To(Equal(map[string][]string{
"config1-interval": {"2m", "10s"},
}))

// Mutate config2
config2.ManagementClusterName = "config2-cluster-name"
config2.Images[0].Name = "config2-image"
config2.Providers[0].Name = "config2-provider"
config2.Variables["config2-variable"] = "config2-variable-value"
config2.Intervals["config2-interval"] = []string{"2m", "10s"}

// Validate config1 was not mutated
g.Expect(config1.ManagementClusterName).To(Equal("config1-cluster-name"))
g.Expect(config1.Images).To(Equal([]ContainerImage{
{
Name: "config1-image",
},
}))
g.Expect(config1.Providers).To(Equal([]ProviderConfig{
{
Name: "config1-provider",
},
}))
g.Expect(config1.Variables).To(Equal(map[string]string{
"config1-variable": "config1-variable-value",
}))
g.Expect(config1.Intervals).To(Equal(map[string][]string{
"config1-interval": {"2m", "10s"},
}))
}

0 comments on commit f6ab43c

Please sign in to comment.