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