Skip to content

Commit

Permalink
chore: refactored test to be easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainStandby committed Dec 16, 2022
1 parent 2e7aa53 commit 1397fcc
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions driver/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/ghodss/yaml"
"github.com/spf13/cobra"

"github.com/ory/x/watcherx"

"github.com/ory/kratos/internal/testhelpers"

"github.com/ory/x/configx"
Expand Down Expand Up @@ -942,32 +940,31 @@ func TestIdentitySchemaValidation(t *testing.T) {
assert.NoError(t, tmpFile.Sync())
}

testWatch := func(t *testing.T, ctx context.Context, cmd *cobra.Command, i *configFile) (*config.Config, *test.Hook, *os.File, *configFile, chan bool) {
c := make(chan bool, 1)
testWatch := func(t *testing.T, ctx context.Context, cmd *cobra.Command, identity *configFile) (*config.Config, *test.Hook, func([]map[string]string)) {
tdir := t.TempDir()
assert.NoError(t,
os.MkdirAll(tdir, // DO NOT CHANGE THIS: https://github.com/fsnotify/fsnotify/issues/340
os.ModePerm))
configFileName := randx.MustString(8, randx.Alpha)
tmpConfig, err := os.Create(filepath.Join(tdir, configFileName+".config.yaml"))
assert.NoError(t, err)
t.Cleanup(func() { tmpConfig.Close() })

marshalAndWrite(t, ctx, tmpConfig, i)
marshalAndWrite(t, ctx, tmpConfig, identity)

l := logrusx.New("kratos-"+tmpConfig.Name(), "test")
hook := test.NewLocal(l.Logger)

conf, err := config.New(ctx, l, os.Stderr,
configx.WithConfigFiles(tmpConfig.Name()),
configx.AttachWatcher(func(event watcherx.Event, err error) {
c <- true
}))
conf, err := config.New(ctx, l, os.Stderr, configx.WithConfigFiles(tmpConfig.Name()))
assert.NoError(t, err)

// clean the hooks since it will throw an event on first boot
hook.Reset()

return conf, hook, tmpConfig, i, c
return conf, hook, func(schemas []map[string]string) {
identity.Identity.Schemas = schemas
marshalAndWrite(t, ctx, tmpConfig, identity)
}
}

t.Run("case=skip invalid schema validation", func(t *testing.T) {
Expand Down Expand Up @@ -1023,30 +1020,19 @@ func TestIdentitySchemaValidation(t *testing.T) {

invalidIdentity := setup(t, "stub/.identity.invalid.json")

for _, i := range identities {
t.Run("test=identity file "+i.identityFileName, func(t *testing.T) {
for _, identity := range identities {
t.Run("test=identity file "+identity.identityFileName, func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
t.Cleanup(cancel)

_, hook, tmpConfig, i, c := testWatch(t, ctx, &cobra.Command{}, i)
// Change the identity config to an invalid file
i.Identity.Schemas = invalidIdentity.Identity.Schemas

t.Cleanup(func() {
cancel()
tmpConfig.Close()
})
_, hook, writeSchema := testWatch(t, ctx, &cobra.Command{}, identity)

var wg sync.WaitGroup
wg.Add(1)
go func(t *testing.T, ctx context.Context, tmpFile *os.File, identity *configFile) {
defer wg.Done()
marshalAndWrite(t, ctx, tmpConfig, i)
}(t, ctx, tmpConfig, i)

go func() {
for range c {
// If we don't drain c the sender will block
}
defer wg.Done()
// Change the identity config to an invalid file
writeSchema(invalidIdentity.Identity.Schemas)
}()

// There are a bunch of log messages beeing logged. We are looking for a specific one.
Expand Down

0 comments on commit 1397fcc

Please sign in to comment.