From 75693af00394d775f5491bd492b7c804efb87090 Mon Sep 17 00:00:00 2001 From: syedowais312 Date: Mon, 22 Jun 2026 14:30:26 +0530 Subject: [PATCH 1/2] fix: remove stale instance entry on recreate and guard stop against missing containers Signed-off-by: syedowais312 --- cmd/start.go | 2 +- cmd/stop.go | 2 +- pkg/config/localconfig.go | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/start.go b/cmd/start.go index 1d5066d7..3f745afd 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -66,8 +66,8 @@ microcks start --name [name of you container/instance]`, errors.CheckError(err) if !exists { fmt.Printf("Container for instance %s no longer exists, recreating it\n", name) + localConfig.RemoveInstance(instance.ContainerID) instance.Status = "" - instance.ContainerID = "" } } diff --git a/cmd/stop.go b/cmd/stop.go index eaba91fd..f94c884a 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -59,7 +59,7 @@ func NewStopCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command { _ = localConfig.RemoveServer(ctx.Server.Server) _ = localConfig.RemoveUser(ctx.User.Name) _ = localConfig.RemoveAuth(ctx.Server.Server) - _ = localConfig.RemoveInstance(instance.Name) + _ = localConfig.RemoveInstance(instance.ContainerID) localConfig.CurrentContext = "" log.Printf("Instance %s removed successfully", instance.Name) diff --git a/pkg/config/localconfig.go b/pkg/config/localconfig.go index 1e5eeda6..5deb95ea 100644 --- a/pkg/config/localconfig.go +++ b/pkg/config/localconfig.go @@ -321,12 +321,12 @@ func (l *LocalConfig) UpsertInstance(instance Instance) { } // Returns true if server was removed successfully -func (l *LocalConfig) RemoveInstance(instanceName string) bool { - if instanceName == "" { +func (l *LocalConfig) RemoveInstance(instanceID string) bool { + if instanceID == "" { return true } for a, i := range l.Instances { - if i.Name == instanceName { + if i.ContainerID == instanceID { l.Instances = append(l.Instances[:a], l.Instances[a+1:]...) return true } From 700e6db61fb9152ce870e61e75452ed33d4bf7c5 Mon Sep 17 00:00:00 2001 From: syedowais312 Date: Wed, 24 Jun 2026 14:53:49 +0530 Subject: [PATCH 2/2] added tests for duplicate instace in localconfig Signed-off-by: syedowais312 --- pkg/config/localconfig_test.go | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 pkg/config/localconfig_test.go diff --git a/pkg/config/localconfig_test.go b/pkg/config/localconfig_test.go new file mode 100644 index 00000000..3eed1422 --- /dev/null +++ b/pkg/config/localconfig_test.go @@ -0,0 +1,56 @@ +package config + +import ( + "testing" +) + +func TestRemoveInstance_ByContainerID(t *testing.T) { + localConfig := &LocalConfig{ + Instances: []Instance{ + {Name: "microcks", ContainerID: "old-id-123", Status: "Running", Port: "8585"}, + {Name: "staging", ContainerID: "staging-id-456", Status: "Running", Port: "8586"}, + }, + } + + removed := localConfig.RemoveInstance("old-id-123") + + if !removed { + t.Error("expected RemoveInstance to return true") + } + // staging should still be there + if len(localConfig.Instances) != 1 { + t.Errorf("expected 1 instance remaining, got %d", len(localConfig.Instances)) + } + if localConfig.Instances[0].ContainerID != "staging-id-456" { + t.Errorf("expected staging instance to remain, got %s", localConfig.Instances[0].ContainerID) + } +} + +func TestRemoveInstance_NoDuplicatesAfterRecreate(t *testing.T) { + localConfig := &LocalConfig{ + Instances: []Instance{ + {Name: "microcks", ContainerID: "old-id-123", Status: "Running", Port: "8585"}, + {Name: "staging", ContainerID: "staging-id-456", Status: "Running", Port: "8586"}, + }, + } + + localConfig.RemoveInstance("old-id-123") + + localConfig.UpsertInstance(Instance{ + Name: "microcks", + ContainerID: "new-id-789", + Status: "Running", + Port: "8585", + }) + + // staging + recreated microcks = 2, no duplicates + if len(localConfig.Instances) != 2 { + t.Errorf("expected 2 instances, got %d — duplicate entries present", len(localConfig.Instances)) + } + // verify microcks has new ID + for _, i := range localConfig.Instances { + if i.Name == "microcks" && i.ContainerID != "new-id-789" { + t.Errorf("expected new-id-789, got %s", i.ContainerID) + } + } +}