From 59a57100acfcff2cab38e6919d40388850dc27c2 Mon Sep 17 00:00:00 2001 From: ederst Date: Wed, 21 Apr 2021 02:21:59 +0200 Subject: [PATCH] WIP: Try first test --- .../fi/cloudup/openstacktasks/network_test.go | 18 +++++--- .../fi/cloudup/openstacktasks/render_test.go | 45 ++++++++++++++++++- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/upup/pkg/fi/cloudup/openstacktasks/network_test.go b/upup/pkg/fi/cloudup/openstacktasks/network_test.go index 95454c29631a5..583ed48eb203a 100644 --- a/upup/pkg/fi/cloudup/openstacktasks/network_test.go +++ b/upup/pkg/fi/cloudup/openstacktasks/network_test.go @@ -29,13 +29,19 @@ func TestNetworkTerraformRender(t *testing.T) { Name: fi.String("test"), }, Expected: `provider "openstack" { - region = "nova" - } + region = "nova" +} + +resource "openstack_networking_network_v2" "test" { + name = "test" +} - resource "openstack_networking_network_v2" "test" { - name = "test" - } - `, +terraform { + required_version = ">= 0.12.26" + required_providers { + } +} +`, }, } diff --git a/upup/pkg/fi/cloudup/openstacktasks/render_test.go b/upup/pkg/fi/cloudup/openstacktasks/render_test.go index 464749b410ad7..744d9dcf4f62c 100644 --- a/upup/pkg/fi/cloudup/openstacktasks/render_test.go +++ b/upup/pkg/fi/cloudup/openstacktasks/render_test.go @@ -19,8 +19,13 @@ package openstacktasks import ( "io/ioutil" "os" + "path" + "reflect" "testing" + + "k8s.io/kops/pkg/diff" + "k8s.io/kops/upup/pkg/fi" "k8s.io/kops/upup/pkg/fi/cloudup/openstack" "k8s.io/kops/upup/pkg/fi/cloudup/terraform" ) @@ -58,8 +63,44 @@ func doRenderTests(t *testing.T, method string, cases []*renderTest) { t.FailNow() } - t.Logf("do something with %s and %s", filename, target) + // @step: build the inputs for the methods - hopefully these don't change between them + var inputs []reflect.Value + for _, x := range []interface{}{target, c.Resource, c.Resource, c.Resource} { + inputs = append(inputs, reflect.ValueOf(x)) + } + + err := func() error { + // @step: invoke the rendering method of the target + resp := reflect.ValueOf(c.Resource).MethodByName(method).Call(inputs) + if err := resp[0].Interface(); err != nil { + return err.(error) + } + + // @step: invoke the target finish up + in := []reflect.Value{reflect.ValueOf(make(map[string]fi.Task))} + resp = reflect.ValueOf(target).MethodByName("Finish").Call(in) + if err := resp[0].Interface(); err != nil { + return err.(error) + } - t.Logf("case %d, expected: %s", i, c.Expected) + // @step: check the render is as expected + if c.Expected != "" { + content, err := ioutil.ReadFile(path.Join(outdir, filename)) + if err != nil { + return err + } + if c.Expected != string(content) { + diffString := diff.FormatDiff(c.Expected, string(content)) + t.Logf("diff:\n%s\n", diffString) + t.Errorf("case %d, expected: %s\n,got: %s\n", i, c.Expected, string(content)) + //assert.Equal(t, "", string(content)) + } + } + + return nil + }() + if err != nil { + t.Errorf("case %d, did not expect an error: %s", i, err) + } } } \ No newline at end of file