Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
cluster and identity_cluster tables (#24)
Browse files Browse the repository at this point in the history
* Initial migration

* Rename F8CLUSTER env vars to F8

* clean up packages

* migration tests

* Fix goa generation

* address comments
  • Loading branch information
alexeykazakov authored Nov 7, 2018
1 parent 1a7e440 commit bddfb4e
Show file tree
Hide file tree
Showing 42 changed files with 236 additions and 1,861 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ client/
swagger/
tool/
migration/sqlbindata.go
migration/sqlbindata_test.go
configuration/confbindata.go
wit/witservice
account/tenant
6 changes: 3 additions & 3 deletions .make/docker.mk
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ else
-t \
$(DOCKER_RUN_INTERACTIVE_SWITCH) \
--name="$(DOCKER_CONTAINER_NAME)" \
-e F8CLUSTER_POSTGRES_PORT=5432 \
-e F8_POSTGRES_PORT=5432 \
-v $(CUR_DIR):$(PACKAGE_PATH):Z \
-u $(shell id -u $(USER)):$(shell id -g $(USER)) \
-e GOPATH=$(GOPATH_IN_CONTAINER) \
Expand Down Expand Up @@ -114,8 +114,8 @@ endif
ifeq ($(strip $(shell docker inspect --format '{{ .NetworkSettings.IPAddress }}' make_postgres_integration_test_1 2>/dev/null)),)
$(error Failed to find PostgreSQL container. Try running "make integration-test-env-prepare")
endif
$(eval F8CLUSTER_POSTGRES_HOST := $(shell docker inspect --format '{{ .NetworkSettings.IPAddress }}' make_postgres_integration_test_1 2>/dev/null))
docker exec -t $(DOCKER_RUN_INTERACTIVE_SWITCH) "$(DOCKER_CONTAINER_NAME)" bash -ec 'export F8CLUSTER_POSTGRES_HOST=$(F8CLUSTER_POSTGRES_HOST); make $(makecommand)'
$(eval F8_POSTGRES_HOST := $(shell docker inspect --format '{{ .NetworkSettings.IPAddress }}' make_postgres_integration_test_1 2>/dev/null))
docker exec -t $(DOCKER_RUN_INTERACTIVE_SWITCH) "$(DOCKER_CONTAINER_NAME)" bash -ec 'export F8_POSTGRES_HOST=$(F8_POSTGRES_HOST); make $(makecommand)'

# This is a wildcard target to let you call any make target from the normal makefile
# but it will run inside the docker container. This target will only get executed if
Expand Down
18 changes: 9 additions & 9 deletions .make/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ GO_TEST_VERBOSITY_FLAG ?=

# By default use the "localhost" or specify manually during make invocation:
#
# F8CLUSTER_POSTGRES_HOST=somehost make test-integration
# F8_POSTGRES_HOST=somehost make test-integration
#
F8CLUSTER_POSTGRES_HOST ?= localhost
F8_POSTGRES_HOST ?= localhost

# By default reduce the amount of log output from tests
F8_LOG_LEVEL ?= error
Expand Down Expand Up @@ -147,7 +147,7 @@ test-unit-with-coverage: prebuild-check clean-coverage-unit $(COV_PATH_UNIT)
test-unit: prebuild-check $(SOURCES)
$(call log-info,"Running test: $@")
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
F8CLUSTER_DEVELOPER_MODE_ENABLED=1 F8CLUSTER_RESOURCE_UNIT_TEST=1 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)
F8_DEVELOPER_MODE_ENABLED=1 F8_RESOURCE_UNIT_TEST=1 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)

.PHONY: test-unit-junit
test-unit-junit: prebuild-check ${GO_JUNIT_BIN} ${TMP_PATH}
Expand All @@ -164,12 +164,12 @@ test-integration-with-coverage: prebuild-check clean-coverage-integration migrat
test-integration: prebuild-check migrate-database $(SOURCES)
$(call log-info,"Running test: $@")
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
F8CLUSTER_DEVELOPER_MODE_ENABLED=1 F8CLUSTER_RESOURCE_DATABASE=1 F8CLUSTER_RESOURCE_UNIT_TEST=0 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)
F8_DEVELOPER_MODE_ENABLED=1 F8_RESOURCE_DATABASE=1 F8_RESOURCE_UNIT_TEST=0 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)

