Skip to content

Commit

Permalink
Merge pull request #9989 from sbueringer/pr-add-e2econfig-deepcopy-1.6
Browse files Browse the repository at this point in the history
[release-1.6] 🌱 Add DeepCopy method for E2EConfig
  • Loading branch information
k8s-ci-robot authored Jan 11, 2024
2 parents b4158b3 + 052bfd3 commit 166025a
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/framework/clusterctl/e2e_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,27 @@ 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))
copy(out.Images, c.Images)
out.Providers = make([]ProviderConfig, len(c.Providers))
copy(out.Providers, c.Providers)
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
}

// Defaults assigns default values to the object. More specifically:
// - ManagementClusterName gets a default name if empty.
// - Providers version gets type KustomizeSource if not otherwise specified.
Expand Down
94 changes: 94 additions & 0 deletions test/framework/clusterctl/e2e_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package clusterctl

import (
"testing"

. "github.com/onsi/gomega"
)

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 166025a

Please sign in to comment.