github.com/supabase/cli@v1.168.1/test/db_test.go (about) 1 package integration 2 3 // Basic imports 4 import ( 5 "io" 6 "net/http" 7 "os" 8 "path/filepath" 9 "sync" 10 "testing" 11 12 "github.com/gin-gonic/gin" 13 gonanoid "github.com/matoous/go-nanoid/v2" 14 "github.com/spf13/cobra" 15 "github.com/stretchr/testify/require" 16 "github.com/stretchr/testify/suite" 17 clicmd "github.com/supabase/cli/cmd" 18 "github.com/supabase/cli/test/mocks/docker" 19 ) 20 21 type DBTestSuite struct { 22 suite.Suite 23 cmd *cobra.Command 24 tempDir string 25 ids []string 26 bodies []string 27 params []gin.Params 28 mtx sync.RWMutex 29 } 30 31 // test functions 32 // add tests here <- 33 34 // hooks 35 func (suite *DBTestSuite) SetupTest() { 36 suite.tempDir = NewTempDir(Logger, TempDir) 37 suite.mtx.Lock() 38 suite.ids = []string{} 39 suite.bodies = []string{} 40 suite.params = []gin.Params{} 41 suite.mtx.Unlock() 42 43 // add docker mock handlers 44 DockerMock.ExecCreateHandler = func(c *gin.Context) { 45 suite.addParams(c.Copy()) 46 body, err := io.ReadAll(c.Request.Body) 47 if err != nil { 48 c.JSON(http.StatusBadRequest, gin.H{ 49 "message": "error reading body", 50 }) 51 return 52 } 53 suite.addBody(body) 54 55 id := gonanoid.MustGenerate(docker.IDAlphabet, docker.IDLength) 56 c.JSON(http.StatusCreated, gin.H{ 57 "Id": id, 58 }) 59 suite.addID(id) 60 } 61 62 DockerMock.ExecStartHandler = func(c *gin.Context) { 63 suite.addParams(c.Copy()) 64 body, err := io.ReadAll(c.Request.Body) 65 if err != nil { 66 c.JSON(http.StatusBadRequest, gin.H{ 67 "message": "error reading body", 68 }) 69 return 70 } 71 suite.addBody(body) 72 73 docker.HijackedResponse(c, "0") 74 } 75 76 DockerMock.ContainerInspectHandler = func(c *gin.Context) { 77 c.JSON(http.StatusOK, gin.H{}) 78 } 79 80 // create supabase dir 81 suite.cmd = clicmd.GetRootCmd() 82 init, _, err := suite.cmd.Find([]string{"init"}) 83 if err != nil { 84 suite.Fail("failed to find init command") 85 } 86 err = init.RunE(init, []string{}) 87 if err != nil { 88 suite.Fail("failed to init supabase cli") 89 } 90 91 err = os.Mkdir("supabase/.branches", os.FileMode(0755)) 92 if err != nil { 93 suite.Fail("failed to create supabase/.branches directory") 94 } 95 } 96 97 func (suite *DBTestSuite) TeardownTest() { 98 require.NoError(suite.T(), os.Chdir(TempDir)) 99 } 100 101 // In order for 'go test' to run this suite, we need to create 102 // a normal test function and pass our suite to suite.Run 103 func TestDBTestSuite(t *testing.T) { 104 suite.Run(t, new(DBTestSuite)) 105 } 106 107 // helper functions 108 func (suite *DBTestSuite) addParams(c *gin.Context) { 109 suite.mtx.Lock() 110 defer suite.mtx.Unlock() 111 suite.params = append(suite.params, c.Params) 112 } 113 114 func (suite *DBTestSuite) addBody(body []byte) { 115 suite.mtx.Lock() 116 defer suite.mtx.Unlock() 117 suite.bodies = append(suite.bodies, string(body)) 118 } 119 120 func (suite *DBTestSuite) addID(id string) { 121 suite.mtx.Lock() 122 defer suite.mtx.Unlock() 123 suite.ids = append(suite.ids, id) 124 } 125 126 func (suite *DBTestSuite) constructParams() []gin.Params { 127 ids := []gin.Params{} 128 // for each exec docker call we have to calls to docker api: 129 // one to create exec, one to start exec 130 for _, id := range suite.ids { 131 // this one represents call to create exec 132 ids = append(ids, gin.Params{ 133 gin.Param{ 134 Key: "id", 135 Value: "supabase_db_" + filepath.Base(suite.tempDir), 136 }, 137 }) 138 139 // this one represents call to start exec 140 ids = append(ids, gin.Params{ 141 gin.Param{ 142 Key: "id", 143 Value: id, 144 }, 145 }) 146 } 147 return ids 148 }