test-integration-benchmark: prebuild-check migrate-database $(SOURCES)
$(call log-info,"Running benchmarks: $@")
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
F8CLUSTER_DEVELOPER_MODE_ENABLED=1 F8CLUSTER_LOG_LEVEL=error F8CLUSTER_RESOURCE_DATABASE=1 F8CLUSTER_RESOURCE_UNIT_TEST=0 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test -run=^$$ -bench=. -cpu 1,2,4 -test.benchmem $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)
F8_DEVELOPER_MODE_ENABLED=1 F8_LOG_LEVEL=error F8_RESOURCE_DATABASE=1 F8_RESOURCE_UNIT_TEST=0 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test -run=^$$ -bench=. -cpu 1,2,4 -test.benchmem $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)

.PHONY: test-remote-with-coverage
## Runs the remote tests and produces coverage files for each package.
Expand All @@ -180,13 +180,13 @@ test-remote-with-coverage: prebuild-check clean-coverage-remote $(COV_PATH_REMOT
test-remote: prebuild-check $(SOURCES)
$(call log-info,"Running test: $@")
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
F8CLUSTER_DEVELOPER_MODE_ENABLED=1 F8CLUSTER_RESOURCE_REMOTE=1 F8CLUSTER_RESOURCE_UNIT_TEST=0 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)
F8_DEVELOPER_MODE_ENABLED=1 F8_RESOURCE_REMOTE=1 F8_RESOURCE_UNIT_TEST=0 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES)

.PHONY: test-migration
## Runs the migration tests and should be executed before running the integration tests
## in order to have a clean database
test-migration: prebuild-check
F8CLUSTER_RESOURCE_DATABASE=1 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test $(GO_TEST_VERBOSITY_FLAG) github.com/fabric8-services/fabric8-cluster/migration
F8_RESOURCE_DATABASE=1 F8_LOG_LEVEL=$(F8_LOG_LEVEL) go test $(GO_TEST_VERBOSITY_FLAG) github.com/fabric8-services/fabric8-cluster/migration

# Downloads docker-compose to tmp/docker-compose if it does not already exist.
define download-docker-compose
Expand Down Expand Up @@ -422,7 +422,7 @@ $(eval ENV_VAR := $(5))
$(eval ALL_PKGS_COMMA_SEPARATED := $(6))
@mkdir -p $(COV_DIR)/$(PACKAGE_NAME);
$(eval COV_OUT_FILE := $(COV_DIR)/$(PACKAGE_NAME)/coverage.$(TEST_NAME).mode-$(COVERAGE_MODE))
@$(ENV_VAR) F8CLUSTER_DEVELOPER_MODE_ENABLED=1 F8CLUSTER_POSTGRES_HOST=$(F8CLUSTER_POSTGRES_HOST) F8_LOG_LEVEL=$(F8_LOG_LEVEL) \
@$(ENV_VAR) F8_DEVELOPER_MODE_ENABLED=1 F8_POSTGRES_HOST=$(F8_POSTGRES_HOST) F8_LOG_LEVEL=$(F8_LOG_LEVEL) \
go test $(PACKAGE_NAME) \
$(GO_TEST_VERBOSITY_FLAG) \
-coverprofile $(COV_OUT_FILE) \
Expand Down Expand Up @@ -482,7 +482,7 @@ $(COV_PATH_INTEGRATION): $(SOURCES) $(GOCOVMERGE_BIN)
@-rm -f $(ERRORS_FILE)
$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN)))
$(eval ALL_PKGS_COMMA_SEPARATED:=$(shell echo $(TEST_PACKAGES) | tr ' ' ,))
$(foreach package, $(TEST_PACKAGES), $(call test-package,$(TEST_NAME),$(package),$(COV_PATH_INTEGRATION),$(ERRORS_FILE),F8CLUSTER_RESOURCE_DATABASE=1 F8CLUSTER_RESOURCE_UNIT_TEST=0,$(ALL_PKGS_COMMA_SEPARATED)))
$(foreach package, $(TEST_PACKAGES), $(call test-package,$(TEST_NAME),$(package),$(COV_PATH_INTEGRATION),$(ERRORS_FILE),F8_RESOURCE_DATABASE=1 F8_RESOURCE_UNIT_TEST=0,$(ALL_PKGS_COMMA_SEPARATED)))
$(call check-test-results,$(ERRORS_FILE))

