github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/caasoperatorprovisioner/provisioner_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasoperatorprovisioner_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/apiserver/common"
    15  	"github.com/juju/juju/apiserver/facades/controller/caasoperatorprovisioner"
    16  	"github.com/juju/juju/apiserver/params"
    17  	apiservertesting "github.com/juju/juju/apiserver/testing"
    18  	"github.com/juju/juju/state"
    19  	coretesting "github.com/juju/juju/testing"
    20  	"github.com/juju/juju/version"
    21  )
    22  
    23  var _ = gc.Suite(&CAASProvisionerSuite{})
    24  
    25  type CAASProvisionerSuite struct {
    26  	coretesting.BaseSuite
    27  
    28  	resources               *common.Resources
    29  	authorizer              *apiservertesting.FakeAuthorizer
    30  	api                     *caasoperatorprovisioner.API
    31  	st                      *mockState
    32  	storageProviderRegistry *mockStorageProviderRegistry
    33  	storagePoolManager      *mockStoragePoolManager
    34  }
    35  
    36  func (s *CAASProvisionerSuite) SetUpTest(c *gc.C) {
    37  	s.BaseSuite.SetUpTest(c)
    38  
    39  	s.resources = common.NewResources()
    40  	s.AddCleanup(func(_ *gc.C) { s.resources.StopAll() })
    41  
    42  	s.authorizer = &apiservertesting.FakeAuthorizer{
    43  		Tag:        names.NewMachineTag("0"),
    44  		Controller: true,
    45  	}
    46  
    47  	s.st = newMockState()
    48  	s.storageProviderRegistry = &mockStorageProviderRegistry{}
    49  	s.storagePoolManager = &mockStoragePoolManager{}
    50  	api, err := caasoperatorprovisioner.NewCAASOperatorProvisionerAPI(s.resources, s.authorizer, s.st, s.storageProviderRegistry, s.storagePoolManager)
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	s.api = api
    53  }
    54  
    55  func (s *CAASProvisionerSuite) TestPermission(c *gc.C) {
    56  	s.authorizer = &apiservertesting.FakeAuthorizer{
    57  		Tag: names.NewMachineTag("0"),
    58  	}
    59  	_, err := caasoperatorprovisioner.NewCAASOperatorProvisionerAPI(s.resources, s.authorizer, s.st, s.storageProviderRegistry, s.storagePoolManager)
    60  	c.Assert(err, gc.ErrorMatches, "permission denied")
    61  }
    62  
    63  func (s *CAASProvisionerSuite) TestWatchApplications(c *gc.C) {
    64  	applicationNames := []string{"db2", "hadoop"}
    65  	s.st.applicationWatcher.changes <- applicationNames
    66  	result, err := s.api.WatchApplications()
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	c.Assert(result.Error, gc.IsNil)
    69  	c.Assert(result.StringsWatcherId, gc.Equals, "1")
    70  	c.Assert(result.Changes, jc.DeepEquals, applicationNames)
    71  
    72  	resource := s.resources.Get("1")
    73  	c.Assert(resource, gc.NotNil)
    74  	c.Assert(resource, gc.Implements, new(state.StringsWatcher))
    75  }
    76  
    77  func (s *CAASProvisionerSuite) TestSetPasswords(c *gc.C) {
    78  	s.st.app = &mockApplication{
    79  		tag: names.NewApplicationTag("app"),
    80  	}
    81  
    82  	args := params.EntityPasswords{
    83  		Changes: []params.EntityPassword{
    84  			{Tag: "application-app", Password: "xxx-12345678901234567890"},
    85  			{Tag: "application-another", Password: "yyy-12345678901234567890"},
    86  			{Tag: "machine-0", Password: "zzz-12345678901234567890"},
    87  		},
    88  	}
    89  	results, err := s.api.SetPasswords(args)
    90  	c.Assert(err, jc.ErrorIsNil)
    91  	c.Assert(results, gc.DeepEquals, params.ErrorResults{
    92  		Results: []params.ErrorResult{
    93  			{nil},
    94  			{&params.Error{Message: "entity application-another not found", Code: "not found"}},
    95  			{&params.Error{Message: "permission denied", Code: "unauthorized access"}},
    96  		},
    97  	})
    98  	c.Assert(s.st.app.password, gc.Equals, "xxx-12345678901234567890")
    99  }
   100  
   101  func (s *CAASProvisionerSuite) TestLife(c *gc.C) {
   102  	s.st.app = &mockApplication{
   103  		tag: names.NewApplicationTag("app"),
   104  	}
   105  	results, err := s.api.Life(params.Entities{
   106  		Entities: []params.Entity{
   107  			{Tag: "application-app"},
   108  			{Tag: "machine-0"},
   109  		},
   110  	})
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	c.Assert(results, jc.DeepEquals, params.LifeResults{
   113  		Results: []params.LifeResult{{
   114  			Life: params.Alive,
   115  		}, {
   116  			Error: &params.Error{
   117  				Code:    "unauthorized access",
   118  				Message: "permission denied",
   119  			},
   120  		}},
   121  	})
   122  }
   123  
   124  func (s *CAASProvisionerSuite) TestOperatorProvisioningInfoDefault(c *gc.C) {
   125  	result, err := s.api.OperatorProvisioningInfo()
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	c.Assert(result, jc.DeepEquals, params.OperatorProvisioningInfo{
   128  		ImagePath:    fmt.Sprintf("jujusolutions/caas-jujud-operator:%s", version.Current.String()),
   129  		Version:      version.Current,
   130  		APIAddresses: []string{"10.0.0.1:1"},
   131  		Tags: map[string]string{
   132  			"juju-model-uuid":      coretesting.ModelTag.Id(),
   133  			"juju-controller-uuid": coretesting.ControllerTag.Id()},
   134  		CharmStorage: params.KubernetesFilesystemParams{
   135  			Size:       uint64(1024),
   136  			Provider:   "kubernetes",
   137  			Attributes: map[string]interface{}{"foo": "bar"},
   138  			Tags: map[string]string{
   139  				"juju-model-uuid":      coretesting.ModelTag.Id(),
   140  				"juju-controller-uuid": coretesting.ControllerTag.Id()},
   141  		},
   142  	})
   143  }
   144  
   145  func (s *CAASProvisionerSuite) TestOperatorProvisioningInfo(c *gc.C) {
   146  	s.st.operatorImage = "jujusolutions/caas-jujud-operator"
   147  	result, err := s.api.OperatorProvisioningInfo()
   148  	c.Assert(err, jc.ErrorIsNil)
   149  	c.Assert(result, jc.DeepEquals, params.OperatorProvisioningInfo{
   150  		ImagePath:    s.st.operatorImage,
   151  		Version:      version.Current,
   152  		APIAddresses: []string{"10.0.0.1:1"},
   153  		Tags: map[string]string{
   154  			"juju-model-uuid":      coretesting.ModelTag.Id(),
   155  			"juju-controller-uuid": coretesting.ControllerTag.Id()},
   156  		CharmStorage: params.KubernetesFilesystemParams{
   157  			Size:       uint64(1024),
   158  			Provider:   "kubernetes",
   159  			Attributes: map[string]interface{}{"foo": "bar"},
   160  			Tags: map[string]string{
   161  				"juju-model-uuid":      coretesting.ModelTag.Id(),
   162  				"juju-controller-uuid": coretesting.ControllerTag.Id()},
   163  		},
   164  	})
   165  }
   166  
   167  func (s *CAASProvisionerSuite) TestOperatorProvisioningInfoNoStoragePool(c *gc.C) {
   168  	s.storagePoolManager.SetErrors(errors.NotFoundf("pool"))
   169  	s.st.operatorImage = "jujusolutions/caas-jujud-operator"
   170  	result, err := s.api.OperatorProvisioningInfo()
   171  	c.Assert(err, jc.ErrorIsNil)
   172  	c.Assert(result, jc.DeepEquals, params.OperatorProvisioningInfo{
   173  		ImagePath:    s.st.operatorImage,
   174  		Version:      version.Current,
   175  		APIAddresses: []string{"10.0.0.1:1"},
   176  		Tags: map[string]string{
   177  			"juju-model-uuid":      coretesting.ModelTag.Id(),
   178  			"juju-controller-uuid": coretesting.ControllerTag.Id()},
   179  		CharmStorage: params.KubernetesFilesystemParams{
   180  			Size:     uint64(1024),
   181  			Provider: "kubernetes",
   182  			Tags: map[string]string{
   183  				"juju-model-uuid":      coretesting.ModelTag.Id(),
   184  				"juju-controller-uuid": coretesting.ControllerTag.Id()},
   185  		},
   186  	})
   187  }
   188  
   189  func (s *CAASProvisionerSuite) TestAddresses(c *gc.C) {
   190  	_, err := s.api.APIAddresses()
   191  	c.Assert(err, jc.ErrorIsNil)
   192  	s.st.CheckCallNames(c, "APIHostPortsForAgents")
   193  }