Skip to content
This repository was archived by the owner on Dec 22, 2025. It is now read-only.

Commit 1f12e9d

Browse files
authored
PT-6909 Handle invalid anonymiser (#114)
1 parent 48b20a3 commit 1f12e9d

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

pkg/anonymiser/anonymiser.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,30 @@ func (a *anonymiser) ReadTable(tableName string, rowChan chan<- database.Row, op
6666
continue
6767
}
6868

69-
for name, faker := range Functions {
70-
if fakerType != name {
71-
continue
72-
}
69+
faker, found := Functions[fakerType]
70+
if !found {
71+
logger.WithField("anonymiser", fakerType).Error("Anonymiser is not found")
72+
// TODO: actually we should stop the whole process here,
73+
// but currently there is no simple way of doing this, so as a workaround
74+
// we'll just break dump in case log error will be missed by the user
75+
row[column] = fmt.Sprintf("Invalid anonymiser: %s", fakerType)
76+
continue
77+
}
7378

74-
var value string
75-
switch name {
76-
case email, username:
77-
b := make([]byte, 2)
78-
rand.Read(b)
79-
value = fmt.Sprintf(
80-
"%s.%s",
81-
faker.Call([]reflect.Value{})[0].String(),
82-
hex.EncodeToString(b),
83-
)
84-
default:
85-
value = faker.Call([]reflect.Value{})[0].String()
86-
}
87-
row[column] = value
79+
var value string
80+
switch fakerType {
81+
case email, username:
82+
b := make([]byte, 2)
83+
rand.Read(b)
84+
value = fmt.Sprintf(
85+
"%s.%s",
86+
faker.Call([]reflect.Value{})[0].String(),
87+
hex.EncodeToString(b),
88+
)
89+
default:
90+
value = faker.Call([]reflect.Value{})[0].String()
8891
}
92+
row[column] = value
8993
}
9094

9195
rowChan <- row

pkg/anonymiser/anonymiser_test.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strconv"
66
"testing"
7+
"time"
78

89
"github.com/hellofresh/klepto/pkg/config"
910
"github.com/hellofresh/klepto/pkg/database"
@@ -12,6 +13,8 @@ import (
1213
"github.com/stretchr/testify/require"
1314
)
1415

16+
const waitTimeout = time.Second
17+
1518
func TestReadTable(t *testing.T) {
1619
t.Parallel()
1720

@@ -45,6 +48,12 @@ func TestReadTable(t *testing.T) {
4548
opts: reader.ReadTableOpt{},
4649
config: config.Tables{{Name: "test", Anonymise: map[string]string{"column_test": "literal:Hello"}}},
4750
},
51+
{
52+
scenario: "when column anonymiser in invalid",
53+
function: testWhenColumnAnonymiserIsInvalid,
54+
opts: reader.ReadTableOpt{},
55+
config: config.Tables{{Name: "test", Anonymise: map[string]string{"column_test": "Hello"}}},
56+
},
4857
}
4958

5059
for _, test := range tests {
@@ -83,10 +92,12 @@ func testWhenColumnIsAnonymised(t *testing.T, opts reader.ReadTableOpt, tables c
8392
err := anonymiser.ReadTable("test", rowChan, opts)
8493
require.NoError(t, err)
8594

86-
for {
87-
row := <-rowChan
95+
timeoutChan := time.After(waitTimeout)
96+
select {
97+
case row := <-rowChan:
8898
assert.NotEqual(t, "to_be_anonimised", row["column_test"])
89-
break
99+
case <-timeoutChan:
100+
assert.FailNow(t, "Failing due to timeout")
90101
}
91102
}
92103

@@ -99,10 +110,30 @@ func testWhenColumnIsAnonymisedWithLiteral(t *testing.T, opts reader.ReadTableOp
99110
err := anonymiser.ReadTable("test", rowChan, opts)
100111
require.NoError(t, err)
101112

102-
for {
103-
row := <-rowChan
113+
timeoutChan := time.After(waitTimeout)
114+
select {
115+
case row := <-rowChan:
104116
assert.Equal(t, "Hello", row["column_test"])
105-
break
117+
case <-timeoutChan:
118+
assert.FailNow(t, "Failing due to timeout")
119+
}
120+
}
121+
122+
func testWhenColumnAnonymiserIsInvalid(t *testing.T, opts reader.ReadTableOpt, tables config.Tables) {
123+
anonymiser := NewAnonymiser(&mockReader{}, tables)
124+
125+
rowChan := make(chan database.Row)
126+
defer close(rowChan)
127+
128+
err := anonymiser.ReadTable("test", rowChan, opts)
129+
require.NoError(t, err)
130+
131+
timeoutChan := time.After(waitTimeout)
132+
select {
133+
case row := <-rowChan:
134+
assert.Equal(t, "Invalid anonymiser: Hello", row["column_test"])
135+
case <-timeoutChan:
136+
assert.FailNow(t, "Failing due to timeout")
106137
}
107138
}
108139

0 commit comments

Comments
 (0)