diff --git a/driver/config/config_test.go b/driver/config/config_test.go index c0567ccf4677..b167b97e9c1b 100644 --- a/driver/config/config_test.go +++ b/driver/config/config_test.go @@ -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" @@ -942,8 +940,7 @@ 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 @@ -951,23 +948,23 @@ func TestIdentitySchemaValidation(t *testing.T) { 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) { @@ -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.