github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/backups/package_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package backups_test
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"io/ioutil"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/juju/cmd"
    14  	"github.com/juju/errors"
    15  	jc "github.com/juju/testing/checkers"
    16  	gc "gopkg.in/check.v1"
    17  
    18  	apibackups "github.com/juju/juju/api/backups"
    19  	"github.com/juju/juju/apiserver/params"
    20  	"github.com/juju/juju/cmd/juju/backups"
    21  	jujutesting "github.com/juju/juju/testing"
    22  )
    23  
    24  // MetaResultString is the expected output of running dumpMetadata() on
    25  // s.metaresult.
    26  var MetaResultString = `
    27  backup ID:       "spam"
    28  checksum:        ""
    29  checksum format: ""
    30  size (B):        0
    31  stored:          0001-01-01 00:00:00 +0000 UTC
    32  started:         0001-01-01 00:00:00 +0000 UTC
    33  finished:        0001-01-01 00:00:00 +0000 UTC
    34  notes:           ""
    35  model ID:        ""
    36  machine ID:      ""
    37  created on host: ""
    38  juju version:    0.0.0
    39  `[1:]
    40  
    41  func TestPackage(t *testing.T) {
    42  	gc.TestingT(t)
    43  }
    44  
    45  type BaseBackupsSuite struct {
    46  	jujutesting.FakeJujuXDGDataHomeSuite
    47  
    48  	command    cmd.Command
    49  	metaresult *params.BackupsMetadataResult
    50  	data       string
    51  
    52  	filename string
    53  }
    54  
    55  func (s *BaseBackupsSuite) SetUpTest(c *gc.C) {
    56  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    57  
    58  	s.metaresult = &params.BackupsMetadataResult{
    59  		ID: "spam",
    60  	}
    61  	s.data = "<compressed archive data>"
    62  }
    63  
    64  func (s *BaseBackupsSuite) TearDownTest(c *gc.C) {
    65  	if s.filename != "" {
    66  		err := os.Remove(s.filename)
    67  		if !os.IsNotExist(err) {
    68  			c.Check(err, jc.ErrorIsNil)
    69  		}
    70  	}
    71  
    72  	s.FakeJujuXDGDataHomeSuite.TearDownTest(c)
    73  }
    74  
    75  func (s *BaseBackupsSuite) patchAPIClient(client backups.APIClient) {
    76  	s.PatchValue(backups.NewAPIClient,
    77  		func(c *backups.CommandBase) (backups.APIClient, error) {
    78  			return client, nil
    79  		},
    80  	)
    81  }
    82  
    83  func (s *BaseBackupsSuite) setSuccess() *fakeAPIClient {
    84  	client := &fakeAPIClient{metaresult: s.metaresult}
    85  	s.patchAPIClient(client)
    86  	return client
    87  }
    88  
    89  func (s *BaseBackupsSuite) setFailure(failure string) *fakeAPIClient {
    90  	client := &fakeAPIClient{err: errors.New(failure)}
    91  	s.patchAPIClient(client)
    92  	return client
    93  }
    94  
    95  func (s *BaseBackupsSuite) setDownload() *fakeAPIClient {
    96  	client := s.setSuccess()
    97  	client.archive = ioutil.NopCloser(bytes.NewBufferString(s.data))
    98  	return client
    99  }
   100  
   101  func (s *BaseBackupsSuite) checkArchive(c *gc.C) {
   102  	c.Assert(s.filename, gc.Not(gc.Equals), "")
   103  	archive, err := os.Open(s.filename)
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	defer archive.Close()
   106  
   107  	data, err := ioutil.ReadAll(archive)
   108  	c.Check(string(data), gc.Equals, s.data)
   109  }
   110  
   111  func (s *BaseBackupsSuite) checkStd(c *gc.C, ctx *cmd.Context, out, err string) {
   112  	c.Check(ctx.Stdin.(*bytes.Buffer).Len(), gc.Equals, 0)
   113  	jujutesting.CheckString(c, ctx.Stdout.(*bytes.Buffer).String(), out)
   114  	jujutesting.CheckString(c, ctx.Stderr.(*bytes.Buffer).String(), err)
   115  }
   116  
   117  type fakeAPIClient struct {
   118  	metaresult *params.BackupsMetadataResult
   119  	archive    io.ReadCloser
   120  	err        error
   121  
   122  	calls []string
   123  	args  []string
   124  	idArg string
   125  	notes string
   126  }
   127  
   128  func (f *fakeAPIClient) Check(c *gc.C, id, notes string, calls ...string) {
   129  	c.Check(f.calls, jc.DeepEquals, calls)
   130  	c.Check(f.idArg, gc.Equals, id)
   131  	c.Check(f.notes, gc.Equals, notes)
   132  }
   133  
   134  func (c *fakeAPIClient) Create(notes string) (*params.BackupsMetadataResult, error) {
   135  	c.calls = append(c.calls, "Create")
   136  	c.args = append(c.args, "notes")
   137  	c.notes = notes
   138  	if c.err != nil {
   139  		return nil, c.err
   140  	}
   141  	return c.metaresult, nil
   142  }
   143  
   144  func (c *fakeAPIClient) Info(id string) (*params.BackupsMetadataResult, error) {
   145  	c.calls = append(c.calls, "Info")
   146  	c.args = append(c.args, "id")
   147  	c.idArg = id
   148  	if c.err != nil {
   149  		return nil, c.err
   150  	}
   151  	return c.metaresult, nil
   152  }
   153  
   154  func (c *fakeAPIClient) List() (*params.BackupsListResult, error) {
   155  	c.calls = append(c.calls, "List")
   156  	if c.err != nil {
   157  		return nil, c.err
   158  	}
   159  	var result params.BackupsListResult
   160  	result.List = []params.BackupsMetadataResult{*c.metaresult}
   161  	return &result, nil
   162  }
   163  
   164  func (c *fakeAPIClient) Download(id string) (io.ReadCloser, error) {
   165  	c.calls = append(c.calls, "Download")
   166  	c.args = append(c.args, "id")
   167  	c.idArg = id
   168  	if c.err != nil {
   169  		return nil, c.err
   170  	}
   171  	return c.archive, nil
   172  }
   173  
   174  func (c *fakeAPIClient) Upload(ar io.ReadSeeker, meta params.BackupsMetadataResult) (string, error) {
   175  	c.args = append(c.args, "ar", "meta")
   176  	if c.err != nil {
   177  		return "", c.err
   178  	}
   179  	return c.metaresult.ID, nil
   180  }
   181  
   182  func (c *fakeAPIClient) Remove(id string) error {
   183  	c.calls = append(c.calls, "Remove")
   184  	c.args = append(c.args, "id")
   185  	c.idArg = id
   186  	if c.err != nil {
   187  		return c.err
   188  	}
   189  	return nil
   190  }
   191  
   192  func (c *fakeAPIClient) Close() error {
   193  	return nil
   194  }
   195  
   196  func (c *fakeAPIClient) RestoreReader(io.ReadSeeker, *params.BackupsMetadataResult, apibackups.ClientConnection) error {
   197  	return nil
   198  }
   199  
   200  func (c *fakeAPIClient) Restore(string, apibackups.ClientConnection) error {
   201  	return nil
   202  }