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
3 changes: 2 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ microcks start --name [name of you container/instance]`,
if _, err := fmt.Fprintf(progress, "Container for instance %s no longer exists, recreating it\n", name); err != nil {
return errors.Wrap(errors.KindEnvironment, err)
}

localConfig.RemoveInstance(instance.ContainerID)
instance.Status = ""
instance.ContainerID = ""
}
}

Expand Down
20 changes: 14 additions & 6 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ func NewStopCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
}
defer containerClient.CloseClient()

if err := containerClient.StopContainer(instance.ContainerID); err != nil {
return errors.Wrap(errors.KindEnvironment, fmt.Errorf("failed to stop container: %w", err))
exists, err := containerClient.ContainerExists(instance.ContainerID)
if err != nil {
return errors.Wrap(errors.KindEnvironment, err)
}
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)
} else {
if err := containerClient.StopContainer(instance.ContainerID); err != nil {
return errors.Wrap(errors.KindEnvironment, fmt.Errorf("failed to stop container: %w", err))
}
fmt.Println("")
log.Printf("Instance %s stopped successfully", instance.Name)
Comment on lines +69 to +77

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! Thanks for moving StopContainer to the else block.

However, the return nil and the 'Run microcks start to bring it back' print statement are still inside the if !exists block. Because of that return nil, the code still exits early and skips the // update configs section at the bottom.

Could you please remove the return nil (and optionally the second print statement) so that execution falls through to the config cleanup?

It should look exactly like this:

  		if !exists {
  			fmt.Printf("Container for instance %s no longer exists\n", instance.Name)
  		} else {
  			if err := containerClient.StopContainer(instance.ContainerID); err != nil {
  				return errors.Wrap(errors.KindEnvironment, fmt.Errorf("failed to stop container: %w", err))
  			}
  			fmt.Println("")
  			log.Printf("Instance %s stopped successfully", instance.Name)
  		}

  		// update configs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! Removed the return nil ,but I kept the second print statement since it tells the user how to bring the container back, which seemed useful for UX? or would you prefer I remove it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it.

}
fmt.Println("")
log.Printf("Instance %s stopped successfully", instance.Name)

// update configs

if instance.AutoRemove {
Expand All @@ -78,7 +86,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
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func TestLocalConfigCRUDAndValidation(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "cont456", inst2Updated.ContainerID)

ok = loadedCfg.RemoveInstance("inst2-updated")
ok = loadedCfg.RemoveInstance("cont456")
assert.True(t, ok)

ok = loadedCfg.RemoveInstance("")
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/localconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,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
71 changes: 71 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,71 @@
/*
* Copyright The Microcks Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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)
}
}
}
Loading