# NOTE: We don't have prebuild-check as a dependency here because it would cause
Expand Down
14 changes: 7 additions & 7 deletions Dockerfile.deploy
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ FROM centos:7
LABEL maintainer "Devtools <devtools@redhat.com>"
LABEL author "Konrad Kleine <kkleine@redhat.com>"
ENV LANG=en_US.utf8
ENV F8CLUSTER_INSTALL_PREFIX=/usr/local/cluster
ENV F8_INSTALL_PREFIX=/usr/local/cluster

# Create a non-root user and a group with the same name: "cluster"
ENV F8CLUSTER_USER_NAME=cluster
RUN useradd --no-create-home -s /bin/bash ${F8CLUSTER_USER_NAME}
ENV F8_USER_NAME=cluster
RUN useradd --no-create-home -s /bin/bash ${F8_USER_NAME}

COPY bin/cluster ${F8CLUSTER_INSTALL_PREFIX}/bin/cluster
COPY config.yaml ${F8CLUSTER_INSTALL_PREFIX}/etc/config.yaml
COPY bin/cluster ${F8_INSTALL_PREFIX}/bin/cluster
COPY config.yaml ${F8_INSTALL_PREFIX}/etc/config.yaml

# Install little pcp pmcd server for metrics collection
# would prefer only pmcd, and not the /bin/pm*tools etc.
Expand All @@ -23,9 +23,9 @@ EXPOSE 44321


# From here onwards, any RUN, CMD, or ENTRYPOINT will be run under the following user
USER ${F8CLUSTER_USER_NAME}
USER ${F8_USER_NAME}

WORKDIR ${F8CLUSTER_INSTALL_PREFIX}
WORKDIR ${F8_INSTALL_PREFIX}
ENTRYPOINT [ "/cluster+pmcd.sh" ]

EXPOSE 8087
14 changes: 7 additions & 7 deletions Dockerfile.deploy.rhel
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ FROM quay.io/openshiftio/rhel-base-golang:latest
LABEL maintainer "Devtools <devtools@redhat.com>"
LABEL author "Devtools <devtools@redhat.com>"
ENV LANG=en_US.utf8
ENV F8CLUSTER_INSTALL_PREFIX=/usr/local/cluster
ENV F8_INSTALL_PREFIX=/usr/local/cluster

# Create a non-root user and a group with the same name: "cluster"
ENV F8CLUSTER_USER_NAME=cluster
RUN useradd --no-create-home -s /bin/bash ${F8CLUSTER_USER_NAME}
ENV F8_USER_NAME=cluster
RUN useradd --no-create-home -s /bin/bash ${F8_USER_NAME}

COPY bin/cluster ${F8CLUSTER_INSTALL_PREFIX}/bin/cluster
COPY config.yaml ${F8CLUSTER_INSTALL_PREFIX}/etc/config.yaml
COPY bin/cluster ${F8_INSTALL_PREFIX}/bin/cluster
COPY config.yaml ${F8_INSTALL_PREFIX}/etc/config.yaml

COPY ./cluster+pmcd.sh /cluster+pmcd.sh
EXPOSE 44321

# From here onwards, any RUN, CMD, or ENTRYPOINT will be run under the following user
USER ${F8CLUSTER_USER_NAME}
USER ${F8_USER_NAME}

WORKDIR ${F8CLUSTER_INSTALL_PREFIX}
WORKDIR ${F8_INSTALL_PREFIX}
ENTRYPOINT [ "/cluster+pmcd.sh" ]

EXPOSE 8087
11 changes: 8 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# unused-packages = true

