-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic assign mac to peer id (#11)
* added automatic assign mac to peer id * refactor config into separate file * add test validation error on bcache_test * log test * update docs and use single source of peerID
- Loading branch information
1 parent
9f85f9e
commit 3aa67f0
Showing
7 changed files
with
164 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package bcache | ||
|
||
import "github.com/weaveworks/mesh" | ||
|
||
// Config represents bcache configuration | ||
type Config struct { | ||
// PeerID is unique ID of this bcache | ||
// if PeerID = 0, it will be set using mac address | ||
PeerID uint64 | ||
|
||
// ListenAddr is listen addr of this bcache peer. | ||
// used to communicate with other peers | ||
ListenAddr string | ||
|
||
// Peers is the address of the known peers. | ||
// we don't need to know all of the other peers, | ||
// gossip protocol will find the other peers | ||
Peers []string | ||
|
||
// MaxKeys defines max number of keys in this cache | ||
MaxKeys int | ||
|
||
// Logger to be used | ||
// leave it nil to use default logger which do nothing | ||
Logger Logger | ||
} | ||
|
||
func (c *Config) setDefault() error { | ||
if c.PeerID == uint64(0) { | ||
mac, err := getMacAddress() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pName, err := mesh.PeerNameFromString(mac) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.PeerID = uint64(pName) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package bcache | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
// should be using mac addr | ||
c := Config{ | ||
ListenAddr: "127.0.0.1:12345", | ||
Peers: nil, | ||
MaxKeys: 1000, | ||
Logger: logrus.New(), | ||
} | ||
|
||
err := c.setDefault() | ||
require.NoError(t, err) | ||
require.NotEqual(t, uint64(0), c.PeerID) | ||
|
||
// should be using predefined id | ||
cfgManual := Config{ | ||
ListenAddr: "127.0.0.1:12345", | ||
Peers: nil, | ||
MaxKeys: 1000, | ||
Logger: logrus.New(), | ||
PeerID: uint64(2), | ||
} | ||
|
||
err = cfgManual.setDefault() | ||
require.NoError(t, err) | ||
require.Equal(t, uint64(2), cfgManual.PeerID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package bcache | ||
|
||
import "testing" | ||
|
||
func TestLog(t *testing.T) { | ||
logger := &nopLogger{} | ||
logger.Errorf("Nothing %s", "nothing") | ||
logger.Printf("Nothing %s", "nothing") | ||
logger.Debugf("Nothing %s", "nothing") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package bcache | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
) | ||
|
||
var ( | ||
errMacAddressNotFound = errors.New("mac address not found in this machine") | ||
) | ||
|
||
// getMacAddress get mac address of this machine | ||
func getMacAddress() (string, error) { | ||
interfaces, err := net.Interfaces() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, intf := range interfaces { | ||
if intf.Flags&net.FlagUp != 0 && intf.HardwareAddr != nil { | ||
return intf.HardwareAddr.String(), nil | ||
} | ||
} | ||
|
||
return "", errMacAddressNotFound | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package bcache | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetMacAddress(t *testing.T) { | ||
mac, err := getMacAddress() | ||
require.NoError(t, err) | ||
require.NotEqual(t, "", mac) | ||
} |