Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ycliuhw committed Oct 17, 2018
1 parent f633ba8 commit 20d78dc
Show file tree
Hide file tree
Showing 40 changed files with 368 additions and 339 deletions.
3 changes: 2 additions & 1 deletion apiserver/common/modelwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ func (*modelWatcherSuite) TestModelConfigFetchError(c *gc.C) {
}

func testingEnvConfig(c *gc.C) *config.Config {
env, err := bootstrap.Prepare(
env, err := bootstrap.PrepareController(
false,
modelcmd.BootstrapContext(cmdtesting.Context(c)),
jujuclient.NewMemStore(),
bootstrap.PrepareParams{
Expand Down
8 changes: 4 additions & 4 deletions apiserver/common/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (s *toolsSuite) TestFindTools(c *gc.C) {
SHA256: "feedface",
}}

s.PatchValue(common.EnvtoolsFindTools, func(e environs.Environ, major, minor int, streams []string, filter coretools.Filter) (coretools.List, error) {
s.PatchValue(common.EnvtoolsFindTools, func(e environs.BootstrapEnviron, major, minor int, streams []string, filter coretools.Filter) (coretools.List, error) {
c.Assert(major, gc.Equals, 123)
c.Assert(minor, gc.Equals, 456)
c.Assert(streams, gc.DeepEquals, test.agentStreamsUsed)
Expand Down Expand Up @@ -218,7 +218,7 @@ func (s *toolsSuite) TestFindTools(c *gc.C) {
}

func (s *toolsSuite) TestFindToolsNotFound(c *gc.C) {
s.PatchValue(common.EnvtoolsFindTools, func(e environs.Environ, major, minor int, stream []string, filter coretools.Filter) (list coretools.List, err error) {
s.PatchValue(common.EnvtoolsFindTools, func(e environs.BootstrapEnviron, major, minor int, stream []string, filter coretools.Filter) (list coretools.List, err error) {
return nil, errors.NotFoundf("tools")
})
toolsFinder := common.NewToolsFinder(stateenvirons.EnvironConfigGetter{s.State, s.Model}, s.State, sprintfURLGetter("%s"))
Expand Down Expand Up @@ -253,7 +253,7 @@ func (s *toolsSuite) TestFindToolsExactNotInStorage(c *gc.C) {

func (s *toolsSuite) testFindToolsExact(c *gc.C, t common.ToolsStorageGetter, inStorage bool, develVersion bool) {
var called bool
s.PatchValue(common.EnvtoolsFindTools, func(e environs.Environ, major, minor int, stream []string, filter coretools.Filter) (list coretools.List, err error) {
s.PatchValue(common.EnvtoolsFindTools, func(e environs.BootstrapEnviron, major, minor int, stream []string, filter coretools.Filter) (list coretools.List, err error) {
called = true
c.Assert(filter.Number, gc.Equals, jujuversion.Current)
c.Assert(filter.Series, gc.Equals, series.MustHostSeries())
Expand Down Expand Up @@ -285,7 +285,7 @@ func (s *toolsSuite) testFindToolsExact(c *gc.C, t common.ToolsStorageGetter, in

func (s *toolsSuite) TestFindToolsToolsStorageError(c *gc.C) {
var called bool
s.PatchValue(common.EnvtoolsFindTools, func(e environs.Environ, major, minor int, stream []string, filter coretools.Filter) (list coretools.List, err error) {
s.PatchValue(common.EnvtoolsFindTools, func(e environs.BootstrapEnviron, major, minor int, stream []string, filter coretools.Filter) (list coretools.List, err error) {
called = true
return nil, errors.NotFoundf("tools")
})
Expand Down
3 changes: 2 additions & 1 deletion apiserver/facades/agent/fanconfigurer/fanconfigurer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ func (s *fanconfigurerSuite) TestFanConfigFetchError(c *gc.C) {
}

func testingEnvConfig(c *gc.C) *config.Config {
env, err := bootstrap.Prepare(
env, err := bootstrap.PrepareController(
false,
modelcmd.BootstrapContext(cmdtesting.Context(c)),
jujuclient.NewMemStore(),
bootstrap.PrepareParams{
Expand Down
8 changes: 4 additions & 4 deletions apiserver/facades/controller/agenttools/agenttools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *AgentToolsSuite) TestCheckTools(c *gc.C) {
var (
calledWithMajor, calledWithMinor int
)
fakeToolFinder := func(e environs.Environ, maj int, min int, streams []string, filter coretools.Filter) (coretools.List, error) {
fakeToolFinder := func(e environs.BootstrapEnviron, maj int, min int, streams []string, filter coretools.Filter) (coretools.List, error) {
calledWithMajor = maj
calledWithMinor = min
ver := version.Binary{Number: version.Number{Major: maj, Minor: min}}
Expand Down Expand Up @@ -73,7 +73,7 @@ func (s *AgentToolsSuite) TestCheckToolsNonReleasedStream(c *gc.C) {
calledWithMajor, calledWithMinor int
calledWithStreams [][]string
)
fakeToolFinder := func(e environs.Environ, maj int, min int, streams []string, filter coretools.Filter) (coretools.List, error) {
fakeToolFinder := func(e environs.BootstrapEnviron, maj int, min int, streams []string, filter coretools.Filter) (coretools.List, error) {
calledWithMajor = maj
calledWithMinor = min
calledWithStreams = append(calledWithStreams, streams)
Expand Down Expand Up @@ -111,7 +111,7 @@ func (s *AgentToolsSuite) TestUpdateToolsAvailability(c *gc.C) {
}
s.PatchValue(&modelConfig, fakeModelConfig)

fakeToolFinder := func(_ environs.Environ, _ int, _ int, _ []string, _ coretools.Filter) (coretools.List, error) {
fakeToolFinder := func(_ environs.BootstrapEnviron, _ int, _ int, _ []string, _ coretools.Filter) (coretools.List, error) {
ver := version.Binary{Number: version.Number{Major: 2, Minor: 5, Patch: 2}}
olderVer := version.Binary{Number: version.Number{Major: 2, Minor: 5, Patch: 1}}
t := coretools.Tools{Version: ver, URL: "http://example.com", Size: 1}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (s *AgentToolsSuite) TestUpdateToolsAvailabilityNoMatches(c *gc.C) {
s.PatchValue(&modelConfig, fakeModelConfig)

// No new tools available.
fakeToolFinder := func(_ environs.Environ, _ int, _ int, _ []string, _ coretools.Filter) (coretools.List, error) {
fakeToolFinder := func(_ environs.BootstrapEnviron, _ int, _ int, _ []string, _ coretools.Filter) (coretools.List, error) {
return nil, errors.NotFoundf("tools")
}

Expand Down
13 changes: 6 additions & 7 deletions caas/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/juju/juju/core/status"
"github.com/juju/juju/core/watcher"
"github.com/juju/juju/environs"
// "github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/network"
"github.com/juju/juju/storage"
Expand Down Expand Up @@ -97,6 +96,12 @@ type ServiceParams struct {

// Broker instances interact with the CAAS substrate.
type Broker interface {
// Provider returns the ContainerEnvironProvider that created this Broker.
Provider() ContainerEnvironProvider

// Destroy terminates all containers and other resources in this broker's namespace.
Destroy(context.ProviderCallContext) error

// EnsureNamespace ensures this broker's namespace is created.
EnsureNamespace() error

Expand Down Expand Up @@ -138,12 +143,6 @@ type Broker interface {
// via volumes bound to the unit.
Units(appName string) ([]Unit, error)

// Provider returns the ContainerEnvironProvider that created this Broker.
Provider() ContainerEnvironProvider

// Destroy terminates all containers and other resources in this broker's namespace.
Destroy(context.ProviderCallContext) error

// ProviderRegistry is an interface for obtaining storage providers.
storage.ProviderRegistry

Expand Down
3 changes: 3 additions & 0 deletions caas/kubernetes/provider/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type BaseSuite struct {

broker caas.Broker

cfg *config.Config

k8sClient *mocks.MockInterface
mockNamespaces *mocks.MockNamespaceInterface
mockApps *mocks.MockAppsV1Interface
Expand Down Expand Up @@ -66,6 +68,7 @@ func (s *BaseSuite) setupBroker(c *gc.C) *gomock.Controller {
config.NameKey: testNamespace,
}))
c.Assert(err, jc.ErrorIsNil)
s.cfg = cfg

ctrl := gomock.NewController(c)

Expand Down
73 changes: 10 additions & 63 deletions caas/kubernetes/provider/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,71 +156,13 @@ func (k *kubernetesClient) PrepareForBootstrap(ctx environs.BootstrapContext) er
return nil
}

// func (k *kubernetesClient) prepareAgentConfig(series string, args bootstrap.BootstrapParams) (agent.ConfigSetterWriter, error) {
// const machineID = "0"
// cfg := k.Config()
// agentVersion, _ := cfg.AgentVersion()

// controllerCfg := args.ControllerConfig
// caCert, hasCACert := controllerCfg.CACert()
// if !hasCACert {
// return nil, errors.New("controller configuration has no ca-cert")
// }

// dataDir, err := paths.DataDir(series)
// if err != nil {
// return nil, err
// }
// logDir, err := paths.LogDir(series)
// if err != nil {
// return nil, err
// }
// metricsSpoolDir, err := paths.MetricsSpoolDir(series)
// if err != nil {
// return nil, err
// }
// cert, key, err := controller.GenerateControllerCertAndKey(caCert, args.CAPrivateKey, nil)
// if err != nil {
// return ni, errors.Annotate(err, "cannot generate controller certificate")
// }
// configParams := agent.AgentConfigParams{
// Paths: agent.Paths{
// DataDir: dataDir,
// LogDir: path.Join(logDir, "juju"),
// MetricsSpoolDir: metricsSpoolDir,
// },
// Jobs: []multiwatcher.MachineJob{multiwatcher.JobManageModel},
// Tag: names.NewUnitTag(machineID),
// UpgradedToVersion: agentVersion,
// Password: args.AdminSecret,
// Nonce: agent.BootstrapNonce,
// APIAddresses: []string{
// net.JoinHostPort("localhost", strconv.Itoa(controllerCfg.APIPort())),
// },
// CACert: caCert,
// Values: map[string]string{
// agent.ProviderType: cfg.Type(),
// },
// Controller: names.NewControllerTag(controllerCfg.ControllerUUID()),
// Model: names.NewModelTag(cfg.UUID()),
// }
// return agent.NewStateMachineConfig(configParams, params.StateServingInfo{
// StatePort: controllerCfg.StatePort(),
// APIPort: controllerCfg.APIPort(),
// Cert: cert,
// PrivateKey: key,
// CAPrivateKey: args.CAPrivateKey,
// })
// }

// Bootstrap deploys controller with mongoDB together into k8s cluster.
func (k *kubernetesClient) Bootstrap(ctx environs.BootstrapContext, callCtx context.ProviderCallContext, args environs.BootstrapParams) (*environs.BootstrapResult, error) {
const (
Series = "bionic"
Arch = arch.AMD64
)

logger.Criticalf("kubernetesClient Bootstrap -> \n%#v, \n%#v, \n%#v", ctx, callCtx, args)
finalizer := func(ctx environs.BootstrapContext, pcfg *podcfg.ControllerPodConfig, opts environs.BootstrapDialOpts) error {
envConfig := k.Config()
if err := podcfg.FinishControllerPodConfig(pcfg, envConfig); err != nil {
Expand All @@ -231,15 +173,12 @@ func (k *kubernetesClient) Bootstrap(ctx environs.BootstrapContext, callCtx cont
return errors.Trace(err)
}

// No need to start instance for CAAS, so do everything for bootstraping controller here.
logger.Criticalf("kubernetesClient Finalizer, \nctx -> %#v, \npcfg -> %#v, \nopts -> %#v", ctx, pcfg, opts)

// prepare bootstrapParamsFile
bootstrapParamsFileContent, err := pcfg.Bootstrap.StateInitializationParams.Marshal()
if err != nil {
return errors.Trace(err)
}
logger.Criticalf("bootstrapParamsFileContent -> \n%s", string(bootstrapParamsFileContent))
logger.Debugf("bootstrapParams file content: \n%s", string(bootstrapParamsFileContent))

machineTag := names.NewMachineTag(pcfg.MachineId)
acfg, err := pcfg.AgentConfig(machineTag, pcfg.AgentVersion().Number)
Expand All @@ -250,7 +189,15 @@ func (k *kubernetesClient) Bootstrap(ctx environs.BootstrapContext, callCtx cont
if err != nil {
return errors.Trace(err)
}
logger.Criticalf("agentConfigFileContent -> \n%s", string(agentConfigFileContent))
logger.Debugf("agentConfig file content: \n%s", string(agentConfigFileContent))

// TODO(caas): prepare
// agent.conf,
// bootstrap-params,
// server.pem,
// system-identity,
// shared-secret, then generate configmap/secret.
// Lastly, create StatefulSet for controller.
return nil
}
return &environs.BootstrapResult{
Expand Down
13 changes: 13 additions & 0 deletions caas/kubernetes/provider/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ type K8sBrokerSuite struct {

var _ = gc.Suite(&K8sBrokerSuite{})

func (s *K8sBrokerSuite) TestConfig(c *gc.C) {
ctrl := s.setupBroker(c)
defer ctrl.Finish()
c.Assert(s.broker.Config(), jc.DeepEquals, s.cfg)
}

func (s *K8sBrokerSuite) TestSetConfig(c *gc.C) {
ctrl := s.setupBroker(c)
defer ctrl.Finish()
err := s.broker.SetConfig(s.cfg)
c.Assert(err, jc.ErrorIsNil)
}

func (s *K8sBrokerSuite) TestEnsureNamespace(c *gc.C) {
ctrl := s.setupBroker(c)
defer ctrl.Finish()
Expand Down
6 changes: 3 additions & 3 deletions cloudconfig/instancecfg/instancecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (cfg *InstanceConfig) ToolsList() coretools.List {
if cfg.tools == nil {
return nil
}
return copyToolsList(cfg.tools)
return CopyToolsList(cfg.tools)
}

// SetTools sets the tools that should be tried when provisioning this
Expand Down Expand Up @@ -545,11 +545,11 @@ func (cfg *InstanceConfig) SetTools(toolsList coretools.List) error {
return errors.Errorf("agent binary info mismatch (%v, %v)", *tools, info)
}
}
cfg.tools = copyToolsList(toolsList)
cfg.tools = CopyToolsList(toolsList)
return nil
}

func copyToolsList(in coretools.List) coretools.List {
func CopyToolsList(in coretools.List) coretools.List {
out := make(coretools.List, len(in))
for i, tools := range in {
copied := *tools
Expand Down
14 changes: 14 additions & 0 deletions cloudconfig/podcfg/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package podcfg_test

import (
"testing"

gc "gopkg.in/check.v1"
)

func Test(t *testing.T) {
gc.TestingT(t)
}
Loading

0 comments on commit 20d78dc

Please sign in to comment.