diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2021-03-21 20:08:25 -0400 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2021-03-21 20:08:25 -0400 |
| commit | 9831b95eb3813552cb14e9423c67c0e39b73c5df (patch) | |
| tree | 93203d8b35cd13fa02ccee2950df3fa633d2a7b6 /internal | |
| parent | 56d0d39c4a39917b2e35d4e3d71849d2e77855c5 (diff) | |
A few integration test simplifications
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/tests/inprocess-integration/datasources_blob_test.go | 25 | ||||
| -rw-r--r-- | internal/tests/inprocess-integration/datasources_http_test.go | 45 | ||||
| -rw-r--r-- | internal/tests/inprocess-integration/datasources_merge_test.go | 22 | ||||
| -rw-r--r-- | internal/tests/inprocess-integration/datasources_vault_ec2_test.go | 31 | ||||
| -rw-r--r-- | internal/tests/integration/base64_test.go | 18 | ||||
| -rw-r--r-- | internal/tests/integration/collection_test.go | 43 | ||||
| -rw-r--r-- | internal/tests/integration/datasources_blob_test.go | 25 | ||||
| -rw-r--r-- | internal/tests/integration/datasources_http_test.go | 44 | ||||
| -rw-r--r-- | internal/tests/integration/datasources_merge_test.go | 23 | ||||
| -rw-r--r-- | internal/tests/integration/datasources_vault_ec2_test.go | 31 | ||||
| -rw-r--r-- | internal/tests/integration/net_test.go | 5 | ||||
| -rw-r--r-- | internal/tests/integration/regexp_test.go | 10 | ||||
| -rw-r--r-- | internal/tests/integration/strings_test.go | 18 | ||||
| -rw-r--r-- | internal/tests/integration/tmpl_test.go | 5 |
14 files changed, 145 insertions, 200 deletions
diff --git a/internal/tests/inprocess-integration/datasources_blob_test.go b/internal/tests/inprocess-integration/datasources_blob_test.go index 7fa502b6..eb9942f7 100644 --- a/internal/tests/inprocess-integration/datasources_blob_test.go +++ b/internal/tests/inprocess-integration/datasources_blob_test.go @@ -2,8 +2,7 @@ package integration import ( "bytes" - "net" - "net/http" + "net/http/httptest" "os" "github.com/johannesboyne/gofakes3" @@ -12,7 +11,8 @@ import ( ) type BlobDatasourcesSuite struct { - l *net.TCPListener + addr string + srv *httptest.Server } var _ = Suite(&BlobDatasourcesSuite{}) @@ -20,14 +20,11 @@ var _ = Suite(&BlobDatasourcesSuite{}) func (s *BlobDatasourcesSuite) SetUpSuite(c *C) { backend := s3mem.New() s3 := gofakes3.New(backend) - var err error - s.l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}) - handle(c, err) - http.Handle("/", s3.Server()) - go http.Serve(s.l, nil) + s.srv = httptest.NewServer(s3.Server()) + s.addr = s.srv.Listener.Addr().String() - err = backend.CreateBucket("mybucket") + err := backend.CreateBucket("mybucket") handle(c, err) contents := `{"value":"json", "name":"foo"}` _, err = backend.PutObject("mybucket", "foo.json", map[string]string{"Content-Type": "application/json"}, bytes.NewBufferString(contents), int64(len(contents))) @@ -51,7 +48,7 @@ func (s *BlobDatasourcesSuite) SetUpSuite(c *C) { } func (s *BlobDatasourcesSuite) TearDownSuite(c *C) { - s.l.Close() + s.srv.Close() } func (s *BlobDatasourcesSuite) TestS3Datasource(c *C) { @@ -68,7 +65,7 @@ func (s *BlobDatasourcesSuite) TestS3Datasource(c *C) { "-c", "data=s3://mybucket/foo.json?" + "region=us-east-1&" + "disableSSL=true&" + - "endpoint=" + s.l.Addr().String() + "&" + + "endpoint=" + s.addr + "&" + "s3ForcePathStyle=true", "-i", "{{ .data.value }}"}, map[string]string{ @@ -85,7 +82,7 @@ func (s *BlobDatasourcesSuite) TestS3Datasource(c *C) { "-i", "{{ .data.value }}"}, map[string]string{ "AWS_ANON": "true", - "AWS_S3_ENDPOINT": s.l.Addr().String(), + "AWS_S3_ENDPOINT": s.addr, }) assertSuccess(c, o, e, err, "json") } @@ -102,7 +99,7 @@ func (s *BlobDatasourcesSuite) TestS3Directory(c *C) { o, e, err = cmdWithEnv(c, []string{"-c", "data=s3://mybucket/a/b/c/?" + "region=us-east-1&" + "disableSSL=true&" + - "endpoint=" + s.l.Addr().String() + "&" + + "endpoint=" + s.addr + "&" + "s3ForcePathStyle=true", "-i", "{{ .data }}"}, map[string]string{ @@ -116,7 +113,7 @@ func (s *BlobDatasourcesSuite) TestS3MIMETypes(c *C) { o, e, err := cmdWithEnv(c, []string{"-c", "data=s3://mybucket/a/b/c/d?" + "region=us-east-1&" + "disableSSL=true&" + - "endpoint=" + s.l.Addr().String() + "&" + + "endpoint=" + s.addr + "&" + "s3ForcePathStyle=true", "-i", "{{ .data.c.cc }}"}, map[string]string{ diff --git a/internal/tests/inprocess-integration/datasources_http_test.go b/internal/tests/inprocess-integration/datasources_http_test.go index b81180e3..717bc7ef 100644 --- a/internal/tests/inprocess-integration/datasources_http_test.go +++ b/internal/tests/inprocess-integration/datasources_http_test.go @@ -1,83 +1,82 @@ package integration import ( - "net" "net/http" + "net/http/httptest" . "gopkg.in/check.v1" ) type DatasourcesHTTPSuite struct { - l *net.TCPListener + srv *httptest.Server } var _ = Suite(&DatasourcesHTTPSuite{}) func (s *DatasourcesHTTPSuite) SetUpSuite(c *C) { - var err error - s.l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}) - handle(c, err) - - http.HandleFunc("/mirror", mirrorHandler) - http.HandleFunc("/not.json", typeHandler("application/yaml", "value: notjson\n")) - http.HandleFunc("/foo", typeHandler("application/json", `{"value": "json"}`)) - http.HandleFunc("/actually.json", typeHandler("", `{"value": "json"}`)) - http.HandleFunc("/bogus.csv", typeHandler("text/plain", `{"value": "json"}`)) - http.HandleFunc("/list", typeHandler("application/array+json", `[1, 2, 3, 4, 5]`)) - go http.Serve(s.l, nil) + mux := http.NewServeMux() + + mux.HandleFunc("/mirror", mirrorHandler) + mux.HandleFunc("/not.json", typeHandler("application/yaml", "value: notjson\n")) + mux.HandleFunc("/foo", typeHandler("application/json", `{"value": "json"}`)) + mux.HandleFunc("/actually.json", typeHandler("", `{"value": "json"}`)) + mux.HandleFunc("/bogus.csv", typeHandler("text/plain", `{"value": "json"}`)) + mux.HandleFunc("/list", typeHandler("application/array+json", `[1, 2, 3, 4, 5]`)) + + s.srv = httptest.NewServer(mux) } func (s *DatasourcesHTTPSuite) TearDownSuite(c *C) { - s.l.Close() + s.srv.Close() } func (s *DatasourcesHTTPSuite) TestHTTPDatasource(c *C) { o, e, err := cmdTest(c, - "-d", "foo=http://"+s.l.Addr().String()+"/mirror", + "-d", "foo="+s.srv.URL+"/mirror", "-H", "foo=Foo:bar", "-i", "{{ index (ds `foo`).headers.Foo 0 }}") assertSuccess(c, o, e, err, "bar") o, e, err = cmdTest(c, "-H", "foo=Foo:bar", - "-i", "{{defineDatasource `foo` `http://"+s.l.Addr().String()+"/mirror`}}{{ index (ds `foo`).headers.Foo 0 }}") + "-i", "{{defineDatasource `foo` `"+s.srv.URL+"/mirror`}}{{ index (ds `foo`).headers.Foo 0 }}") assertSuccess(c, o, e, err, "bar") o, e, err = cmdTest(c, - "-i", "{{ $d := ds `http://"+s.l.Addr().String()+"/mirror`}}{{ index (index $d.headers `Accept-Encoding`) 0 }}") + "-i", "{{ $d := ds `"+s.srv.URL+"/mirror`}}{{ index (index $d.headers `Accept-Encoding`) 0 }}") assertSuccess(c, o, e, err, "gzip") } func (s *DatasourcesHTTPSuite) TestTypeOverridePrecedence(c *C) { o, e, err := cmdTest(c, - "-d", "foo=http://"+s.l.Addr().String()+"/foo", + "-d", "foo="+s.srv.URL+"/foo", "-i", "{{ (ds `foo`).value }}") assertSuccess(c, o, e, err, "json") o, e, err = cmdTest(c, - "-d", "foo=http://"+s.l.Addr().String()+"/not.json", + "-d", "foo="+s.srv.URL+"/not.json", "-i", "{{ (ds `foo`).value }}") assertSuccess(c, o, e, err, "notjson") o, e, err = cmdTest(c, - "-d", "foo=http://"+s.l.Addr().String()+"/actually.json", + "-d", "foo="+s.srv.URL+"/actually.json", "-i", "{{ (ds `foo`).value }}") assertSuccess(c, o, e, err, "json") o, e, err = cmdTest(c, - "-d", "foo=http://"+s.l.Addr().String()+"/bogus.csv?type=application/json", + "-d", "foo="+s.srv.URL+"/bogus.csv?type=application/json", "-i", "{{ (ds `foo`).value }}") assertSuccess(c, o, e, err, "json") o, e, err = cmdTest(c, - "-c", ".=http://"+s.l.Addr().String()+"/list?type=application/array+json", + "-c", ".="+s.srv.URL+"/list?type=application/array+json", "-i", "{{ range . }}{{ . }}{{ end }}") assertSuccess(c, o, e, err, "12345") } func (s *DatasourcesHTTPSuite) TestAppendQueryAfterSubPaths(c *C) { o, e, err := cmdTest(c, - "-d", "foo=http://"+s.l.Addr().String()+"/?type=application/json", + "-d", "foo="+s.srv.URL+"/?type=application/json", "-i", "{{ (ds `foo` `bogus.csv`).value }}") assertSuccess(c, o, e, err, "json") } diff --git a/internal/tests/inprocess-integration/datasources_merge_test.go b/internal/tests/inprocess-integration/datasources_merge_test.go index 4b304a93..f5424ed0 100644 --- a/internal/tests/inprocess-integration/datasources_merge_test.go +++ b/internal/tests/inprocess-integration/datasources_merge_test.go @@ -1,8 +1,8 @@ package integration import ( - "net" "net/http" + "net/http/httptest" . "gopkg.in/check.v1" @@ -11,7 +11,7 @@ import ( type MergeDatasourceSuite struct { tmpDir *fs.Dir - l *net.TCPListener + srv *httptest.Server } var _ = Suite(&MergeDatasourceSuite{}) @@ -24,18 +24,16 @@ func (s *MergeDatasourceSuite) SetUpSuite(c *C) { }), ) - var err error - s.l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}) - handle(c, err) + mux := http.NewServeMux() + mux.HandleFunc("/foo.json", typeHandler("application/json", `{"foo": "bar"}`)) + mux.HandleFunc("/1.env", typeHandler("application/x-env", "FOO=1\nBAR=2\n")) + mux.HandleFunc("/2.env", typeHandler("application/x-env", "FOO=3\n")) - http.HandleFunc("/foo.json", typeHandler("application/json", `{"foo": "bar"}`)) - http.HandleFunc("/1.env", typeHandler("application/x-env", "FOO=1\nBAR=2\n")) - http.HandleFunc("/2.env", typeHandler("application/x-env", "FOO=3\n")) - go http.Serve(s.l, nil) + s.srv = httptest.NewServer(mux) } func (s *MergeDatasourceSuite) TearDownSuite(c *C) { - s.l.Close() + s.srv.Close() s.tmpDir.Remove() } @@ -57,13 +55,13 @@ func (s *MergeDatasourceSuite) TestMergeDatasource(c *C) { o, e, err = cmdTest(c, "-d", "default="+s.tmpDir.Join("default.yml"), - "-d", "config=merge:http://"+s.l.Addr().String()+"/foo.json|default", + "-d", "config=merge:"+s.srv.URL+"/foo.json|default", "-i", `{{ ds "config" | toJSON }}`, ) assertSuccess(c, o, e, err, `{"foo":"bar","isDefault":true,"isOverride":false,"other":true}`) o, e, err = cmdTest(c, - "-c", "merged=merge:http://"+s.l.Addr().String()+"/2.env|http://"+s.l.Addr().String()+"/1.env", + "-c", "merged=merge:"+s.srv.URL+"/2.env|"+s.srv.URL+"/1.env", "-i", `FOO is {{ .merged.FOO }}`, ) assertSuccess(c, o, e, err, `FOO is 3`) diff --git a/internal/tests/inprocess-integration/datasources_vault_ec2_test.go b/internal/tests/inprocess-integration/datasources_vault_ec2_test.go index ec18d2cf..8a1b60da 100644 --- a/internal/tests/inprocess-integration/datasources_vault_ec2_test.go +++ b/internal/tests/inprocess-integration/datasources_vault_ec2_test.go @@ -5,8 +5,8 @@ package integration import ( "encoding/pem" "io/ioutil" - "net" "net/http" + "net/http/httptest" "os" "os/user" "path" @@ -24,26 +24,27 @@ type VaultEc2DatasourcesSuite struct { vaultAddr string vaultResult *icmd.Result v *vaultClient - l *net.TCPListener + srv *httptest.Server cert []byte } var _ = Suite(&VaultEc2DatasourcesSuite{}) func (s *VaultEc2DatasourcesSuite) SetUpSuite(c *C) { - var err error - s.l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}) - handle(c, err) priv, der, _ := certificateGenerate() s.cert = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) - http.HandleFunc("/latest/dynamic/instance-identity/pkcs7", pkcsHandler(priv, der)) - http.HandleFunc("/latest/dynamic/instance-identity/document", instanceDocumentHandler) - http.HandleFunc("/sts/", stsHandler) - http.HandleFunc("/ec2/", ec2Handler) - go http.Serve(s.l, nil) + + mux := http.NewServeMux() + mux.HandleFunc("/latest/dynamic/instance-identity/pkcs7", pkcsHandler(priv, der)) + mux.HandleFunc("/latest/dynamic/instance-identity/document", instanceDocumentHandler) + mux.HandleFunc("/sts/", stsHandler) + mux.HandleFunc("/ec2/", ec2Handler) + + s.srv = httptest.NewServer(mux) s.pidDir, s.tmpDir, s.vaultAddr, s.vaultResult = startVault(c) + var err error s.v, err = createVaultClient(s.vaultAddr, vaultRootToken) handle(c, err) @@ -58,7 +59,7 @@ func (s *VaultEc2DatasourcesSuite) SetUpSuite(c *C) { } func (s *VaultEc2DatasourcesSuite) TearDownSuite(c *C) { - s.l.Close() + s.srv.Close() defer s.tmpDir.Remove() defer s.pidDir.Remove() @@ -90,9 +91,9 @@ func (s *VaultEc2DatasourcesSuite) TestEc2Auth(c *C) { defer s.v.vc.Sys().DisableAuth("aws") _, err = s.v.vc.Logical().Write("auth/aws/config/client", map[string]interface{}{ "secret_key": "secret", "access_key": "access", - "endpoint": "http://" + s.l.Addr().String() + "/ec2", - "iam_endpoint": "http://" + s.l.Addr().String() + "/iam", - "sts_endpoint": "http://" + s.l.Addr().String() + "/sts", + "endpoint": s.srv.URL + "/ec2", + "iam_endpoint": s.srv.URL + "/iam", + "sts_endpoint": s.srv.URL + "/sts", }) handle(c, err) @@ -113,7 +114,7 @@ func (s *VaultEc2DatasourcesSuite) TestEc2Auth(c *C) { }, map[string]string{ "HOME": s.tmpDir.Join("home"), "VAULT_ADDR": "http://" + s.v.addr, - "AWS_META_ENDPOINT": "http://" + s.l.Addr().String(), + "AWS_META_ENDPOINT": s.srv.URL, }) assertSuccess(c, o, e, err, "bar") } diff --git a/internal/tests/integration/base64_test.go b/internal/tests/integration/base64_test.go index 829c6891..a9aeafce 100644 --- a/internal/tests/integration/base64_test.go +++ b/internal/tests/integration/base64_test.go @@ -3,23 +3,17 @@ package integration import ( - . "gopkg.in/check.v1" - - "gotest.tools/v3/icmd" + "gopkg.in/check.v1" ) type Base64Suite struct{} -var _ = Suite(&Base64Suite{}) +var _ = check.Suite(&Base64Suite{}) -func (s *Base64Suite) TestBase64Encode(c *C) { - result := icmd.RunCommand(GomplateBin, "-i", - `{{ "foo" | base64.Encode }}`) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "Zm9v"}) +func (s *Base64Suite) TestBase64Encode(c *check.C) { + inOutTest(c, `{{ "foo" | base64.Encode }}`, "Zm9v") } -func (s *Base64Suite) TestBase64Decode(c *C) { - result := icmd.RunCommand(GomplateBin, "-i", - `{{ "Zm9v" | base64.Decode }}`) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "foo"}) +func (s *Base64Suite) TestBase64Decode(c *check.C) { + inOutTest(c, `{{ "Zm9v" | base64.Decode }}`, "foo") } diff --git a/internal/tests/integration/collection_test.go b/internal/tests/integration/collection_test.go index 521729cc..85031dcf 100644 --- a/internal/tests/integration/collection_test.go +++ b/internal/tests/integration/collection_test.go @@ -64,28 +64,22 @@ func (s *CollSuite) TestMerge(c *C) { } func (s *CollSuite) TestSort(c *C) { - result := icmd.RunCmd(icmd.Command(GomplateBin, - "-i", `{{ $maps := jsonArray "[{\"a\": \"foo\", \"b\": 1}, {\"a\": \"bar\", \"b\": 8}, {\"a\": \"baz\", \"b\": 3}]" -}} + inOutTest(c, `{{ $maps := jsonArray "[{\"a\": \"foo\", \"b\": 1}, {\"a\": \"bar\", \"b\": 8}, {\"a\": \"baz\", \"b\": 3}]" -}} {{ range coll.Sort "b" $maps -}} {{ .a }} {{ end -}} -`)) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "foo\nbaz\nbar\n"}) +`, "foo\nbaz\nbar\n") - result = icmd.RunCmd(icmd.Command(GomplateBin, - "-i", ` + inOutTest(c, ` {{- coll.Sort (slice "b" "a" "c" "aa") }} {{ coll.Sort (slice "b" 14 "c" "aa") }} {{ coll.Sort (slice 3.14 3.0 4.0) }} {{ coll.Sort "Scheme" (coll.Slice (conv.URL "zzz:///") (conv.URL "https:///") (conv.URL "http:///")) }} -`)) - result.Assert(c, icmd.Expected{ExitCode: 0, - Out: `[a aa b c] +`, `[a aa b c] [b 14 c aa] [3 3.14 4] [http:/// https:/// zzz:///] -`, - }) +`) } func (s *CollSuite) TestJSONPath(c *C) { @@ -102,31 +96,16 @@ func (s *CollSuite) TestJSONPath(c *C) { func (s *CollSuite) TestFlatten(c *C) { in := "[[1,2],[],[[3,4],[[[5],6],7]]]" - result := icmd.RunCmd(icmd.Command(GomplateBin, - "-i", "{{ `"+in+"` | jsonArray | coll.Flatten | toJSON }}")) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "[1,2,3,4,5,6,7]"}) - - result = icmd.RunCmd(icmd.Command(GomplateBin, - "-i", "{{ `"+in+"` | jsonArray | flatten 0 | toJSON }}")) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: in}) - - result = icmd.RunCmd(icmd.Command(GomplateBin, - "-i", "{{ coll.Flatten 1 (`"+in+"` | jsonArray) | toJSON }}")) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "[1,2,[3,4],[[[5],6],7]]"}) - - result = icmd.RunCmd(icmd.Command(GomplateBin, - "-i", "{{ `"+in+"` | jsonArray | coll.Flatten 2 | toJSON }}")) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "[1,2,3,4,[[5],6],7]"}) + inOutTest(c, "{{ `"+in+"` | jsonArray | coll.Flatten | toJSON }}", "[1,2,3,4,5,6,7]") + inOutTest(c, "{{ `"+in+"` | jsonArray | flatten 0 | toJSON }}", in) + inOutTest(c, "{{ coll.Flatten 1 (`"+in+"` | jsonArray) | toJSON }}", "[1,2,[3,4],[[[5],6],7]]") + inOutTest(c, "{{ `"+in+"` | jsonArray | coll.Flatten 2 | toJSON }}", "[1,2,3,4,[[5],6],7]") } func (s *CollSuite) TestPick(c *C) { - result := icmd.RunCmd(icmd.Command(GomplateBin, - "-i", `{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }}{{ coll.Pick "foo" "baz" $data }}`)) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "map[baz:3 foo:1]"}) + inOutTest(c, `{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }}{{ coll.Pick "foo" "baz" $data }}`, "map[baz:3 foo:1]") } func (s *CollSuite) TestOmit(c *C) { - result := icmd.RunCmd(icmd.Command(GomplateBin, - "-i", `{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }}{{ coll.Omit "foo" "baz" $data }}`)) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "map[bar:2]"}) + inOutTest(c, `{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }}{{ coll.Omit "foo" "baz" $data }}`, "map[bar:2]") } diff --git a/internal/tests/integration/datasources_blob_test.go b/internal/tests/integration/datasources_blob_test.go index e5787682..66077b59 100644 --- a/internal/tests/integration/datasources_blob_test.go +++ b/internal/tests/integration/datasources_blob_test.go @@ -4,8 +4,7 @@ package integration import ( "bytes" - "net" - "net/http" + "net/http/httptest" "os" "github.com/johannesboyne/gofakes3" @@ -16,7 +15,8 @@ import ( ) type BlobDatasourcesSuite struct { - l *net.TCPListener + addr string + srv *httptest.Server } var _ = Suite(&BlobDatasourcesSuite{}) @@ -24,14 +24,11 @@ var _ = Suite(&BlobDatasourcesSuite{}) func (s *BlobDatasourcesSuite) SetUpSuite(c *C) { backend := s3mem.New() s3 := gofakes3.New(backend) - var err error - s.l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}) - handle(c, err) - http.Handle("/", s3.Server()) - go http.Serve(s.l, nil) + s.srv = httptest.NewServer(s3.Server()) + s.addr = s.srv.Listener.Addr().String() - err = backend.CreateBucket("mybucket") + err := backend.CreateBucket("mybucket") handle(c, err) contents := `{"value":"json", "name":"foo"}` _, err = backend.PutObject("mybucket", "foo.json", map[string]string{"Content-Type": "application/json"}, bytes.NewBufferString(contents), int64(len(contents))) @@ -55,7 +52,7 @@ func (s *BlobDatasourcesSuite) SetUpSuite(c *C) { } func (s *BlobDatasourcesSuite) TearDownSuite(c *C) { - s.l.Close() + s.srv.Close() } func (s *BlobDatasourcesSuite) TestS3Datasource(c *C) { @@ -74,7 +71,7 @@ func (s *BlobDatasourcesSuite) TestS3Datasource(c *C) { "-c", "data=s3://mybucket/foo.json?"+ "region=us-east-1&"+ "disableSSL=true&"+ - "endpoint="+s.l.Addr().String()+"&"+ + "endpoint="+s.addr+"&"+ "s3ForcePathStyle=true", "-i", "{{ .data.value }}", ), func(c *icmd.Cmd) { @@ -94,7 +91,7 @@ func (s *BlobDatasourcesSuite) TestS3Datasource(c *C) { ), func(c *icmd.Cmd) { c.Env = []string{ "AWS_ANON=true", - "AWS_S3_ENDPOINT=" + s.l.Addr().String(), + "AWS_S3_ENDPOINT=" + s.addr, } }) result.Assert(c, icmd.Expected{ExitCode: 0, Out: "json"}) @@ -116,7 +113,7 @@ func (s *BlobDatasourcesSuite) TestS3Directory(c *C) { "-c", "data=s3://mybucket/a/b/c/?"+ "region=us-east-1&"+ "disableSSL=true&"+ - "endpoint="+s.l.Addr().String()+"&"+ + "endpoint="+s.addr+"&"+ "s3ForcePathStyle=true", "-i", "{{ .data }}", ), func(c *icmd.Cmd) { @@ -133,7 +130,7 @@ func (s *BlobDatasourcesSuite) TestS3MIMETypes(c *C) { "-c", "data=s3://mybucket/a/b/c/d?"+ "region=us-east-1&"+ "disableSSL=true&"+ - "endpoint="+s.l.Addr().String()+"&"+ + "endpoint="+s.addr+"&"+ "s3ForcePathStyle=true", "-i", "{{ .data.c.cc }}", ), func(c *icmd.Cmd) { diff --git a/internal/tests/integration/datasources_http_test.go b/internal/tests/integration/datasources_http_test.go index 63d2fc86..e52e5bac 100644 --- a/internal/tests/integration/datasources_http_test.go +++ b/internal/tests/integration/datasources_http_test.go @@ -3,8 +3,8 @@ package integration import ( - "net" "net/http" + "net/http/httptest" . "gopkg.in/check.v1" @@ -12,76 +12,74 @@ import ( ) type DatasourcesHTTPSuite struct { - l *net.TCPListener + srv *httptest.Server } var _ = Suite(&DatasourcesHTTPSuite{}) func (s *DatasourcesHTTPSuite) SetUpSuite(c *C) { - var err error - s.l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}) - handle(c, err) - - http.HandleFunc("/mirror", mirrorHandler) - http.HandleFunc("/not.json", typeHandler("application/yaml", "value: notjson\n")) - http.HandleFunc("/foo", typeHandler("application/json", `{"value": "json"}`)) - http.HandleFunc("/actually.json", typeHandler("", `{"value": "json"}`)) - http.HandleFunc("/bogus.csv", typeHandler("text/plain", `{"value": "json"}`)) - http.HandleFunc("/list", typeHandler("application/array+json", `[1, 2, 3, 4, 5]`)) - go http.Serve(s.l, nil) + mux := http.NewServeMux() + mux.HandleFunc("/mirror", mirrorHandler) + mux.HandleFunc("/not.json", typeHandler("application/yaml", "value: notjson\n")) + mux.HandleFunc("/foo", typeHandler("application/json", `{"value": "json"}`)) + mux.HandleFunc("/actually.json", typeHandler("", `{"value": "json"}`)) + mux.HandleFunc("/bogus.csv", typeHandler("text/plain", `{"value": "json"}`)) + mux.HandleFunc("/list", typeHandler("application/array+json", `[1, 2, 3, 4, 5]`)) + + s.srv = httptest.NewServer(mux) } func (s *DatasourcesHTTPSuite) TearDownSuite(c *C) { - s.l.Close() + s.srv.Close() } func (s *DatasourcesHTTPSuite) TestReportsVersion(c *C) { result := icmd.RunCommand(GomplateBin, - "-d", "foo=http://"+s.l.Addr().String()+"/mirror", + "-d", "foo="+s.srv.URL+"/mirror", "-H", "foo=Foo:bar", "-i", "{{ index (ds `foo`).headers.Foo 0 }}") result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"}) result = icmd.RunCommand(GomplateBin, "-H", "foo=Foo:bar", - "-i", "{{defineDatasource `foo` `http://"+s.l.Addr().String()+"/mirror`}}{{ index (ds `foo`).headers.Foo 0 }}") + "-i", "{{defineDatasource `foo` `"+s.srv.URL+"/mirror`}}{{ index (ds `foo`).headers.Foo 0 }}") result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"}) result = icmd.RunCommand(GomplateBin, - "-i", "{{ $d := ds `http://"+s.l.Addr().String()+"/mirror`}}{{ index (index $d.headers `Accept-Encoding`) 0 }}") + "-i", "{{ $d := ds `"+s.srv.URL+"/mirror`}}{{ index (index $d.headers `Accept-Encoding`) 0 }}") result.Assert(c, icmd.Expected{ExitCode: 0, Out: "gzip"}) } func (s *DatasourcesHTTPSuite) TestTypeOverridePrecedence(c *C) { result := icmd.RunCommand(GomplateBin, - "-d", "foo=http://"+s.l.Addr().String()+"/foo", + "-d", "foo="+s.srv.URL+"/foo", "-i", "{{ (ds `foo`).value }}") result.Assert(c, icmd.Expected{ExitCode: 0, Out: "json"}) result = icmd.RunCommand(GomplateBin, - "-d", "foo=http://"+s.l.Addr().String()+"/not.json", + "-d", "foo="+s.srv.URL+"/not.json", "-i", "{{ (ds `foo`).value }}") result.Assert(c, icmd.Expected{ExitCode: 0, Out: "notjson"}) result = icmd.RunCommand(GomplateBin, - "-d", "foo=http://"+s.l.Addr().String()+"/actually.json", + "-d", "foo="+s.srv.URL+"/actually.json", "-i", "{{ (ds `foo`).value }}") result.Assert(c, icmd.Expected{ExitCode: 0, Out: "json"}) result = icmd.RunCommand(GomplateBin, - "-d", "foo=http://"+s.l.Addr().String()+"/bogus.csv?type=application/json", + "-d", "foo="+s.srv.URL+"/bogus.csv?type=application/json", "-i", "{{ (ds `foo`).value }}") result.Assert(c, icmd.Expected{ExitCode: 0, Out: "json"}) result = icmd.RunCommand(GomplateBin, - "-c", ".=http://"+s.l.Addr().String()+"/list?type=application/array+json", + "-c", ".="+s.srv.URL+"/list?type=application/array+json", "-i", "{{ range . }}{{ . }}{{ end }}") result.Assert(c, icmd.Expected{ExitCode: 0, Out: "12345"}) } func (s *DatasourcesHTTPSuite) TestAppendQueryAfterSubPaths(c *C) { result := icmd.RunCommand(GomplateBin, - "-d", "foo=http://"+s.l.Addr().String()+"/?type=application/json", + "-d", "foo="+s.srv.URL+"/?type=application/json", "-i", "{{ (ds `foo` `bogus.csv`).value }}") result.Assert(c, icmd.Expected{ExitCode: 0, Out: "json"}) } diff --git a/internal/tests/integration/datasources_merge_test.go b/internal/tests/integration/datasources_merge_test.go index 7b7140bf..cf9c3173 100644 --- a/internal/tests/integration/datasources_merge_test.go +++ b/internal/tests/integration/datasources_merge_test.go @@ -3,8 +3,8 @@ package integration import ( - "net" "net/http" + "net/http/httptest" . "gopkg.in/check.v1" @@ -14,7 +14,7 @@ import ( type MergeDatasourceSuite struct { tmpDir *fs.Dir - l *net.TCPListener + srv *httptest.Server } var _ = Suite(&MergeDatasourceSuite{}) @@ -27,18 +27,17 @@ func (s *MergeDatasourceSuite) SetUpSuite(c *C) { }), ) - var err error - s.l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}) - handle(c, err) + mux := http.NewServeMux() - http.HandleFunc("/foo.json", typeHandler("application/json", `{"foo": "bar"}`)) - http.HandleFunc("/1.env", typeHandler("application/x-env", "FOO=1\nBAR=2\n")) - http.HandleFunc("/2.env", typeHandler("application/x-env", "FOO=3\n")) - go http.Serve(s.l, nil) + mux.HandleFunc("/foo.json", typeHandler("application/json", `{"foo": "bar"}`)) + mux.HandleFunc("/1.env", typeHandler("application/x-env", "FOO=1\nBAR=2\n")) + mux.HandleFunc("/2.env", typeHandler("application/x-env", "FOO=3\n")) + + s.srv = httptest.NewServer(mux) } func (s *MergeDatasourceSuite) TearDownSuite(c *C) { - s.l.Close() + s.srv.Close() s.tmpDir.Remove() } @@ -60,13 +59,13 @@ func (s *MergeDatasourceSuite) TestMergeDatasource(c *C) { result = icmd.RunCommand(GomplateBin, "-d", "default="+s.tmpDir.Join("default.yml"), - "-d", "config=merge:http://"+s.l.Addr().String()+"/foo.json|default", + "-d", "config=merge:"+s.srv.URL+"/foo.json|default", "-i", `{{ ds "config" | toJSON }}`, ) result.Assert(c, icmd.Expected{ExitCode: 0, Out: `{"foo":"bar","isDefault":true,"isOverride":false,"other":true}`}) result = icmd.RunCommand(GomplateBin, - "-c", "merged=merge:http://"+s.l.Addr().String()+"/2.env|http://"+s.l.Addr().String()+"/1.env", + "-c", "merged=merge:"+s.srv.URL+"/2.env|"+s.srv.URL+"/1.env", "-i", `FOO is {{ .merged.FOO }}`, ) result.Assert(c, icmd.Expected{ExitCode: 0, Out: `FOO is 3`}) diff --git a/internal/tests/integration/datasources_vault_ec2_test.go b/internal/tests/integration/datasources_vault_ec2_test.go index 3ab19db0..c2696215 100644 --- a/internal/tests/integration/datasources_vault_ec2_test.go +++ b/internal/tests/integration/datasources_vault_ec2_test.go @@ -6,8 +6,8 @@ package integration import ( "encoding/pem" "io/ioutil" - "net" "net/http" + "net/http/httptest" "os" "os/user" "path" @@ -25,26 +25,27 @@ type VaultEc2DatasourcesSuite struct { vaultAddr string vaultResult *icmd.Result v *vaultClient - l *net.TCPListener + srv *httptest.Server cert []byte } var _ = Suite(&VaultEc2DatasourcesSuite{}) func (s *VaultEc2DatasourcesSuite) SetUpSuite(c *C) { - var err error - s.l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")}) - handle(c, err) priv, der, _ := certificateGenerate() s.cert = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) - http.HandleFunc("/latest/dynamic/instance-identity/pkcs7", pkcsHandler(priv, der)) - http.HandleFunc("/latest/dynamic/instance-identity/document", instanceDocumentHandler) - http.HandleFunc("/sts/", stsHandler) - http.HandleFunc("/ec2/", ec2Handler) - go http.Serve(s.l, nil) + + mux := http.NewServeMux() + mux.HandleFunc("/latest/dynamic/instance-identity/pkcs7", pkcsHandler(priv, der)) + mux.HandleFunc("/latest/dynamic/instance-identity/document", instanceDocumentHandler) + mux.HandleFunc("/sts/", stsHandler) + mux.HandleFunc("/ec2/", ec2Handler) + + s.srv = httptest.NewServer(mux) s.pidDir, s.tmpDir, s.vaultAddr, s.vaultResult = startVault(c) + var err error s.v, err = createVaultClient(s.vaultAddr, vaultRootToken) handle(c, err) @@ -59,7 +60,7 @@ func (s *VaultEc2DatasourcesSuite) SetUpSuite(c *C) { } func (s *VaultEc2DatasourcesSuite) TearDownSuite(c *C) { - s.l.Close() + s.srv.Close() defer s.tmpDir.Remove() defer s.pidDir.Remove() @@ -91,9 +92,9 @@ func (s *VaultEc2DatasourcesSuite) TestEc2Auth(c *C) { defer s.v.vc.Sys().DisableAuth("aws") _, err = s.v.vc.Logical().Write("auth/aws/config/client", map[string]interface{}{ "secret_key": "secret", "access_key": "access", - "endpoint": "http://" + s.l.Addr().String() + "/ec2", - "iam_endpoint": "http://" + s.l.Addr().String() + "/iam", - "sts_endpoint": "http://" + s.l.Addr().String() + "/sts", + "endpoint": s.srv.URL + "/ec2", + "iam_endpoint": s.srv.URL + "/iam", + "sts_endpoint": s.srv.URL + "/sts", }) handle(c, err) @@ -115,7 +116,7 @@ func (s *VaultEc2DatasourcesSuite) TestEc2Auth(c *C) { c.Env = []string{ "HOME=" + s.tmpDir.Join("home"), "VAULT_ADDR=http://" + s.v.addr, - "AWS_META_ENDPOINT=http://" + s.l.Addr().String(), + "AWS_META_ENDPOINT=" + s.srv.URL, } }) result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"}) diff --git a/internal/tests/integration/net_test.go b/internal/tests/integration/net_test.go index 052d62cb..9943378f 100644 --- a/internal/tests/integration/net_test.go +++ b/internal/tests/integration/net_test.go @@ -4,8 +4,6 @@ package integration import ( . "gopkg.in/check.v1" - - "gotest.tools/v3/icmd" ) type NetSuite struct{} @@ -13,6 +11,5 @@ type NetSuite struct{} var _ = Suite(&NetSuite{}) func (s *NetSuite) TestLookupIP(c *C) { - result := icmd.RunCommand(GomplateBin, "-i", `{{ net.LookupIP "localhost" }}`) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "127.0.0.1"}) + inOutTest(c, `{{ net.LookupIP "localhost" }}`, "127.0.0.1") } diff --git a/internal/tests/integration/regexp_test.go b/internal/tests/integration/regexp_test.go index d5cd9ff3..074959ce 100644 --- a/internal/tests/integration/regexp_test.go +++ b/internal/tests/integration/regexp_test.go @@ -4,8 +4,6 @@ package integration import ( . "gopkg.in/check.v1" - - "gotest.tools/v3/icmd" ) type RegexpSuite struct{} @@ -13,13 +11,9 @@ type RegexpSuite struct{} var _ = Suite(&RegexpSuite{}) func (s *RegexpSuite) TestReplace(c *C) { - result := icmd.RunCommand(GomplateBin, "-i", - `{{ "1.2.3-59" | regexp.Replace "-([0-9]*)" ".$1" }}`) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: "1.2.3.59"}) + inOutTest(c, `{{ "1.2.3-59" | regexp.Replace "-([0-9]*)" ".$1" }}`, "1.2.3.59") } func (s *RegexpSuite) TestQuoteMeta(c *C) { - result := icmd.RunCommand(GomplateBin, "-i", - "{{ regexp.QuoteMeta `foo{(\\` }}") - result.Assert(c, icmd.Expected{ExitCode: 0, Out: `foo\{\(\\`}) + inOutTest(c, "{{ regexp.QuoteMeta `foo{(\\` }}", `foo\{\(\\`) } diff --git a/internal/tests/integration/strings_test.go b/internal/tests/integration/strings_test.go index 951ff81c..87cb998c 100644 --- a/internal/tests/integration/strings_test.go +++ b/internal/tests/integration/strings_test.go @@ -32,11 +32,9 @@ func (s *StringsSuite) TestIndent(c *C) { } func (s *StringsSuite) TestRepeat(c *C) { - result := icmd.RunCommand(GomplateBin, "-i", - `ba{{ strings.Repeat 2 "na" }}`) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: `banana`}) + inOutTest(c, `ba{{ strings.Repeat 2 "na" }}`, `banana`) - result = icmd.RunCommand(GomplateBin, "-i", + result := icmd.RunCommand(GomplateBin, "-i", `ba{{ strings.Repeat 9223372036854775807 "na" }}`) result.Assert(c, icmd.Expected{ExitCode: 1, Err: `too long: causes overflow`}) @@ -46,19 +44,15 @@ func (s *StringsSuite) TestRepeat(c *C) { } func (s *StringsSuite) TestSlug(c *C) { - result := icmd.RunCommand(GomplateBin, "-i", - `{{ strings.Slug "Hellö, Wôrld! Free @ last..." }}`) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: `hello-world-free-at-last`}) + inOutTest(c, `{{ strings.Slug "Hellö, Wôrld! Free @ last..." }}`, `hello-world-free-at-last`) } func (s *StringsSuite) TestCaseFuncs(c *C) { - result := icmd.RunCommand(GomplateBin, "-i", - `{{ strings.CamelCase "Hellö, Wôrld! Free @ last..." }} + inOutTest(c, `{{ strings.CamelCase "Hellö, Wôrld! Free @ last..." }} {{ strings.SnakeCase "Hellö, Wôrld! Free @ last..." }} -{{ strings.KebabCase "Hellö, Wôrld! Free @ last..." }}`) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: `HellöWôrldFreeLast +{{ strings.KebabCase "Hellö, Wôrld! Free @ last..." }}`, `HellöWôrldFreeLast Hellö_wôrld_free_last -Hellö-wôrld-free-last`}) +Hellö-wôrld-free-last`) } func (s *StringsSuite) TestWordWrap(c *C) { diff --git a/internal/tests/integration/tmpl_test.go b/internal/tests/integration/tmpl_test.go index 93e27148..33f2ec9a 100644 --- a/internal/tests/integration/tmpl_test.go +++ b/internal/tests/integration/tmpl_test.go @@ -59,10 +59,7 @@ func (s *TmplSuite) TestExec(c *C) { )) result.Assert(c, icmd.Expected{ExitCode: 1, Err: `template \"Nope\" not defined`}) - result = icmd.RunCmd(icmd.Command(GomplateBin, - "-i", `{{define "T1"}}hello world{{end}}{{ tmpl.Exec "T1" | strings.ToUpper }}`, - )) - result.Assert(c, icmd.Expected{ExitCode: 0, Out: `HELLO WORLD`}) + inOutTest(c, `{{define "T1"}}hello world{{end}}{{ tmpl.Exec "T1" | strings.ToUpper }}`, `HELLO WORLD`) result = icmd.RunCmd(icmd.Command(GomplateBin, "-c", "in=stdin:///in.json", |
