From 47a8f1e2a4195874ed55949bfaea13ef42c0c47e Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Sun, 26 Nov 2023 10:31:15 -0600 Subject: [PATCH] move test stuff to internal/testdata --- cmd/fetch_test.go | 18 +-- cmd/get_test.go | 107 ++++++------------ cmd/set_test.go | 42 ++++--- internal/op-client/cli_test.go | 12 +- .../testdata/bad-test.yaml | 0 .../testdata/deeply-nested.test.yaml | 0 internal/{testing => testdata}/fixtures.go | 45 +++++++- .../mock => testdata/opconnect}/opconnect.go | 2 +- test.yaml => internal/testdata/test.yaml | 0 internal/vault/backend_test.go | 10 +- internal/vault/config_test.go | 4 +- internal/vault/helper_test.go | 6 +- internal/vault/tree_test.go | 12 +- 13 files changed, 131 insertions(+), 127 deletions(-) rename bad-test.yaml => internal/testdata/bad-test.yaml (100%) rename deeply-nested.test.yaml => internal/testdata/deeply-nested.test.yaml (100%) rename internal/{testing => testdata}/fixtures.go (84%) rename internal/{op-client/mock => testdata/opconnect}/opconnect.go (99%) rename test.yaml => internal/testdata/test.yaml (100%) diff --git a/cmd/fetch_test.go b/cmd/fetch_test.go index 7e03862..93b87d2 100644 --- a/cmd/fetch_test.go +++ b/cmd/fetch_test.go @@ -8,16 +8,17 @@ import ( "testing" . "git.rob.mx/nidito/joao/cmd" - "git.rob.mx/nidito/joao/internal/op-client/mock" - fixtures "git.rob.mx/nidito/joao/internal/testing" + "git.rob.mx/nidito/joao/internal/testdata" + "git.rob.mx/nidito/joao/internal/testdata/opconnect" "github.com/1Password/connect-sdk-go/onepassword" "github.com/spf13/cobra" ) func TestFetch(t *testing.T) { - fixtures.EnableDebugLogging() - mockOPConnect(t) - cfg := fixtures.NewTestConfig("some:test") + testdata.EnableDebugLogging() + testdata.MockOPConnect(t) + root := testdata.FromProjectRoot() + cfg := testdata.NewTestConfig("some:test") cfg.Sections = append(cfg.Sections, &onepassword.ItemSection{ID: "o", Label: "o"}, &onepassword.ItemSection{ID: "e-fez-tambem", Label: "e-fez-tambem"}, @@ -52,8 +53,7 @@ func TestFetch(t *testing.T) { Value: "quém!", }) - mock.Add(cfg) - root := fromProjectRoot() + opconnect.Add(cfg) out := &bytes.Buffer{} cmd := &cobra.Command{} cmd.Flags().Bool("dry-run", true, "") @@ -62,13 +62,13 @@ func TestFetch(t *testing.T) { Fetch.SetBindings() Fetch.Cobra = cmd - err := Fetch.Run(cmd, []string{root + "/test.yaml"}) + err := Fetch.Run(cmd, []string{testdata.YAML("test")}) if err != nil { t.Fatalf("could not get: %s", err) } - expected := `--- /Users/roberto/src/joao/test.yaml + expected := `--- ` + root + `/testdata/test.yaml +++ op://example/some:test @@ -1,4 +1,8 @@ bool: false diff --git a/cmd/get_test.go b/cmd/get_test.go index f7d26f6..f572f42 100644 --- a/cmd/get_test.go +++ b/cmd/get_test.go @@ -6,49 +6,24 @@ import ( "bytes" "encoding/json" "os" - "path" - "runtime" "strings" "testing" . "git.rob.mx/nidito/joao/cmd" - opclient "git.rob.mx/nidito/joao/internal/op-client" - "git.rob.mx/nidito/joao/internal/op-client/mock" - fixtures "git.rob.mx/nidito/joao/internal/testing" - "github.com/1Password/connect-sdk-go/connect" + "git.rob.mx/nidito/joao/internal/testdata" + "git.rob.mx/nidito/joao/internal/testdata/opconnect" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) -func mockOPConnect(t *testing.T) { - t.Helper() - opclient.ConnectClientFactory = func(host, token, userAgent string) connect.Client { - return &mock.Client{} - } - client := opclient.NewConnect("", "") - opclient.Use(client) - mock.Clear() -} - -func fromProjectRoot() string { - _, filename, _, _ := runtime.Caller(0) - dir := path.Join(path.Dir(filename), "../") - if err := os.Chdir(dir); err != nil { - panic(err) - } - wd, _ := os.Getwd() - return wd -} - func TestGetBadYAML(t *testing.T) { - root := fromProjectRoot() Get.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - err := Get.Run(cmd, []string{root + "/bad-test.yaml", "."}) + err := Get.Run(cmd, []string{testdata.YAML("bad-test"), "."}) if err == nil { t.Fatalf("Did not throw on bad path: %s", out.String()) } @@ -60,7 +35,7 @@ func TestGetBadYAML(t *testing.T) { } func TestGetBadPath(t *testing.T) { - root := fromProjectRoot() + root := testdata.FromProjectRoot() Get.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -80,7 +55,6 @@ func TestGetBadPath(t *testing.T) { } func TestGetNormal(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -88,13 +62,13 @@ func TestGetNormal(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - err := Get.Run(cmd, []string{root + "/test.yaml", "."}) + err := Get.Run(cmd, []string{testdata.YAML("test"), "."}) if err != nil { t.Fatalf("could not get: %s", err) } - expected, err := os.ReadFile(root + "/test.yaml") + expected, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file: %s", err) } @@ -105,7 +79,6 @@ func TestGetNormal(t *testing.T) { } func TestGetRedacted(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -113,14 +86,14 @@ func TestGetRedacted(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/test.yaml", ".", "--redacted"} - err := Get.Run(cmd, []string{root + "/test.yaml", "."}) + os.Args = []string{testdata.YAML("test"), ".", "--redacted"} + err := Get.Run(cmd, []string{testdata.YAML("test"), "."}) if err != nil { t.Fatalf("could not get: %s", err) } - expected, err := os.ReadFile(root + "/test.yaml") + expected, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file: %s", err) } @@ -131,7 +104,6 @@ func TestGetRedacted(t *testing.T) { } func TestGetPath(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -140,7 +112,7 @@ func TestGetPath(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/test.yaml", "nested.secret"} + os.Args = []string{testdata.YAML("test"), "nested.secret"} err := Get.Run(cmd, os.Args) if err != nil { @@ -155,8 +127,8 @@ func TestGetPath(t *testing.T) { out = bytes.Buffer{} cmd.SetOut(&out) cmd.SetErr(&out) - os.Args = []string{root + "/test.yaml", "nested", "--output", "diff-yaml"} - err = Get.Run(cmd, []string{root + "/test.yaml", "nested"}) + os.Args = []string{testdata.YAML("test"), "nested", "--output", "diff-yaml"} + err = Get.Run(cmd, []string{testdata.YAML("test"), "nested"}) if err != nil { t.Fatalf("could not get: %s", err) @@ -178,7 +150,6 @@ string: quem` } func TestGetPathCollection(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -187,8 +158,8 @@ func TestGetPathCollection(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/test.yaml", "nested", "--output", "yaml"} - err := Get.Run(cmd, []string{root + "/test.yaml", "nested"}) + os.Args = []string{testdata.YAML("test"), "nested", "--output", "yaml"} + err := Get.Run(cmd, []string{testdata.YAML("test"), "nested"}) if err != nil { t.Fatalf("could not get: %s", err) @@ -210,7 +181,6 @@ string: quem` } func TestGetDiff(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -219,8 +189,8 @@ func TestGetDiff(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/test.yaml", ".", "--output", "diff-yaml"} - err := Get.Run(cmd, []string{root + "/test.yaml", "."}) + os.Args = []string{testdata.YAML("test"), ".", "--output", "diff-yaml"} + err := Get.Run(cmd, []string{testdata.YAML("test"), "."}) if err != nil { t.Fatalf("could not get: %s", err) @@ -254,7 +224,6 @@ string: pato` } func TestGetJSON(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -263,8 +232,8 @@ func TestGetJSON(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/test.yaml", ".", "--output", "json"} - err := Get.Run(cmd, []string{root + "/test.yaml", "."}) + os.Args = []string{testdata.YAML("test"), ".", "--output", "json"} + err := Get.Run(cmd, []string{testdata.YAML("test"), "."}) if err != nil { t.Fatalf("could not get: %s", err) @@ -278,7 +247,6 @@ func TestGetJSON(t *testing.T) { } func TestGetDeepJSON(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -287,8 +255,9 @@ func TestGetDeepJSON(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/deeply-nested.test.yaml", ".", "--output", "json"} - err := Get.Run(cmd, []string{root + "/deeply-nested.test.yaml", "."}) + file := testdata.YAML("deeply-nested.test") + os.Args = []string{file, ".", "--output", "json"} + err := Get.Run(cmd, []string{file, "."}) if err != nil { t.Fatalf("could not get: %s", err) @@ -302,7 +271,6 @@ func TestGetDeepJSON(t *testing.T) { } func TestGetJSONPathScalar(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -311,8 +279,9 @@ func TestGetJSONPathScalar(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/test.yaml", "nested.secret", "--output", "json"} - err := Get.Run(cmd, []string{root + "/test.yaml", "nested.secret"}) + file := testdata.YAML("test") + os.Args = []string{file, "nested.secret", "--output", "json"} + err := Get.Run(cmd, []string{file, "nested.secret"}) if err != nil { t.Fatalf("could not get: %s", err) @@ -326,7 +295,6 @@ func TestGetJSONPathScalar(t *testing.T) { } func TestGetJSONPathCollection(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -335,8 +303,9 @@ func TestGetJSONPathCollection(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/test.yaml", "nested", "--output", "json"} - err := Get.Run(cmd, []string{root + "/test.yaml", "nested"}) + file := testdata.YAML("test") + os.Args = []string{file, "nested", "--output", "json"} + err := Get.Run(cmd, []string{file, "nested"}) if err != nil { t.Fatalf("could not get: %s", err) @@ -350,7 +319,6 @@ func TestGetJSONPathCollection(t *testing.T) { } func TestGetJSONRedacted(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -359,8 +327,9 @@ func TestGetJSONRedacted(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/test.yaml", ".", "--output", "json", "--redacted"} - err := Get.Run(cmd, []string{root + "/test.yaml", "."}) + file := testdata.YAML("test") + os.Args = []string{file, ".", "--output", "json", "--redacted"} + err := Get.Run(cmd, []string{file, "."}) if err != nil { t.Fatalf("could not get: %s", err) @@ -374,7 +343,6 @@ func TestGetJSONRedacted(t *testing.T) { } func TestGetJSONOP(t *testing.T) { - root := fromProjectRoot() out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -382,14 +350,14 @@ func TestGetJSONOP(t *testing.T) { cmd.SetOut(&out) cmd.SetErr(&out) Get.Cobra = cmd - os.Args = []string{root + "/test.yaml", ".", "--output", "op"} - err := Get.Run(cmd, []string{root + "/test.yaml", "."}) + os.Args = []string{testdata.YAML("test"), ".", "--output", "op"} + err := Get.Run(cmd, []string{testdata.YAML("test"), "."}) if err != nil { t.Fatalf("could not get: %s", err) } - cfg := fixtures.NewTestConfig("some:test") + cfg := testdata.NewTestConfig("some:test") id := cfg.ID cfg.ID = "" defer func() { cfg.ID = id }() @@ -404,9 +372,8 @@ func TestGetJSONOP(t *testing.T) { } func TestGetRemote(t *testing.T) { - mockOPConnect(t) - mock.Add(fixtures.NewTestConfig("some:test")) - root := fromProjectRoot() + testdata.MockOPConnect(t) + opconnect.Add(testdata.NewTestConfig("some:test")) out := bytes.Buffer{} Get.SetBindings() cmd := &cobra.Command{} @@ -417,8 +384,8 @@ func TestGetRemote(t *testing.T) { cmd.SetErr(&out) Get.Cobra = cmd logrus.SetLevel(logrus.DebugLevel) - os.Args = []string{root + "/test.yaml", "."} - err := Get.Run(cmd, []string{root + "/test.yaml", "."}) + os.Args = []string{testdata.YAML("test"), "."} + err := Get.Run(cmd, []string{testdata.YAML("test"), "."}) if err != nil { t.Fatalf("could not get: %s", err) diff --git a/cmd/set_test.go b/cmd/set_test.go index 010ea3c..5fad5fe 100644 --- a/cmd/set_test.go +++ b/cmd/set_test.go @@ -11,7 +11,7 @@ import ( "testing" . "git.rob.mx/nidito/joao/cmd" - "github.com/sirupsen/logrus" + "git.rob.mx/nidito/joao/internal/testdata" "github.com/spf13/cobra" ) @@ -24,7 +24,7 @@ func tempTestYaml(root, name string, data []byte) (string, func(), error) { } func TestSet(t *testing.T) { - root := fromProjectRoot() + root := testdata.TempDir(t, "test-set") Set.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -38,9 +38,9 @@ func TestSet(t *testing.T) { cmd.Flags().Bool("delete", false, "") cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("flush", false, "") - original, err := os.ReadFile(root + "/test.yaml") + original, err := os.ReadFile(testdata.YAML("test")) if err != nil { - t.Fatalf("could not read file") + t.Fatalf("could not read test file") } path, cleanup, err := tempTestYaml(root, "set-plain", original) @@ -73,7 +73,7 @@ string: |- } func TestSetSecret(t *testing.T) { - root := fromProjectRoot() + root := testdata.TempDir(t, "test-set-secret") Set.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -87,7 +87,7 @@ func TestSetSecret(t *testing.T) { cmd.Flags().Bool("delete", false, "") cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("flush", false, "") - original, err := os.ReadFile(root + "/test.yaml") + original, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file") } @@ -119,7 +119,7 @@ func TestSetSecret(t *testing.T) { } func TestSetFromFile(t *testing.T) { - root := fromProjectRoot() + root := testdata.TempDir(t, "test-set-from-file") Set.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -130,7 +130,7 @@ func TestSetFromFile(t *testing.T) { cmd.Flags().Bool("delete", false, "") cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("flush", false, "") - original, err := os.ReadFile(root + "/test.yaml") + original, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file") } @@ -168,7 +168,7 @@ func TestSetFromFile(t *testing.T) { } func TestSetNew(t *testing.T) { - root := fromProjectRoot() + root := testdata.TempDir(t, "test-set-new") Set.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -183,7 +183,7 @@ func TestSetNew(t *testing.T) { cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("flush", false, "") cmd.Flags().StringP("input", "i", "/dev/stdin", "") - original, err := os.ReadFile(root + "/test.yaml") + original, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file") } @@ -219,7 +219,7 @@ quarteto: |- } func TestSetNested(t *testing.T) { - root := fromProjectRoot() + root := testdata.TempDir(t, "test-set-nested") Set.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -234,7 +234,7 @@ func TestSetNested(t *testing.T) { cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("flush", false, "") cmd.Flags().StringP("input", "i", "/dev/stdin", "") - original, err := os.ReadFile(root + "/test.yaml") + original, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file") } @@ -267,7 +267,7 @@ func TestSetNested(t *testing.T) { } func TestSetJSON(t *testing.T) { - root := fromProjectRoot() + root := testdata.TempDir(t, "test-set-json") Set.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -282,7 +282,7 @@ func TestSetJSON(t *testing.T) { cmd.Flags().Bool("json", true, "") cmd.Flags().Bool("flush", false, "") cmd.Flags().StringP("input", "i", "/dev/stdin", "") - original, err := os.ReadFile(root + "/test.yaml") + original, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file") } @@ -317,9 +317,7 @@ na-beira-da-lagoa: } func TestSetList(t *testing.T) { - logrus.SetLevel(logrus.DebugLevel) - - root := fromProjectRoot() + root := testdata.TempDir(t, "test-set-list") Set.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -334,7 +332,7 @@ func TestSetList(t *testing.T) { cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("flush", false, "") cmd.Flags().StringP("input", "i", "/dev/stdin", "") - original, err := os.ReadFile(root + "/test.yaml") + original, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file") } @@ -368,7 +366,7 @@ asdf: } func TestDelete(t *testing.T) { - root := fromProjectRoot() + root := testdata.TempDir(t, "test-delete") Set.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -380,7 +378,7 @@ func TestDelete(t *testing.T) { cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("flush", false, "") cmd.Flags().StringP("input", "i", "/dev/stdin", "") - original, err := os.ReadFile(root + "/test.yaml") + original, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file") } @@ -414,7 +412,7 @@ string: pato } func TestDeleteNested(t *testing.T) { - root := fromProjectRoot() + root := testdata.TempDir(t, "test-delete-nested") Set.SetBindings() out := bytes.Buffer{} cmd := &cobra.Command{} @@ -426,7 +424,7 @@ func TestDeleteNested(t *testing.T) { cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("flush", false, "") cmd.Flags().StringP("input", "i", "/dev/stdin", "") - original, err := os.ReadFile(root + "/test.yaml") + original, err := os.ReadFile(testdata.YAML("test")) if err != nil { t.Fatalf("could not read file") } diff --git a/internal/op-client/cli_test.go b/internal/op-client/cli_test.go index 2f2d8b8..39b4046 100644 --- a/internal/op-client/cli_test.go +++ b/internal/op-client/cli_test.go @@ -8,7 +8,7 @@ import ( "testing" opclient "git.rob.mx/nidito/joao/internal/op-client" - fixtures "git.rob.mx/nidito/joao/internal/testing" + "git.rob.mx/nidito/joao/internal/testdata" "github.com/1Password/connect-sdk-go/onepassword" "github.com/sirupsen/logrus" ) @@ -76,9 +76,9 @@ func TestDeprecatedUpdate(t *testing.T) { return *bytes.NewBufferString("updated"), nil } - original := fieldify(fixtures.NewTestConfig("some:test")) + original := fieldify(testdata.NewTestConfig("some:test")) - updated := fieldify(fixtures.NewTestConfig(original.Title)) + updated := fieldify(testdata.NewTestConfig(original.Title)) updated.Fields[2].Value = "42" updated.Fields[4].Value = "42" @@ -104,7 +104,7 @@ func TestDeprecatedUpdate(t *testing.T) { } func TestUpdate(t *testing.T) { - fixtures.EnableDebugLogging() + testdata.EnableDebugLogging() client := &opclient.CLI{} queriedVersion := false var calledArgs []string @@ -123,9 +123,9 @@ func TestUpdate(t *testing.T) { return *bytes.NewBufferString("updated"), nil } - original := fieldify(fixtures.NewTestConfig("some:test")) + original := fieldify(testdata.NewTestConfig("some:test")) - updated := fieldify(fixtures.NewTestConfig(original.Title)) + updated := fieldify(testdata.NewTestConfig(original.Title)) updated.Fields[2].Value = "42" updated.Fields[4].Value = "42" diff --git a/bad-test.yaml b/internal/testdata/bad-test.yaml similarity index 100% rename from bad-test.yaml rename to internal/testdata/bad-test.yaml diff --git a/deeply-nested.test.yaml b/internal/testdata/deeply-nested.test.yaml similarity index 100% rename from deeply-nested.test.yaml rename to internal/testdata/deeply-nested.test.yaml diff --git a/internal/testing/fixtures.go b/internal/testdata/fixtures.go similarity index 84% rename from internal/testing/fixtures.go rename to internal/testdata/fixtures.go index 5e51c4a..e5f29a9 100644 --- a/internal/testing/fixtures.go +++ b/internal/testdata/fixtures.go @@ -1,10 +1,51 @@ -package fixtures +package testdata import ( + "fmt" + "os" + "path" + "runtime" + "testing" + + opclient "git.rob.mx/nidito/joao/internal/op-client" + "git.rob.mx/nidito/joao/internal/testdata/opconnect" + "github.com/1Password/connect-sdk-go/connect" "github.com/1Password/connect-sdk-go/onepassword" "github.com/sirupsen/logrus" ) +func MockOPConnect(t *testing.T) { + t.Helper() + opclient.ConnectClientFactory = func(host, token, userAgent string) connect.Client { + return &opconnect.Client{} + } + client := opclient.NewConnect("", "") + opclient.Use(client) + opconnect.Clear() +} + +func FromProjectRoot() string { + _, filename, _, _ := runtime.Caller(0) + dir := path.Join(path.Dir(filename), "../") + if err := os.Chdir(dir); err != nil { + panic(err) + } + wd, _ := os.Getwd() + return wd +} + +func TempDir(t *testing.T, name string) string { + newDir, err := os.MkdirTemp("", name+"-*") + if err != nil { + t.Fatalf("could not create tempdir") + } + return newDir +} + +func YAML(name string) string { + return path.Join(FromProjectRoot(), "testdata", fmt.Sprintf("%s.yaml", name)) +} + func EnableDebugLogging() { logrus.SetLevel(logrus.DebugLevel) } @@ -208,5 +249,3 @@ func NewTestConfig(title string) *onepassword.Item { }, } } - -var TestConfig = NewTestConfig("some:test") diff --git a/internal/op-client/mock/opconnect.go b/internal/testdata/opconnect/opconnect.go similarity index 99% rename from internal/op-client/mock/opconnect.go rename to internal/testdata/opconnect/opconnect.go index 4e025f8..986bf40 100644 --- a/internal/op-client/mock/opconnect.go +++ b/internal/testdata/opconnect/opconnect.go @@ -1,6 +1,6 @@ // Copyright © 2022 Roberto Hidalgo // SPDX-License-Identifier: Apache-2.0 -package mock +package opconnect import ( "fmt" diff --git a/test.yaml b/internal/testdata/test.yaml similarity index 100% rename from test.yaml rename to internal/testdata/test.yaml diff --git a/internal/vault/backend_test.go b/internal/vault/backend_test.go index 2b7e117..b277a27 100644 --- a/internal/vault/backend_test.go +++ b/internal/vault/backend_test.go @@ -6,7 +6,7 @@ import ( "context" "testing" - "git.rob.mx/nidito/joao/internal/op-client/mock" + "git.rob.mx/nidito/joao/internal/testdata/opconnect" "git.rob.mx/nidito/joao/internal/vault" "git.rob.mx/nidito/joao/internal/vault/middleware" "github.com/1Password/connect-sdk-go/connect" @@ -15,7 +15,7 @@ import ( func init() { vault.ConnectClientFactory = func(s logical.Storage) (connect.Client, error) { - return &mock.Client{}, nil + return &opconnect.Client{}, nil } } @@ -32,15 +32,15 @@ func TestConfiguredBackend(t *testing.T) { t.Fatalf("Unexpected error with config set: %s => %v", err, resp) } - if resp.Data["token"] != mock.Token { + if resp.Data["token"] != opconnect.Token { t.Errorf("Found unknown token: %s", resp.Data["token"]) } - if resp.Data["host"] != mock.Host { + if resp.Data["host"] != opconnect.Host { t.Errorf("Found unknown host: %s", resp.Data["host"]) } - if resp.Data["vault"] != mock.Vaults[0].ID { + if resp.Data["vault"] != opconnect.Vaults[0].ID { t.Errorf("Found unknown vault: %s", resp.Data["vault"]) } } diff --git a/internal/vault/config_test.go b/internal/vault/config_test.go index 4a0c02e..41259cd 100644 --- a/internal/vault/config_test.go +++ b/internal/vault/config_test.go @@ -6,7 +6,7 @@ import ( "context" "testing" - "git.rob.mx/nidito/joao/internal/op-client/mock" + "git.rob.mx/nidito/joao/internal/testdata/opconnect" "github.com/hashicorp/vault/sdk/logical" ) @@ -49,7 +49,7 @@ func TestConfigDefault(t *testing.T) { t.Fatal("got no response, expected something!") } - mapsEqual(t, resp.Data, map[string]any{"host": mock.Host, "token": mock.Token, "vault": mock.Vaults[0].ID}) + mapsEqual(t, resp.Data, map[string]any{"host": opconnect.Host, "token": opconnect.Token, "vault": opconnect.Vaults[0].ID}) } func TestConfigUpdate(t *testing.T) { diff --git a/internal/vault/helper_test.go b/internal/vault/helper_test.go index 2b4c99c..4f9af70 100644 --- a/internal/vault/helper_test.go +++ b/internal/vault/helper_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "git.rob.mx/nidito/joao/internal/op-client/mock" + "git.rob.mx/nidito/joao/internal/testdata/opconnect" "git.rob.mx/nidito/joao/internal/vault" "git.rob.mx/nidito/joao/internal/vault/middleware" "github.com/1Password/connect-sdk-go/connect" @@ -44,7 +44,7 @@ func getBackend(tb testing.TB) (logical.Backend, logical.Storage) { cfg := testConfig() ctx := context.Background() - data, err := json.Marshal(map[string]string{"host": mock.Host, "token": mock.Token, "vault": mock.Vaults[0].ID}) + data, err := json.Marshal(map[string]string{"host": opconnect.Host, "token": opconnect.Token, "vault": opconnect.Vaults[0].ID}) if err != nil { tb.Fatalf("Could not serialize config for client: %s", err) } @@ -77,6 +77,6 @@ func getUnconfiguredBackend(tb testing.TB) (logical.Backend, logical.Storage) { func setOnePassswordConnectMocks() { vault.ConnectClientFactory = func(s logical.Storage) (connect.Client, error) { - return &mock.Client{}, nil + return &opconnect.Client{}, nil } } diff --git a/internal/vault/tree_test.go b/internal/vault/tree_test.go index ce925d0..5fd7bb5 100644 --- a/internal/vault/tree_test.go +++ b/internal/vault/tree_test.go @@ -8,7 +8,7 @@ import ( "fmt" "testing" - "git.rob.mx/nidito/joao/internal/op-client/mock" + "git.rob.mx/nidito/joao/internal/testdata/opconnect" "github.com/1Password/connect-sdk-go/onepassword" "github.com/hashicorp/vault/sdk/logical" ) @@ -20,8 +20,8 @@ func getTestBackendWithConfig(t *testing.T) (logical.Backend, logical.Storage) { func TestReadEntry(t *testing.T) { b, reqStorage := getTestBackendWithConfig(t) - mock.Clear() - item := mock.Add(generateConfigItem("service:test")) + opconnect.Clear() + item := opconnect.Add(generateConfigItem("service:test")) expected := map[string]any{ "boolean": false, "integer": 42, @@ -89,8 +89,8 @@ func TestReadEntry(t *testing.T) { func TestListEntries(t *testing.T) { b, reqStorage := getTestBackendWithConfig(t) - mock.Clear() - item := mock.Add(generateConfigItem("service:test")) + opconnect.Clear() + item := opconnect.Add(generateConfigItem("service:test")) expected := map[string]any{ "keys": []string{ @@ -174,7 +174,7 @@ func generateConfigItem(title string) *onepassword.Item { Category: "password", Title: title, Vault: onepassword.ItemVault{ - ID: mock.Vaults[0].ID, + ID: opconnect.Vaults[0].ID, }, Fields: []*onepassword.ItemField{ {