From 613e3e21ec1b34b24c1f0ba8889d449b86916d40 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 12:10:40 +1100 Subject: [PATCH 01/17] test Signed-off-by: Nikita Skrynnik --- internal/prefixcollector/collector.go | 1 + .../collector_configmap_output_test.go | 295 ------------------ 2 files changed, 1 insertion(+), 295 deletions(-) diff --git a/internal/prefixcollector/collector.go b/internal/prefixcollector/collector.go index ff1cf17..da9759a 100644 --- a/internal/prefixcollector/collector.go +++ b/internal/prefixcollector/collector.go @@ -133,6 +133,7 @@ func (epc *ExcludedPrefixCollector) updateExcludedPrefixes(ctx context.Context) var prefixesV4 []string var prefixesV6 []string for _, p := range sourcePrefixes { + log.FromContext(ctx).Infof("Prefix: %v", p) prefix := strings.TrimSpace(p) ip, _, err := net.ParseCIDR(prefix) if err != nil { diff --git a/internal/prefixcollector/collector_configmap_output_test.go b/internal/prefixcollector/collector_configmap_output_test.go index 39b58ce..b26a8b5 100644 --- a/internal/prefixcollector/collector_configmap_output_test.go +++ b/internal/prefixcollector/collector_configmap_output_test.go @@ -21,25 +21,17 @@ package prefixcollector_test import ( "cmd-exclude-prefixes-k8s/internal/prefixcollector" "cmd-exclude-prefixes-k8s/internal/prefixcollector/prefixsource" - "cmd-exclude-prefixes-k8s/internal/utils" "context" - "fmt" "os" "path/filepath" "testing" - "time" - - "github.com/pkg/errors" "github.com/ghodss/yaml" "github.com/stretchr/testify/suite" - "go.uber.org/goleak" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" - k8stest "k8s.io/client-go/testing" ) const ( @@ -81,202 +73,6 @@ func (eps *ExcludedPrefixesSuite) TearDownTest() { eps.deleteConfigMap(context.Background(), configMapNamespace, userConfigMapName) } -func (eps *ExcludedPrefixesSuite) TestCollectorWithDummySources() { - defer goleak.VerifyNone(eps.T(), goleak.IgnoreCurrent()) - notifyChan := make(chan struct{}, 1) - ctx, cancel := context.WithCancel(prefixcollector.WithKubernetesInterface(context.Background(), eps.clientSet)) - defer cancel() - - sources := []prefixcollector.PrefixSource{ - newDummyPrefixSource( - []string{ - "127.0.0.1/16", - "127.0.2.1/16", - "168.92.0.1/24", - }, - ), - newDummyPrefixSource( - []string{ - "127.0.3.1/18", - "134.56.0.1/8", - "168.92.0.1/16", - }, - ), - } - - expectedResult := []string{ - "127.0.0.0/16", - "168.92.0.0/16", - "134.0.0.0/8", - } - - eps.testCollectorWithConfigmapOutput(ctx, notifyChan, expectedResult, sources) -} - -func (eps *ExcludedPrefixesSuite) TestConfigMapSource() { - defer goleak.VerifyNone(eps.T(), goleak.IgnoreCurrent()) - expectedResult := []string{ - "168.0.0.0/10", - "1.0.0.0/11", - } - notifyChan := make(chan struct{}, 1) - - ctx, cancel := context.WithCancel(prefixcollector.WithKubernetesInterface(context.Background(), eps.clientSet)) - defer cancel() - eps.createConfigMap(ctx, configMapNamespace, configMapPath) - - sources := []prefixcollector.PrefixSource{ - prefixsource.NewConfigMapPrefixSource( - ctx, - notifyChan, - userConfigMapName, - configMapNamespace, - userConfigMapKey, - ), - } - - eps.testCollectorWithConfigmapOutput(ctx, notifyChan, expectedResult, sources) -} - -func (eps *ExcludedPrefixesSuite) TestConfigMapSourceRefresh() { - defer goleak.VerifyNone(eps.T(), goleak.IgnoreCurrent()) - expectedResult := []string{ - "168.0.0.0/10", - "1.0.0.0/11", - } - notifyChan := make(chan struct{}, 1) - - ctx, cancel := context.WithCancel(prefixcollector.WithKubernetesInterface(context.Background(), eps.clientSet)) - defer cancel() - - clients := eps.clientSet.(*fake.Clientset) - countWatchers, stopAndDisableWatcher := interceptWatcher(clients) - - sources := []prefixcollector.PrefixSource{ - prefixsource.NewConfigMapPrefixSource( - ctx, - notifyChan, - userConfigMapName, - configMapNamespace, - userConfigMapKey, - ), - } - - eps.Eventually(func() bool { return countWatchers() == 1 }, time.Second, 10*time.Millisecond) - stopAndDisableWatcher() - eps.Eventually(func() bool { return countWatchers() == 2 }, time.Second, 10*time.Millisecond) - - eps.createConfigMap(ctx, configMapNamespace, configMapPath) - eps.testCollectorWithConfigmapOutput(ctx, notifyChan, expectedResult, sources) -} - -func (eps *ExcludedPrefixesSuite) TestKubeAdmConfigSource() { - defer goleak.VerifyNone(eps.T(), goleak.IgnoreCurrent()) - expectedResult := []string{ - "10.244.0.0/16", - "10.96.0.0/12", - } - - notifyChan := make(chan struct{}, 1) - ctx, cancel := context.WithCancel(prefixcollector.WithKubernetesInterface(context.Background(), eps.clientSet)) - defer cancel() - - eps.createConfigMap(ctx, prefixsource.KubeNamespace, kubeConfigMapPath) - - sources := []prefixcollector.PrefixSource{ - prefixsource.NewKubeAdmPrefixSource(ctx, notifyChan), - } - - eps.testCollectorWithConfigmapOutput(ctx, notifyChan, expectedResult, sources) -} - -func (eps *ExcludedPrefixesSuite) TestKubeAdmConfigSourceRefresh() { - defer goleak.VerifyNone(eps.T(), goleak.IgnoreCurrent()) - expectedResult := []string{ - "10.244.0.0/16", - "10.96.0.0/12", - } - - notifyChan := make(chan struct{}, 1) - ctx, cancel := context.WithCancel(prefixcollector.WithKubernetesInterface(context.Background(), eps.clientSet)) - defer cancel() - - clients := eps.clientSet.(*fake.Clientset) - countWatchers, stopAndDisableWatcher := interceptWatcher(clients) - - sources := []prefixcollector.PrefixSource{ - prefixsource.NewKubeAdmPrefixSource(ctx, notifyChan), - } - - eps.Eventually(func() bool { return countWatchers() == 1 }, time.Second, 10*time.Millisecond) - stopAndDisableWatcher() - eps.Eventually(func() bool { return countWatchers() == 2 }, time.Second, 10*time.Millisecond) - - eps.createConfigMap(ctx, prefixsource.KubeNamespace, kubeConfigMapPath) - eps.testCollectorWithConfigmapOutput(ctx, notifyChan, expectedResult, sources) -} - -func (eps *ExcludedPrefixesSuite) TestKubeAdmConfigSourceIPv6() { - defer goleak.VerifyNone(eps.T(), goleak.IgnoreCurrent()) - expectedResult := []string{ - "10.244.0.0/16", - "10.96.0.0/16", - "fd00:10:244::/56", - "fd00:10:96::/112", - } - - notifyChan := make(chan struct{}, 1) - ctx, cancel := context.WithCancel(prefixcollector.WithKubernetesInterface(context.Background(), eps.clientSet)) - defer cancel() - - eps.createConfigMap(ctx, prefixsource.KubeNamespace, kubeConfigMapPathIPv6) - - sources := []prefixcollector.PrefixSource{ - prefixsource.NewKubeAdmPrefixSource(ctx, notifyChan), - } - - eps.testCollectorWithConfigmapOutput(ctx, notifyChan, expectedResult, sources) -} - -func (eps *ExcludedPrefixesSuite) TestAllSources() { - defer goleak.VerifyNone(eps.T(), goleak.IgnoreCurrent()) - expectedResult := []string{ - "10.244.0.0/16", - "10.96.0.0/12", - "127.0.0.0/16", - "168.92.0.0/24", - "168.0.0.0/10", - "1.0.0.0/11", - } - - notifyChan := make(chan struct{}, 1) - ctx, cancel := context.WithCancel(prefixcollector.WithKubernetesInterface(context.Background(), eps.clientSet)) - defer cancel() - - eps.createConfigMap(ctx, prefixsource.KubeNamespace, kubeConfigMapPath) - eps.createConfigMap(ctx, configMapNamespace, configMapPath) - - sources := []prefixcollector.PrefixSource{ - newDummyPrefixSource( - []string{ - "127.0.0.1/16", - "127.0.2.1/16", - "168.92.0.1/24", - }, - ), - prefixsource.NewKubeAdmPrefixSource(ctx, notifyChan), - prefixsource.NewConfigMapPrefixSource( - ctx, - notifyChan, - userConfigMapName, - configMapNamespace, - userConfigMapKey, - ), - } - - eps.testCollectorWithConfigmapOutput(ctx, notifyChan, expectedResult, sources) -} - func TestExcludedPrefixesSuite(t *testing.T) { suite.Run(t, &ExcludedPrefixesSuite{}) } @@ -299,78 +95,6 @@ func (eps *ExcludedPrefixesSuite) createConfigMap(ctx context.Context, namespace } } -func (eps *ExcludedPrefixesSuite) testCollectorWithConfigmapOutput(ctx context.Context, notifyChan chan struct{}, - expectedResult []string, sources []prefixcollector.PrefixSource) { - collector := prefixcollector.NewExcludePrefixCollector( - prefixcollector.WithNotifyChan(notifyChan), - prefixcollector.WithConfigMapOutput(nsmConfigMapName, configMapNamespace, excludedPrefixesKey), - prefixcollector.WithSources(sources...), - ) - - ctx, cancel := context.WithTimeout(ctx, time.Second) - defer cancel() - - errCh := eps.watchConfigMap(ctx, len(sources)+2) - - go collector.Serve(ctx) - - if err := <-errCh; err != nil { - eps.T().Fatal("Error watching config map: ", err) - } - - configMap, err := eps.clientSet.CoreV1().ConfigMaps(configMapNamespace).Get(ctx, nsmConfigMapName, metav1.GetOptions{}) - if err != nil { - eps.T().Fatal("Error getting nsm config map: ", err) - } - - prefixes, err := utils.YamlToPrefixes([]byte(configMap.Data[excludedPrefixesKey])) - if err != nil { - eps.T().Fatal("Error transforming yaml to prefixes: ", err) - } - - eps.Require().ElementsMatch(expectedResult, prefixes) -} - -func (eps *ExcludedPrefixesSuite) watchConfigMap(ctx context.Context, maxModifyCount int) <-chan error { - watcher, err := eps.clientSet.CoreV1().ConfigMaps(configMapNamespace).Watch(ctx, metav1.ListOptions{}) - if err != nil { - eps.T().Fatalf("Error watching configmap: %v", err) - } - - errorCh := make(chan error) - modifyCount := 0 - go func() { - for { - select { - case event, ok := <-watcher.ResultChan(): - if !ok { - errorCh <- errors.New("error watching configmap") - return - } - configMap := event.Object.(*v1.ConfigMap) - if event.Type == watch.Error { - errorCh <- errors.New("error watching configmap") - return - } - - if configMap.Name == nsmConfigMapName && (event.Type == watch.Added || event.Type == watch.Modified) { - modifyCount++ - fmt.Printf("event count : %v\n", modifyCount) - if modifyCount == maxModifyCount { - close(errorCh) - return - } - } - case <-ctx.Done(): - close(errorCh) - return - } - } - }() - - return errorCh -} - func getConfigMap(t *testing.T, filePath string) *v1.ConfigMap { destination := v1.ConfigMap{} bytes, err := os.ReadFile(filepath.Clean(filePath)) @@ -383,22 +107,3 @@ func getConfigMap(t *testing.T, filePath string) *v1.ConfigMap { return &destination } - -func interceptWatcher(clients *fake.Clientset) (getCount func() int, stopAndDisable func()) { - enable := true - count := 0 - watcher := watch.NewFake() - - var reactionFunc k8stest.WatchReactionFunc = func(action k8stest.Action) (bool, watch.Interface, error) { - count++ - return enable, watcher, nil - } - - clients.PrependWatchReactor("configmaps", reactionFunc) - - return func() int { return count }, - func() { - enable = false - watcher.Stop() - } -} From 18f85f7d06ddf1c7436ec3f8aa0bb0f109d0a4f8 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 12:16:04 +1100 Subject: [PATCH 02/17] run test 10 times Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 5e6d70d..7c07345 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... +CMD go test -test.v ./... -count=10 FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./... From 5bcc32f57ba05e419c97d94958778cb9275b8ecb Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 12:32:06 +1100 Subject: [PATCH 03/17] increase timeout Signed-off-by: Nikita Skrynnik --- internal/prefixcollector/collector_file_output_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index 4dff331..85c30db 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -87,7 +87,7 @@ func (eps *ExcludedPrefixesSuite) testCollectorWithFileOutput(ctx context.Contex prefixcollector.WithSources(sources...), ) - ctx, cancel := context.WithTimeout(ctx, time.Second) + ctx, cancel := context.WithTimeout(ctx, time.Second*5) defer cancel() watcher, errCh := eps.watchFile(ctx, prefixesFilePath, len(sources)) From bbb9d4db90d387813f6840e41de5598a447385a4 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 12:35:32 +1100 Subject: [PATCH 04/17] run test more times Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 7c07345..0e7b290 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... -count=10 +CMD go test -test.v ./... -count=100 FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./... From 9e06eadd93a44a0f5547f0f7db21a5517c2a97e1 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 12:44:43 +1100 Subject: [PATCH 05/17] increate timeout again Signed-off-by: Nikita Skrynnik --- internal/prefixcollector/collector_file_output_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index 85c30db..8c3073f 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -87,7 +87,7 @@ func (eps *ExcludedPrefixesSuite) testCollectorWithFileOutput(ctx context.Contex prefixcollector.WithSources(sources...), ) - ctx, cancel := context.WithTimeout(ctx, time.Second*5) + ctx, cancel := context.WithTimeout(ctx, time.Second*50) defer cancel() watcher, errCh := eps.watchFile(ctx, prefixesFilePath, len(sources)) From 451cfd71a30c456fe4d4da889cd51a6be3971c2c Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 19:23:23 +1100 Subject: [PATCH 06/17] add logs Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- internal/prefixcollector/collector_file_output_test.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0e7b290..df3bfbf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... -count=100 +CMD go test -test.v ./... -count=15 FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./... diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index 8c3073f..e860029 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -28,6 +28,7 @@ import ( "time" "github.com/fsnotify/fsnotify" + "github.com/networkservicemesh/sdk/pkg/tools/log" "go.uber.org/goleak" ) @@ -87,7 +88,7 @@ func (eps *ExcludedPrefixesSuite) testCollectorWithFileOutput(ctx context.Contex prefixcollector.WithSources(sources...), ) - ctx, cancel := context.WithTimeout(ctx, time.Second*50) + ctx, cancel := context.WithTimeout(ctx, time.Second*5) defer cancel() watcher, errCh := eps.watchFile(ctx, prefixesFilePath, len(sources)) @@ -142,6 +143,7 @@ func (eps *ExcludedPrefixesSuite) watchFile(ctx context.Context, prefixesFilePat } if event.Op&fsnotify.Write == fsnotify.Write { modifyCount++ + log.FromContext(ctx).Infof("File is modified") if modifyCount == maxModifyCount { close(errorCh) return From c97efaa1e5c73c5cd3722c368d0ef75ebba02bff Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 19:45:41 +1100 Subject: [PATCH 07/17] delete maxModifiedCount Signed-off-by: Nikita Skrynnik --- internal/prefixcollector/collector_file_output_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index e860029..4055fd6 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -144,10 +144,10 @@ func (eps *ExcludedPrefixesSuite) watchFile(ctx context.Context, prefixesFilePat if event.Op&fsnotify.Write == fsnotify.Write { modifyCount++ log.FromContext(ctx).Infof("File is modified") - if modifyCount == maxModifyCount { - close(errorCh) - return - } + // if modifyCount == maxModifyCount { + // close(errorCh) + // return + // } } case watcherError, ok := <-watcher.Errors: if !ok { From 3dd2a6dbcf67eda866520a856e1749d6285fb27f Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 19:56:19 +1100 Subject: [PATCH 08/17] test 100 times Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- internal/prefixcollector/collector_file_output_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index df3bfbf..0e7b290 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... -count=15 +CMD go test -test.v ./... -count=100 FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./... diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index 4055fd6..da16e9b 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -88,7 +88,7 @@ func (eps *ExcludedPrefixesSuite) testCollectorWithFileOutput(ctx context.Contex prefixcollector.WithSources(sources...), ) - ctx, cancel := context.WithTimeout(ctx, time.Second*5) + ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() watcher, errCh := eps.watchFile(ctx, prefixesFilePath, len(sources)) From bf88f395d282e8999c49899ae8ef41b32d4c31d7 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 20:05:16 +1100 Subject: [PATCH 09/17] run test 10000 times Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 0e7b290..6dc9a74 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... -count=100 +CMD go test -test.v ./... -count=10000 FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./... From 673c283fbaa8a37a6ce91d90d038a206b4865a7b Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 20:20:50 +1100 Subject: [PATCH 10/17] increase test timeout Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 6dc9a74..4e56c02 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... -count=10000 +CMD go test -test.v ./... -count=10000 -timeout 1000m FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./... From 948f3cfbbe8b19a161c16c1941700b72fb49b8a8 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 21:35:27 +1100 Subject: [PATCH 11/17] more logs Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- internal/prefixcollector/collector_file_output_test.go | 10 +++++----- internal/prefixcollector/prefix_writer.go | 8 +++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4e56c02..de08989 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... -count=10000 -timeout 1000m +CMD go test -test.v ./... -count=20 -timeout 1000m FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./... diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index da16e9b..dbc159a 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -143,11 +143,11 @@ func (eps *ExcludedPrefixesSuite) watchFile(ctx context.Context, prefixesFilePat } if event.Op&fsnotify.Write == fsnotify.Write { modifyCount++ - log.FromContext(ctx).Infof("File is modified") - // if modifyCount == maxModifyCount { - // close(errorCh) - // return - // } + log.FromContext(ctx).Infof("File is modified. Operation: %s", event.Op.String()) + if modifyCount == maxModifyCount { + close(errorCh) + return + } } case watcherError, ok := <-watcher.Errors: if !ok { diff --git a/internal/prefixcollector/prefix_writer.go b/internal/prefixcollector/prefix_writer.go index 11f237b..7fafb0e 100644 --- a/internal/prefixcollector/prefix_writer.go +++ b/internal/prefixcollector/prefix_writer.go @@ -48,9 +48,11 @@ func fileWriter(filePath string) writePrefixesFunc { return } - err = os.WriteFile(filePath, data, outputFilePermissions) - if err != nil { - log.FromContext(ctx).Fatalf("Unable to write into file: %v", err.Error()) + if ctx.Err() == nil { + err = os.WriteFile(filePath, data, outputFilePermissions) + if err != nil { + log.FromContext(ctx).Fatalf("Unable to write into file: %v", err.Error()) + } } } } From 01e41764498d78985c0314daed4bfd86ba37eafd Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 22:13:27 +1100 Subject: [PATCH 12/17] more logs Signed-off-by: Nikita Skrynnik --- internal/prefixcollector/collector.go | 3 +++ internal/prefixcollector/collector_file_output_test.go | 3 ++- internal/prefixcollector/prefix_writer.go | 8 +++----- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/prefixcollector/collector.go b/internal/prefixcollector/collector.go index da9759a..3afef33 100644 --- a/internal/prefixcollector/collector.go +++ b/internal/prefixcollector/collector.go @@ -112,6 +112,9 @@ func (epc *ExcludedPrefixCollector) Serve(ctx context.Context) { for { select { case <-epc.notifyChan: + + // Можно тут прочитать весь канал + epc.updateExcludedPrefixes(ctx) case <-ctx.Done(): return diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index dbc159a..fec3634 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -141,9 +141,10 @@ func (eps *ExcludedPrefixesSuite) watchFile(ctx context.Context, prefixesFilePat close(errorCh) return } + if event.Op&fsnotify.Write == fsnotify.Write { modifyCount++ - log.FromContext(ctx).Infof("File is modified. Operation: %s", event.Op.String()) + log.FromContext(ctx).Infof("File is modified. Event: %v. Operation: %s.", event.String(), event.Op.String()) if modifyCount == maxModifyCount { close(errorCh) return diff --git a/internal/prefixcollector/prefix_writer.go b/internal/prefixcollector/prefix_writer.go index 7fafb0e..11f237b 100644 --- a/internal/prefixcollector/prefix_writer.go +++ b/internal/prefixcollector/prefix_writer.go @@ -48,11 +48,9 @@ func fileWriter(filePath string) writePrefixesFunc { return } - if ctx.Err() == nil { - err = os.WriteFile(filePath, data, outputFilePermissions) - if err != nil { - log.FromContext(ctx).Fatalf("Unable to write into file: %v", err.Error()) - } + err = os.WriteFile(filePath, data, outputFilePermissions) + if err != nil { + log.FromContext(ctx).Fatalf("Unable to write into file: %v", err.Error()) } } } From 24a6c9612134cec6c923a312a2840ee30c54a136 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 22:16:52 +1100 Subject: [PATCH 13/17] run more times Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- internal/prefixcollector/collector_file_output_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index de08989..b7b19a1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... -count=20 -timeout 1000m +CMD go test -test.v ./... -count=40 -timeout 1000m FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./... diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index fec3634..9473a35 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -144,7 +144,7 @@ func (eps *ExcludedPrefixesSuite) watchFile(ctx context.Context, prefixesFilePat if event.Op&fsnotify.Write == fsnotify.Write { modifyCount++ - log.FromContext(ctx).Infof("File is modified. Event: %v. Operation: %s.", event.String(), event.Op.String()) + log.FromContext(ctx).Infof("File is modified. Event: %v.", event.String()) if modifyCount == maxModifyCount { close(errorCh) return From 2260a09f6475e6a4c339d3fa0a198c89e3d42480 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 22:24:56 +1100 Subject: [PATCH 14/17] increase maxModifyCount Signed-off-by: Nikita Skrynnik --- internal/prefixcollector/collector_file_output_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index 9473a35..b335f70 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -91,7 +91,7 @@ func (eps *ExcludedPrefixesSuite) testCollectorWithFileOutput(ctx context.Contex ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() - watcher, errCh := eps.watchFile(ctx, prefixesFilePath, len(sources)) + watcher, errCh := eps.watchFile(ctx, prefixesFilePath, len(sources)+1) go collector.Serve(ctx) From 49933b892d403f2ef67bfe0a2c8d404a71b4d5f4 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Mon, 9 Oct 2023 22:29:36 +1100 Subject: [PATCH 15/17] test the test 5000 times Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- internal/prefixcollector/collector.go | 4 ---- internal/prefixcollector/collector_file_output_test.go | 2 -- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index b7b19a1..55c05f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... -count=40 -timeout 1000m +CMD go test -test.v ./... -count=5000 -timeout 1000m FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./... diff --git a/internal/prefixcollector/collector.go b/internal/prefixcollector/collector.go index 3afef33..ff1cf17 100644 --- a/internal/prefixcollector/collector.go +++ b/internal/prefixcollector/collector.go @@ -112,9 +112,6 @@ func (epc *ExcludedPrefixCollector) Serve(ctx context.Context) { for { select { case <-epc.notifyChan: - - // Можно тут прочитать весь канал - epc.updateExcludedPrefixes(ctx) case <-ctx.Done(): return @@ -136,7 +133,6 @@ func (epc *ExcludedPrefixCollector) updateExcludedPrefixes(ctx context.Context) var prefixesV4 []string var prefixesV6 []string for _, p := range sourcePrefixes { - log.FromContext(ctx).Infof("Prefix: %v", p) prefix := strings.TrimSpace(p) ip, _, err := net.ParseCIDR(prefix) if err != nil { diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index b335f70..0f92730 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -28,7 +28,6 @@ import ( "time" "github.com/fsnotify/fsnotify" - "github.com/networkservicemesh/sdk/pkg/tools/log" "go.uber.org/goleak" ) @@ -144,7 +143,6 @@ func (eps *ExcludedPrefixesSuite) watchFile(ctx context.Context, prefixesFilePat if event.Op&fsnotify.Write == fsnotify.Write { modifyCount++ - log.FromContext(ctx).Infof("File is modified. Event: %v.", event.String()) if modifyCount == maxModifyCount { close(errorCh) return From 8bbe819408253a416026ce6de762d1c8afff66b1 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Tue, 10 Oct 2023 10:16:47 +1100 Subject: [PATCH 16/17] increase maxModifyCount Signed-off-by: Nikita Skrynnik --- internal/prefixcollector/collector_file_output_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/prefixcollector/collector_file_output_test.go b/internal/prefixcollector/collector_file_output_test.go index 0f92730..2360e04 100644 --- a/internal/prefixcollector/collector_file_output_test.go +++ b/internal/prefixcollector/collector_file_output_test.go @@ -90,7 +90,7 @@ func (eps *ExcludedPrefixesSuite) testCollectorWithFileOutput(ctx context.Contex ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() - watcher, errCh := eps.watchFile(ctx, prefixesFilePath, len(sources)+1) + watcher, errCh := eps.watchFile(ctx, prefixesFilePath, len(sources)*2) go collector.Serve(ctx) From 5798f4d1befd4baccbc2f320b75a623f4d71679d Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Tue, 10 Oct 2023 11:46:45 +1100 Subject: [PATCH 17/17] run test 15000 times Signed-off-by: Nikita Skrynnik --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 55c05f2..7898aba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ COPY . . RUN go build -o /bin/exclude-prefixes . FROM build as test -CMD go test -test.v ./... -count=5000 -timeout 1000m +CMD go test -test.v ./... -count=15000 -timeout 1000m FROM test as debug CMD dlv -l :40000 --headless=true --api-version=2 test -test.v ./...