github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/client/get_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package client_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/charm.v5"
    12  
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/constraints"
    15  )
    16  
    17  type getSuite struct {
    18  	baseSuite
    19  }
    20  
    21  var _ = gc.Suite(&getSuite{})
    22  
    23  func (s *getSuite) TestClientServiceGetSmoketest(c *gc.C) {
    24  	s.setUpScenario(c)
    25  	results, err := s.APIState.Client().ServiceGet("wordpress")
    26  	c.Assert(err, jc.ErrorIsNil)
    27  	c.Assert(results, gc.DeepEquals, &params.ServiceGetResults{
    28  		Service: "wordpress",
    29  		Charm:   "wordpress",
    30  		Config: map[string]interface{}{
    31  			"blog-title": map[string]interface{}{
    32  				"type":        "string",
    33  				"value":       "My Title",
    34  				"description": "A descriptive title used for the blog.",
    35  				"default":     true,
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func (s *getSuite) TestServiceGetUnknownService(c *gc.C) {
    42  	apiclient := s.APIState.Client()
    43  	_, err := apiclient.ServiceGet("unknown")
    44  	c.Assert(err, gc.ErrorMatches, `service "unknown" not found`)
    45  }
    46  
    47  var getTests = []struct {
    48  	about       string
    49  	charm       string
    50  	constraints string
    51  	config      charm.Settings
    52  	expect      params.ServiceGetResults
    53  }{{
    54  	about:       "deployed service",
    55  	charm:       "dummy",
    56  	constraints: "mem=2G cpu-power=400",
    57  	config: charm.Settings{
    58  		// Different from default.
    59  		"title": "Look To Windward",
    60  		// Same as default.
    61  		"username": "admin001",
    62  		// Use default (but there's no charm default)
    63  		"skill-level": nil,
    64  		// Outlook is left unset.
    65  	},
    66  	expect: params.ServiceGetResults{
    67  		Config: map[string]interface{}{
    68  			"title": map[string]interface{}{
    69  				"description": "A descriptive title used for the service.",
    70  				"type":        "string",
    71  				"value":       "Look To Windward",
    72  			},
    73  			"outlook": map[string]interface{}{
    74  				"description": "No default outlook.",
    75  				"type":        "string",
    76  				"default":     true,
    77  			},
    78  			"username": map[string]interface{}{
    79  				"description": "The name of the initial account (given admin permissions).",
    80  				"type":        "string",
    81  				"value":       "admin001",
    82  			},
    83  			"skill-level": map[string]interface{}{
    84  				"description": "A number indicating skill.",
    85  				"type":        "int",
    86  				"default":     true,
    87  			},
    88  		},
    89  	},
    90  }, {
    91  	about: "deployed service  #2",
    92  	charm: "dummy",
    93  	config: charm.Settings{
    94  		// Set title to default.
    95  		"title": nil,
    96  		// Value when there's a default.
    97  		"username": "foobie",
    98  		// Numeric value.
    99  		"skill-level": 0,
   100  		// String value.
   101  		"outlook": "phlegmatic",
   102  	},
   103  	expect: params.ServiceGetResults{
   104  		Config: map[string]interface{}{
   105  			"title": map[string]interface{}{
   106  				"description": "A descriptive title used for the service.",
   107  				"type":        "string",
   108  				"value":       "My Title",
   109  				"default":     true,
   110  			},
   111  			"outlook": map[string]interface{}{
   112  				"description": "No default outlook.",
   113  				"type":        "string",
   114  				"value":       "phlegmatic",
   115  			},
   116  			"username": map[string]interface{}{
   117  				"description": "The name of the initial account (given admin permissions).",
   118  				"type":        "string",
   119  				"value":       "foobie",
   120  			},
   121  			"skill-level": map[string]interface{}{
   122  				"description": "A number indicating skill.",
   123  				"type":        "int",
   124  				// TODO(jam): 2013-08-28 bug #1217742
   125  				// we have to use float64() here, because the
   126  				// API does not preserve int types. This used
   127  				// to be int64() but we end up with a type
   128  				// mismatch when comparing the content
   129  				"value": float64(0),
   130  			},
   131  		},
   132  	},
   133  }, {
   134  	about: "subordinate service",
   135  	charm: "logging",
   136  	expect: params.ServiceGetResults{
   137  		Config: map[string]interface{}{},
   138  	},
   139  }}
   140  
   141  func (s *getSuite) TestServiceGet(c *gc.C) {
   142  	for i, t := range getTests {
   143  		c.Logf("test %d. %s", i, t.about)
   144  		ch := s.AddTestingCharm(c, t.charm)
   145  		svc := s.AddTestingService(c, fmt.Sprintf("test%d", i), ch)
   146  
   147  		var constraintsv constraints.Value
   148  		if t.constraints != "" {
   149  			constraintsv = constraints.MustParse(t.constraints)
   150  			err := svc.SetConstraints(constraintsv)
   151  			c.Assert(err, jc.ErrorIsNil)
   152  		}
   153  		if t.config != nil {
   154  			err := svc.UpdateConfigSettings(t.config)
   155  			c.Assert(err, jc.ErrorIsNil)
   156  		}
   157  		expect := t.expect
   158  		expect.Constraints = constraintsv
   159  		expect.Service = svc.Name()
   160  		expect.Charm = ch.Meta().Name
   161  		apiclient := s.APIState.Client()
   162  		got, err := apiclient.ServiceGet(svc.Name())
   163  		c.Assert(err, jc.ErrorIsNil)
   164  		c.Assert(*got, gc.DeepEquals, expect)
   165  	}
   166  }
   167  
   168  func (s *getSuite) TestServiceGetMaxResolutionInt(c *gc.C) {
   169  	// See the bug http://pad.lv/1217742
   170  	// ServiceGet ends up pushing a map[string]interface{} which containts
   171  	// an int64 through a JSON Marshal & Unmarshal which ends up changing
   172  	// the int64 into a float64. We will fix it if we find it is actually a
   173  	// problem.
   174  	const nonFloatInt = (int64(1) << 54) + 1
   175  	const asFloat = float64(nonFloatInt)
   176  	c.Assert(int64(asFloat), gc.Not(gc.Equals), nonFloatInt)
   177  	c.Assert(int64(asFloat)+1, gc.Equals, nonFloatInt)
   178  
   179  	ch := s.AddTestingCharm(c, "dummy")
   180  	svc := s.AddTestingService(c, "test-service", ch)
   181  
   182  	err := svc.UpdateConfigSettings(map[string]interface{}{"skill-level": nonFloatInt})
   183  	c.Assert(err, jc.ErrorIsNil)
   184  	got, err := s.APIState.Client().ServiceGet(svc.Name())
   185  	c.Assert(err, jc.ErrorIsNil)
   186  	c.Assert(got.Config["skill-level"], gc.DeepEquals, map[string]interface{}{
   187  		"description": "A number indicating skill.",
   188  		"type":        "int",
   189  		"value":       asFloat,
   190  	})
   191  }
   192  
   193  func (s *getSuite) TestServiceGetCharmURL(c *gc.C) {
   194  	s.setUpScenario(c)
   195  	charmURL, err := s.APIState.Client().ServiceGetCharmURL("wordpress")
   196  	c.Assert(err, jc.ErrorIsNil)
   197  	c.Assert(charmURL.String(), gc.Equals, "local:quantal/wordpress-3")
   198  }