Skip to content

Commit

Permalink
cmd/createdkg: add existing cluster definition check (#1111)
Browse files Browse the repository at this point in the history
Add a check for existing cluster-definition.json file. This will error out and leave the current file intact.

category: bug
ticket: #1062
  • Loading branch information
dB2510 authored Sep 11, 2022
1 parent 50e6b5c commit 8b1993c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/createdkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func runCreateDKG(ctx context.Context, conf createDKGConfig) (err error) {
}
}()

if _, err := os.Stat(path.Join(conf.OutputDir, "cluster-definition.json")); err == nil {
return errors.New("existing cluster-definition.json found. Try again after deleting it")
}

// Don't allow cluster size to be less than 4.
if len(conf.OperatorENRs) < minNodes {
return errors.New("insufficient operator ENRs (min = 4)")
Expand Down
15 changes: 15 additions & 0 deletions cmd/createdkg_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd
import (
"context"
"os"
"path"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -126,3 +127,17 @@ func TestRequireOperatorENRFlag(t *testing.T) {
})
}
}

func TestExistingClusterDefinition(t *testing.T) {
outDir := ".charon"
b := []byte("sample definition")
require.NoError(t, os.Mkdir(".charon", 0o755))
require.NoError(t, os.WriteFile(path.Join(outDir, "cluster-definition.json"), b, 0o600))
defer func() {
require.NoError(t, os.RemoveAll(outDir))
}()

cmd := newCreateCmd(newCreateDKGCmd(runCreateDKG))
cmd.SetArgs([]string{"dkg", "--operator-enrs=enr:-JG4QG472ZVvl8ySSnUK9uNVDrP_hjkUrUqIxUC75aayzmDVQedXkjbqc7QKyOOS71VmlqnYzri_taV8ZesFYaoQSIOGAYHtv1WsgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQKwwq_CAld6oVKOrixE-JzMtvvNgb9yyI-_rwq4NFtajIN0Y3CCDhqDdWRwgg4u"})
require.EqualError(t, cmd.Execute(), "existing cluster-definition.json found. Try again after deleting it")
}

0 comments on commit 8b1993c

Please sign in to comment.