예제 #1
0
func Custom(image, assets string, dockerClient docker.Client, connect *sqlx.DB, build *common.Build) error {

	var (
		startedAt time.Time
	)

	if err := connect.Get(&startedAt, "SELECT CURRENT_TIMESTAMP"); err != nil {

		return fmt.Errorf("Could not retrieve startup parameters: %v", err)
	}

	log.Debugf("Custom runner. Image: %s", image)

	log.Debugf("Create container: %s", image)

	container, err := dockerClient.CreateConteiner(image, docker.CreateContainerOptions{
		Env: build.Config.Env,
		Binds: []string{
			fmt.Sprintf("%s:%s", build.WorkingDir, WorkingDir),
		},
		Entrypoint: build.Config.Entrypoint,
		WorkingDir: WorkingDir,
	})

	if err != nil {

		return fmt.Errorf("Could not create container: %v", err)
	}

	defer container.Destroy()

	for _, command := range build.Config.Commands {

		log.Debugf("Run cmd: %s", command)

		if err := container.RunCmd(command); err != nil {

			return fmt.Errorf("Execute failed. Cmd: %s, output: %s", command, container.Output())
		}
	}

	var testsWithErrors []string

	for _, test := range build.Config.Tests {

		log.Debugf("Run test: %s", test)

		if err := container.RunCmd(test); err != nil {

			log.Debugf("Test failed: %s, output: %s", test, container.Output())

			testsWithErrors = append(testsWithErrors, test)
		}
	}

	log.Debugf("Output: %s", container.Output())

	_, err = connect.Exec(`SELECT build.add_part($1, $2, $3, $4, $5, $6, $7)`,
		build.BuildID,
		"",
		image,
		container.ID(),
		container.Output(),
		startedAt,
		"[]",
	)

	if err != nil {

		return fmt.Errorf("Could not commit a part of the build: %v", err)
	}

	if len(testsWithErrors) > 0 {

		return fmt.Errorf("Test(s) failed: %s", strings.Join(testsWithErrors, ", "))
	}

	return nil
}
예제 #2
0
func PLpgSQL(image, assets string, dockerClient docker.Client, connect *sqlx.DB, build *common.Build) error {

	var (
		startedAt     time.Time
		serverVersion string
	)

	if err := connect.Get(&startedAt, "SELECT CURRENT_TIMESTAMP"); err != nil {

		return fmt.Errorf("Could not retrieve startup parameters: %v", err)
	}

	log.Debugf("Create container: %s", image)

	container, err := dockerClient.CreateConteiner(image, docker.CreateContainerOptions{
		Env: append(build.Config.Env, []string{
			"POSTGRES_USER=postgres",
			"POSTGRES_PASSWORD=postgres",
			"POSTGRES_DB=postgres",

			fmt.Sprintf("TEST_DATABASE=%s", build.Config.Postgres.Database),
			fmt.Sprintf("TEST_USERNAME=%s", build.Config.Postgres.Username),
			fmt.Sprintf("TEST_PASSWORD=%s", build.Config.Postgres.Password),
		}...),
		Binds: []string{
			fmt.Sprintf("%s:%s", build.WorkingDir, WorkingDir),
			fmt.Sprintf("%s:%s", assets, AssetsDir),
		},
		Entrypoint: build.Config.Entrypoint,
		WorkingDir: WorkingDir,
	})

	if err != nil {

		return fmt.Errorf("Could not create container: %v", err)
	}

	defer container.Destroy()

	serverVersion, err = waitingForStartup(container.IPAddress)

	if err != nil {

		return fmt.Errorf("Could not run PostgreSQL server: %v", err)
	}

	log.Debugf("PostgreSQL %s database server started", serverVersion)

	for _, command := range append([]string{"bash /opt/postgres-ci/assets/setup.sh"}, build.Config.Commands...) {

		log.Debugf("Run cmd: %s", command)

		if err := container.RunCmd(command); err != nil {

			return fmt.Errorf("Execute failed. Cmd: %s, output: %s", command, container.Output())
		}
	}

	log.Debugf("Output: %s", container.Output())

	connectToContainer, err := sqlx.Connect("postgres", build.DSN(container.IPAddress))

	if err != nil {

		return fmt.Errorf("Could not connect to PostgreSQL server: %v", err)
	}

	defer connectToContainer.Close()

	var (
		tests plpgsqlTests
		sql   = `
		SELECT 
			namespace,
			procedure,
			to_json(errors) AS errors,
			started_at,
			finished_at
		FROM assert.test_runner()
		`
	)
	if err := connectToContainer.Select(&tests, sql); err != nil {

		return fmt.Errorf("Error when running a tests: %v", err)
	}

	for _, test := range tests {

		if len(test.Errors) != 0 {

			log.Debugf("--- FAIL: %s.%s %v (%.4fs)\n\t", test.Namespace, test.Procedure, test.Errors, test.FinishedAt.Sub(test.StartedAt).Seconds())

		} else {

			log.Debugf("--- PASS: %s.%s (%.4fs)", test.Namespace, test.Procedure, test.FinishedAt.Sub(test.StartedAt).Seconds())
		}
	}

	_, err = connect.Exec(`SELECT build.add_part($1, $2, $3, $4, $5, $6, $7)`,
		build.BuildID,
		serverVersion,
		image,
		container.ID(),
		container.Output(),
		startedAt,
		tests,
	)

	if err != nil {

		return fmt.Errorf("Could not commit a part of the build: %v", err)
	}

	return nil
}