Skip to content

Commit

Permalink
add unit tests for config package
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh committed Mar 12, 2024
1 parent ef31bc4 commit 62b3f1c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 14 deletions.
31 changes: 17 additions & 14 deletions cmd/zetatool/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ package config

import (
"encoding/json"
"os"

"github.com/spf13/afero"
)

var AppFs = afero.NewOsFs()

const (
Flag = "config"
defaultCfgFileName = "InboundTxFilter_config.json"
ZetaURL = "http://46.4.15.110:1317" //http://100.71.167.102:26657
TssAddressBTC = "bc1qm24wp577nk8aacckv8np465z3dvmu7ry45el6y"
TssAddressEVM = "0x70e967acfcc17c3941e87562161406d41676fd83"
BtcExplorer = "https://blockstream.info/api/address/bc1qm24wp577nk8aacckv8np465z3dvmu7ry45el6y/txs"
EthRPC = "https://rpc.ankr.com/eth/2da24e4a1fd28f2bec1569eceb2c38a5694b7f5c83fd24c69ae714a89a514f9b"
ConnectorAddress = "0x000007Cf399229b2f5A4D043F20E90C9C98B7C6a"
CustodyAddress = "0x0000030Ec64DF25301d8414eE5a29588C4B0dE10"
EvmStartBlock = 19200110
EvmMaxRange = 1000
Flag = "config"
defaultCfgFileName = "InboundTxFilter_config.json"
ZetaURL = "http://46.4.15.110:1317" //http://100.71.167.102:26657
TssAddressBTC = "bc1qm24wp577nk8aacckv8np465z3dvmu7ry45el6y"
TssAddressEVM = "0x70e967acfcc17c3941e87562161406d41676fd83"
BtcExplorer = "https://blockstream.info/api/address/bc1qm24wp577nk8aacckv8np465z3dvmu7ry45el6y/txs"
EthRPC = "https://rpc.ankr.com/eth/2da24e4a1fd28f2bec1569eceb2c38a5694b7f5c83fd24c69ae714a89a514f9b"
ConnectorAddress = "0x000007Cf399229b2f5A4D043F20E90C9C98B7C6a"
CustodyAddress = "0x0000030Ec64DF25301d8414eE5a29588C4B0dE10"
EvmStartBlock uint64 = 19200110
EvmMaxRange uint64 = 1000
)

type Config struct {
Expand Down Expand Up @@ -50,12 +53,12 @@ func (c *Config) Save() error {
if err != nil {
return err
}
err = os.WriteFile(defaultCfgFileName, file, 0600)
err = afero.WriteFile(AppFs, defaultCfgFileName, file, 0600)
return err
}

func (c *Config) Read(filename string) error {
data, err := os.ReadFile(filename)
data, err := afero.ReadFile(AppFs, filename)
if err != nil {
return err
}
Expand Down
80 changes: 80 additions & 0 deletions cmd/zetatool/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package config

import (
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"testing"
)

func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
require.Equal(t, cfg.EthRPC, EthRPC)
require.Equal(t, cfg.ZetaURL, ZetaURL)
require.Equal(t, cfg.TssAddressBTC, TssAddressBTC)
require.Equal(t, cfg.TssAddressEVM, TssAddressEVM)
require.Equal(t, cfg.BtcExplorer, BtcExplorer)
require.Equal(t, cfg.ConnectorAddress, ConnectorAddress)
require.Equal(t, cfg.CustodyAddress, CustodyAddress)
require.Equal(t, cfg.EvmStartBlock, EvmStartBlock)
require.Equal(t, cfg.EvmMaxRange, EvmMaxRange)
}

func TestGetConfig(t *testing.T) {
AppFs = afero.NewMemMapFs()
defaultCfg := DefaultConfig()

t.Run("No config file specified", func(t *testing.T) {
cfg, err := GetConfig("")
require.NoError(t, err)
require.Equal(t, cfg, defaultCfg)

exists, err := afero.Exists(AppFs, defaultCfgFileName)
require.NoError(t, err)
require.True(t, exists)
})

t.Run("config file specified", func(t *testing.T) {
cfg, err := GetConfig(defaultCfgFileName)
require.NoError(t, err)
require.Equal(t, cfg, defaultCfg)
})
}

func TestConfig_Read(t *testing.T) {
AppFs = afero.NewMemMapFs()
cfg, err := GetConfig("")
require.NoError(t, err)

t.Run("read existing file", func(t *testing.T) {
c := &Config{}
err := c.Read(defaultCfgFileName)
require.NoError(t, err)
require.Equal(t, c, cfg)
})

t.Run("read non-existent file", func(t *testing.T) {
err := AppFs.Remove(defaultCfgFileName)
require.NoError(t, err)
c := &Config{}
err = c.Read(defaultCfgFileName)
require.ErrorContains(t, err, "file does not exist")
require.NotEqual(t, c, cfg)
})
}

func TestConfig_Save(t *testing.T) {
AppFs = afero.NewMemMapFs()
cfg := DefaultConfig()
cfg.EvmMaxRange = uint64(2000)

t.Run("save modified cfg", func(t *testing.T) {
err := cfg.Save()
require.NoError(t, err)

newCfg, err := GetConfig(defaultCfgFileName)
require.NoError(t, err)
require.Equal(t, cfg, newCfg)
})

// Should test invalid json encoding but currently not able to without interface
}

0 comments on commit 62b3f1c

Please sign in to comment.