github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/facades/controller/firewaller/mock_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package firewaller_test
     5  
     6  import (
     7  	"github.com/juju/collections/set"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/testing"
    10  	"gopkg.in/tomb.v2"
    11  
    12  	"github.com/juju/juju/apiserver/common/cloudspec"
    13  	"github.com/juju/juju/apiserver/common/firewall"
    14  	"github.com/juju/juju/core/network"
    15  	"github.com/juju/juju/core/status"
    16  	"github.com/juju/juju/rpc/params"
    17  	"github.com/juju/juju/state"
    18  )
    19  
    20  type mockCloudSpecAPI struct {
    21  	// TODO - implement when remaining firewaller tests become unit tests
    22  	cloudspec.CloudSpecAPI
    23  }
    24  
    25  type mockWatcher struct {
    26  	testing.Stub
    27  	tomb.Tomb
    28  }
    29  
    30  func (w *mockWatcher) Kill() {
    31  	w.MethodCall(w, "Kill")
    32  	w.Tomb.Kill(nil)
    33  }
    34  
    35  func (w *mockWatcher) Stop() error {
    36  	w.MethodCall(w, "Stop")
    37  	if err := w.NextErr(); err != nil {
    38  		return err
    39  	}
    40  	w.Tomb.Kill(nil)
    41  	return w.Tomb.Wait()
    42  }
    43  
    44  func (w *mockWatcher) Err() error {
    45  	w.MethodCall(w, "Err")
    46  	return w.Tomb.Err()
    47  }
    48  
    49  type mockStringsWatcher struct {
    50  	mockWatcher
    51  	changes chan []string
    52  }
    53  
    54  func newMockStringsWatcher() *mockStringsWatcher {
    55  	w := &mockStringsWatcher{changes: make(chan []string, 1)}
    56  	w.Tomb.Go(func() error {
    57  		<-w.Tomb.Dying()
    58  		return nil
    59  	})
    60  	return w
    61  }
    62  
    63  func (w *mockStringsWatcher) Changes() <-chan []string {
    64  	w.MethodCall(w, "Changes")
    65  	return w.changes
    66  }
    67  
    68  type mockRelation struct {
    69  	testing.Stub
    70  	firewall.Relation
    71  	id      int
    72  	ruw     *mockRelationUnitsWatcher
    73  	inScope set.Strings
    74  	status  status.StatusInfo
    75  }
    76  
    77  func newMockRelation(id int) *mockRelation {
    78  	return &mockRelation{
    79  		id:      id,
    80  		ruw:     newMockRelationUnitsWatcher(),
    81  		inScope: make(set.Strings),
    82  	}
    83  }
    84  
    85  func (r *mockRelation) Id() int {
    86  	r.MethodCall(r, "Id")
    87  	return r.id
    88  }
    89  
    90  func (r *mockRelation) WatchRelationIngressNetworks() state.StringsWatcher {
    91  	w := newMockStringsWatcher()
    92  	w.changes <- []string{"1.2.3.4/32"}
    93  	return w
    94  }
    95  
    96  func (r *mockRelation) SetStatus(info status.StatusInfo) error {
    97  	r.status = info
    98  	return nil
    99  }
   100  
   101  func newMockRelationUnitsWatcher() *mockRelationUnitsWatcher {
   102  	w := &mockRelationUnitsWatcher{changes: make(chan params.RelationUnitsChange, 1)}
   103  	w.Tomb.Go(func() error {
   104  		<-w.Tomb.Dying()
   105  		return nil
   106  	})
   107  	return w
   108  }
   109  
   110  type mockRelationUnitsWatcher struct {
   111  	mockWatcher
   112  	changes chan params.RelationUnitsChange
   113  }
   114  
   115  func (w *mockRelationUnitsWatcher) Changes() <-chan params.RelationUnitsChange {
   116  	return w.changes
   117  }
   118  
   119  type mockMachine struct {
   120  	testing.Stub
   121  	firewall.Machine
   122  
   123  	id               string
   124  	openedPortRanges *mockMachinePortRanges
   125  	isManual         bool
   126  }
   127  
   128  func newMockMachine(id string) *mockMachine {
   129  	return &mockMachine{
   130  		id: id,
   131  	}
   132  }
   133  
   134  func (st *mockMachine) Id() string {
   135  	st.MethodCall(st, "Id")
   136  	return st.id
   137  }
   138  
   139  func (st *mockMachine) IsManual() (bool, error) {
   140  	st.MethodCall(st, "IsManual")
   141  	if err := st.NextErr(); err != nil {
   142  		return false, err
   143  	}
   144  	return st.isManual, nil
   145  }
   146  
   147  func (st *mockMachine) OpenedPortRanges() (state.MachinePortRanges, error) {
   148  	st.MethodCall(st, "OpenedPortRanges")
   149  	if err := st.NextErr(); err != nil {
   150  		return nil, err
   151  	}
   152  	if st.openedPortRanges == nil {
   153  		return nil, errors.NotFoundf("opened port ranges for machine %q", st.id)
   154  	}
   155  	return st.openedPortRanges, nil
   156  }
   157  
   158  type mockMachinePortRanges struct {
   159  	state.MachinePortRanges
   160  
   161  	byUnit map[string]*mockUnitPortRanges
   162  }
   163  
   164  func newMockMachinePortRanges(unitRanges ...*mockUnitPortRanges) *mockMachinePortRanges {
   165  	byUnit := make(map[string]*mockUnitPortRanges)
   166  	for _, upr := range unitRanges {
   167  		byUnit[upr.unitName] = upr
   168  	}
   169  
   170  	return &mockMachinePortRanges{
   171  		byUnit: byUnit,
   172  	}
   173  }
   174  
   175  func (st *mockMachinePortRanges) ByUnit() map[string]state.UnitPortRanges {
   176  	out := make(map[string]state.UnitPortRanges)
   177  	for k, v := range st.byUnit {
   178  		out[k] = v
   179  	}
   180  
   181  	return out
   182  }
   183  
   184  type mockUnitPortRanges struct {
   185  	state.UnitPortRanges
   186  
   187  	unitName   string
   188  	byEndpoint network.GroupedPortRanges
   189  }
   190  
   191  func newMockUnitPortRanges(unitName string, byEndpoint network.GroupedPortRanges) *mockUnitPortRanges {
   192  	return &mockUnitPortRanges{
   193  		unitName:   unitName,
   194  		byEndpoint: byEndpoint,
   195  	}
   196  }
   197  
   198  func (st *mockUnitPortRanges) ByEndpoint() network.GroupedPortRanges {
   199  	return st.byEndpoint
   200  }