Skip to content

Commit

Permalink
refactor test and fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Jun 12, 2024
1 parent ec346b0 commit fd7c5f2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
13 changes: 3 additions & 10 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const (
"These peers can be either Feeder or regular nodes."
p2pFeederNodeUsage = "EXPERIMENTAL: Run juno as a feeder node which will only sync from feeder gateway and gossip the new" +
" blocks to the network."
p2pPrivateKeyUsage = "EXPERIMENTAL: Hexadecimal representation of a private key on the Ed25519 elliptic curve."
p2pPrivateKeyUsage = "EXPERIMENTAL: Hexadecimal representation of a private key on the Ed25519 elliptic curve. Also accepts a file path."
metricsUsage = "Enables the Prometheus metrics endpoint on the default port."
metricsHostUsage = "The interface on which the Prometheus endpoint will listen for requests."
metricsPortUsage = "The port on which the Prometheus endpoint will listen for requests."
Expand Down Expand Up @@ -291,15 +291,8 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "P2P Private Key:") {
key := strings.TrimPrefix(line, "P2P Private Key:")
key = strings.TrimSpace(key)
config.P2PPrivateKey = key
break
}
}
scanner.Scan()
config.P2PPrivateKey = strings.TrimSpace(strings.TrimPrefix(scanner.Text(), "P2P Private Key:"))

if err := scanner.Err(); err != nil {
return err

Check warning on line 298 in cmd/juno/juno.go

View check run for this annotation

Codecov / codecov/patch

cmd/juno/juno.go#L298

Added line #L298 was not covered by tests
Expand Down
63 changes: 42 additions & 21 deletions cmd/juno/juno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,29 +682,50 @@ func TestGenP2PKeyPair(t *testing.T) {
}

func TestP2PPrivateKey(t *testing.T) {
// Create a temporary file
tmpfile, err := os.CreateTemp("", "temp")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up

// Write the keys to the file
privateKey := "38c54451f80617955671608da06ed6dd1749d66263103a9118235ab1ea2b3e4503bb86b895e17fc1618359d13e998ca05aa60751b762bf26ac570ac1e575ad81"
content := fmt.Sprintf("P2P Private Key: %s\n", privateKey)
if _, err := tmpfile.WriteString(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}

// Pass the lines to the command
config := new(node.Config)
cmd := juno.NewCmd(config, func(_ *cobra.Command, _ []string) error { return nil })
cmd.SetArgs([]string{"--p2p-private-key", tmpfile.Name()})
require.NoError(t, cmd.Execute())
require.Equal(t, privateKey, config.P2PPrivateKey)
t.Run("private key with message in a file", func(t *testing.T) {
tmpfile, err := os.CreateTemp("", "temp")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

content := fmt.Sprintf("P2P Private Key: %s\n", privateKey)
if _, err := tmpfile.WriteString(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}

config := new(node.Config)
cmd := juno.NewCmd(config, func(_ *cobra.Command, _ []string) error { return nil })
cmd.SetArgs([]string{"--p2p-private-key", tmpfile.Name()})
require.NoError(t, cmd.Execute())
require.Equal(t, privateKey, config.P2PPrivateKey)
})

t.Run("plain private key in a file", func(t *testing.T) {
tmpfile, err := os.CreateTemp("", "temp")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

if _, err := tmpfile.WriteString(privateKey); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}

config := new(node.Config)
cmd := juno.NewCmd(config, func(_ *cobra.Command, _ []string) error { return nil })
cmd.SetArgs([]string{"--p2p-private-key", tmpfile.Name()})
require.NoError(t, cmd.Execute())
require.Equal(t, privateKey, config.P2PPrivateKey)
})
}

func tempCfgFile(t *testing.T, cfg string) string {
Expand Down

0 comments on commit fd7c5f2

Please sign in to comment.