(dbSetting TestDBSetting)
| 137 | } |
| 138 | |
| 139 | func initDatabaseImage(dbSetting TestDBSetting) (connection string, cleanup func(), err error) { |
| 140 | // sqlite3 don't need to set up image |
| 141 | if dbSetting.Driver == string(schemas.SQLITE) { |
| 142 | return dbSetting.Connection, func() { |
| 143 | log.Info("remove database", dbSetting.Connection) |
| 144 | err = os.Remove(dbSetting.Connection) |
| 145 | if err != nil { |
| 146 | log.Error(err) |
| 147 | } |
| 148 | }, nil |
| 149 | } |
| 150 | pool, err := dockertest.NewPool("") |
| 151 | pool.MaxWait = time.Minute * 5 |
| 152 | if err != nil { |
| 153 | return "", nil, fmt.Errorf("could not connect to docker: %s", err) |
| 154 | } |
| 155 | |
| 156 | // resource, err := pool.Run(dbSetting.ImageName, dbSetting.ImageVersion, dbSetting.ENV) |
| 157 | resource, err := pool.RunWithOptions(&dockertest.RunOptions{ |
| 158 | Repository: dbSetting.ImageName, |
| 159 | Tag: dbSetting.ImageVersion, |
| 160 | Env: dbSetting.ENV, |
| 161 | }, func(config *docker.HostConfig) { |
| 162 | config.AutoRemove = true |
| 163 | config.RestartPolicy = docker.RestartPolicy{Name: "no"} |
| 164 | }) |
| 165 | if err != nil { |
| 166 | return "", nil, fmt.Errorf("could not pull resource: %s", err) |
| 167 | } |
| 168 | |
| 169 | connection = fmt.Sprintf(dbSetting.Connection, resource.GetPort(dbSetting.PortID)) |
| 170 | if err := pool.Retry(func() error { |
| 171 | db, err := sql.Open(dbSetting.Driver, connection) |
| 172 | if err != nil { |
| 173 | return err |
| 174 | } |
| 175 | return db.Ping() |
| 176 | }); err != nil { |
| 177 | return "", nil, fmt.Errorf("could not connect to database: %s", err) |
| 178 | } |
| 179 | return connection, func() { _ = pool.Purge(resource) }, nil |
| 180 | } |
| 181 | |
| 182 | func initDatabase(dbSetting TestDBSetting) (dbEngine *xorm.Engine, err error) { |
| 183 | dataConf := &data.Database{Driver: dbSetting.Driver, Connection: dbSetting.Connection} |
no test coverage detected