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