Skip to content

Instantly share code, notes, and snippets.

@Boomatang
Created May 11, 2020 10:59
Show Gist options
  • Save Boomatang/ca598a90a769988fd4b1d99cd68414f3 to your computer and use it in GitHub Desktop.
Save Boomatang/ca598a90a769988fd4b1d99cd68414f3 to your computer and use it in GitHub Desktop.
package common
import (
goctx "context"
enmasse "github.com/integr8ly/integreatly-operator/pkg/apis-products/enmasse/v1beta2"
"reflect"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"testing"
)
type Container []interface{}
func (c *Container) Put(elem interface{}) {
*c = append(*c, elem)
}
func (c *Container) Get() interface{} {
elem := (*c)[0]
*c = (*c)[1:]
return elem
}
type resourceType interface {
crType() reflect.Type
copyRequiredValues(t *testing.T, intContainer Container, phase string)
changeCRValues(t *testing.T, intContainer Container, phase string)
}
type addressPlan2BR struct {
IntegreatlyName string
IntegreatlyNamespace string
crKind enmasse.AddressPlan
}
func (i *addressPlan2BR) crType() reflect.Type {
return reflect.TypeOf(i.crKind)
}
func (i *addressPlan2BR) copyRequiredValues(t *testing.T, intContainer Container, phase string) {
cr, ok := intContainer.Get().(enmasse.AddressPlan)
if !ok {
t.Fatalf("%s : Unable to read enmasse.AddressPlan from intContainer", phase)
}
ant := cr.GetAnnotations()
i.IntegreatlyName = ant[integreatlyName]
i.IntegreatlyNamespace = ant[integreatlyNamespace]
}
func (i *addressPlan2BR) changeCRValues(t *testing.T, intContainer Container, phase string) {
cr, ok := intContainer.Get().(enmasse.AddressPlan)
if !ok {
t.Fatalf("%s : Unable to read enmasse.AddressPlan from intContainer", phase)
}
ant := cr.GetAnnotations()
ant[integreatlyName] = "Bad Value"
ant[integreatlyNamespace] = "Bad Value"
cr.SetAnnotations(ant)
}
func runTests(t *testing.T, ctx *TestingContext) {
apl := &enmasse.AddressPlanList{}
listOpts := &k8sclient.ListOptions{
Namespace: amqOnline,
}
err := ctx.Client.List(goctx.TODO(), apl, listOpts)
if err != nil {
t.Fatal("addressPlan : Failed to get a list of address plan CR's from cluster")
}
for _, cr := range apl.Items {
ap := addressPlan2BR{}
ap.crKind = enmasse.AddressPlan{}
addressPlanContainer := &Container{}
addressPlanContainer.Put(cr)
modifyExistingValues(t, ctx, &ap, *addressPlanContainer)
break
}
}
// Needs to be generic
func modifyExistingValues(t *testing.T, ctx *TestingContext, rt resourceType, crData Container) {
phase := "Modify Existing Values"
rt.copyRequiredValues(t, crData, phase)
rt.changeCRValues(t, crData, phase)
updateClusterCr(t, ctx, rt, crData, phase)
}
func updateClusterCr(t *testing.T, ctx *TestingContext, rt resourceType, intContainer Container, phase string){
cr, ok := intContainer.Get().(rt.crType())
if !ok {
t.Log(cr)
t.Fatalf("%s : Unable to read CR from intContainer", phase)
}
err := ctx.Client.Update(goctx.TODO(), &cr)
if err != nil {
t.Log(cr)
t.Fatalf("%s : Failed to update CR on cluster", phase)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment