Skip to content

Commit b277a08

Browse files
Merge pull request #300 from sdcio/FixKeyDeletionIssue
Fixing the unintended deletion of key attributes
2 parents cf6a601 + 62ed699 commit b277a08

File tree

4 files changed

+126
-7
lines changed

4 files changed

+126
-7
lines changed

pkg/tree/entry_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func Test_Entry_Three(t *testing.T) {
340340
highPri := LeafEntriesToUpdates(highPriLe)
341341

342342
// diff the result with the expected
343-
if diff := testhelper.DiffUpdates([]*types.Update{n1, n2}, highPri); diff != "" {
343+
if diff := testhelper.DiffUpdates([]*types.Update{n1, n2, u0, u1_1, u2_1}, highPri); diff != "" {
344344
t.Errorf("root.GetByOwner() mismatch (-want +got):\n%s", diff)
345345
}
346346
})
@@ -374,11 +374,11 @@ func Test_Entry_Four(t *testing.T) {
374374
u4 := types.NewUpdate([]string{"interface", "ethernet-1/1", "subinterface", "13", "description"}, desc3, prio50, owner1, ts1)
375375
u4_1 := types.NewUpdate([]string{"interface", "ethernet-1/1", "subinterface", "13", "index"}, testhelper.GetUIntTvProto(13), prio50, owner1, ts1)
376376

377-
u1o2_0 := types.NewUpdate([]string{"interface", "ethernet-1/1", "name"}, testhelper.GetStringTvProto("ethernet-1/1"), prio55, owner2, ts1)
377+
// u1o2_0 := types.NewUpdate([]string{"interface", "ethernet-1/1", "name"}, testhelper.GetStringTvProto("ethernet-1/1"), prio55, owner2, ts1)
378378
u1o2 := types.NewUpdate([]string{"interface", "ethernet-1/1", "subinterface", "10", "description"}, desc3, prio55, owner2, ts1)
379-
u1o2_1 := types.NewUpdate([]string{"interface", "ethernet-1/1", "subinterface", "10", "index"}, testhelper.GetUIntTvProto(10), prio55, owner2, ts1)
379+
// u1o2_1 := types.NewUpdate([]string{"interface", "ethernet-1/1", "subinterface", "10", "index"}, testhelper.GetUIntTvProto(10), prio55, owner2, ts1)
380380
u2o2 := types.NewUpdate([]string{"interface", "ethernet-1/1", "subinterface", "11", "description"}, desc3, prio55, owner2, ts1)
381-
u2o2_1 := types.NewUpdate([]string{"interface", "ethernet-1/1", "subinterface", "11", "index"}, testhelper.GetUIntTvProto(11), prio55, owner2, ts1)
381+
// u2o2_1 := types.NewUpdate([]string{"interface", "ethernet-1/1", "subinterface", "11", "index"}, testhelper.GetUIntTvProto(11), prio55, owner2, ts1)
382382

383383
ctx := context.TODO()
384384

@@ -461,7 +461,7 @@ func Test_Entry_Four(t *testing.T) {
461461
highPri := LeafEntriesToUpdates(highPriLe)
462462

463463
// diff the result with the expected
464-
if diff := testhelper.DiffUpdates([]*types.Update{n1, n2}, highPri); diff != "" {
464+
if diff := testhelper.DiffUpdates([]*types.Update{n1, n2, u1o1_0, u1o1_1, u2o1_1}, highPri); diff != "" {
465465
t.Errorf("root.GetByOwner() mismatch (-want +got):\n%s", diff)
466466
}
467467
})
@@ -477,7 +477,7 @@ func Test_Entry_Four(t *testing.T) {
477477
t.Run("Check the old entries are gone from highest (Only New Or Updated)", func(t *testing.T) {
478478
highpri := root.GetHighestPrecedence(true)
479479
// diff the result with the expected
480-
if diff := testhelper.DiffUpdates([]*types.Update{u1o2_0, n1, n2, u2o2_1, u1o2_1}, highpri.ToUpdateSlice()); diff != "" {
480+
if diff := testhelper.DiffUpdates([]*types.Update{u1o1_0, n1, n2, u2o1_1, u1o1_1}, highpri.ToUpdateSlice()); diff != "" {
481481
t.Errorf("root.GetHighestPrecedence() mismatch (-want +got):\n%s", diff)
482482
}
483483
})

pkg/tree/leaf_entry.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func (l *LeafEntry) MarkNew() {
6363
l.IsNew = true
6464
}
6565

66+
func (l *LeafEntry) RemoveDeleteFlag() {
67+
l.Delete = false
68+
}
69+
6670
func (l *LeafEntry) GetDeleteFlag() bool {
6771
l.mu.RLock()
6872
defer l.mu.RUnlock()

pkg/tree/sharedEntryAttributes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func (s *sharedEntryAttributes) checkAndCreateKeysAsLeafs(ctx context.Context, i
222222
var result []*LeafEntry
223223
lvs := child.GetByOwner(intentName, result)
224224
if len(lvs) > 0 {
225+
lvs[0].RemoveDeleteFlag()
225226
continue
226227
}
227228
}
@@ -1608,7 +1609,7 @@ func (s *sharedEntryAttributes) TreeExport(owner string) ([]*tree_persist.TreeEl
16081609
func (s *sharedEntryAttributes) BlameConfig(includeDefaults bool) (*sdcpb.BlameTreeElement, error) {
16091610
name := s.pathElemName
16101611
if s.GetLevel() == 0 {
1611-
name = fmt.Sprintf("root")
1612+
name = "root"
16121613
}
16131614
result := sdcpb.NewBlameTreeElement(name)
16141615

pkg/tree/sharedEntryAttributes_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/sdcio/data-server/pkg/config"
1515
schemaClient "github.com/sdcio/data-server/pkg/datastore/clients/schema"
1616
jsonImporter "github.com/sdcio/data-server/pkg/tree/importer/json"
17+
"github.com/sdcio/data-server/pkg/tree/importer/proto"
1718
"github.com/sdcio/data-server/pkg/tree/types"
1819
"github.com/sdcio/data-server/pkg/utils/testhelper"
1920
sdcio_schema "github.com/sdcio/data-server/tests/sdcioygot"
@@ -921,3 +922,116 @@ func Test_sharedEntryAttributes_BlameConfig(t *testing.T) {
921922
})
922923
}
923924
}
925+
926+
func Test_sharedEntryAttributes_ReApply(t *testing.T) {
927+
ctx := context.TODO()
928+
owner1 := "owner1"
929+
owner1Prio := int32(50)
930+
931+
tests := []struct {
932+
name string
933+
r func(t *testing.T) *sdcio_schema.Device
934+
numDelete int
935+
}{
936+
{
937+
name: "multiple keys",
938+
r: func(t *testing.T) *sdcio_schema.Device {
939+
conf1 := &sdcio_schema.Device{
940+
Doublekey: map[sdcio_schema.SdcioModel_Doublekey_Key]*sdcio_schema.SdcioModel_Doublekey{
941+
{
942+
Key1: "k1.1",
943+
Key2: "k1.2",
944+
}: {
945+
Key1: ygot.String("k1.1"),
946+
Key2: ygot.String("k1.2"),
947+
Cont: &sdcio_schema.SdcioModel_Doublekey_Cont{
948+
Value1: ygot.String("containerval1.1"),
949+
Value2: ygot.String("containerval1.2"),
950+
},
951+
Mandato: ygot.String("foo"),
952+
},
953+
},
954+
}
955+
return conf1
956+
},
957+
numDelete: 0,
958+
},
959+
}
960+
for _, tt := range tests {
961+
t.Run(tt.name, func(t *testing.T) {
962+
mockCtrl := gomock.NewController(t)
963+
defer mockCtrl.Finish()
964+
965+
scb, err := testhelper.GetSchemaClientBound(t, mockCtrl)
966+
if err != nil {
967+
t.Fatal(err)
968+
}
969+
970+
tc := NewTreeContext(scb, owner1)
971+
root, err := NewTreeRoot(ctx, tc)
972+
if err != nil {
973+
t.Fatal(err)
974+
}
975+
updSlice := types.UpdateSlice{
976+
types.NewUpdate([]string{"doublekey", "k1.1", "k1.2", "mandato"}, testhelper.GetStringTvProto("TheMandatoryValue1"), owner1Prio, owner1, 0),
977+
}
978+
979+
err = root.AddUpdatesRecursive(ctx, updSlice, flagsNew)
980+
if err != nil {
981+
t.Error(err)
982+
}
983+
984+
fmt.Println(root.String())
985+
986+
treepersist, err := root.TreeExport(owner1, owner1Prio)
987+
if err != nil {
988+
t.Error(err)
989+
return
990+
}
991+
992+
persistByte, err := protojson.Marshal(treepersist)
993+
if err != nil {
994+
t.Error(err)
995+
return
996+
}
997+
fmt.Println("\nTreeExport:")
998+
fmt.Println(string(persistByte))
999+
1000+
tcNew := NewTreeContext(scb, owner1)
1001+
newRoot, err := NewTreeRoot(ctx, tcNew)
1002+
if err != nil {
1003+
t.Fatal(err)
1004+
}
1005+
1006+
err = newRoot.ImportConfig(ctx, types.PathSlice{}, proto.NewProtoTreeImporter(treepersist.Root), owner1, owner1Prio, flagsExisting)
1007+
if err != nil {
1008+
t.Error(err)
1009+
return
1010+
}
1011+
1012+
// mark owner delete
1013+
newRoot.MarkOwnerDelete(owner1, false)
1014+
1015+
err = newRoot.AddUpdatesRecursive(ctx, updSlice, flagsNew)
1016+
if err != nil {
1017+
t.Error(err)
1018+
}
1019+
1020+
fmt.Println(newRoot.String())
1021+
1022+
err = newRoot.FinishInsertionPhase(ctx)
1023+
if err != nil {
1024+
t.Fatal(err)
1025+
}
1026+
1027+
deleteList, err := newRoot.GetDeletes(true)
1028+
if err != nil {
1029+
t.Error(err)
1030+
}
1031+
if len(deleteList) != tt.numDelete {
1032+
t.Errorf("%d deltes expected, got %d\n%s", tt.numDelete, len(deleteList), strings.Join(deleteList.PathSlices().StringSlice(), ", "))
1033+
}
1034+
1035+
})
1036+
}
1037+
}

0 commit comments

Comments
 (0)