Example #1
0
func TestNewFromDocker(t *testing.T) {
	cfg, err := config.NewFromFile("config/testdata/compose.yml", containerTestVars, map[string]interface{}{}, false)
	if err != nil {
		t.Fatal(err)
	}

	container := NewContainerFromConfig(config.NewContainerName("myapp", "main"), cfg.Containers["main"])

	opts, err := container.CreateContainerOptions()
	if err != nil {
		t.Fatal(err)
	}

	apiContainer := &docker.Container{
		Config: &docker.Config{
			Labels: opts.Config.Labels,
		},
		State: docker.State{
			Running: true,
		},
		Name: "/myapp.main",
	}

	configFromAPI, err := config.NewFromDocker(apiContainer)
	if err != nil {
		t.Fatal(err)
	}

	compareResult := cfg.Containers["main"].IsEqualTo(configFromAPI)
	assert.True(t, compareResult,
		"container spec converted from API should be equal to one fetched from config file, failed on field: %s", cfg.Containers["main"].LastCompareField())
}
Example #2
0
func TestConfigGetContainers(t *testing.T) {
	cfg, err := config.NewFromFile("config/testdata/compose.yml", containerTestVars, map[string]interface{}{}, false)
	if err != nil {
		t.Fatal(err)
	}

	containers := GetContainersFromConfig(cfg)

	assert.Equal(t, 5, len(containers), "bad containers number from config")
}
Example #3
0
func TestCreateContainerOptions(t *testing.T) {
	cfg, err := config.NewFromFile("config/testdata/compose.yml", containerTestVars, map[string]interface{}{}, false)
	if err != nil {
		t.Fatal(err)
	}

	container := NewContainerFromConfig(config.NewContainerName("myapp", "main"), cfg.Containers["main"])

	opts, err := container.CreateContainerOptions()
	if err != nil {
		t.Fatal(err)
	}

	assert.IsType(t, &docker.CreateContainerOptions{}, opts)
}
Example #4
0
func initComposeConfig(ctx *cli.Context, dockerCli *docker.Client) *config.Config {
	file := ctx.String("file")

	if file == "" {
		log.Fatalf("Manifest file is empty")
		os.Exit(1)
	}

	var (
		manifest *config.Config
		err      error
		bridgeIP *string
		print    = ctx.Bool("print")
	)

	vars, err := template.VarsFromFileMulti(ctx.StringSlice("vars"))
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	cliVars, err := template.VarsFromStrings(ctx.StringSlice("var"))
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	vars = vars.Merge(cliVars)

	if ctx.Bool("demand-artifacts") {
		vars["DemandArtifacts"] = true
	}

	// TODO: find better place for providing this helper
	funcs := map[string]interface{}{
		// lazy get bridge ip
		"bridgeIp": func() (ip string, err error) {
			if bridgeIP == nil {
				ip, err = compose.GetBridgeIP(dockerCli)
				if err != nil {
					return "", err
				}
				bridgeIP = &ip
			}
			return *bridgeIP, nil
		},
	}

	if file == "-" {
		if !print {
			log.Infof("Reading manifest from STDIN")
		}
		manifest, err = config.ReadConfig(file, os.Stdin, vars, funcs, print)
	} else {
		if !print {
			log.Infof("Reading manifest: %s", file)
		}
		manifest, err = config.NewFromFile(file, vars, funcs, print)
	}

	if err != nil {
		log.Fatal(err)
	}

	// Check the docker connection before we actually run
	if err := dockerclient.Ping(dockerCli, 5000); err != nil {
		log.Fatal(err)
	}

	return manifest
}