github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/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/proxy"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/api/base"
    13  	apitesting "github.com/juju/juju/api/base/testing"
    14  	"github.com/juju/juju/api/proxyupdater"
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/core/watcher"
    17  	coretesting "github.com/juju/juju/testing"
    18  )
    19  
    20  type ProxyUpdaterSuite struct {
    21  	coretesting.BaseSuite
    22  }
    23  
    24  var _ = gc.Suite(&ProxyUpdaterSuite{})
    25  
    26  func newAPI(c *gc.C, version int, args ...apitesting.APICall) (*int, *proxyupdater.API) {
    27  	apiCaller := apitesting.APICallChecker(c, args...)
    28  	api, err := proxyupdater.NewAPI(
    29  		apitesting.BestVersionCaller{
    30  			APICallerFunc: apiCaller.APICallerFunc,
    31  			BestVersion:   version,
    32  		}, names.NewUnitTag("u/0"))
    33  	c.Assert(err, gc.IsNil)
    34  	c.Assert(api, gc.NotNil)
    35  	c.Assert(apiCaller.CallCount, gc.Equals, 0)
    36  
    37  	return &apiCaller.CallCount, api
    38  }
    39  
    40  func (s *ProxyUpdaterSuite) TestNewAPISuccess(c *gc.C) {
    41  	newAPI(c, 2)
    42  }
    43  
    44  func (s *ProxyUpdaterSuite) TestNilAPICallerFails(c *gc.C) {
    45  	api, err := proxyupdater.NewAPI(nil, names.NewUnitTag("u/0"))
    46  	c.Check(api, gc.IsNil)
    47  	c.Check(err, gc.ErrorMatches, "caller is nil")
    48  }
    49  
    50  func (s *ProxyUpdaterSuite) TestNilTagFails(c *gc.C) {
    51  	apiCaller := apitesting.APICallChecker(c)
    52  	api, err := proxyupdater.NewAPI(apiCaller, nil)
    53  	c.Check(api, gc.IsNil)
    54  	c.Check(err, gc.ErrorMatches, "tag is nil")
    55  }
    56  
    57  func (s *ProxyUpdaterSuite) TestWatchForProxyConfigAndAPIHostPortChanges(c *gc.C) {
    58  	res := params.NotifyWatchResults{
    59  		Results: []params.NotifyWatchResult{{
    60  			NotifyWatcherId: "4242",
    61  		}},
    62  	}
    63  
    64  	fake := &struct {
    65  		watcher.NotifyWatcher
    66  	}{}
    67  	s.PatchValue(proxyupdater.NewNotifyWatcher, func(caller base.APICaller, result params.NotifyWatchResult) watcher.NotifyWatcher {
    68  		c.Assert(result, gc.DeepEquals, res.Results[0])
    69  		return fake
    70  	})
    71  
    72  	called, api := newAPI(c, 2, apitesting.APICall{
    73  		Facade:  "ProxyUpdater",
    74  		Method:  "WatchForProxyConfigAndAPIHostPortChanges",
    75  		Results: res,
    76  	})
    77  
    78  	watcher, err := api.WatchForProxyConfigAndAPIHostPortChanges()
    79  	c.Check(*called, jc.GreaterThan, 0)
    80  	c.Check(err, jc.ErrorIsNil)
    81  	c.Check(watcher, gc.Equals, fake)
    82  }
    83  
    84  func (s *ProxyUpdaterSuite) TestProxyConfig(c *gc.C) {
    85  	conf := params.ProxyConfigResult{
    86  		LegacyProxySettings: params.ProxyConfig{
    87  			HTTP:    "http-legacy",
    88  			HTTPS:   "https-legacy",
    89  			FTP:     "ftp-legacy",
    90  			NoProxy: "no-proxy-legacy",
    91  		},
    92  		JujuProxySettings: params.ProxyConfig{
    93  			HTTP:    "http-juju",
    94  			HTTPS:   "https-juju",
    95  			FTP:     "ftp-juju",
    96  			NoProxy: "no-proxy-juju",
    97  		},
    98  		APTProxySettings: params.ProxyConfig{
    99  			HTTP:  "http-apt",
   100  			HTTPS: "https-apt",
   101  			FTP:   "ftp-apt",
   102  		},
   103  		SnapProxySettings: params.ProxyConfig{
   104  			HTTP:  "http-snap",
   105  			HTTPS: "https-snap",
   106  		},
   107  	}
   108  
   109  	called, api := newAPI(c, 2, apitesting.APICall{
   110  		Facade: "ProxyUpdater",
   111  		Method: "ProxyConfig",
   112  		Results: params.ProxyConfigResults{
   113  			Results: []params.ProxyConfigResult{conf},
   114  		},
   115  	})
   116  
   117  	config, err := api.ProxyConfig()
   118  	c.Assert(*called, gc.Equals, 1)
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	c.Check(config.LegacyProxy, jc.DeepEquals, proxy.Settings{
   121  		Http:    "http-legacy",
   122  		Https:   "https-legacy",
   123  		Ftp:     "ftp-legacy",
   124  		NoProxy: "no-proxy-legacy",
   125  	})
   126  	c.Check(config.JujuProxy, jc.DeepEquals, proxy.Settings{
   127  		Http:    "http-juju",
   128  		Https:   "https-juju",
   129  		Ftp:     "ftp-juju",
   130  		NoProxy: "no-proxy-juju",
   131  	})
   132  	c.Check(config.APTProxy, jc.DeepEquals, proxy.Settings{
   133  		Http:  "http-apt",
   134  		Https: "https-apt",
   135  		Ftp:   "ftp-apt",
   136  	})
   137  	c.Check(config.SnapProxy, jc.DeepEquals, proxy.Settings{
   138  		Http:  "http-snap",
   139  		Https: "https-snap",
   140  	})
   141  }
   142  
   143  func (s *ProxyUpdaterSuite) TestProxyConfigV1(c *gc.C) {
   144  	conf := params.ProxyConfigResultV1{
   145  		ProxySettings: params.ProxyConfig{
   146  			HTTP:    "http-legacy",
   147  			HTTPS:   "https-legacy",
   148  			FTP:     "ftp-legacy",
   149  			NoProxy: "no-proxy-legacy",
   150  		},
   151  		APTProxySettings: params.ProxyConfig{
   152  			HTTP:  "http-apt",
   153  			HTTPS: "https-apt",
   154  			FTP:   "ftp-apt",
   155  		},
   156  	}
   157  
   158  	called, api := newAPI(c, 1, apitesting.APICall{
   159  		Facade: "ProxyUpdater",
   160  		Method: "ProxyConfig",
   161  		Results: params.ProxyConfigResultsV1{
   162  			Results: []params.ProxyConfigResultV1{conf},
   163  		},
   164  	})
   165  
   166  	config, err := api.ProxyConfig()
   167  	c.Assert(*called, gc.Equals, 1)
   168  	c.Assert(err, jc.ErrorIsNil)
   169  	c.Check(config.LegacyProxy, jc.DeepEquals, proxy.Settings{
   170  		Http:    "http-legacy",
   171  		Https:   "https-legacy",
   172  		Ftp:     "ftp-legacy",
   173  		NoProxy: "no-proxy-legacy",
   174  	})
   175  	c.Check(config.JujuProxy, jc.DeepEquals, proxy.Settings{})
   176  	c.Check(config.APTProxy, jc.DeepEquals, proxy.Settings{
   177  		Http:  "http-apt",
   178  		Https: "https-apt",
   179  		Ftp:   "ftp-apt",
   180  	})
   181  	c.Check(config.SnapProxy, jc.DeepEquals, proxy.Settings{})
   182  }