Skip to content

Commit

Permalink
WIP: Try first test
Browse files Browse the repository at this point in the history
  • Loading branch information
ederst committed May 4, 2021
1 parent d3cf3e8 commit 59a5710
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
18 changes: 12 additions & 6 deletions upup/pkg/fi/cloudup/openstacktasks/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
}
`,
},
}

Expand Down
45 changes: 43 additions & 2 deletions upup/pkg/fi/cloudup/openstacktasks/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 59a5710

Please sign in to comment.