github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/api/controller/firewaller/application_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package firewaller_test
     5  
     6  import (
     7  	"github.com/juju/names/v5"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/api/controller/firewaller"
    12  	"github.com/juju/juju/core/network"
    13  	"github.com/juju/juju/core/watcher/watchertest"
    14  	"github.com/juju/juju/rpc/params"
    15  	"github.com/juju/juju/state"
    16  )
    17  
    18  type applicationSuite struct {
    19  	firewallerSuite
    20  
    21  	apiApplication *firewaller.Application
    22  }
    23  
    24  var _ = gc.Suite(&applicationSuite{})
    25  
    26  func (s *applicationSuite) SetUpTest(c *gc.C) {
    27  	s.firewallerSuite.SetUpTest(c)
    28  
    29  	apiUnit, err := s.firewaller.Unit(s.units[0].Tag().(names.UnitTag))
    30  	c.Assert(err, jc.ErrorIsNil)
    31  	s.apiApplication, err = apiUnit.Application()
    32  	c.Assert(err, jc.ErrorIsNil)
    33  }
    34  
    35  func (s *applicationSuite) TearDownTest(c *gc.C) {
    36  	s.firewallerSuite.TearDownTest(c)
    37  }
    38  
    39  func (s *applicationSuite) TestName(c *gc.C) {
    40  	c.Assert(s.apiApplication.Name(), gc.Equals, s.application.Name())
    41  }
    42  
    43  func (s *applicationSuite) TestTag(c *gc.C) {
    44  	c.Assert(s.apiApplication.Tag(), gc.Equals, names.NewApplicationTag(s.application.Name()))
    45  }
    46  
    47  func (s *applicationSuite) TestWatch(c *gc.C) {
    48  	s.WaitForModelWatchersIdle(c, s.Model.UUID())
    49  
    50  	w, err := s.apiApplication.Watch()
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	wc := watchertest.NewNotifyWatcherC(c, w)
    53  	defer wc.AssertStops()
    54  
    55  	// Initial event.
    56  	wc.AssertOneChange()
    57  
    58  	// Change something and check it's detected.
    59  	err = s.application.MergeExposeSettings(nil)
    60  	c.Assert(err, jc.ErrorIsNil)
    61  	wc.AssertOneChange()
    62  
    63  	// Destroy the application and check it's detected.
    64  	err = s.application.Destroy()
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	wc.AssertOneChange()
    67  }
    68  
    69  func (s *applicationSuite) TestExposeInfo(c *gc.C) {
    70  	err := s.application.MergeExposeSettings(map[string]state.ExposedEndpoint{
    71  		"": {
    72  			ExposeToSpaceIDs: []string{network.AlphaSpaceId},
    73  			ExposeToCIDRs:    []string{"10.0.0.0/16", "192.168.0.0/24"},
    74  		},
    75  	})
    76  	c.Assert(err, jc.ErrorIsNil)
    77  
    78  	isExposed, exposedEndpoints, err := s.apiApplication.ExposeInfo()
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	c.Assert(isExposed, jc.IsTrue)
    81  	c.Assert(exposedEndpoints, gc.DeepEquals, map[string]params.ExposedEndpoint{
    82  		"": {
    83  			ExposeToSpaces: []string{network.AlphaSpaceId},
    84  			ExposeToCIDRs:  []string{"10.0.0.0/16", "192.168.0.0/24"},
    85  		},
    86  	})
    87  
    88  	err = s.application.ClearExposed()
    89  	c.Assert(err, jc.ErrorIsNil)
    90  
    91  	isExposed, exposedEndpoints, err = s.apiApplication.ExposeInfo()
    92  	c.Assert(err, jc.ErrorIsNil)
    93  	c.Assert(isExposed, jc.IsFalse)
    94  	c.Assert(exposedEndpoints, gc.HasLen, 0)
    95  }