|
| 1 | +package parser_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/sirupsen/logrus" |
| 7 | + "github.com/sirupsen/logrus/hooks/test" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | + |
| 12 | + "github.com/kong/kubernetes-ingress-controller/v2/internal/dataplane/parser" |
| 13 | + kongv1 "github.com/kong/kubernetes-ingress-controller/v2/pkg/apis/configuration/v1" |
| 14 | +) |
| 15 | + |
| 16 | +const someValidTranslationFailureReason = "some valid reason" |
| 17 | + |
| 18 | +func someValidTranslationFailureCausingObjects() []client.Object { |
| 19 | + return []client.Object{&kongv1.KongIngress{}, &kongv1.KongPlugin{}} |
| 20 | +} |
| 21 | + |
| 22 | +func TestTranslationFailure(t *testing.T) { |
| 23 | + t.Run("is created and returns reason and causing objects", func(t *testing.T) { |
| 24 | + transErr, err := parser.NewTranslationFailure(someValidTranslationFailureReason, someValidTranslationFailureCausingObjects()...) |
| 25 | + require.NoError(t, err) |
| 26 | + |
| 27 | + assert.Equal(t, someValidTranslationFailureReason, transErr.Reason()) |
| 28 | + assert.ElementsMatch(t, someValidTranslationFailureCausingObjects(), transErr.CausingObjects()) |
| 29 | + }) |
| 30 | + |
| 31 | + t.Run("fallbacks to unknown reason when empty", func(t *testing.T) { |
| 32 | + transErr, err := parser.NewTranslationFailure("", someValidTranslationFailureCausingObjects()...) |
| 33 | + require.NoError(t, err) |
| 34 | + require.Equal(t, parser.TranslationFailureReasonUnknown, transErr.Reason()) |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("requires at least one causing object", func(t *testing.T) { |
| 38 | + _, err := parser.NewTranslationFailure(someValidTranslationFailureReason, someValidTranslationFailureCausingObjects()[0]) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + _, err = parser.NewTranslationFailure(someValidTranslationFailureReason) |
| 42 | + require.Error(t, err) |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +func TestTranslationFailuresCollector(t *testing.T) { |
| 47 | + testLogger, _ := test.NewNullLogger() |
| 48 | + |
| 49 | + t.Run("is created when logger valid", func(t *testing.T) { |
| 50 | + collector, err := parser.NewTranslationFailuresCollector(testLogger) |
| 51 | + require.NoError(t, err) |
| 52 | + require.NotNil(t, collector) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("requires non nil logger", func(t *testing.T) { |
| 56 | + _, err := parser.NewTranslationFailuresCollector(nil) |
| 57 | + require.Error(t, err) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("pushes and pops translation failures", func(t *testing.T) { |
| 61 | + collector, err := parser.NewTranslationFailuresCollector(testLogger) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + collector.PushTranslationFailure(someValidTranslationFailureReason, someValidTranslationFailureCausingObjects()...) |
| 65 | + collector.PushTranslationFailure(someValidTranslationFailureReason, someValidTranslationFailureCausingObjects()...) |
| 66 | + |
| 67 | + collectedErrors := collector.PopTranslationFailures() |
| 68 | + require.Len(t, collectedErrors, 2) |
| 69 | + require.Empty(t, collector.PopTranslationFailures(), "second call should not return any failure") |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("does not crash but logs warning when no causing objects passed", func(t *testing.T) { |
| 73 | + logger, loggerHook := test.NewNullLogger() |
| 74 | + collector, err := parser.NewTranslationFailuresCollector(logger) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + collector.PushTranslationFailure(someValidTranslationFailureReason) |
| 78 | + |
| 79 | + lastLog := loggerHook.LastEntry() |
| 80 | + require.NotNil(t, lastLog) |
| 81 | + require.Equal(t, logrus.WarnLevel, lastLog.Level) |
| 82 | + require.Len(t, collector.PopTranslationFailures(), 0, "no failures expected - causing objects missing") |
| 83 | + }) |
| 84 | +} |
0 commit comments