github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/service/get_test.go (about)

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