Skip to content

Commit

Permalink
chore: add a check for Instance state when cloning (#570)
Browse files Browse the repository at this point in the history
* chore: add a check for Instance state when cloning

* chore: update the func comment

* fix: a failing unit test
  • Loading branch information
mojtaba-esk authored Oct 14, 2024
1 parent dc5ae81 commit f86e86c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/instance/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,5 @@ var (
ErrAddingHostToProxyNotAllowed = errors.New("AddingHostToProxyNotAllowed", "adding host to proxy is only allowed in state 'Started' and 'Preparing'. Current state is '%s'")
ErrInstanceNameAlreadyExists = errors.New("InstanceNameAlreadyExists", "instance name '%s' already exists")
ErrSettingSidecarName = errors.New("SettingSidecarName", "error setting sidecar name with prefix '%s' for instance '%s'")
ErrCannotCloneInstance = errors.New("CannotCloneInstance", "cannot clone instance '%s' in state '%s'")
)
6 changes: 5 additions & 1 deletion pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ func (i *Instance) CloneWithSuffix(suffix string) (*Instance, error) {
}

// CloneWithName creates a clone of the instance with a given name
// This function can only be called in the state 'Committed'
// This function can only be called in the state 'Committed' or 'Stopped'
// When cloning an instance that is a sidecar, the clone will be not a sidecar
// When cloning an instance with sidecars, the sidecars will be cloned as well
func (i *Instance) CloneWithName(name string) (*Instance, error) {
if !i.IsInState(StateCommitted, StateStopped) {
return nil, ErrCannotCloneInstance.WithParams(i.name, i.state)
}

clonedSidecars, err := i.sidecars.clone(name)
if err != nil {
return nil, err
Expand Down
13 changes: 11 additions & 2 deletions pkg/sidecars/tshark/tshark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,16 @@ func TestTsharkValidateConfig(t *testing.T) {
}

func TestTsharkClone(t *testing.T) {
testInstance, err := instance.New("testInstance", &system.SystemDependencies{})
testInstance, err := instance.New("testInstance",
&system.SystemDependencies{
Logger: logrus.New(),
})
require.NoError(t, err)

err = testInstance.Build().SetImage(context.Background(), "testImage")
require.NoError(t, err)

err = testInstance.Build().Commit(context.Background())
require.NoError(t, err)

tshark := &Tshark{
Expand All @@ -184,8 +193,8 @@ func TestTsharkClone(t *testing.T) {
UploadInterval: time.Minute * 5,
instance: testInstance,
}

clonePrefixName := "test-clone-prefix"

clone, err := tshark.Clone(clonePrefixName)
require.NoError(t, err)

Expand Down

0 comments on commit f86e86c

Please sign in to comment.