github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/plugins/juju-metadata/validateimagemetadata_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	"github.com/juju/utils"
    12  	"gopkg.in/amz.v3/aws"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/cloud"
    16  	"github.com/juju/juju/cmd/modelcmd"
    17  	"github.com/juju/juju/environs/config"
    18  	"github.com/juju/juju/environs/filestorage"
    19  	"github.com/juju/juju/environs/imagemetadata"
    20  	"github.com/juju/juju/environs/simplestreams"
    21  	"github.com/juju/juju/jujuclient"
    22  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    23  	coretesting "github.com/juju/juju/testing"
    24  )
    25  
    26  type ValidateImageMetadataSuite struct {
    27  	coretesting.FakeJujuXDGDataHomeSuite
    28  	metadataDir string
    29  	store       *jujuclienttesting.MemStore
    30  }
    31  
    32  var _ = gc.Suite(&ValidateImageMetadataSuite{})
    33  
    34  func runValidateImageMetadata(c *gc.C, store jujuclient.ClientStore, args ...string) (*cmd.Context, error) {
    35  	cmd := &validateImageMetadataCommand{}
    36  	cmd.SetClientStore(store)
    37  	return coretesting.RunCommand(c, modelcmd.Wrap(cmd), args...)
    38  }
    39  
    40  var validateInitImageErrorTests = []struct {
    41  	args []string
    42  	err  string
    43  }{
    44  	{
    45  		args: []string{"-p", "ec2", "-r", "region", "-d", "dir"},
    46  		err:  `series required if provider type is specified`,
    47  	}, {
    48  		args: []string{"-p", "ec2", "-s", "series", "-d", "dir"},
    49  		err:  `region required if provider type is specified`,
    50  	}, {
    51  		args: []string{"-p", "ec2", "-s", "series", "-r", "region"},
    52  		err:  `metadata directory required if provider type is specified`,
    53  	},
    54  }
    55  
    56  func (s *ValidateImageMetadataSuite) TestInitErrors(c *gc.C) {
    57  	for i, t := range validateInitImageErrorTests {
    58  		c.Logf("test %d", i)
    59  		err := coretesting.InitCommand(newValidateImageMetadataCommand(), t.args)
    60  		c.Check(err, gc.ErrorMatches, t.err)
    61  	}
    62  }
    63  
    64  func (s *ValidateImageMetadataSuite) TestInvalidProviderError(c *gc.C) {
    65  	_, err := runValidateImageMetadata(c, s.store, "-p", "foo", "-s", "series", "-r", "region", "-d", "dir")
    66  	c.Check(err, gc.ErrorMatches, `no registered provider for "foo"`)
    67  }
    68  
    69  func (s *ValidateImageMetadataSuite) TestUnsupportedProviderError(c *gc.C) {
    70  	_, err := runValidateImageMetadata(c, s.store, "-p", "maas", "-s", "series", "-r", "region", "-d", "dir")
    71  	c.Check(err, gc.ErrorMatches, `maas provider does not support image metadata validation`)
    72  }
    73  
    74  func (s *ValidateImageMetadataSuite) makeLocalMetadata(c *gc.C, id, region, series, endpoint, stream string) error {
    75  	im := &imagemetadata.ImageMetadata{
    76  		Id:     id,
    77  		Arch:   "amd64",
    78  		Stream: stream,
    79  	}
    80  	cloudSpec := simplestreams.CloudSpec{
    81  		Region:   region,
    82  		Endpoint: endpoint,
    83  	}
    84  	targetStorage, err := filestorage.NewFileStorageWriter(s.metadataDir)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	err = imagemetadata.MergeAndWriteMetadata(series, []*imagemetadata.ImageMetadata{im}, &cloudSpec, targetStorage)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	return nil
    93  }
    94  
    95  func cacheTestEnvConfig(c *gc.C, store *jujuclienttesting.MemStore) {
    96  	ec2UUID := utils.MustNewUUID().String()
    97  	ec2Config, err := config.New(config.UseDefaults, map[string]interface{}{
    98  		"name":            "ec2",
    99  		"type":            "ec2",
   100  		"default-series":  "precise",
   101  		"region":          "us-east-1",
   102  		"controller-uuid": ec2UUID,
   103  		"uuid":            ec2UUID,
   104  	})
   105  	c.Assert(err, jc.ErrorIsNil)
   106  	store.Controllers["ec2-controller"] = jujuclient.ControllerDetails{
   107  		ControllerUUID: coretesting.ModelTag.Id(),
   108  		CACert:         coretesting.CACert,
   109  	}
   110  	store.Accounts["ec2-controller"] = &jujuclient.ControllerAccounts{
   111  		CurrentAccount: "admin@local",
   112  	}
   113  	store.BootstrapConfig["ec2-controller"] = jujuclient.BootstrapConfig{
   114  		Config:      ec2Config.AllAttrs(),
   115  		Cloud:       "ec2",
   116  		CloudRegion: "us-east-1",
   117  	}
   118  
   119  	azureUUID := utils.MustNewUUID().String()
   120  	azureConfig, err := config.New(config.UseDefaults, map[string]interface{}{
   121  		"name":                 "azure",
   122  		"type":                 "azure",
   123  		"controller-uuid":      azureUUID,
   124  		"uuid":                 azureUUID,
   125  		"default-series":       "raring",
   126  		"location":             "West US",
   127  		"subscription-id":      "foo",
   128  		"application-id":       "bar",
   129  		"application-password": "baz",
   130  		"tenant-id":            "qux",
   131  	})
   132  	c.Assert(err, jc.ErrorIsNil)
   133  	store.Controllers["azure-controller"] = jujuclient.ControllerDetails{
   134  		ControllerUUID: coretesting.ModelTag.Id(),
   135  		CACert:         coretesting.CACert,
   136  	}
   137  	store.Accounts["azure-controller"] = &jujuclient.ControllerAccounts{
   138  		CurrentAccount: "admin@local",
   139  	}
   140  	store.BootstrapConfig["azure-controller"] = jujuclient.BootstrapConfig{
   141  		Config:               azureConfig.AllAttrs(),
   142  		Cloud:                "azure",
   143  		CloudRegion:          "West US",
   144  		CloudEndpoint:        "https://management.azure.com",
   145  		CloudStorageEndpoint: "https://core.windows.net",
   146  		Credential:           "default",
   147  	}
   148  	store.Credentials["azure"] = cloud.CloudCredential{
   149  		AuthCredentials: map[string]cloud.Credential{
   150  			"default": cloud.NewCredential(
   151  				cloud.UserPassAuthType,
   152  				map[string]string{
   153  					"application-id":       "application-id",
   154  					"subscription-id":      "subscription-id",
   155  					"tenant-id":            "tenant-id",
   156  					"application-password": "application-password",
   157  				},
   158  			),
   159  		},
   160  	}
   161  }
   162  
   163  func (s *ValidateImageMetadataSuite) SetUpTest(c *gc.C) {
   164  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
   165  	s.metadataDir = c.MkDir()
   166  
   167  	s.store = jujuclienttesting.NewMemStore()
   168  	cacheTestEnvConfig(c, s.store)
   169  
   170  	s.PatchEnvironment("AWS_ACCESS_KEY_ID", "access")
   171  	s.PatchEnvironment("AWS_SECRET_ACCESS_KEY", "secret")
   172  	// All of the following are recognized as fallbacks by goamz.
   173  	s.PatchEnvironment("AWS_ACCESS_KEY", "")
   174  	s.PatchEnvironment("AWS_SECRET_KEY", "")
   175  	s.PatchEnvironment("EC2_ACCESS_KEY", "")
   176  	s.PatchEnvironment("EC2_SECRET_KEY", "")
   177  }
   178  
   179  func (s *ValidateImageMetadataSuite) setupEc2LocalMetadata(c *gc.C, region, stream string) {
   180  	ec2Region, ok := aws.Regions[region]
   181  	if !ok {
   182  		c.Fatalf("unknown ec2 region %q", region)
   183  	}
   184  	endpoint := ec2Region.EC2Endpoint
   185  	s.makeLocalMetadata(c, "1234", region, "precise", endpoint, stream)
   186  }
   187  
   188  func (s *ValidateImageMetadataSuite) assertEc2LocalMetadataUsingEnvironment(c *gc.C, stream string) {
   189  	s.setupEc2LocalMetadata(c, "us-east-1", stream)
   190  	ctx, err := runValidateImageMetadata(c, s.store, "-m", "ec2-controller:ec2", "-d", s.metadataDir, "--stream", stream)
   191  	c.Assert(err, jc.ErrorIsNil)
   192  	stdout := coretesting.Stdout(ctx)
   193  	stderr := coretesting.Stderr(ctx)
   194  	strippedOut := strings.Replace(stdout, "\n", "", -1)
   195  	c.Check(strippedOut, gc.Matches,
   196  		`ImageIds:.*"1234".*Region:.*us-east-1.*Resolve Metadata:.*source: local metadata directory.*`,
   197  	)
   198  	c.Check(stderr, gc.Matches, "")
   199  }
   200  
   201  func (s *ValidateImageMetadataSuite) TestEc2LocalMetadataUsingEnvironment(c *gc.C) {
   202  	s.assertEc2LocalMetadataUsingEnvironment(c, "")
   203  	s.assertEc2LocalMetadataUsingEnvironment(c, imagemetadata.ReleasedStream)
   204  	s.assertEc2LocalMetadataUsingEnvironment(c, "daily")
   205  }
   206  
   207  func (s *ValidateImageMetadataSuite) TestEc2LocalMetadataUsingIncompleteEnvironment(c *gc.C) {
   208  	s.PatchEnvironment("AWS_ACCESS_KEY_ID", "")
   209  	s.PatchEnvironment("AWS_SECRET_ACCESS_KEY", "")
   210  	s.PatchEnvironment("EC2_ACCESS_KEY", "")
   211  	s.PatchEnvironment("EC2_SECRET_KEY", "")
   212  	s.setupEc2LocalMetadata(c, "us-east-1", "")
   213  	_, err := runValidateImageMetadata(c, s.store, "-m", "ec2-controller:ec2", "-d", s.metadataDir)
   214  	c.Assert(err, gc.ErrorMatches, `detecting credentials.*AWS_SECRET_ACCESS_KEY not found in environment`)
   215  }
   216  
   217  func (s *ValidateImageMetadataSuite) TestEc2LocalMetadataWithManualParams(c *gc.C) {
   218  	s.setupEc2LocalMetadata(c, "us-west-1", "")
   219  	ctx, err := runValidateImageMetadata(c, s.store,
   220  		"-p", "ec2", "-s", "precise", "-r", "us-west-1",
   221  		"-u", "https://ec2.us-west-1.amazonaws.com", "-d", s.metadataDir,
   222  	)
   223  	c.Assert(err, jc.ErrorIsNil)
   224  	errOut := coretesting.Stdout(ctx)
   225  	strippedOut := strings.Replace(errOut, "\n", "", -1)
   226  	c.Check(
   227  		strippedOut, gc.Matches,
   228  		`ImageIds:.*"1234".*Region:.*us-west-1.*Resolve Metadata:.*source: local metadata directory.*`)
   229  }
   230  
   231  func (s *ValidateImageMetadataSuite) TestEc2LocalMetadataNoMatch(c *gc.C) {
   232  	s.setupEc2LocalMetadata(c, "us-east-1", "")
   233  	_, err := runValidateImageMetadata(c, s.store,
   234  		"-p", "ec2", "-s", "raring", "-r", "us-west-1",
   235  		"-u", "https://ec2.us-west-1.amazonaws.com", "-d", s.metadataDir,
   236  	)
   237  	c.Check(err, gc.ErrorMatches, "(.|\n)*Resolve Metadata:(.|\n)*")
   238  	_, err = runValidateImageMetadata(c, s.store,
   239  		"-p", "ec2", "-s", "precise", "-r", "region",
   240  		"-u", "https://ec2.region.amazonaws.com", "-d", s.metadataDir,
   241  	)
   242  	c.Check(err, gc.ErrorMatches, `unknown region "region"`)
   243  }
   244  
   245  func (s *ValidateImageMetadataSuite) TestOpenstackLocalMetadataWithManualParams(c *gc.C) {
   246  	s.makeLocalMetadata(c, "1234", "region-2", "raring", "some-auth-url", "")
   247  	ctx, err := runValidateImageMetadata(c, s.store,
   248  		"-p", "openstack", "-s", "raring", "-r", "region-2",
   249  		"-u", "some-auth-url", "-d", s.metadataDir,
   250  	)
   251  	c.Assert(err, jc.ErrorIsNil)
   252  	errOut := coretesting.Stdout(ctx)
   253  	strippedOut := strings.Replace(errOut, "\n", "", -1)
   254  	c.Check(
   255  		strippedOut, gc.Matches,
   256  		`ImageIds:.*"1234".*Region:.*region-2.*Resolve Metadata:.*source: local metadata directory.*`)
   257  }
   258  
   259  func (s *ValidateImageMetadataSuite) TestOpenstackLocalMetadataNoMatch(c *gc.C) {
   260  	s.makeLocalMetadata(c, "1234", "region-2", "raring", "some-auth-url", "")
   261  	_, err := runValidateImageMetadata(c, s.store,
   262  		"-p", "openstack", "-s", "precise", "-r", "region-2",
   263  		"-u", "some-auth-url", "-d", s.metadataDir,
   264  	)
   265  	c.Check(err, gc.ErrorMatches, "(.|\n)*Resolve Metadata:(.|\n)*")
   266  	_, err = runValidateImageMetadata(c, s.store,
   267  		"-p", "openstack", "-s", "raring", "-r", "region-3",
   268  		"-u", "some-auth-url", "-d", s.metadataDir,
   269  	)
   270  	c.Check(err, gc.ErrorMatches, "(.|\n)*Resolve Metadata:(.|\n)*")
   271  }
   272  
   273  func (s *ValidateImageMetadataSuite) TestImagesDataSourceHasKey(c *gc.C) {
   274  	ds := imagesDataSources("test.me")
   275  	// This data source does not require to contain signed data.
   276  	// However, it may still contain it.
   277  	// Since we will always try to read signed data first,
   278  	// we want to be able to try to read this signed data
   279  	// with a user provided public key. For this test, none is provided.
   280  	// Bugs #1542127, #1542131
   281  	c.Assert(ds[0].PublicSigningKey(), gc.Equals, "")
   282  }