github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/action/fetch_test.go (about)

     1  // Copyright 2014-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package action_test
     5  
     6  import (
     7  	"bytes"
     8  	"errors"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/juju/names"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/apiserver/common"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/cmd/juju/action"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type FetchSuite struct {
    22  	BaseActionSuite
    23  	subcommand *action.FetchCommand
    24  }
    25  
    26  var _ = gc.Suite(&FetchSuite{})
    27  
    28  func (s *FetchSuite) SetUpTest(c *gc.C) {
    29  	s.BaseActionSuite.SetUpTest(c)
    30  }
    31  
    32  func (s *FetchSuite) TestHelp(c *gc.C) {
    33  	s.checkHelp(c, s.subcommand)
    34  }
    35  
    36  func (s *FetchSuite) TestInit(c *gc.C) {
    37  	tests := []struct {
    38  		should      string
    39  		args        []string
    40  		expectTag   names.ActionTag
    41  		expectError string
    42  	}{{
    43  		should:      "fail with missing arg",
    44  		args:        []string{},
    45  		expectError: "no action UUID specified",
    46  	}, {
    47  		should:      "fail with multiple args",
    48  		args:        []string{"12345", "54321"},
    49  		expectError: `unrecognized args: \["54321"\]`,
    50  	}}
    51  
    52  	for i, t := range tests {
    53  		s.subcommand = &action.FetchCommand{}
    54  		c.Logf("test %d: it should %s: juju actions fetch %s", i,
    55  			t.should, strings.Join(t.args, " "))
    56  		err := testing.InitCommand(s.subcommand, t.args)
    57  		if t.expectError != "" {
    58  			c.Check(err, gc.ErrorMatches, t.expectError)
    59  		}
    60  	}
    61  }
    62  
    63  func (s *FetchSuite) TestRun(c *gc.C) {
    64  	tests := []struct {
    65  		should         string
    66  		withTags       params.FindTagsResults
    67  		withResults    []params.ActionResult
    68  		withAPIError   string
    69  		expectedErr    string
    70  		expectedOutput string
    71  	}{{
    72  		should:       "pass api error through properly",
    73  		withAPIError: "api call error",
    74  		expectedErr:  "api call error",
    75  	}, {
    76  		should:      "fail with no results",
    77  		withTags:    tagsForIdPrefix(validActionId),
    78  		withResults: []params.ActionResult{},
    79  		expectedErr: `actions for identifier "` + validActionId + `" not found`,
    80  	}, {
    81  		should:      "error correctly with multiple results",
    82  		withTags:    tagsForIdPrefix(validActionId, validActionTagString),
    83  		withResults: []params.ActionResult{{}, {}},
    84  		expectedErr: "too many results for action " + validActionId,
    85  	}, {
    86  		should:   "pass through an error from the API server",
    87  		withTags: tagsForIdPrefix(validActionId, validActionTagString),
    88  		withResults: []params.ActionResult{{
    89  			Error: common.ServerError(errors.New("an apiserver error")),
    90  		}},
    91  		expectedErr: "an apiserver error",
    92  	}, {
    93  		should:   "pretty-print action output",
    94  		withTags: tagsForIdPrefix(validActionId, validActionTagString),
    95  		withResults: []params.ActionResult{{
    96  			Status:  "complete",
    97  			Message: "oh dear",
    98  			Output: map[string]interface{}{
    99  				"foo": map[string]interface{}{
   100  					"bar": "baz",
   101  				},
   102  			},
   103  			Enqueued:  time.Date(2015, time.February, 14, 8, 13, 0, 0, time.UTC),
   104  			Started:   time.Date(2015, time.February, 14, 8, 15, 0, 0, time.UTC),
   105  			Completed: time.Date(2015, time.February, 14, 8, 15, 30, 0, time.UTC),
   106  		}},
   107  		expectedOutput: "" +
   108  			"message: oh dear\n" +
   109  			"results:\n" +
   110  			"  foo:\n" +
   111  			"    bar: baz\n" +
   112  			"status: complete\n" +
   113  			"timing:\n" +
   114  			"  completed: 2015-02-14 08:15:30 \\+0000 UTC\n" +
   115  			"  enqueued: 2015-02-14 08:13:00 \\+0000 UTC\n" +
   116  			"  started: 2015-02-14 08:15:00 \\+0000 UTC\n" +
   117  			"",
   118  	}}
   119  
   120  	for i, t := range tests {
   121  		func() { // for the defer of restoring patch function
   122  			client := &fakeAPIClient{
   123  				actionTagMatches: t.withTags,
   124  				actionResults:    t.withResults,
   125  			}
   126  			if t.withAPIError != "" {
   127  				client.apiErr = errors.New(t.withAPIError)
   128  			}
   129  			defer s.BaseActionSuite.patchAPIClient(client)()
   130  
   131  			s.subcommand = &action.FetchCommand{}
   132  			c.Logf("test %d: it should %s", i, t.should)
   133  
   134  			ctx, err := testing.RunCommand(c, s.subcommand, validActionId)
   135  			if t.expectedErr != "" || t.withAPIError != "" {
   136  				c.Check(err, gc.ErrorMatches, t.expectedErr)
   137  			} else {
   138  				c.Assert(err, gc.IsNil)
   139  				c.Check(ctx.Stdout.(*bytes.Buffer).String(), gc.Matches, t.expectedOutput)
   140  			}
   141  		}()
   142  	}
   143  }