• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uber / cadence / 018e86dc-121c-4962-9b30-23df2fb36173

28 Mar 2024 08:59PM UTC coverage: 65.259% (+0.007%) from 65.252%
018e86dc-121c-4962-9b30-23df2fb36173

push

buildkite

web-flow
Do not panic when setting env values (#5811)

89 of 122 new or added lines in 11 files covered. (72.95%)

32 existing lines in 10 files now uncovered.

95429 of 146231 relevant lines covered (65.26%)

2375.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.05
/common/persistence/sql/sql_testing_util.go
1
// Copyright (c) 2017 Uber Technologies, Inc.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a copy
4
// of this software and associated documentation files (the "Software"), to deal
5
// in the Software without restriction, including without limitation the rights
6
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
// copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in
11
// all copies or substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
// THE SOFTWARE.
20

21
package sql
22

23
import (
24
        "context"
25
        "errors"
26
        "fmt"
27
        "io/ioutil"
28
        "log"
29
        "os"
30
        "strings"
31

32
        "github.com/uber/cadence/common"
33
        "github.com/uber/cadence/common/config"
34
        "github.com/uber/cadence/common/dynamicconfig"
35
        "github.com/uber/cadence/common/persistence/persistence-tests/testcluster"
36
        "github.com/uber/cadence/environment"
37
)
38

39
// testCluster allows executing cassandra operations in testing.
40
type testCluster struct {
41
        dbName    string
42
        schemaDir string
43
        cfg       config.SQL
44
}
45

46
var _ testcluster.PersistenceTestCluster = (*testCluster)(nil)
47

48
// NewTestCluster returns a new SQL test cluster
49
func NewTestCluster(pluginName, dbName, username, password, host string, port int, schemaDir string) (testcluster.PersistenceTestCluster, error) {
10✔
50
        var result testCluster
10✔
51
        var err error
10✔
52
        if port == 0 {
10✔
NEW
53
                port, err = environment.GetMySQLPort()
×
NEW
54
                if err != nil {
×
NEW
55
                        return nil, err
×
NEW
56
                }
×
57
        }
58

59
        if schemaDir == "" {
10✔
NEW
60
                return nil, errors.New("schemaDir is empty")
×
UNCOV
61
        }
×
62
        result.dbName = dbName
10✔
63
        result.schemaDir = schemaDir
10✔
64
        result.cfg = config.SQL{
10✔
65
                User:            username,
10✔
66
                Password:        password,
10✔
67
                ConnectAddr:     fmt.Sprintf("%v:%v", host, port),
10✔
68
                ConnectProtocol: "tcp",
10✔
69
                PluginName:      pluginName,
10✔
70
                DatabaseName:    dbName,
10✔
71
                NumShards:       4,
10✔
72
                EncodingType:    "thriftrw",
10✔
73
                DecodingTypes:   []string{"thriftrw"},
10✔
74
        }
10✔
75
        return &result, nil
10✔
76
}
77

78
// DatabaseName from PersistenceTestCluster interface
79
func (s *testCluster) DatabaseName() string {
×
80
        return s.dbName
×
81
}
×
82

83
// SetupTestDatabase from PersistenceTestCluster interface
84
func (s *testCluster) SetupTestDatabase() {
10✔
85
        s.createDatabase()
10✔
86

10✔
87
        schemaDir := s.schemaDir + "/"
10✔
88
        if !strings.HasPrefix(schemaDir, "/") && !strings.HasPrefix(schemaDir, "../") {
20✔
89
                cadencePackageDir, err := getCadencePackageDir()
10✔
90
                if err != nil {
10✔
91
                        log.Fatal(err)
×
92
                }
×
93
                schemaDir = cadencePackageDir + schemaDir
10✔
94
        }
95
        s.loadSchema([]string{"schema.sql"}, schemaDir)
10✔
96
        s.loadVisibilitySchema([]string{"schema.sql"}, schemaDir)
10✔
97
}
98

99
// Config returns the persistence config for connecting to this test cluster
100
func (s *testCluster) Config() config.Persistence {
18✔
101
        cfg := s.cfg
18✔
102
        return config.Persistence{
18✔
103
                DefaultStore:    "test",
18✔
104
                VisibilityStore: "test",
18✔
105
                DataStores: map[string]config.DataStore{
18✔
106
                        "test": {SQL: &cfg},
18✔
107
                },
18✔
108
                TransactionSizeLimit: dynamicconfig.GetIntPropertyFn(common.DefaultTransactionSizeLimit),
18✔
109
                ErrorInjectionRate:   dynamicconfig.GetFloatPropertyFn(0),
18✔
110
        }
18✔
111
}
18✔
112

113
// TearDownTestDatabase from PersistenceTestCluster interface
114
func (s *testCluster) TearDownTestDatabase() {
10✔
115
        s.dropDatabase()
10✔
116
}
10✔
117

118
// createDatabase from PersistenceTestCluster interface
119
func (s *testCluster) createDatabase() {
10✔
120
        cfg2 := s.cfg
10✔
121
        // NOTE need to connect with empty name to create new database
10✔
122
        cfg2.DatabaseName = ""
10✔
123
        db, err := NewSQLAdminDB(&cfg2)
10✔
124
        if err != nil {
10✔
125
                panic(err)
×
126
        }
127
        defer func() {
20✔
128
                err := db.Close()
10✔
129
                if err != nil {
10✔
130
                        panic(err)
×
131
                }
132
        }()
133
        err = db.CreateDatabase(s.cfg.DatabaseName)
10✔
134
        if err != nil {
10✔
135
                panic(err)
×
136
        }
137
}
138

139
// dropDatabase from PersistenceTestCluster interface
140
func (s *testCluster) dropDatabase() {
10✔
141
        cfg2 := s.cfg
10✔
142
        // NOTE need to connect with empty name to drop the database
10✔
143
        cfg2.DatabaseName = ""
10✔
144
        db, err := NewSQLAdminDB(&cfg2)
10✔
145
        if err != nil {
10✔
146
                panic(err)
×
147
        }
148
        defer func() {
20✔
149
                err := db.Close()
10✔
150
                if err != nil {
10✔
151
                        panic(err)
×
152
                }
153
        }()
154
        err = db.DropDatabase(s.cfg.DatabaseName)
10✔
155
        if err != nil {
10✔
156
                panic(err)
×
157
        }
158
}
159

160
// loadSchema from PersistenceTestCluster interface
161
func (s *testCluster) loadSchema(fileNames []string, schemaDir string) {
10✔
162
        workflowSchemaDir := schemaDir + "/cadence"
10✔
163
        err := s.loadDatabaseSchema(workflowSchemaDir, fileNames, true)
10✔
164
        if err != nil {
10✔
165
                log.Fatal(err)
×
166
        }
×
167
}
168

169
// loadVisibilitySchema from PersistenceTestCluster interface
170
func (s *testCluster) loadVisibilitySchema(fileNames []string, schemaDir string) {
10✔
171
        workflowSchemaDir := schemaDir + "/visibility"
10✔
172
        err := s.loadDatabaseSchema(workflowSchemaDir, fileNames, true)
10✔
173
        if err != nil {
10✔
174
                log.Fatal(err)
×
175
        }
×
176
}
177

178
func getCadencePackageDir() (string, error) {
10✔
179
        cadencePackageDir, err := os.Getwd()
10✔
180
        if err != nil {
10✔
181
                panic(err)
×
182
        }
183
        cadenceIndex := strings.LastIndex(cadencePackageDir, "/cadence/")
10✔
184
        cadencePackageDir = cadencePackageDir[:cadenceIndex+len("/cadence/")]
10✔
185
        return cadencePackageDir, err
10✔
186
}
187

188
// loadDatabaseSchema loads the schema from the given .sql files on this database
189
func (s *testCluster) loadDatabaseSchema(dir string, fileNames []string, override bool) (err error) {
18✔
190
        db, err := NewSQLAdminDB(&s.cfg)
18✔
191
        if err != nil {
18✔
192
                panic(err)
×
193
        }
194
        defer func() {
36✔
195
                err := db.Close()
18✔
196
                if err != nil {
18✔
197
                        panic(err)
×
198
                }
199
        }()
200

201
        for _, file := range fileNames {
36✔
202
                // This is only used in tests. Excluding it from security scanners
18✔
203
                // #nosec
18✔
204
                content, err := ioutil.ReadFile(dir + "/" + file)
18✔
205
                if err != nil {
18✔
206
                        return fmt.Errorf("error reading contents of file %v:%v", file, err.Error())
×
207
                }
×
208
                err = db.ExecSchemaOperationQuery(context.Background(), string(content))
18✔
209
                if err != nil {
18✔
210
                        return fmt.Errorf("error loading schema from %v: %v", file, err.Error())
×
211
                }
×
212
        }
213
        return nil
18✔
214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc