Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func NewStopCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
errors.CheckError(err)
defer containerClient.CloseClient()

exists, _ := containerClient.ContainerExists(instance.ContainerID)
errors.CheckError(err)
Comment thread
Syedowais312 marked this conversation as resolved.
Outdated
if !exists {
fmt.Printf("Container for instance %s no longer exists\n", instance.Name)
fmt.Printf("Run 'microcks start --name %s' to bring the container back\n", instance.Name)
return
}

err = containerClient.StopContainer(instance.ContainerID)
if err != nil {
log.Fatalf("Failed to stop a container: %v", err)
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
Comment thread
Syedowais312 marked this conversation as resolved.
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)
}
}
}