required = [
"github.com/fabric8-services/fabric8-common/goasupport/jsonapi_errors_helpers", # needed by goa generator
"github.com/golang/lint/golint",
"github.com/fzipp/gocyclo",
"github.com/goadesign/goa/cors",
Expand Down Expand Up @@ -52,10 +53,6 @@ required = [
name = "github.com/goadesign/goa"
version = "1.3.0"

[[constraint]]
name = "github.com/jinzhu/gorm"
version = "1.9.1"

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
Expand Down
13 changes: 3 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,14 @@ $(GOCYCLO_BIN):
cd $(VENDOR_DIR)/github.com/fzipp/gocyclo && go build -v

# Pack all migration SQL files into a compilable Go file
migration/sqlbindata.go: $(GO_BINDATA_BIN) $(wildcard migration/sql-files/*.sql) migration/sqlbindata_test.go
migration/sqlbindata.go: $(GO_BINDATA_BIN) $(wildcard migration/sql-files/*.sql)
$(GO_BINDATA_BIN) \
-o migration/sqlbindata.go \
-pkg migration \
-prefix migration/sql-files \
-nocompress \
migration/sql-files

migration/sqlbindata_test.go: $(GO_BINDATA_BIN) $(wildcard migration/sql-test-files/*.sql)
$(GO_BINDATA_BIN) \
-o migration/sqlbindata_test.go \
-pkg migration_test \
-prefix migration/sql-test-files \
-nocompress \
migration/sql-test-files

# Pack configuration files into a compilable Go file
configuration/confbindata.go: $(GO_BINDATA_BIN) $(wildcard configuration/conf-files/*.conf)
$(GO_BINDATA_BIN) \
Expand Down Expand Up @@ -261,6 +253,7 @@ app/controllers.go: $(DESIGNS) $(GOAGEN_BIN) $(VENDOR_DIR)
$(GOAGEN_BIN) app -d ${PACKAGE_NAME}/${DESIGN_DIR}
$(GOAGEN_BIN) controller -d ${PACKAGE_NAME}/${DESIGN_DIR} -o controller/ --pkg controller --app-pkg ${PACKAGE_NAME}/app
$(GOAGEN_BIN) client -d ${PACKAGE_NAME}/${DESIGN_DIR}
$(GOAGEN_BIN) gen -d ${PACKAGE_NAME}/${DESIGN_DIR} --pkg-path=github.com/fabric8-services/fabric8-common/goasupport/jsonapi_errors_helpers --out app
$(GOAGEN_BIN) swagger -d ${PACKAGE_NAME}/${DESIGN_DIR}

.PHONY: migrate-database
Expand All @@ -283,7 +276,7 @@ regenerate: clean-generated generate
.PHONY: dev
dev: prebuild-check deps generate $(FRESH_BIN)
docker-compose up -d db
F8CLUSTER_DEVELOPER_MODE_ENABLED=true $(FRESH_BIN)
F8_DEVELOPER_MODE_ENABLED=true $(FRESH_BIN)

include ./.make/test.mk

Expand Down
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ $ cd $GOPATH/src/github.com/fabric8-services/fabric8-cluster
$ make test-all
----

By default, test data is removed from the database after each test, unless the `F8CLUSTER_CLEAN_TEST_DATA` environment variable is set to `false`. This can be particularily useful to run queries on the test data after a test failure, in order to understand why the result did not match the expectations.
By default, test data is removed from the database after each test, unless the `F8_CLEAN_TEST_DATA` environment variable is set to `false`. This can be particularily useful to run queries on the test data after a test failure, in order to understand why the result did not match the expectations.

Also, all SQL queries can be displayed in the output if the `F8CLUSTER_ENABLE_DB_LOGS` environment variable is set to `true. Beware that this can be very verbose, though ;)
Also, all SQL queries can be displayed in the output if the `F8_ENABLE_DB_LOGS` environment variable is set to `true. Beware that this can be very verbose, though ;)

===== Coverage [[coverage]]

Expand Down
20 changes: 9 additions & 11 deletions application/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,30 @@ import (

"github.com/fabric8-services/fabric8-cluster/application/transaction"
"github.com/fabric8-services/fabric8-cluster/gormtestsupport"
"github.com/fabric8-services/fabric8-cluster/resource"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type TestTransaction struct {
type TransactionTestSuite struct {
gormtestsupport.DBTestSuite
}

func TestRunTransaction(t *testing.T) {
resource.Require(t, resource.Database)
suite.Run(t, &TestTransaction{DBTestSuite: gormtestsupport.NewDBTestSuite()})
func TestTransaction(t *testing.T) {
suite.Run(t, &TransactionTestSuite{DBTestSuite: gormtestsupport.NewDBTestSuite()})
}

func (test *TestTransaction) TestTransactionOK() {
err := transaction.Transactional(test.Application, func(tr transaction.TransactionalResources) error {
func (s *TransactionTestSuite) TestTransactionOK() {
err := transaction.Transactional(s.Application, func(tr transaction.TransactionalResources) error {
return nil
})
require.NoError(test.T(), err)
require.NoError(s.T(), err)
}

func (test *TestTransaction) TestTransactionFail() {
err := transaction.Transactional(test.Application, func(tr transaction.TransactionalResources) error {
func (s *TransactionTestSuite) TestTransactionFail() {
err := transaction.Transactional(s.Application, func(tr transaction.TransactionalResources) error {
return errors.New("Oopsie Woopsie")
})
require.Error(test.T(), err)
require.Error(s.T(), err)
}
14 changes: 7 additions & 7 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"sync"
"time"

"github.com/fabric8-services/fabric8-cluster/rest"
commoncfg "github.com/fabric8-services/fabric8-common/configuration"
"github.com/fabric8-services/fabric8-common/httpsupport"

"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
Expand Down Expand Up @@ -112,7 +112,7 @@ func NewConfigurationData(mainConfigFile string, osoClusterConfigFile string) (*
}

// Set up the main configuration
c.v.SetEnvPrefix("F8CLUSTER")
c.v.SetEnvPrefix("F8")
c.v.AutomaticEnv()
c.v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
c.v.SetTypeByDefaultValue(true)
Expand Down Expand Up @@ -250,7 +250,7 @@ func convertAPIURL(apiURL string, newPrefix string, newPath string) (string, err
if err != nil {
return "", err
}
newHost, err := rest.ReplaceDomainPrefix(newURL.Host, newPrefix)
newHost, err := httpsupport.ReplaceDomainPrefix(newURL.Host, newPrefix)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -327,12 +327,12 @@ func pathExists(pathToCheck string) (string, error) {

func getMainConfigFile() string {
// This was either passed as a env var or set inside main.go from --config
envConfigPath, _ := os.LookupEnv("F8CLUSTER_CONFIG_FILE_PATH")
envConfigPath, _ := os.LookupEnv("F8_CONFIG_FILE_PATH")
return envConfigPath
}

func getOSOClusterConfigFile() string {
envOSOClusterConfigFile, _ := os.LookupEnv("F8CLUSTER_OSO_CLUSTER_CONFIG_FILE")
envOSOClusterConfigFile, _ := os.LookupEnv("F8_OSO_CLUSTER_CONFIG_FILE")
return envOSOClusterConfigFile
}

Expand Down Expand Up @@ -475,7 +475,7 @@ func (c *ConfigurationData) GetOSOClusterByURL(url string) *OSOCluster {
defer c.mux.RUnlock()

for apiURL, cluster := range c.clusters {
if strings.HasPrefix(rest.AddTrailingSlashToURL(url), apiURL) {
if strings.HasPrefix(httpsupport.AddTrailingSlashToURL(url), apiURL) {
return &cluster
}
}
Expand Down Expand Up @@ -666,7 +666,7 @@ func (c *ConfigurationData) IsLogJSON() bool {

// GetEnvironment returns the current environment application is deployed in
// like 'production', 'prod-preview', 'local', etc as the value of environment variable
// `F8CLUSTER_ENVIRONMENT` is set.
// `F8_ENVIRONMENT` is set.
func (c *ConfigurationData) GetEnvironment() string {
return c.v.GetString(varEnvironment)
}
Expand Down
Loading

0 comments on commit bddfb4e

Please sign in to comment.