Skip to content

Commit 700e6db

Browse files
committed
added tests for duplicate instace in localconfig
Signed-off-by: syedowais312 <syedowais312sf@gmail.com>
1 parent 75693af commit 700e6db

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

pkg/config/localconfig_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestRemoveInstance_ByContainerID(t *testing.T) {
8+
localConfig := &LocalConfig{
9+
Instances: []Instance{
10+
{Name: "microcks", ContainerID: "old-id-123", Status: "Running", Port: "8585"},
11+
{Name: "staging", ContainerID: "staging-id-456", Status: "Running", Port: "8586"},
12+
},
13+
}
14+
15+
removed := localConfig.RemoveInstance("old-id-123")
16+
17+
if !removed {
18+
t.Error("expected RemoveInstance to return true")
19+
}
20+
// staging should still be there
21+
if len(localConfig.Instances) != 1 {
22+
t.Errorf("expected 1 instance remaining, got %d", len(localConfig.Instances))
23+
}
24+
if localConfig.Instances[0].ContainerID != "staging-id-456" {
25+
t.Errorf("expected staging instance to remain, got %s", localConfig.Instances[0].ContainerID)
26+
}
27+
}
28+
29+
func TestRemoveInstance_NoDuplicatesAfterRecreate(t *testing.T) {
30+
localConfig := &LocalConfig{
31+
Instances: []Instance{
32+
{Name: "microcks", ContainerID: "old-id-123", Status: "Running", Port: "8585"},
33+
{Name: "staging", ContainerID: "staging-id-456", Status: "Running", Port: "8586"},
34+
},
35+
}
36+
37+
localConfig.RemoveInstance("old-id-123")
38+
39+
localConfig.UpsertInstance(Instance{
40+
Name: "microcks",
41+
ContainerID: "new-id-789",
42+
Status: "Running",
43+
Port: "8585",
44+
})
45+
46+
// staging + recreated microcks = 2, no duplicates
47+
if len(localConfig.Instances) != 2 {
48+
t.Errorf("expected 2 instances, got %d — duplicate entries present", len(localConfig.Instances))
49+
}
50+
// verify microcks has new ID
51+
for _, i := range localConfig.Instances {
52+
if i.Name == "microcks" && i.ContainerID != "new-id-789" {
53+
t.Errorf("expected new-id-789, got %s", i.ContainerID)
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)