Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cramja committed Jun 28, 2021
1 parent ebf2e54 commit f4d8c1b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
5 changes: 2 additions & 3 deletions cmd/namespace/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package namespace

import (
"bytes"
"context"
"fmt"
"io/ioutil"

Expand Down Expand Up @@ -147,7 +146,7 @@ func validateConfigFile(cmd *cobra.Command, fn string) error {
switch t := ns.(type) {
case string:
logger := logrusx.New("cmd", "0")
cw, err := config.NewNamespaceWatcher(context.Background(), logger, t)
cw, err := config.NewNamespaceWatcher(cmd.Context(), logger, t)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Encountered error reading config: %+v\n", err)
return cmdx.FailSilently(cmd)
Expand Down Expand Up @@ -188,7 +187,7 @@ func validateConfigFile(cmd *cobra.Command, fn string) error {
}
return nil
default:
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "unknown type for key 'namespaces' in config file: %v\n", t)
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "unknown type %T for key 'namespaces' in config file: %v\n", t, t)
return cmdx.FailSilently(cmd)
}
} else {
Expand Down
31 changes: 16 additions & 15 deletions cmd/namespace/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package namespace
import (
"fmt"
"io/ioutil"
"path/filepath"
"testing"

"github.com/ory/x/cmdx"
Expand Down Expand Up @@ -50,13 +51,13 @@ func TestValidateConfigNamespaces(t *testing.T) {

t.Run("case=read valid config with embedded namespaces", func(t *testing.T) {
dir := t.TempDir()
fnyaml := dir + "/keto.yaml"
fnyaml := filepath.Join(dir, "keto.yaml")
require.NoError(t, ioutil.WriteFile(fnyaml, []byte(configEmbeddedYaml), fileMode))

fnjson := dir + "/keto.json"
fnjson := filepath.Join(dir, "keto.json")
require.NoError(t, ioutil.WriteFile(fnjson, []byte(configEmbeddedJson), fileMode))

fntoml := dir + "/keto.toml"
fntoml := filepath.Join(dir, "keto.toml")
require.NoError(t, ioutil.WriteFile(fntoml, []byte(configEmbeddedToml), fileMode))

stdOut := cmd.ExecNoErr(t, "validate", "-c", fnyaml+","+fnjson+","+fntoml)
Expand All @@ -66,9 +67,9 @@ func TestValidateConfigNamespaces(t *testing.T) {
t.Run("case=supports 3 namespace file formats", func(t *testing.T) {
dir := t.TempDir()
nsfiles := map[string]string{
dir + "/ns.yaml": "name: testns0\nid: 0",
dir + "/ns.json": "{\"name\": \"testns0\",\"id\": 0}",
dir + "/ns.toml": "name = \"testns0\"\nid = 0",
filepath.Join(dir, "ns.yaml"): "name: testns0\nid: 0",
filepath.Join(dir, "ns.json"): "{\"name\": \"testns0\",\"id\": 0}",
filepath.Join(dir, "ns.toml"): "name = \"testns0\"\nid = 0",
}
for fn, contents := range nsfiles {
require.NoError(t, ioutil.WriteFile(fn, []byte(contents), fileMode))
Expand All @@ -79,15 +80,15 @@ func TestValidateConfigNamespaces(t *testing.T) {
})

t.Run("case=unknown namespace format gives error", func(t *testing.T) {
fn := t.TempDir() + "/ns.txt"
fn := filepath.Join(t.TempDir(), "ns.txt")
require.NoError(t, ioutil.WriteFile(fn, []byte("name: ns\nid: 0"), fileMode))

stdOut := cmd.ExecExpectedErr(t, "validate", fn)
assert.Contains(t, stdOut, "Unable to infer file type")
})

t.Run("case=config passed as varg fails", func(t *testing.T) {
fn := t.TempDir() + "/keto.yaml"
fn := filepath.Join(t.TempDir(), "keto.yaml")
require.NoError(t, ioutil.WriteFile(fn, []byte(configEmbeddedYaml), fileMode))

// interprets config file as namespace file when `-c` flag is not passed
Expand All @@ -96,7 +97,7 @@ func TestValidateConfigNamespaces(t *testing.T) {
})

t.Run("case=read config with invalid embedded namespace", func(t *testing.T) {
fn := t.TempDir() + "/keto.yaml"
fn := filepath.Join(t.TempDir(), "keto.yaml")
require.NoError(t, ioutil.WriteFile(fn, []byte(`{"namespaces":[{"id":"x","name":"x"}]}`), fileMode))

stdOut := cmd.ExecExpectedErr(t, "validate", "--config", fn)
Expand All @@ -106,9 +107,9 @@ func TestValidateConfigNamespaces(t *testing.T) {

t.Run("case=read config with namespace as dir reference", func(t *testing.T) {
nsdir := t.TempDir()
fn := t.TempDir() + "/config.yaml"
require.NoError(t, ioutil.WriteFile(nsdir+"/ns0.yaml", []byte(nsyaml), fileMode))
require.NoError(t, ioutil.WriteFile(nsdir+"/ns1.json", []byte(nsjson), fileMode))
fn := filepath.Join(t.TempDir(), "config.yaml")
require.NoError(t, ioutil.WriteFile(filepath.Join(nsdir, "ns0.yaml"), []byte(nsyaml), fileMode))
require.NoError(t, ioutil.WriteFile(filepath.Join(nsdir, "ns1.json"), []byte(nsjson), fileMode))
require.NoError(t, ioutil.WriteFile(fn, []byte(fmt.Sprintf(configReference, nsdir)), fileMode))

cmd.PersistentArgs = []string{}
Expand All @@ -121,8 +122,8 @@ func TestValidateConfigNamespaces(t *testing.T) {

t.Run("case=read config with dir reference bad namespaces", func(t *testing.T) {
nsdir := t.TempDir()
fn := t.TempDir() + "/config.yaml"
require.NoError(t, ioutil.WriteFile(nsdir+"/ns0.yaml", []byte("name: badId\nid: foo"), fileMode))
fn := filepath.Join(t.TempDir(), "config.yaml")
require.NoError(t, ioutil.WriteFile(filepath.Join(nsdir, "ns0.yaml"), []byte("name: badId\nid: foo"), fileMode))
require.NoError(t, ioutil.WriteFile(fn, []byte(fmt.Sprintf(configReference, nsdir)), fileMode))

cmd.PersistentArgs = []string{}
Expand All @@ -136,7 +137,7 @@ func validateCommand() *cobra.Command {
Use: "keto",
Short: "Global and consistent permission and authorization server",
}
configx.RegisterConfigFlag(cmd.PersistentFlags(), []string{"/tmp/non-exist.yaml"})
configx.RegisterConfigFlag(cmd.PersistentFlags(), []string{})
cmd.AddCommand(NewValidateCmd())
return cmd
}
Expand Down

0 comments on commit f4d8c1b

Please sign in to comment.