launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/agent/agent_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package agent_test
     5  
     6  import (
     7  	gc "launchpad.net/gocheck"
     8  
     9  	"launchpad.net/juju-core/agent"
    10  	"launchpad.net/juju-core/testing/testbase"
    11  )
    12  
    13  type suite struct {
    14  	testbase.LoggingSuite
    15  }
    16  
    17  var _ = gc.Suite(&suite{})
    18  
    19  var agentConfigTests = []struct {
    20  	about    string
    21  	params   agent.AgentConfigParams
    22  	checkErr string
    23  }{{
    24  	about:    "missing data directory",
    25  	checkErr: "data directory not found in configuration",
    26  }, {
    27  	about: "missing tag directory",
    28  	params: agent.AgentConfigParams{
    29  		DataDir: "/data/dir",
    30  	},
    31  	checkErr: "entity tag not found in configuration",
    32  }, {
    33  	about: "missing password",
    34  	params: agent.AgentConfigParams{
    35  		DataDir: "/data/dir",
    36  		Tag:     "omg",
    37  	},
    38  	checkErr: "password not found in configuration",
    39  }, {
    40  	about: "missing CA cert",
    41  	params: agent.AgentConfigParams{
    42  		DataDir:  "/data/dir",
    43  		Tag:      "omg",
    44  		Password: "sekrit",
    45  	},
    46  	checkErr: "CA certificate not found in configuration",
    47  }, {
    48  	about: "need either state or api addresses",
    49  	params: agent.AgentConfigParams{
    50  		DataDir:  "/data/dir",
    51  		Tag:      "omg",
    52  		Password: "sekrit",
    53  		CACert:   []byte("ca cert"),
    54  	},
    55  	checkErr: "state or API addresses not found in configuration",
    56  }, {
    57  	about: "invalid state address",
    58  	params: agent.AgentConfigParams{
    59  		DataDir:        "/data/dir",
    60  		Tag:            "omg",
    61  		Password:       "sekrit",
    62  		CACert:         []byte("ca cert"),
    63  		StateAddresses: []string{"localhost:8080", "bad-address"},
    64  	},
    65  	checkErr: `invalid state server address "bad-address"`,
    66  }, {
    67  	about: "invalid api address",
    68  	params: agent.AgentConfigParams{
    69  		DataDir:      "/data/dir",
    70  		Tag:          "omg",
    71  		Password:     "sekrit",
    72  		CACert:       []byte("ca cert"),
    73  		APIAddresses: []string{"localhost:8080", "bad-address"},
    74  	},
    75  	checkErr: `invalid API server address "bad-address"`,
    76  }, {
    77  	about: "good state addresses",
    78  	params: agent.AgentConfigParams{
    79  		DataDir:        "/data/dir",
    80  		Tag:            "omg",
    81  		Password:       "sekrit",
    82  		CACert:         []byte("ca cert"),
    83  		StateAddresses: []string{"localhost:1234"},
    84  	},
    85  }, {
    86  	about: "good api addresses",
    87  	params: agent.AgentConfigParams{
    88  		DataDir:      "/data/dir",
    89  		Tag:          "omg",
    90  		Password:     "sekrit",
    91  		CACert:       []byte("ca cert"),
    92  		APIAddresses: []string{"localhost:1234"},
    93  	},
    94  }, {
    95  	about: "both state and api addresses",
    96  	params: agent.AgentConfigParams{
    97  		DataDir:        "/data/dir",
    98  		Tag:            "omg",
    99  		Password:       "sekrit",
   100  		CACert:         []byte("ca cert"),
   101  		StateAddresses: []string{"localhost:1234"},
   102  		APIAddresses:   []string{"localhost:1235"},
   103  	},
   104  }, {
   105  	about: "everything...",
   106  	params: agent.AgentConfigParams{
   107  		DataDir:        "/data/dir",
   108  		Tag:            "omg",
   109  		Password:       "sekrit",
   110  		CACert:         []byte("ca cert"),
   111  		StateAddresses: []string{"localhost:1234"},
   112  		APIAddresses:   []string{"localhost:1235"},
   113  		Nonce:          "a nonce",
   114  	},
   115  }}
   116  
   117  func (*suite) TestNewAgentConfig(c *gc.C) {
   118  
   119  	for i, test := range agentConfigTests {
   120  		c.Logf("%v: %s", i, test.about)
   121  		_, err := agent.NewAgentConfig(test.params)
   122  		if test.checkErr == "" {
   123  			c.Assert(err, gc.IsNil)
   124  		} else {
   125  			c.Assert(err, gc.ErrorMatches, test.checkErr)
   126  		}
   127  	}
   128  }
   129  
   130  func (*suite) TestNewStateMachineConfig(c *gc.C) {
   131  	type testStruct struct {
   132  		about    string
   133  		params   agent.StateMachineConfigParams
   134  		checkErr string
   135  	}
   136  	var tests = []testStruct{{
   137  		about:    "missing state server cert",
   138  		checkErr: "state server cert not found in configuration",
   139  	}, {
   140  		about: "missing state server key",
   141  		params: agent.StateMachineConfigParams{
   142  			StateServerCert: []byte("server cert"),
   143  		},
   144  		checkErr: "state server key not found in configuration",
   145  	}}
   146  
   147  	for _, test := range agentConfigTests {
   148  		tests = append(tests, testStruct{
   149  			about: test.about,
   150  			params: agent.StateMachineConfigParams{
   151  				StateServerCert:   []byte("server cert"),
   152  				StateServerKey:    []byte("server key"),
   153  				AgentConfigParams: test.params,
   154  			},
   155  			checkErr: test.checkErr,
   156  		})
   157  	}
   158  
   159  	for i, test := range tests {
   160  		c.Logf("%v: %s", i, test.about)
   161  		_, err := agent.NewStateMachineConfig(test.params)
   162  		if test.checkErr == "" {
   163  			c.Assert(err, gc.IsNil)
   164  		} else {
   165  			c.Assert(err, gc.ErrorMatches, test.checkErr)
   166  		}
   167  	}
   168  }
   169  
   170  var attributeParams = agent.AgentConfigParams{
   171  	DataDir:        "/data/dir",
   172  	Tag:            "omg",
   173  	Password:       "sekrit",
   174  	CACert:         []byte("ca cert"),
   175  	StateAddresses: []string{"localhost:1234"},
   176  	APIAddresses:   []string{"localhost:1235"},
   177  	Nonce:          "a nonce",
   178  }
   179  
   180  func (*suite) TestAttributes(c *gc.C) {
   181  	conf, err := agent.NewAgentConfig(attributeParams)
   182  	c.Assert(err, gc.IsNil)
   183  	c.Assert(conf.DataDir(), gc.Equals, "/data/dir")
   184  	c.Assert(conf.Tag(), gc.Equals, "omg")
   185  	c.Assert(conf.Dir(), gc.Equals, "/data/dir/agents/omg")
   186  	c.Assert(conf.Nonce(), gc.Equals, "a nonce")
   187  }
   188  
   189  func (s *suite) TestApiAddressesCantWriteBack(c *gc.C) {
   190  	conf, err := agent.NewAgentConfig(attributeParams)
   191  	c.Assert(err, gc.IsNil)
   192  	value, err := conf.APIAddresses()
   193  	c.Assert(err, gc.IsNil)
   194  	c.Assert(value, gc.DeepEquals, []string{"localhost:1235"})
   195  	value[0] = "invalidAdr"
   196  	//Check out change hasn't gone back into the internals
   197  	newValue, err := conf.APIAddresses()
   198  	c.Assert(err, gc.IsNil)
   199  	c.Assert(newValue, gc.DeepEquals, []string{"localhost:1235"})
   200  }
   201  func (*suite) TestWriteAndRead(c *gc.C) {
   202  	testParams := attributeParams
   203  	testParams.DataDir = c.MkDir()
   204  	conf, err := agent.NewAgentConfig(testParams)
   205  	c.Assert(err, gc.IsNil)
   206  
   207  	c.Assert(conf.Write(), gc.IsNil)
   208  	reread, err := agent.ReadConf(conf.DataDir(), conf.Tag())
   209  	c.Assert(err, gc.IsNil)
   210  	// Since we can't directly poke the internals, we'll use the WriteCommands
   211  	// method.
   212  	confCommands, err := conf.WriteCommands()
   213  	c.Assert(err, gc.IsNil)
   214  	rereadCommands, err := reread.WriteCommands()
   215  	c.Assert(err, gc.IsNil)
   216  	c.Assert(confCommands, gc.DeepEquals, rereadCommands)
   217  }
   218  
   219  func (*suite) TestWriteNewPassword(c *gc.C) {
   220  
   221  	for i, test := range []struct {
   222  		about  string
   223  		params agent.AgentConfigParams
   224  	}{{
   225  		about: "good state addresses",
   226  		params: agent.AgentConfigParams{
   227  			DataDir:        c.MkDir(),
   228  			Tag:            "omg",
   229  			Password:       "sekrit",
   230  			CACert:         []byte("ca cert"),
   231  			StateAddresses: []string{"localhost:1234"},
   232  		},
   233  	}, {
   234  		about: "good api addresses",
   235  		params: agent.AgentConfigParams{
   236  			DataDir:      c.MkDir(),
   237  			Tag:          "omg",
   238  			Password:     "sekrit",
   239  			CACert:       []byte("ca cert"),
   240  			APIAddresses: []string{"localhost:1234"},
   241  		},
   242  	}, {
   243  		about: "both state and api addresses",
   244  		params: agent.AgentConfigParams{
   245  			DataDir:        c.MkDir(),
   246  			Tag:            "omg",
   247  			Password:       "sekrit",
   248  			CACert:         []byte("ca cert"),
   249  			StateAddresses: []string{"localhost:1234"},
   250  			APIAddresses:   []string{"localhost:1235"},
   251  		},
   252  	}} {
   253  		c.Logf("%v: %s", i, test.about)
   254  
   255  		conf, err := agent.NewAgentConfig(test.params)
   256  		c.Assert(err, gc.IsNil)
   257  		newPass, err := agent.WriteNewPassword(conf)
   258  		c.Assert(err, gc.IsNil)
   259  		// Show that the password is saved.
   260  		reread, err := agent.ReadConf(conf.DataDir(), conf.Tag())
   261  		c.Assert(agent.Password(conf), gc.Equals, agent.Password(reread))
   262  		c.Assert(newPass, gc.Equals, agent.Password(conf))
   263  	}
   264  }
   265  
   266  // Actual opening of state and api requires a lot more boiler plate to make
   267  // sure they are valid connections.  This is done in the cmd/jujud tests for
   268  // bootstrap, machine and unit tests.