Skip to content

Commit

Permalink
Add server startup test to prevent docker image master-auto-setup get…
Browse files Browse the repository at this point in the history
…ting broken
  • Loading branch information
longquanzheng authored Oct 5, 2021
1 parent b1e3001 commit e949687
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ steps:
agents:
queue: "workers"
docker: "*"
command: "make cover_profile"
command: "CASSANDRA_HOST=cassandra make install-schema && make cover_profile" # make install-schema is needed for a server startup test. See main_test.go
artifact_paths:
- ".build/coverage/*.out"
retry:
Expand Down
18 changes: 11 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,11 @@ COVER_PKGS = client common host service tools
# pkg -> pkg/... -> github.com/uber/cadence/pkg/... -> join with commas
GOCOVERPKG_ARG := -coverpkg="$(subst $(SPACE),$(COMMA),$(addprefix $(PROJECT_ROOT)/,$(addsuffix /...,$(COVER_PKGS))))"

test: bins ## Build and run all tests
test: bins ## Build and run all tests. This target is for local development. The pipeline is using cover_profile target
@rm -f test
@rm -f test.log
@echo Running special test cases without race detector:
@go test -v ./cmd/server/cadence/
@for dir in $(PKG_TEST_DIRS); do \
go test $(TEST_ARG) -coverprofile=$@ "$$dir" $(TEST_TAG) | tee -a test.log; \
done;
Expand All @@ -487,6 +489,8 @@ cover_profile: bins
@mkdir -p $(COVER_ROOT)
@echo "mode: atomic" > $(UNIT_COVER_FILE)

@echo Running special test cases without race detector:
@go test -v ./cmd/server/cadence/
@echo Running package tests:
@for dir in $(PKG_TEST_DIRS); do \
mkdir -p $(BUILD)/"$$dir"; \
Expand Down Expand Up @@ -531,12 +535,12 @@ cover_ci: $(COVER_ROOT)/cover.out $(BIN)/goveralls
$(BIN)/goveralls -coverprofile=$(COVER_ROOT)/cover.out -service=buildkite || echo Coveralls failed;

install-schema: cadence-cassandra-tool
./cadence-cassandra-tool --ep 127.0.0.1 create -k cadence --rf 1
./cadence-cassandra-tool --ep 127.0.0.1 -k cadence setup-schema -v 0.0
./cadence-cassandra-tool --ep 127.0.0.1 -k cadence update-schema -d ./schema/cassandra/cadence/versioned
./cadence-cassandra-tool --ep 127.0.0.1 create -k cadence_visibility --rf 1
./cadence-cassandra-tool --ep 127.0.0.1 -k cadence_visibility setup-schema -v 0.0
./cadence-cassandra-tool --ep 127.0.0.1 -k cadence_visibility update-schema -d ./schema/cassandra/visibility/versioned
./cadence-cassandra-tool create -k cadence --rf 1
./cadence-cassandra-tool -k cadence setup-schema -v 0.0
./cadence-cassandra-tool -k cadence update-schema -d ./schema/cassandra/cadence/versioned
./cadence-cassandra-tool create -k cadence_visibility --rf 1
./cadence-cassandra-tool -k cadence_visibility setup-schema -v 0.0
./cadence-cassandra-tool -k cadence_visibility update-schema -d ./schema/cassandra/visibility/versioned

install-schema-mysql: cadence-sql-tool
./cadence-sql-tool --ep 127.0.0.1 --user root --pw cadence create --db cadence
Expand Down
110 changes: 110 additions & 0 deletions cmd/server/cadence/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// +build !race

package cadence

import (
"log"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/config"
_ "github.com/uber/cadence/common/persistence/nosql/nosqlplugin/cassandra" // needed to load cassandra plugin
_ "github.com/uber/cadence/common/persistence/nosql/nosqlplugin/cassandra/gocql/public" // needed to load the default gocql client
"github.com/uber/cadence/common/service"
"github.com/uber/cadence/tools/cassandra"
)

type ServerSuite struct {
*require.Assertions
suite.Suite
}

func TestServerSuite(t *testing.T) {
suite.Run(t, new(ServerSuite))
}

func (s *ServerSuite) SetupTest() {
s.Assertions = require.New(s.T())
}

/*
TestServerStartup tests the startup logic for the binary. When this fails, you should be able to reproduce by running "cadence-server start"
*/
func (s *ServerSuite) TestServerStartup() {
// If you want to test it locally, change it to false
runInBuildKite := true

env := "development"
zone := ""
rootDir := "../../../"
configDir := constructPathIfNeed(rootDir, "config")

log.Printf("Loading config; env=%v,zone=%v,configDir=%v\n", env, zone, configDir)

var cfg config.Config
err := config.Load(env, configDir, zone, &cfg)
if err != nil {
log.Fatal("Config file corrupted.", err)
}
// replace local host to docker network
if runInBuildKite {
ds := cfg.Persistence.DataStores[cfg.Persistence.DefaultStore]
ds.NoSQL.Hosts = "cassandra"
cfg.Persistence.DataStores[cfg.Persistence.DefaultStore] = ds

ds = cfg.Persistence.DataStores[cfg.Persistence.VisibilityStore]
ds.NoSQL.Hosts = "cassandra"
cfg.Persistence.DataStores[cfg.Persistence.VisibilityStore] = ds
}

log.Printf("config=\n%v\n", cfg.String())

cfg.DynamicConfig.FileBased.Filepath = constructPathIfNeed(rootDir, cfg.DynamicConfig.FileBased.Filepath)

if err := cfg.ValidateAndFillDefaults(); err != nil {
log.Fatalf("config validation failed: %v", err)
}
// cassandra schema version validation
if err := cassandra.VerifyCompatibleVersion(cfg.Persistence); err != nil {
log.Fatal("cassandra schema version compatibility check failed: ", err)
}

var daemons []common.Daemon
services := service.ShortNames(service.List)
for _, svc := range services {
server := newServer(svc, &cfg)
daemons = append(daemons, server)
server.Start()
}

timer := time.NewTimer(time.Second * 10)

<-timer.C
for _, daemon := range daemons {
daemon.Stop()
}
}
2 changes: 2 additions & 0 deletions docker/buildkite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ unit tests:
docker-compose -f docker/buildkite/docker-compose-local.yml build unit-test
```

NOTE: You would expect TestServerStartup to fail here as we don't have a way to install the schema like we do in pipeline.yml

integration tests:
```bash
docker-compose -f docker/buildkite/docker-compose-local.yml build integration-test-cassandra
Expand Down

0 comments on commit e949687

Please sign in to comment.