github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/proxyupdater/proxyupdater_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package proxyupdater_test
     5  
     6  import (
     7  	"time"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/apiserver/common"
    14  	"github.com/juju/juju/apiserver/params"
    15  	"github.com/juju/juju/apiserver/proxyupdater"
    16  	apiservertesting "github.com/juju/juju/apiserver/testing"
    17  	"github.com/juju/juju/environs/config"
    18  	"github.com/juju/juju/network"
    19  	"github.com/juju/juju/state"
    20  	coretesting "github.com/juju/juju/testing"
    21  	"github.com/juju/juju/worker/workertest"
    22  	"github.com/juju/testing"
    23  )
    24  
    25  type ProxyUpdaterSuite struct {
    26  	coretesting.BaseSuite
    27  	apiservertesting.StubNetwork
    28  
    29  	state      *stubBackend
    30  	resources  *common.Resources
    31  	authorizer apiservertesting.FakeAuthorizer
    32  	facade     *proxyupdater.ProxyUpdaterAPI
    33  	tag        names.MachineTag
    34  }
    35  
    36  var _ = gc.Suite(&ProxyUpdaterSuite{})
    37  
    38  func (s *ProxyUpdaterSuite) SetUpSuite(c *gc.C) {
    39  	s.BaseSuite.SetUpSuite(c)
    40  	s.StubNetwork.SetUpSuite(c)
    41  }
    42  
    43  func (s *ProxyUpdaterSuite) SetUpTest(c *gc.C) {
    44  	s.BaseSuite.SetUpTest(c)
    45  	s.resources = common.NewResources()
    46  	s.AddCleanup(func(_ *gc.C) { s.resources.StopAll() })
    47  	s.authorizer = apiservertesting.FakeAuthorizer{
    48  		Tag:            names.NewMachineTag("1"),
    49  		EnvironManager: false,
    50  	}
    51  	s.tag = names.NewMachineTag("1")
    52  	s.state = &stubBackend{}
    53  	s.state.SetUp(c)
    54  	s.AddCleanup(func(_ *gc.C) { s.state.Kill() })
    55  
    56  	var err error
    57  	s.facade, err = proxyupdater.NewAPIWithBacking(s.state, s.resources, s.authorizer)
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	c.Assert(s.facade, gc.NotNil)
    60  
    61  	// Shouldn't have any calls yet
    62  	apiservertesting.CheckMethodCalls(c, s.state.Stub)
    63  }
    64  
    65  func (s *ProxyUpdaterSuite) TestWatchForProxyConfigAndAPIHostPortChanges(c *gc.C) {
    66  	// WatchForProxyConfigAndAPIHostPortChanges combines WatchForModelConfigChanges
    67  	// and WatchAPIHostPorts. Check that they are both called and we get the
    68  	result := s.facade.WatchForProxyConfigAndAPIHostPortChanges(s.oneEntity())
    69  	c.Assert(result.Results, gc.HasLen, 1)
    70  	c.Assert(result.Results[0].Error, gc.IsNil)
    71  
    72  	s.state.Stub.CheckCallNames(c,
    73  		"WatchForModelConfigChanges",
    74  		"WatchAPIHostPorts",
    75  	)
    76  
    77  	// Verify the watcher resource was registered.
    78  	c.Assert(s.resources.Count(), gc.Equals, 1)
    79  	resource := s.resources.Get(result.Results[0].NotifyWatcherId)
    80  	watcher, ok := resource.(state.NotifyWatcher)
    81  	c.Assert(ok, jc.IsTrue)
    82  
    83  	// Verify the initial event was consumed.
    84  	select {
    85  	case <-watcher.Changes():
    86  		c.Fatalf("initial event never consumed")
    87  	case <-time.After(coretesting.ShortWait):
    88  	}
    89  }
    90  
    91  func (s *ProxyUpdaterSuite) oneEntity() params.Entities {
    92  	entities := params.Entities{
    93  		make([]params.Entity, 1),
    94  	}
    95  	entities.Entities[0].Tag = s.tag.String()
    96  	return entities
    97  }
    98  
    99  func (s *ProxyUpdaterSuite) TestProxyConfig(c *gc.C) {
   100  	// Check that the ProxyConfig combines data from ModelConfig and APIHostPorts
   101  	cfg := s.facade.ProxyConfig(s.oneEntity())
   102  
   103  	s.state.Stub.CheckCallNames(c,
   104  		"ModelConfig",
   105  		"APIHostPorts",
   106  	)
   107  
   108  	noProxy := "0.1.2.3,0.1.2.4,0.1.2.5"
   109  
   110  	r := params.ProxyConfigResult{
   111  		ProxySettings: params.ProxyConfig{
   112  			HTTP: "http proxy", HTTPS: "https proxy", FTP: "", NoProxy: noProxy},
   113  		APTProxySettings: params.ProxyConfig{
   114  			HTTP: "http://http proxy", HTTPS: "https://https proxy", FTP: "", NoProxy: ""},
   115  	}
   116  	c.Assert(cfg.Results[0], jc.DeepEquals, r)
   117  }
   118  
   119  func (s *ProxyUpdaterSuite) TestProxyConfigExtendsExisting(c *gc.C) {
   120  	// Check that the ProxyConfig combines data from ModelConfig and APIHostPorts
   121  	s.state.SetModelConfig(coretesting.Attrs{
   122  		"http-proxy":  "http proxy",
   123  		"https-proxy": "https proxy",
   124  		"no-proxy":    "9.9.9.9",
   125  	})
   126  	cfg := s.facade.ProxyConfig(s.oneEntity())
   127  	s.state.Stub.CheckCallNames(c,
   128  		"ModelConfig",
   129  		"APIHostPorts",
   130  	)
   131  
   132  	expectedNoProxy := "0.1.2.3,0.1.2.4,0.1.2.5,9.9.9.9"
   133  
   134  	c.Assert(cfg.Results[0], jc.DeepEquals, params.ProxyConfigResult{
   135  		ProxySettings: params.ProxyConfig{
   136  			HTTP: "http proxy", HTTPS: "https proxy", FTP: "", NoProxy: expectedNoProxy},
   137  		APTProxySettings: params.ProxyConfig{
   138  			HTTP: "http://http proxy", HTTPS: "https://https proxy", FTP: "", NoProxy: ""},
   139  	})
   140  }
   141  
   142  func (s *ProxyUpdaterSuite) TestProxyConfigNoDuplicates(c *gc.C) {
   143  	// Check that the ProxyConfig combines data from ModelConfig and APIHostPorts
   144  	s.state.SetModelConfig(coretesting.Attrs{
   145  		"http-proxy":  "http proxy",
   146  		"https-proxy": "https proxy",
   147  		"no-proxy":    "0.1.2.3",
   148  	})
   149  	cfg := s.facade.ProxyConfig(s.oneEntity())
   150  	s.state.Stub.CheckCallNames(c,
   151  		"ModelConfig",
   152  		"APIHostPorts",
   153  	)
   154  
   155  	expectedNoProxy := "0.1.2.3,0.1.2.4,0.1.2.5"
   156  
   157  	c.Assert(cfg.Results[0], jc.DeepEquals, params.ProxyConfigResult{
   158  		ProxySettings: params.ProxyConfig{
   159  			HTTP: "http proxy", HTTPS: "https proxy", FTP: "", NoProxy: expectedNoProxy},
   160  		APTProxySettings: params.ProxyConfig{
   161  			HTTP: "http://http proxy", HTTPS: "https://https proxy", FTP: "", NoProxy: ""},
   162  	})
   163  }
   164  
   165  type stubBackend struct {
   166  	*testing.Stub
   167  
   168  	EnvConfig   *config.Config
   169  	c           *gc.C
   170  	configAttrs coretesting.Attrs
   171  	hpWatcher   workertest.NotAWatcher
   172  	confWatcher workertest.NotAWatcher
   173  }
   174  
   175  func (sb *stubBackend) SetUp(c *gc.C) {
   176  	sb.Stub = &testing.Stub{}
   177  	sb.c = c
   178  	sb.configAttrs = coretesting.Attrs{
   179  		"http-proxy":  "http proxy",
   180  		"https-proxy": "https proxy",
   181  	}
   182  	sb.hpWatcher = workertest.NewFakeWatcher(1, 1)
   183  	sb.confWatcher = workertest.NewFakeWatcher(1, 1)
   184  }
   185  
   186  func (sb *stubBackend) Kill() {
   187  	sb.hpWatcher.Kill()
   188  	sb.confWatcher.Kill()
   189  }
   190  
   191  func (sb *stubBackend) SetModelConfig(ca coretesting.Attrs) {
   192  	sb.configAttrs = ca
   193  }
   194  
   195  func (sb *stubBackend) ModelConfig() (*config.Config, error) {
   196  	sb.MethodCall(sb, "ModelConfig")
   197  	if err := sb.NextErr(); err != nil {
   198  		return nil, err
   199  	}
   200  	return coretesting.CustomModelConfig(sb.c, sb.configAttrs), nil
   201  }
   202  
   203  func (sb *stubBackend) APIHostPorts() ([][]network.HostPort, error) {
   204  	sb.MethodCall(sb, "APIHostPorts")
   205  	if err := sb.NextErr(); err != nil {
   206  		return nil, err
   207  	}
   208  	hps := [][]network.HostPort{
   209  		network.NewHostPorts(1234, "0.1.2.3"),
   210  		network.NewHostPorts(1234, "0.1.2.4"),
   211  		network.NewHostPorts(1234, "0.1.2.5"),
   212  	}
   213  	return hps, nil
   214  }
   215  
   216  func (sb *stubBackend) WatchAPIHostPorts() state.NotifyWatcher {
   217  	sb.MethodCall(sb, "WatchAPIHostPorts")
   218  	return sb.hpWatcher
   219  }
   220  
   221  func (sb *stubBackend) WatchForModelConfigChanges() state.NotifyWatcher {
   222  	sb.MethodCall(sb, "WatchForModelConfigChanges")
   223  	return sb.confWatcher
   224  }