github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/caasfirewaller/firewaller_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasfirewaller_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/juju/names.v2"
    10  	"gopkg.in/juju/worker.v1/workertest"
    11  
    12  	"github.com/juju/juju/apiserver/common"
    13  	"github.com/juju/juju/apiserver/facades/controller/caasfirewaller"
    14  	"github.com/juju/juju/apiserver/params"
    15  	apiservertesting "github.com/juju/juju/apiserver/testing"
    16  	"github.com/juju/juju/state"
    17  	statetesting "github.com/juju/juju/state/testing"
    18  	coretesting "github.com/juju/juju/testing"
    19  )
    20  
    21  var _ = gc.Suite(&CAASFirewallerSuite{})
    22  
    23  type CAASFirewallerSuite struct {
    24  	coretesting.BaseSuite
    25  
    26  	st                  *mockState
    27  	applicationsChanges chan []string
    28  	appExposedChanges   chan struct{}
    29  
    30  	resources  *common.Resources
    31  	authorizer *apiservertesting.FakeAuthorizer
    32  	facade     *caasfirewaller.Facade
    33  }
    34  
    35  func (s *CAASFirewallerSuite) SetUpTest(c *gc.C) {
    36  	s.BaseSuite.SetUpTest(c)
    37  
    38  	s.applicationsChanges = make(chan []string, 1)
    39  	s.appExposedChanges = make(chan struct{}, 1)
    40  	appExposedWatcher := statetesting.NewMockNotifyWatcher(s.appExposedChanges)
    41  	s.st = &mockState{
    42  		application: mockApplication{
    43  			life:    state.Alive,
    44  			watcher: appExposedWatcher,
    45  		},
    46  		applicationsWatcher: statetesting.NewMockStringsWatcher(s.applicationsChanges),
    47  		appExposedWatcher:   appExposedWatcher,
    48  	}
    49  	s.AddCleanup(func(c *gc.C) { workertest.DirtyKill(c, s.st.applicationsWatcher) })
    50  	s.AddCleanup(func(c *gc.C) { workertest.DirtyKill(c, s.st.appExposedWatcher) })
    51  
    52  	s.resources = common.NewResources()
    53  	s.authorizer = &apiservertesting.FakeAuthorizer{
    54  		Tag:        names.NewMachineTag("0"),
    55  		Controller: true,
    56  	}
    57  
    58  	facade, err := caasfirewaller.NewFacade(s.resources, s.authorizer, s.st)
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	s.facade = facade
    61  }
    62  
    63  func (s *CAASFirewallerSuite) TestPermission(c *gc.C) {
    64  	s.authorizer = &apiservertesting.FakeAuthorizer{
    65  		Tag: names.NewMachineTag("0"),
    66  	}
    67  	_, err := caasfirewaller.NewFacade(s.resources, s.authorizer, s.st)
    68  	c.Assert(err, gc.ErrorMatches, "permission denied")
    69  }
    70  
    71  func (s *CAASFirewallerSuite) TestWatchApplications(c *gc.C) {
    72  	applicationNames := []string{"db2", "hadoop"}
    73  	s.applicationsChanges <- applicationNames
    74  	result, err := s.facade.WatchApplications()
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Assert(result.Error, gc.IsNil)
    77  	c.Assert(result.StringsWatcherId, gc.Equals, "1")
    78  	c.Assert(result.Changes, jc.DeepEquals, applicationNames)
    79  
    80  	resource := s.resources.Get("1")
    81  	c.Assert(resource, gc.Equals, s.st.applicationsWatcher)
    82  }
    83  
    84  func (s *CAASFirewallerSuite) TestWatchApplication(c *gc.C) {
    85  	s.appExposedChanges <- struct{}{}
    86  
    87  	results, err := s.facade.Watch(params.Entities{
    88  		Entities: []params.Entity{
    89  			{Tag: "application-gitlab"},
    90  			{Tag: "unit-gitlab-0"},
    91  		},
    92  	})
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	c.Assert(results.Results, gc.HasLen, 2)
    95  	c.Assert(results.Results[0].Error, gc.IsNil)
    96  	c.Assert(results.Results[1].Error, jc.DeepEquals, &params.Error{
    97  		Message: "permission denied",
    98  		Code:    "unauthorized access",
    99  	})
   100  
   101  	c.Assert(results.Results[0].NotifyWatcherId, gc.Equals, "1")
   102  	resource := s.resources.Get("1")
   103  	c.Assert(resource, gc.Equals, s.st.appExposedWatcher)
   104  }
   105  
   106  func (s *CAASFirewallerSuite) TestIsExposed(c *gc.C) {
   107  	s.st.application.exposed = true
   108  	results, err := s.facade.IsExposed(params.Entities{
   109  		Entities: []params.Entity{
   110  			{Tag: "application-gitlab"},
   111  			{Tag: "unit-gitlab-0"},
   112  		},
   113  	})
   114  	c.Assert(err, jc.ErrorIsNil)
   115  	c.Assert(results, jc.DeepEquals, params.BoolResults{
   116  		Results: []params.BoolResult{{
   117  			Result: true,
   118  		}, {
   119  			Error: &params.Error{
   120  				Message: `"unit-gitlab-0" is not a valid application tag`,
   121  			},
   122  		}},
   123  	})
   124  }
   125  
   126  func (s *CAASFirewallerSuite) TestLife(c *gc.C) {
   127  	results, err := s.facade.Life(params.Entities{
   128  		Entities: []params.Entity{
   129  			{Tag: "application-gitlab"},
   130  			{Tag: "machine-0"},
   131  		},
   132  	})
   133  	c.Assert(err, jc.ErrorIsNil)
   134  	c.Assert(results, jc.DeepEquals, params.LifeResults{
   135  		Results: []params.LifeResult{{
   136  			Life: params.Alive,
   137  		}, {
   138  			Error: &params.Error{
   139  				Code:    "unauthorized access",
   140  				Message: "permission denied",
   141  			},
   142  		}},
   143  	})
   144  }
   145  
   146  func (s *CAASFirewallerSuite) TestApplicationConfig(c *gc.C) {
   147  	results, err := s.facade.ApplicationsConfig(params.Entities{
   148  		Entities: []params.Entity{
   149  			{Tag: "application-gitlab"},
   150  			{Tag: "unit-gitlab-0"},
   151  		},
   152  	})
   153  	c.Assert(err, jc.ErrorIsNil)
   154  	c.Assert(results.Results, gc.HasLen, 2)
   155  	c.Assert(results.Results[0].Error, gc.IsNil)
   156  	c.Assert(results.Results[1].Error, jc.DeepEquals, &params.Error{
   157  		Message: `"unit-gitlab-0" is not a valid application tag`,
   158  	})
   159  	c.Assert(results.Results[0].Config, jc.DeepEquals, map[string]interface{}{"foo": "bar"})
   160  }