Skip to content

Commit

Permalink
Merge branch 'master' into hf-add-nid-to-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainStandby committed Dec 16, 2022
2 parents f292991 + 4e5dcd0 commit 0ee8c8b
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions driver/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
Expand All @@ -26,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 @@ -941,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 @@ -1022,34 +1020,44 @@ 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) {
go func() {
defer wg.Done()
marshalAndWrite(t, ctx, tmpConfig, i)
}(t, ctx, tmpConfig, i)

select {
case <-ctx.Done():
panic("the test could not complete as the context timed out before the file watcher updated")
case <-c:
lastHook, err := hook.LastEntry().String()
assert.NoError(t, err)

assert.Contains(t, lastHook, "The changed identity schema configuration is invalid and could not be loaded.")
// 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.
timeout := time.After(time.Millisecond * 500)
outer:
for {
for _, v := range hook.AllEntries() {
s, err := v.String()
if err != nil {
t.Errorf("Unexpected Error: %s", err.Error())
continue
}

if strings.Contains(s, "The changed identity schema configuration is invalid and could not be loaded.") {
break outer
}
}

select {
case <-ctx.Done():
panic("the test could not complete as the context timed out before the file watcher updated")
case <-timeout:
t.Fatal("Expected log line was not encountered within specified timeout")
default: //nothing
}
}

wg.Wait()
Expand Down

0 comments on commit 0ee8c8b

Please sign in to comment.