diff --git a/test/framework/clusterctl/client.go b/test/framework/clusterctl/client.go index 8419689de8ed..13606b2d8564 100644 --- a/test/framework/clusterctl/client.go +++ b/test/framework/clusterctl/client.go @@ -155,11 +155,11 @@ type UpgradeInput struct { func Upgrade(ctx context.Context, input UpgradeInput) { if len(input.ClusterctlVariables) > 0 { outputPath := filepath.Join(filepath.Dir(input.ClusterctlConfigPath), fmt.Sprintf("clusterctl-upgrade-config-%s.yaml", input.ClusterName)) - copyAndAmendClusterctlConfig(ctx, copyAndAmendClusterctlConfigInput{ + Expect(CopyAndAmendClusterctlConfig(ctx, CopyAndAmendClusterctlConfigInput{ ClusterctlConfigPath: input.ClusterctlConfigPath, OutputPath: outputPath, Variables: input.ClusterctlVariables, - }) + })).To(Succeed(), "Failed to CopyAndAmendClusterctlConfig") input.ClusterctlConfigPath = outputPath } @@ -288,11 +288,11 @@ func ConfigCluster(ctx context.Context, input ConfigClusterInput) []byte { if len(input.ClusterctlVariables) > 0 { outputPath := filepath.Join(filepath.Dir(input.ClusterctlConfigPath), fmt.Sprintf("clusterctl-upgrade-config-%s.yaml", input.ClusterName)) - copyAndAmendClusterctlConfig(ctx, copyAndAmendClusterctlConfigInput{ + Expect(CopyAndAmendClusterctlConfig(ctx, CopyAndAmendClusterctlConfigInput{ ClusterctlConfigPath: input.ClusterctlConfigPath, OutputPath: outputPath, Variables: input.ClusterctlVariables, - }) + })).To(Succeed(), "Failed to CopyAndAmendClusterctlConfig") input.ClusterctlConfigPath = outputPath } diff --git a/test/framework/clusterctl/clusterctl_config.go b/test/framework/clusterctl/clusterctl_config.go index 96b6d23322d3..e870f0b56b3d 100644 --- a/test/framework/clusterctl/clusterctl_config.go +++ b/test/framework/clusterctl/clusterctl_config.go @@ -19,7 +19,7 @@ package clusterctl import ( "os" - . "github.com/onsi/gomega" + "github.com/pkg/errors" "sigs.k8s.io/yaml" ) @@ -40,18 +40,29 @@ type providerConfig struct { } // write writes a clusterctl config file to disk. -func (c *clusterctlConfig) write() { +func (c *clusterctlConfig) write() error { data, err := yaml.Marshal(c.Values) - Expect(err).ToNot(HaveOccurred(), "Failed to marshal the clusterctl config file") + if err != nil { + return errors.Wrap(err, "failed to marshal the clusterctl config file") + } - Expect(os.WriteFile(c.Path, data, 0600)).To(Succeed(), "Failed to write the clusterctl config file") + if err := os.WriteFile(c.Path, data, 0600); err != nil { + return errors.Wrap(err, "failed to write the clusterctl config file") + } + + return nil } // read reads a clusterctl config file from disk. -func (c *clusterctlConfig) read() { +func (c *clusterctlConfig) read() error { data, err := os.ReadFile(c.Path) - Expect(err).ToNot(HaveOccurred()) + if err != nil { + return errors.Wrapf(err, "failed to read clusterctl config file %q", c.Path) + } + + if err = yaml.Unmarshal(data, &c.Values); err != nil { + return errors.Wrap(err, "failed to unmarshal the clusterctl config file") + } - err = yaml.Unmarshal(data, &c.Values) - Expect(err).ToNot(HaveOccurred(), "Failed to unmarshal the clusterctl config file") + return nil } diff --git a/test/framework/clusterctl/repository.go b/test/framework/clusterctl/repository.go index 42c855a540b9..27f213e51410 100644 --- a/test/framework/clusterctl/repository.go +++ b/test/framework/clusterctl/repository.go @@ -141,7 +141,7 @@ func CreateRepository(ctx context.Context, input CreateRepositoryInput) string { for key := range input.E2EConfig.Variables { clusterctlConfigFile.Values[key] = input.E2EConfig.GetVariable(key) } - clusterctlConfigFile.write() + Expect(clusterctlConfigFile.write()).To(Succeed(), "Failed to write clusterctlConfigFile") // creates a clusterctl config file to be used for working with such repository with only the providers supported in clusterctl < v1.3 clusterctlConfigFileV1_2 := &clusterctlConfig{ @@ -154,26 +154,28 @@ func CreateRepository(ctx context.Context, input CreateRepositoryInput) string { for key := range input.E2EConfig.Variables { clusterctlConfigFileV1_2.Values[key] = input.E2EConfig.GetVariable(key) } - clusterctlConfigFileV1_2.write() + Expect(clusterctlConfigFileV1_2.write()).To(Succeed(), "Failed to write v1.2 clusterctlConfigFile") return clusterctlConfigFile.Path } -// copyAndAmendClusterctlConfigInput is the input for copyAndAmendClusterctlConfig. -type copyAndAmendClusterctlConfigInput struct { +// CopyAndAmendClusterctlConfigInput is the input for copyAndAmendClusterctlConfig. +type CopyAndAmendClusterctlConfigInput struct { ClusterctlConfigPath string OutputPath string Variables map[string]string } -// copyAndAmendClusterctlConfig copies the clusterctl-config from ClusterctlConfigPath to +// CopyAndAmendClusterctlConfig copies the clusterctl-config from ClusterctlConfigPath to // OutputPath and adds the given Variables. -func copyAndAmendClusterctlConfig(_ context.Context, input copyAndAmendClusterctlConfigInput) { +func CopyAndAmendClusterctlConfig(_ context.Context, input CopyAndAmendClusterctlConfigInput) error { // Read clusterctl config from ClusterctlConfigPath. clusterctlConfigFile := &clusterctlConfig{ Path: input.ClusterctlConfigPath, } - clusterctlConfigFile.read() + if err := clusterctlConfigFile.read(); err != nil { + return err + } // Overwrite variables. if clusterctlConfigFile.Values == nil { @@ -185,7 +187,7 @@ func copyAndAmendClusterctlConfig(_ context.Context, input copyAndAmendClusterct // Write clusterctl config to OutputPath. clusterctlConfigFile.Path = input.OutputPath - clusterctlConfigFile.write() + return clusterctlConfigFile.write() } // AdjustConfigPathForBinary adjusts the clusterctlConfigPath in case the clusterctl version v1.3.