Skip to content

Commit

Permalink
update testcase logic; temporary commit - needs cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaialexandrescu committed Aug 31, 2023
1 parent 4452863 commit b3bd748
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 36 deletions.
2 changes: 1 addition & 1 deletion tests/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/twmb/franz-go v1.13.5
k8s.io/apiextensions-apiserver v0.26.4
k8s.io/apimachinery v0.26.4
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/yaml v1.3.0
)

Expand Down Expand Up @@ -125,7 +126,6 @@ require (
k8s.io/client-go v0.26.4 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221207184640-f3cff1453715 // indirect
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
4 changes: 2 additions & 2 deletions tests/e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,8 @@ k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=
k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20221207184640-f3cff1453715 h1:tBEbstoM+K0FiBV5KGAKQ0kuvf54v/hwpldiJt69w1s=
k8s.io/kube-openapi v0.0.0-20221207184640-f3cff1453715/go.mod h1:+Axhij7bCpeqhklhUTe3xmOn6bWxolyZEeyaFpjGtl4=
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 h1:KTgPnR10d5zhztWptI952TNtt/4u5h3IzDXkdIMuo2Y=
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
20 changes: 19 additions & 1 deletion tests/e2e/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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]interface{}, extraArgs ...string) error {
func applyK8sResourceFromTemplate(kubectlOptions k8s.KubectlOptions, templateFile string, values map[string]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 @@ -96,6 +96,24 @@ func applyK8sResourceFromTemplate(kubectlOptions k8s.KubectlOptions, templateFil
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...)
}

// isExistingK8SResource queries a Resource by it's kind, namespace and name and
// returns true if it's found, false otherwise
func isExistingK8SResource(
Expand Down
50 changes: 43 additions & 7 deletions tests/e2e/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/gruntwork-io/terratest/modules/k8s"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/utils/ptr"
)

// requireDeleteKafkaTopic deletes kafkaTopic resource.
Expand All @@ -34,14 +35,29 @@ 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,
// err := applyK8sResourceFromTemplate(kubectlOptions,
// kafkaTopicTemplate,
// map[string]interface{}{
// "Name": topicName,
// "TopicName": topicName,
// "Namespace": kubectlOptions.Namespace,
// },
// )
values := kafkaTopicTemplateData{
Annotations: []string{"managedBy: koperator"},
ClusterRef: kafkaTopicClusterRef{
Name: kafkaClusterName,
Namespace: kubectlOptions.Namespace,
},
)
Name: topicName,
Namespace: kubectlOptions.Namespace,
Partitions: ptr.To(int32(2)),
ReplicationFactor: ptr.To(int32(2)),
TopicName: topicName,
}

err := applyK8sResourceFromTemplate_2(kubectlOptions, kafkaTopicTemplate, values)

Expect(err).ShouldNot(HaveOccurred())

err = waitK8sResourceCondition(kubectlOptions, kafkaTopicKind,
Expand Down Expand Up @@ -82,3 +98,23 @@ func requireDeployingKafkaUser(kubectlOptions k8s.KubectlOptions, userName strin
}, defaultUserCreationWaitTime, 3*time.Second).Should(Equal(true))
})
}

// kafkaTopicTemplateData is a struct that holds the relevant information and structure
// to fill out the template used to generate KafkaTopics.
// TODO: long term we should use the structs in the api module instead of these local structs.
type kafkaTopicTemplateData struct {
Annotations []string
ClusterRef kafkaTopicClusterRef
Name string
Namespace string
Partitions *int32
ReplicationFactor *int32
TopicName string
}

// kafkaTopicClusterRef holds the information relevant to identifying a KafkaCluster within a KafkaTopic CR.
// TODO: Long term, we should use the structs in the api module instead of these local structs.
type kafkaTopicClusterRef struct {
Name string
Namespace string
}
Loading

0 comments on commit b3bd748

Please sign in to comment.