Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/localconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/config/localconfig_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}