Ejemplo n.º 1
0
// addDockerfile adds a Dockerfile passed in the command line to the reference
// builder.
func (c *AppConfig) addDockerfile() error {
	if len(c.Strategy) != 0 && c.Strategy != "docker" {
		return fmt.Errorf("when directly referencing a Dockerfile, the strategy must must be 'docker'")
	}
	_, repos, errs := c.refBuilder.Result()
	if err := errors.NewAggregate(errs); err != nil {
		return err
	}
	switch len(repos) {
	case 0:
		// Create a new SourceRepository with the Dockerfile.
		repo, err := app.NewSourceRepositoryForDockerfile(c.Dockerfile)
		if err != nil {
			return fmt.Errorf("provided Dockerfile is not valid: %v", err)
		}
		c.refBuilder.AddExistingSourceRepository(repo)
	case 1:
		// Add the Dockerfile to the existing SourceRepository, so that
		// eventually we generate a single BuildConfig with multiple
		// sources.
		if err := repos[0].AddDockerfile(c.Dockerfile); err != nil {
			return fmt.Errorf("provided Dockerfile is not valid: %v", err)
		}
	default:
		// Invalid.
		return fmt.Errorf("--dockerfile cannot be used with multiple source repositories")
	}
	return nil
}
Ejemplo n.º 2
0
// AddDockerfile adds a Dockerfile passed in the command line to the reference
// builder.
func AddDockerfileToSourceRepositories(b *app.ReferenceBuilder, dockerfile string) error {
	_, repos, errs := b.Result()
	if err := kutilerrors.NewAggregate(errs); err != nil {
		return err
	}
	switch len(repos) {
	case 0:
		// Create a new SourceRepository with the Dockerfile.
		repo, err := app.NewSourceRepositoryForDockerfile(dockerfile)
		if err != nil {
			return fmt.Errorf("provided Dockerfile is not valid: %v", err)
		}
		b.AddExistingSourceRepository(repo)
	case 1:
		// Add the Dockerfile to the existing SourceRepository, so that
		// eventually we generate a single BuildConfig with multiple
		// sources.
		if err := repos[0].AddDockerfile(dockerfile); err != nil {
			return fmt.Errorf("provided Dockerfile is not valid: %v", err)
		}
	default:
		// Invalid.
		return errors.New("--dockerfile cannot be used with multiple source repositories")
	}
	return nil
}
Ejemplo n.º 3
0
func TestDisallowedNonNumericExposedPorts(t *testing.T) {
	tests := []struct {
		strategy             generate.Strategy
		allowNonNumericPorts bool
	}{
		{
			strategy:             generate.StrategyUnspecified,
			allowNonNumericPorts: false,
		},
		{
			strategy:             generate.StrategyDocker,
			allowNonNumericPorts: false,
		},
	}

	for _, test := range tests {
		config := &AppConfig{}
		config.Strategy = test.strategy
		config.AllowNonNumericExposedPorts = test.allowNonNumericPorts

		repo, err := app.NewSourceRepositoryForDockerfile("FROM centos\nARG PORT=80\nEXPOSE 8080 $PORT")
		if err != nil {
			t.Fatalf("Unexpected error during setup: %v", err)
		}
		repos := app.SourceRepositories{repo}

		err = optionallyValidateExposedPorts(config, repos)
		if err == nil {
			t.Error("Expected error wasn't returned")

		} else if !strings.Contains(err.Error(), "invalid EXPOSE") || !strings.Contains(err.Error(), "must be numeric") {
			t.Errorf("Unexpected error: %v", err)
		}
	}
}
Ejemplo n.º 4
0
func TestAllowedNonNumericExposedPorts(t *testing.T) {
	tests := []struct {
		strategy             generate.Strategy
		allowNonNumericPorts bool
	}{
		{
			strategy:             generate.StrategyUnspecified,
			allowNonNumericPorts: true,
		},
		{
			strategy:             generate.StrategySource,
			allowNonNumericPorts: false,
		},
	}

	for _, test := range tests {
		config := &AppConfig{}
		config.Strategy = test.strategy
		config.AllowNonNumericExposedPorts = test.allowNonNumericPorts

		repo, err := app.NewSourceRepositoryForDockerfile("FROM centos\nARG PORT=80\nEXPOSE $PORT")
		if err != nil {
			t.Errorf("Unexpected error during setup: %v", err)
			continue
		}
		repos := app.SourceRepositories{repo}

		err = optionallyValidateExposedPorts(config, repos)
		if err != nil {
			t.Errorf("Unexpected error: %v", err)
		}
	}
}
Ejemplo n.º 5
0
func TestDisallowedNonNumericExposedPorts(t *testing.T) {
	config := &AppConfig{}
	config.Strategy = "docker"
	config.AllowNonNumericExposedPorts = false

	repo, err := app.NewSourceRepositoryForDockerfile("FROM centos\nARG PORT=80\nEXPOSE 8080 $PORT")
	if err != nil {
		t.Fatalf("Unexpected error during setup: %v", err)
	}
	repos := app.SourceRepositories{repo}

	err = optionallyValidateExposedPorts(config, repos)
	if err == nil {
		t.Error("Expected error wasn't returned")

	} else if !strings.Contains(err.Error(), "invalid EXPOSE") || !strings.Contains(err.Error(), "must be numeric") {
		t.Errorf("Unexpected error: %v", err)
	}
}
Ejemplo n.º 6
0
// individualSourceRepositories collects the list of SourceRepositories specified in the
// command line that are not associated with a builder using a '~'.
func (c *AppConfig) individualSourceRepositories() (app.SourceRepositories, error) {
	first := true
	for _, s := range c.SourceRepositories {
		if repo, ok := c.refBuilder.AddSourceRepository(s); ok && first {
			repo.SetContextDir(c.ContextDir)
			first = false
		}
	}
	if len(c.Dockerfile) > 0 {
		switch {
		case c.Strategy == "docker", len(c.Strategy) == 0:
		default:
			return nil, fmt.Errorf("when directly referencing a Dockerfile, the strategy must must be 'docker'")
		}
		repo, err := app.NewSourceRepositoryForDockerfile(c.Dockerfile)
		if err != nil {
			return nil, fmt.Errorf("provided Dockerfile is not valid: %v", err)
		}
		c.refBuilder.AddExistingSourceRepository(repo)
	}
	_, repos, errs := c.refBuilder.Result()
	return repos, errors.NewAggregate(errs)
}