Skip to content

Commit

Permalink
fake: fix delete element from map operation.
Browse files Browse the repository at this point in the history
On delete map element value is not required.
Fix unit test reference to the stale table. fake.Table is a new object
after every transaction, so after each Run() it should be re-assigned.

Signed-off-by: Nadia Pinaeva <n.m.pinaeva@gmail.com>
  • Loading branch information
npinaeva committed Jul 8, 2024
1 parent ba21e1b commit 8da7cec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (fake *Fake) run(tx *Transaction) (*FakeTable, error) {
return nil, fmt.Errorf("unhandled operation %q", op.verb)
}
case *Element:
if len(obj.Value) == 0 {
if obj.Set != "" {
existingSet := updatedTable.Sets[obj.Set]
if existingSet == nil {
return nil, notFoundError("no such set %q", obj.Set)
Expand Down
40 changes: 29 additions & 11 deletions fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,13 @@ func TestFakeRun(t *testing.T) {
t.Fatalf("unexpected error from Run: %v", err)
}

table := fake.Table
if table == nil {
if fake.Table == nil {
t.Fatalf("fake.Table is nil")
}

chain := table.Chains["chain"]
if chain == nil || len(table.Chains) != 2 {
t.Fatalf("unexpected contents of table.Chains: %+v", table.Chains)
chain := fake.Table.Chains["chain"]
if chain == nil || len(fake.Table.Chains) != 2 {
t.Fatalf("unexpected contents of table.Chains: %+v", fake.Table.Chains)
}

if len(chain.Rules) != 2 {
Expand All @@ -143,9 +142,9 @@ func TestFakeRun(t *testing.T) {
// Save this Rule object for later
ruleToDelete := chain.Rules[1]

m := table.Maps["map1"]
if m == nil || len(table.Maps) != 1 {
t.Fatalf("unexpected contents of table.Maps: %+v", table.Maps)
m := fake.Table.Maps["map1"]
if m == nil || len(fake.Table.Maps) != 1 {
t.Fatalf("unexpected contents of table.Maps: %+v", fake.Table.Maps)
}

elem := m.FindElement("192.168.0.2", "tcp", "443")
Expand Down Expand Up @@ -243,9 +242,9 @@ func TestFakeRun(t *testing.T) {
}

// Neither element should have been added
m = table.Maps["map1"]
if m == nil || len(table.Maps) != 1 {
t.Fatalf("unexpected contents of table.Maps: %+v", table.Maps)
m = fake.Table.Maps["map1"]
if m == nil || len(fake.Table.Maps) != 1 {
t.Fatalf("unexpected contents of table.Maps: %+v", fake.Table.Maps)
}

elem = m.FindElement("192.168.0.3", "tcp", "80")
Expand All @@ -260,6 +259,25 @@ func TestFakeRun(t *testing.T) {
t.Errorf("unexpected contents of map1: %+v", m)
}

// Check delete element from map works
tx = fake.NewTransaction()
tx.Delete(&Element{
Map: "map1",
Key: []string{"192.168.0.1", "tcp", "80"},
})
err = fake.Run(context.Background(), tx)
if err != nil {
t.Fatalf("unexpected error from Run: %v", err)
}
m = fake.Table.Maps["map1"]
if len(m.Elements) != 1 {
t.Errorf("unexpected contents of map1: %+v", m)
}
elem = m.FindElement("192.168.0.1", "tcp", "80")
if elem != nil {
t.Errorf("element should have been deleted: %+v", elem)
}

// Ensure that we can't add things that refer to non-existing things
tx = fake.NewTransaction()
tx.Add(&Rule{
Expand Down

0 comments on commit 8da7cec

Please sign in to comment.