github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/application/resolved_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package application_test
     5  
     6  import (
     7  	"github.com/juju/cmd/cmdtesting"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/cmd/juju/application"
    14  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    15  )
    16  
    17  type ResolvedSuite struct {
    18  	testing.IsolationSuite
    19  
    20  	mockAPI *mockResolveAPI
    21  }
    22  
    23  func (s *ResolvedSuite) SetUpTest(c *gc.C) {
    24  	s.IsolationSuite.SetUpTest(c)
    25  	s.mockAPI = &mockResolveAPI{Stub: &testing.Stub{}}
    26  }
    27  
    28  var _ = gc.Suite(&ResolvedSuite{})
    29  
    30  func (s *ResolvedSuite) runResolved(c *gc.C, args []string) error {
    31  	store := jujuclienttesting.MinimalStore()
    32  	cmd := application.NewResolvedCommandForTest(s.mockAPI, s.mockAPI, store)
    33  	_, err := cmdtesting.RunCommand(c, cmd, args...)
    34  	return err
    35  }
    36  
    37  var resolvedTests = []struct {
    38  	args        []string
    39  	err         string
    40  	apiVersion  int
    41  	retry       bool
    42  	all         bool
    43  	legacyUnits []string
    44  	units       []string
    45  }{
    46  	{
    47  		err: `no unit specified`,
    48  	}, {
    49  		args: []string{"jeremy-fisher"},
    50  		err:  `unit name "jeremy-fisher" not valid`,
    51  	}, {
    52  		args: []string{"jeremy-fisher/99", "--all"},
    53  		err:  `specifying unit names\(s\) with --all not supported`,
    54  	}, {
    55  		apiVersion: 5,
    56  		args:       []string{"--all"},
    57  		err:        `resolving all units or more than one unit not support by this version of Juju`,
    58  	}, {
    59  		args: []string{"--all", "--no-retry"},
    60  		all:  true,
    61  	}, {
    62  		apiVersion:  5,
    63  		args:        []string{"jeremy-fisher/98", "jeremy-fisher/99"},
    64  		legacyUnits: []string{"jeremy-fisher/98", "jeremy-fisher/99"},
    65  		retry:       true,
    66  	}, {
    67  		apiVersion:  5,
    68  		args:        []string{"jeremy-fisher/99"},
    69  		legacyUnits: []string{"jeremy-fisher/99"},
    70  		retry:       true,
    71  	}, {
    72  		apiVersion:  5,
    73  		args:        []string{"jeremy-fisher/99", "--no-retry"},
    74  		legacyUnits: []string{"jeremy-fisher/99"},
    75  	}, {
    76  		args:  []string{"jeremy-fisher/98", "jeremy-fisher/99", "--no-retry"},
    77  		units: []string{"jeremy-fisher/98", "jeremy-fisher/99"},
    78  	}, {
    79  		args:  []string{"jeremy-fisher/98", "jeremy-fisher/99"},
    80  		units: []string{"jeremy-fisher/98", "jeremy-fisher/99"},
    81  		retry: true,
    82  	},
    83  }
    84  
    85  func (s *ResolvedSuite) TestResolved(c *gc.C) {
    86  	for i, t := range resolvedTests {
    87  		s.mockAPI.ResetCalls()
    88  		c.Logf("test %d: %v", i, t.args)
    89  		if t.apiVersion > 0 {
    90  			s.mockAPI.version = t.apiVersion
    91  		} else {
    92  			s.mockAPI.version = 6
    93  		}
    94  		err := s.runResolved(c, t.args)
    95  		if t.err != "" {
    96  			c.Assert(err, gc.ErrorMatches, t.err)
    97  			continue
    98  		} else {
    99  			c.Assert(err, jc.ErrorIsNil)
   100  		}
   101  		if len(t.legacyUnits) > 0 && !t.all {
   102  			expected := []string{"BestAPIVersion"}
   103  			for range t.legacyUnits {
   104  				expected = append(expected, "Resolved")
   105  			}
   106  			expected = append(expected, "Close", "Close")
   107  			s.mockAPI.CheckCallNames(c, expected...)
   108  			for j, legacyUnit := range t.legacyUnits {
   109  				s.mockAPI.CheckCall(c, j+1, "Resolved", legacyUnit, t.retry)
   110  			}
   111  		} else {
   112  			s.mockAPI.CheckCallNames(c, "BestAPIVersion", "ResolveUnitErrors", "Close")
   113  			s.mockAPI.CheckCall(c, 1, "ResolveUnitErrors", t.units, t.all, t.retry)
   114  		}
   115  	}
   116  }
   117  
   118  type mockResolveAPI struct {
   119  	*testing.Stub
   120  	version         int
   121  	addRelationFunc func(endpoints, viaCIDRs []string) (*params.AddRelationResults, error)
   122  }
   123  
   124  func (s mockResolveAPI) Close() error {
   125  	s.MethodCall(s, "Close")
   126  	return s.NextErr()
   127  }
   128  
   129  func (s mockResolveAPI) BestAPIVersion() int {
   130  	s.MethodCall(s, "BestAPIVersion")
   131  	return s.version
   132  }
   133  
   134  func (s mockResolveAPI) ResolveUnitErrors(units []string, all, retry bool) error {
   135  	s.MethodCall(s, "ResolveUnitErrors", units, all, retry)
   136  	return nil
   137  }
   138  
   139  func (s mockResolveAPI) Resolved(unit string, retry bool) error {
   140  	s.MethodCall(s, "Resolved", unit, retry)
   141  	return nil
   142  }