Esempio n. 1
0
// Core returns the core for the given template given the configured
// CoreConfig and user variables on this Meta.
func (m *Meta) Core(tpl *template.Template) (*packer.Core, error) {
	// Copy the config so we don't modify it
	config := *m.CoreConfig
	config.Template = tpl
	config.Variables = m.flagVars

	// Init the core
	core, err := packer.NewCore(&config)
	if err != nil {
		return nil, fmt.Errorf("Error initializing core: %s", err)
	}

	return core, nil
}
Esempio n. 2
0
// Test performs an acceptance test on a backend with the given test case.
//
// Tests are not run unless an environmental variable "TF_ACC" is
// set to some non-empty value. This is to avoid test cases surprising
// a user by creating real resources.
//
// Tests will fail unless the verbose flag (`go test -v`, or explicitly
// the "-test.v" flag) is set. Because some acceptance tests take quite
// long, we require the verbose flag so users are able to see progress
// output.
func Test(t TestT, c TestCase) {
	// We only run acceptance tests if an env var is set because they're
	// slow and generally require some outside configuration.
	if os.Getenv(TestEnvVar) == "" {
		t.Skip(fmt.Sprintf(
			"Acceptance tests skipped unless env '%s' set",
			TestEnvVar))
		return
	}

	// We require verbose mode so that the user knows what is going on.
	if !testTesting && !testing.Verbose() {
		t.Fatal("Acceptance tests must be run with the -v flag on tests")
		return
	}

	// Run the PreCheck if we have it
	if c.PreCheck != nil {
		c.PreCheck()
	}

	// Parse the template
	log.Printf("[DEBUG] Parsing template...")
	tpl, err := template.Parse(strings.NewReader(c.Template))
	if err != nil {
		t.Fatal(fmt.Sprintf("Failed to parse template: %s", err))
		return
	}

	// Build the core
	log.Printf("[DEBUG] Initializing core...")
	core, err := packer.NewCore(&packer.CoreConfig{
		Components: packer.ComponentFinder{
			Builder: func(n string) (packer.Builder, error) {
				if n == "test" {
					return c.Builder, nil
				}

				return nil, nil
			},
		},
		Template: tpl,
	})
	if err != nil {
		t.Fatal(fmt.Sprintf("Failed to init core: %s", err))
		return
	}

	// Get the build
	log.Printf("[DEBUG] Retrieving 'test' build")
	build, err := core.Build("test")
	if err != nil {
		t.Fatal(fmt.Sprintf("Failed to get 'test' build: %s", err))
		return
	}

	// Prepare it
	log.Printf("[DEBUG] Preparing 'test' build")
	warnings, err := build.Prepare()
	if err != nil {
		t.Fatal(fmt.Sprintf("Prepare error: %s", err))
		return
	}
	if len(warnings) > 0 {
		t.Fatal(fmt.Sprintf(
			"Prepare warnings:\n\n%s",
			strings.Join(warnings, "\n")))
		return
	}

	// Run it! We use a temporary directory for caching and discard
	// any UI output. We discard since it shows up in logs anyways.
	log.Printf("[DEBUG] Running 'test' build")
	cache := &packer.FileCache{CacheDir: os.TempDir()}
	ui := &packer.BasicUi{
		Reader:      os.Stdin,
		Writer:      ioutil.Discard,
		ErrorWriter: ioutil.Discard,
	}
	artifacts, err := build.Run(ui, cache)
	if err != nil {
		t.Fatal(fmt.Sprintf("Run error:\n\n%s", err))
		goto TEARDOWN
	}

	// Check function
	if c.Check != nil {
		log.Printf("[DEBUG] Running check function")
		if err := c.Check(artifacts); err != nil {
			t.Fatal(fmt.Sprintf("Check error:\n\n%s", err))
			goto TEARDOWN
		}
	}

TEARDOWN:
	if !c.SkipArtifactTeardown {
		// Delete all artifacts
		for _, a := range artifacts {
			if err := a.Destroy(); err != nil {
				t.Error(fmt.Sprintf(
					"!!! ERROR REMOVING ARTIFACT '%s': %s !!!",
					a.String(), err))
			}
		}
	}

	// Teardown
	if c.Teardown != nil {
		log.Printf("[DEBUG] Running teardown function")
		if err := c.Teardown(); err != nil {
			t.Fatal(fmt.Sprintf("Teardown failure:\n\n%s", err))
			return
		}
	}
}