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