From f86e86ca65c758ceaef4cf72d78d1352a2756089 Mon Sep 17 00:00:00 2001 From: Moji Date: Mon, 14 Oct 2024 15:58:22 +0330 Subject: [PATCH] chore: add a check for Instance state when cloning (#570) * chore: add a check for Instance state when cloning * chore: update the func comment * fix: a failing unit test --- pkg/instance/errors.go | 1 + pkg/instance/instance.go | 6 +++++- pkg/sidecars/tshark/tshark_test.go | 13 +++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/instance/errors.go b/pkg/instance/errors.go index 497c09b..6c1e42e 100644 --- a/pkg/instance/errors.go +++ b/pkg/instance/errors.go @@ -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'") ) diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index 878c713..d9aa769 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -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 diff --git a/pkg/sidecars/tshark/tshark_test.go b/pkg/sidecars/tshark/tshark_test.go index e91eadc..47031fb 100644 --- a/pkg/sidecars/tshark/tshark_test.go +++ b/pkg/sidecars/tshark/tshark_test.go @@ -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{ @@ -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)