From 7dc27988f3372b40d66361c6cfb87444c94332ce Mon Sep 17 00:00:00 2001 From: Michele Tartara <mtartara@google.com> Date: Tue, 5 Feb 2013 09:43:16 +0000 Subject: [PATCH] Make Confd client usable for testing Allow the Confd client to be able to connect to an arbitrary server instead of just the real one running on a cluster. This is meant to be used for testing. If a data collector using the Confd client is tested with shelltest, it will need to receive through the CLI the address and port of the server, and also the FQDN for which information is being requested. In normal usage it will likely be the name of the node the data collector is running on, but for testing we need a way to explicitly specify it so that it corresponds to the one used inside the configuration files that the test confd server is reading. So, optConfdAddr and optConfdPort and optNode are provided by this patch, even if not used, because they are an important part of the modifications for making the Confd client testable. Signed-off-by: Michele Tartara <mtartara@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- src/Ganeti/Confd/Client.hs | 15 ++++++++++---- src/Ganeti/DataCollectors/CLI.hs | 34 +++++++++++++++++++++++++++++++ src/Ganeti/DataCollectors/Drbd.hs | 2 +- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/Ganeti/Confd/Client.hs b/src/Ganeti/Confd/Client.hs index 78a782024..d7a5ea048 100644 --- a/src/Ganeti/Confd/Client.hs +++ b/src/Ganeti/Confd/Client.hs @@ -31,6 +31,7 @@ module Ganeti.Confd.Client import Control.Concurrent import Control.Monad import Data.List +import Data.Maybe import qualified Network.Socket as S import System.Posix.Time import qualified Text.JSON as J @@ -42,16 +43,22 @@ import qualified Ganeti.Constants as C import Ganeti.Hash import Ganeti.Ssconf --- | Builds a properly initialized ConfdClient -getConfdClient :: IO ConfdClient -getConfdClient = S.withSocketsDo $ do +-- | Builds a properly initialized ConfdClient. +-- The parameters (an IP address and the port number for the Confd client +-- to connect to) are mainly meant for testing purposes. If they are not +-- provided, the list of master candidates and the default port number will +-- be used. +getConfdClient :: Maybe String -> Maybe Int -> IO ConfdClient +getConfdClient addr portNum = S.withSocketsDo $ do hmac <- getClusterHmac candList <- getMasterCandidatesIps Nothing peerList <- case candList of (Ok p) -> return p (Bad msg) -> fail msg - return . ConfdClient hmac peerList $ fromIntegral C.defaultConfdPort + let addrList = maybe peerList (:[]) addr + port = fromMaybe C.defaultConfdPort portNum + return . ConfdClient hmac addrList $ fromIntegral port -- | Sends a query to all the Confd servers the client is connected to. -- Returns the most up-to-date result according to the serial number, diff --git a/src/Ganeti/DataCollectors/CLI.hs b/src/Ganeti/DataCollectors/CLI.hs index c79307807..3c58a3f09 100644 --- a/src/Ganeti/DataCollectors/CLI.hs +++ b/src/Ganeti/DataCollectors/CLI.hs @@ -36,6 +36,9 @@ module Ganeti.DataCollectors.CLI , oShowComp , oDrbdPairing , oDrbdStatus + , oNode + , oConfdAddr + , oConfdPort , genericOptions ) where @@ -43,6 +46,8 @@ import System.Console.GetOpt import Ganeti.BasicTypes import Ganeti.Common as Common +import Ganeti.Utils + -- * Data types @@ -55,6 +60,10 @@ data Options = Options -- status information , optDrbdPairing :: Maybe FilePath -- ^ Path to the file containing pairings -- between instances and DRBD minors + , optNode :: Maybe String -- ^ Info are requested for this node + , optConfdAddr :: Maybe String -- ^ IP address of the Confd server + , optConfdPort :: Maybe Int -- ^ The port of the Confd server to + -- connect to } deriving Show -- | Default values for the command line options. @@ -65,6 +74,9 @@ defaultOptions = Options , optShowVer = False , optDrbdStatus = Nothing , optDrbdPairing = Nothing + , optNode = Nothing + , optConfdAddr = Nothing + , optConfdPort = Nothing } -- | Abbreviation for the option type. @@ -93,6 +105,28 @@ oDrbdStatus = "the DRBD status FILE", OptComplFile) +oNode :: OptType +oNode = + ( Option "n" ["node"] + (ReqArg (\ n o -> Ok o { optNode = Just n }) "NODE") + "the FQDN of the NODE about which information is requested", + OptComplFile) + +oConfdAddr :: OptType +oConfdAddr = + ( Option "a" ["address"] + (ReqArg (\ a o -> Ok o { optConfdAddr = Just a }) "IP_ADDR") + "the IP address of the Confd server to connect to", + OptComplFile) + +oConfdPort :: OptType +oConfdPort = + (Option "p" ["port"] + (reqWithConversion (tryRead "reading port") + (\port opts -> Ok opts { optConfdPort = Just port }) "PORT") + "Network port of the Confd server to connect to", + OptComplInteger) + -- | Generic options. genericOptions :: [GenericOptType Options] genericOptions = [ oShowVer diff --git a/src/Ganeti/DataCollectors/Drbd.hs b/src/Ganeti/DataCollectors/Drbd.hs index 8f0aeb452..f15479dd2 100644 --- a/src/Ganeti/DataCollectors/Drbd.hs +++ b/src/Ganeti/DataCollectors/Drbd.hs @@ -90,7 +90,7 @@ arguments = [ArgCompletion OptComplFile 0 (Just 0)] getPairingInfo :: Maybe String -> IO (BT.Result [DrbdInstMinor]) getPairingInfo Nothing = do curNode <- getHostName - client <- getConfdClient + client <- getConfdClient Nothing Nothing reply <- query client ReqNodeDrbd $ PlainQuery curNode return $ case fmap (J.readJSONs . confdReplyAnswer) reply of -- GitLab