constellation/joinservice/internal/watcher/watcher_test.go
miampf f16ccf5679
rewrote packages
keyservice
joinservice
upgrade-agent
measurement-reader
debugd
disk-mapper

rewrote joinservice main

rewrote some unit tests

rewrote upgrade-agent + some grpc functions

rewrote measurement-reader

rewrote debugd

removed unused import

removed forgotten zap reference in measurements reader

rewrote disk-mapper + tests

rewrote packages

verify
disk-mapper
malicious join
bootstrapper
attestationconfigapi
versionapi
internal/cloud/azure
disk-mapper tests
image/upload/internal/cmd

rewrote verify (WIP with loglevel increase)

rewrote forgotten zap references in disk-mapper

rewrote malicious join

rewrote bootstrapper

rewrote parts of internal/

rewrote attestationconfigapi (WIP)

rewrote versionapi cli

rewrote internal/cloud/azure

rewrote disk-mapper tests (untested by me rn)

rewrote image/upload/internal/cmd

removed forgotten zap references in verify/cmd

rewrote packages

hack/oci-pin
hack/qemu-metadata-api
debugd/internal/debugd/deploy
hack/bazel-deps-mirror
cli/internal/cmd
cli-k8s-compatibility

rewrote hack/qemu-metadata-api/server

rewrote debugd/internal/debugd/deploy

rewrote hack/bazel-deps-mirror

rewrote rest of hack/qemu-metadata-api

rewrote forgotten zap references in joinservice server

rewrote cli/internal/cmd

rewrote cli-k8s-compatibility

rewrote packages

internal/staticupload
e2d/internal/upgrade
internal/constellation/helm
internal/attestation/aws/snp
internal/attestation/azure/trustedlaunch
joinservice/internal/certcache/amkds

some missed unit tests

rewrote e2e/internal/upgrade

rewrote internal/constellation/helm

internal/attestation/aws/snp

internal/attestation/azure/trustedlaunch

joinservice/internal/certcache/amkds

search and replace test logging over all left *_test.go
2024-02-08 13:14:14 +01:00

179 lines
3.7 KiB
Go

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package watcher
import (
"errors"
"log/slog"
"sync"
"testing"
"time"
"github.com/edgelesssys/constellation/v2/internal/logger"
"github.com/fsnotify/fsnotify"
"github.com/stretchr/testify/assert"
)
func TestWatcher(t *testing.T) {
someErr := errors.New("error")
testCases := map[string]struct {
updater *testUpdater
watcher *testWatcher
events []fsnotify.Event
watchErr error
wantAddCalls int
wantErr bool
}{
"success": {
updater: &testUpdater{},
watcher: &testWatcher{
events: make(chan fsnotify.Event, 1),
errors: make(chan error, 1),
},
events: []fsnotify.Event{
{Op: fsnotify.Write, Name: "test"},
{Op: fsnotify.Chmod, Name: "test"},
{Op: fsnotify.Create, Name: "test"},
{Op: fsnotify.Rename, Name: "test"},
},
wantAddCalls: 1,
},
"failing update does not interrupt execution": {
updater: &testUpdater{
err: someErr,
},
watcher: &testWatcher{
events: make(chan fsnotify.Event, 1),
errors: make(chan error, 1),
},
events: []fsnotify.Event{
{Op: fsnotify.Write, Name: "test"},
{Op: fsnotify.Write, Name: "test"},
},
wantAddCalls: 1,
},
"removed file gets re-added": {
updater: &testUpdater{},
watcher: &testWatcher{
events: make(chan fsnotify.Event, 1),
errors: make(chan error, 1),
},
events: []fsnotify.Event{
{Op: fsnotify.Write, Name: "test"},
{Op: fsnotify.Remove, Name: "test"},
},
wantAddCalls: 2,
},
"re-adding file fails": {
updater: &testUpdater{},
watcher: &testWatcher{
addErr: someErr,
events: make(chan fsnotify.Event, 1),
errors: make(chan error, 1),
},
events: []fsnotify.Event{{Op: fsnotify.Remove, Name: "test"}},
wantAddCalls: 1,
wantErr: true,
},
"add file fails": {
updater: &testUpdater{},
watcher: &testWatcher{
addErr: someErr,
events: make(chan fsnotify.Event, 1),
errors: make(chan error, 1),
},
wantAddCalls: 1,
wantErr: true,
},
"error during watch": {
updater: &testUpdater{},
watcher: &testWatcher{
events: make(chan fsnotify.Event, 1),
errors: make(chan error, 1),
},
wantAddCalls: 1,
watchErr: someErr,
wantErr: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
watcher := &FileWatcher{
log: slog.New(slog.NewTextHandler(logger.TestWriter{T: t}, nil)),
updater: tc.updater,
watcher: tc.watcher,
done: make(chan struct{}, 1),
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := watcher.Watch("test")
if tc.wantErr {
assert.Error(err)
return
}
assert.NoError(err)
}()
time.Sleep(15 * time.Millisecond)
for _, event := range tc.events {
tc.watcher.events <- event
}
if tc.watchErr != nil {
tc.watcher.errors <- tc.watchErr
}
close(tc.watcher.events)
assert.NoError(watcher.Close())
wg.Wait()
// check that the watchers Add method was called the expected number of times
assert.Equal(tc.wantAddCalls, tc.watcher.addCalled)
})
}
}
type testUpdater struct {
err error
}
func (u *testUpdater) Update() error {
return u.err
}
type testWatcher struct {
addCalled int
addErr error
closeErr error
events chan fsnotify.Event
errors chan error
}
func (w *testWatcher) Add(_ string) error {
w.addCalled++
return w.addErr
}
func (w *testWatcher) Close() error {
return w.closeErr
}
func (w *testWatcher) Events() <-chan fsnotify.Event {
return w.events
}
func (w *testWatcher) Errors() <-chan error {
return w.errors
}