github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/api/client_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api_test
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  
    10  	gc "launchpad.net/gocheck"
    11  
    12  	"launchpad.net/juju-core/charm"
    13  	jujutesting "launchpad.net/juju-core/juju/testing"
    14  	"launchpad.net/juju-core/state/api"
    15  	"launchpad.net/juju-core/state/api/params"
    16  	"launchpad.net/juju-core/testing"
    17  	jc "launchpad.net/juju-core/testing/checkers"
    18  )
    19  
    20  type clientSuite struct {
    21  	jujutesting.JujuConnSuite
    22  }
    23  
    24  var _ = gc.Suite(&clientSuite{})
    25  
    26  // TODO(jam) 2013-08-27 http://pad.lv/1217282
    27  // Right now most of the direct tests for api.Client behavior are in
    28  // state/apiserver/client/*_test.go
    29  
    30  func (s *clientSuite) TestCloseMultipleOk(c *gc.C) {
    31  	client := s.APIState.Client()
    32  	c.Assert(client.Close(), gc.IsNil)
    33  	c.Assert(client.Close(), gc.IsNil)
    34  	c.Assert(client.Close(), gc.IsNil)
    35  }
    36  
    37  func (s *clientSuite) TestAddLocalCharm(c *gc.C) {
    38  	charmArchive := testing.Charms.Bundle(c.MkDir(), "dummy")
    39  	curl := charm.MustParseURL(
    40  		fmt.Sprintf("local:quantal/%s-%d", charmArchive.Meta().Name, charmArchive.Revision()),
    41  	)
    42  	client := s.APIState.Client()
    43  
    44  	// Test the sanity checks first.
    45  	_, err := client.AddLocalCharm(charm.MustParseURL("cs:quantal/wordpress-1"), nil)
    46  	c.Assert(err, gc.ErrorMatches, `expected charm URL with local: schema, got "cs:quantal/wordpress-1"`)
    47  
    48  	// Upload an archive with its original revision.
    49  	savedURL, err := client.AddLocalCharm(curl, charmArchive)
    50  	c.Assert(err, gc.IsNil)
    51  	c.Assert(savedURL.String(), gc.Equals, curl.String())
    52  
    53  	// Upload a charm directory with changed revision.
    54  	charmDir := testing.Charms.ClonedDir(c.MkDir(), "dummy")
    55  	charmDir.SetDiskRevision(42)
    56  	savedURL, err = client.AddLocalCharm(curl, charmDir)
    57  	c.Assert(err, gc.IsNil)
    58  	c.Assert(savedURL.Revision, gc.Equals, 42)
    59  
    60  	// Upload a charm directory again, revision should be bumped.
    61  	savedURL, err = client.AddLocalCharm(curl, charmDir)
    62  	c.Assert(err, gc.IsNil)
    63  	c.Assert(savedURL.String(), gc.Equals, curl.WithRevision(43).String())
    64  
    65  	// Finally, try the NotImplementedError by mocking the server
    66  	// address to a handler that returns 405 Method Not Allowed for
    67  	// POST.
    68  	http.HandleFunc("/charms", func(w http.ResponseWriter, r *http.Request) {
    69  		if r.Method == "POST" {
    70  			http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
    71  		}
    72  	})
    73  	go func() {
    74  		err = http.ListenAndServe(":8900", nil)
    75  		c.Assert(err, gc.IsNil)
    76  	}()
    77  
    78  	api.SetServerRoot(client, "http://localhost:8900")
    79  	_, err = client.AddLocalCharm(curl, charmArchive)
    80  	c.Assert(err, jc.Satisfies, params.IsCodeNotImplemented)
    81  }