Skip to content

Commit

Permalink
update testcase logic; use local kafkatopic struct; use local tempfil…
Browse files Browse the repository at this point in the history
…e function
  • Loading branch information
mihaialexandrescu committed Aug 29, 2023
1 parent 505839c commit 38e9f64
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 357 deletions.
28 changes: 3 additions & 25 deletions tests/e2e/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,9 @@ func applyK8sResourceManifest(kubectlOptions k8s.KubectlOptions, manifestPath st
// applyK8sResourceManifestFromString applies the specified manifest in string format to the provided
// kubectl context and namespace.
func applyK8sResourceManifestFromString(kubectlOptions k8s.KubectlOptions, manifest string, extraArgs ...string) error {
// Replicating terratest's k8s.KubectlApplyFromStringE but with the possibility of a variadic argument that allows options like --dry-run
//
// TODO: look for a different implementation for temp files because terratest's version uses the composite test name to generate
// the temp file name which, in our case, includes all the descriptive Ginkgo statements (which are by design quite verbose).
// That can lead to erroring out on temp file creation based on the file name being too long.
tmpfile, err := k8s.StoreConfigToTempFileE(GinkgoT(), manifest)
tmpfile, err := createTempFileFromBytes([]byte(manifest), "", "", 0)
if err != nil {
return err
return fmt.Errorf("storing provided manifest data into temp file failed: %w", err)
}
defer os.Remove(tmpfile)

Expand All @@ -81,7 +76,7 @@ func applyK8sResourceManifestFromString(kubectlOptions k8s.KubectlOptions, manif

// applyK8sResourceFromTemplate generates manifest from the specified go-template based on values
// and applies the specified manifest to the provided kubectl context and namespace.
func applyK8sResourceFromTemplate(kubectlOptions k8s.KubectlOptions, templateFile string, values map[string]any, extraArgs ...string) error {
func applyK8sResourceFromTemplate(kubectlOptions k8s.KubectlOptions, templateFile string, values any, extraArgs ...string) error {
By(fmt.Sprintf("Generating k8s manifest from template %s", templateFile))
var manifest bytes.Buffer
rawTemplate, err := os.ReadFile(templateFile)
Expand All @@ -93,24 +88,7 @@ func applyK8sResourceFromTemplate(kubectlOptions k8s.KubectlOptions, templateFil
if err != nil {
return err
}
return applyK8sResourceManifestFromString(kubectlOptions, manifest.String(), extraArgs...)
}

// applyK8sResourceFromTemplate generates manifest from the specified go-template based on values
// and applies the specified manifest to the provided kubectl context and namespace.
func applyK8sResourceFromTemplate_2(kubectlOptions k8s.KubectlOptions, templateFile string, values any, extraArgs ...string) error {
By(fmt.Sprintf("Generating k8s manifest from template %s", templateFile))
var manifest bytes.Buffer
rawTemplate, err := os.ReadFile(templateFile)
if err != nil {
return err
}
t := template.Must(template.New("template").Funcs(sprig.TxtFuncMap()).Parse(string(rawTemplate)))
err = t.Execute(&manifest, values)
if err != nil {
return err
}
fmt.Printf("###\nManifest is :\n%s\n###\n", manifest.String())
return applyK8sResourceManifestFromString(kubectlOptions, manifest.String(), extraArgs...)
}

Expand Down
10 changes: 1 addition & 9 deletions tests/e2e/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ func requireDeleteKafkaTopic(kubectlOptions k8s.KubectlOptions, topicName string
// requireDeployingKafkaTopic deploys a kafkaTopic resource from a template
func requireDeployingKafkaTopic(kubectlOptions k8s.KubectlOptions, topicName string) {
It("Deploying KafkaTopic CR", func() {
// err := applyK8sResourceFromTemplate(kubectlOptions,
// kafkaTopicTemplate,
// map[string]interface{}{
// "Name": topicName,
// "TopicName": topicName,
// "Namespace": kubectlOptions.Namespace,
// },
// )
values := kafkaTopicTemplateData{
Annotations: []string{"managedBy: koperator"},
ClusterRef: kafkaTopicClusterRef{
Expand All @@ -56,7 +48,7 @@ func requireDeployingKafkaTopic(kubectlOptions k8s.KubectlOptions, topicName str
TopicName: topicName,
}

err := applyK8sResourceFromTemplate_2(kubectlOptions, kafkaTopicTemplate, values)
err := applyK8sResourceFromTemplate(kubectlOptions, kafkaTopicTemplate, values)

Expect(err).ShouldNot(HaveOccurred())

Expand Down
Loading

0 comments on commit 38e9f64

Please sign in to comment.