github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/db/dump/dump_test.go (about) 1 package dump 2 3 import ( 4 "context" 5 "net/http" 6 "testing" 7 8 "github.com/Redstoneguy129/cli/internal/testing/apitest" 9 "github.com/Redstoneguy129/cli/internal/utils" 10 "github.com/spf13/afero" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 "gopkg.in/h2non/gock.v1" 14 ) 15 16 const ( 17 username = "admin" 18 password = "password" 19 database = "postgres" 20 host = "localhost" 21 ) 22 23 func TestPullCommand(t *testing.T) { 24 imageUrl := utils.GetRegistryImageUrl(utils.Pg15Image) 25 const containerId = "test-container" 26 27 t.Run("pulls from remote", func(t *testing.T) { 28 // Setup in-memory fs 29 fsys := afero.NewMemMapFs() 30 // Setup mock docker 31 require.NoError(t, apitest.MockDocker(utils.Docker)) 32 defer gock.OffAll() 33 apitest.MockDockerStart(utils.Docker, imageUrl, containerId) 34 require.NoError(t, apitest.MockDockerLogs(utils.Docker, containerId, "hello world")) 35 // Run test 36 err := Run(context.Background(), "schema.sql", username, password, database, host, false, fsys) 37 // Check error 38 assert.NoError(t, err) 39 assert.Empty(t, apitest.ListUnmatchedRequests()) 40 // Validate migration 41 contents, err := afero.ReadFile(fsys, "schema.sql") 42 assert.NoError(t, err) 43 assert.Equal(t, []byte("hello world"), contents) 44 }) 45 46 t.Run("writes to stdout", func(t *testing.T) { 47 // Setup in-memory fs 48 fsys := afero.NewMemMapFs() 49 // Setup mock docker 50 require.NoError(t, apitest.MockDocker(utils.Docker)) 51 defer gock.OffAll() 52 apitest.MockDockerStart(utils.Docker, imageUrl, containerId) 53 require.NoError(t, apitest.MockDockerLogs(utils.Docker, containerId, "hello world")) 54 // Run test 55 err := Run(context.Background(), "", username, password, database, host, false, fsys) 56 // Check error 57 assert.NoError(t, err) 58 assert.Empty(t, apitest.ListUnmatchedRequests()) 59 }) 60 61 t.Run("throws error on missing docker", func(t *testing.T) { 62 // Setup in-memory fs 63 fsys := afero.NewMemMapFs() 64 // Setup mock docker 65 require.NoError(t, apitest.MockDocker(utils.Docker)) 66 defer gock.OffAll() 67 gock.New(utils.Docker.DaemonHost()). 68 Get("/v" + utils.Docker.ClientVersion() + "/images"). 69 Reply(http.StatusServiceUnavailable) 70 // Run test 71 err := Run(context.Background(), "", username, password, database, host, false, fsys) 72 // Check error 73 assert.ErrorContains(t, err, "Error running pg_dump on remote database: request returned Service Unavailable") 74 assert.Empty(t, apitest.ListUnmatchedRequests()) 75 }) 76 77 t.Run("throws error on permission denied", func(t *testing.T) { 78 // Setup in-memory fs 79 fsys := afero.NewReadOnlyFs(afero.NewMemMapFs()) 80 // Setup mock docker 81 require.NoError(t, apitest.MockDocker(utils.Docker)) 82 defer gock.OffAll() 83 apitest.MockDockerStart(utils.Docker, imageUrl, containerId) 84 require.NoError(t, apitest.MockDockerLogs(utils.Docker, containerId, "hello world")) 85 // Run test 86 err := Run(context.Background(), "schema.sql", username, password, database, host, false, fsys) 87 // Check error 88 assert.ErrorContains(t, err, "operation not permitted") 89 assert.Empty(t, apitest.ListUnmatchedRequests()) 90 }) 91 }