diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 00000000..6fab8c1c --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,21 @@ +FROM mcr.microsoft.com/devcontainers/ruby:2.7 + +# Install the SQL Server command-line tools and the Artistic Style code formatter +RUN curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc \ + && curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list \ + && apt-get update \ + && ACCEPT_EULA=Y apt-get install -y astyle mssql-tools18 unixodbc-dev +ENV PATH=$PATH:/opt/mssql-tools18/bin + +# Install FreeTDS +ENV FREETDS_VERSION=1.5.1 +COPY test/bin/install-freetds.sh /tmp/ +RUN /tmp/install-freetds.sh + +# Add the tiny_tds main Gemfile and install the gems. +RUN mkdir -p /tmp/tiny_tds +COPY Gemfile VERSION tiny_tds.gemspec /tmp/tiny_tds/ +RUN cd /tmp/tiny_tds \ + && bundle install \ + && rm -rf /tmp/tiny_tds +RUN chown -R vscode:vscode /usr/local/rvm && chown -R vscode:vscode /usr/local/bundle diff --git a/.devcontainer/boot.sh b/.devcontainer/boot.sh new file mode 100755 index 00000000..f65e05cd --- /dev/null +++ b/.devcontainer/boot.sh @@ -0,0 +1,6 @@ +# Setup test databases and users. +sqlcmd -C -S sqlserver -U sa -P 'c0MplicatedP@ssword' -i ./test/sql/db-create.sql +sqlcmd -C -S sqlserver -U sa -P 'c0MplicatedP@ssword' -i ./test/sql/db-login.sql + +# Mark directory as safe in Git so that commands run without warnings. +git config --global --add safe.directory /workspaces/tiny_tds diff --git a/.devcontainer/compose.yaml b/.devcontainer/compose.yaml new file mode 100644 index 00000000..d43dc234 --- /dev/null +++ b/.devcontainer/compose.yaml @@ -0,0 +1,40 @@ +version: '3' + +services: + tiny_tds: + build: + context: .. + dockerfile: .devcontainer/Dockerfile + + volumes: + - ../..:/workspaces:cached + + # Overrides default command so things don't shut down after the process ends. + command: sleep infinity + + networks: + - default + depends_on: + - sqlserver + + toxiproxy: + image: ghcr.io/shopify/toxiproxy:2.5.0 + + sqlserver: + image: mcr.microsoft.com/mssql/server:2022-latest + restart: unless-stopped + networks: + - default + volumes: + - sqlserver-data:/var/opt/mssql + ports: + - "1433:1433" + environment: + MSSQL_SA_PASSWORD: 'c0MplicatedP@ssword' + ACCEPT_EULA: Y + +networks: + default: + +volumes: + sqlserver-data: diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..5b48cb2b --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,30 @@ +// For format details, see https://aka.ms/devcontainer.json. +{ + "name": "tiny_tds project development", + "dockerComposeFile": "compose.yaml", + "service": "tiny_tds", + "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", + + // Features to add to the dev container. More info: https://containers.dev/features. + "features": { + "ghcr.io/devcontainers/features/docker-in-docker:2": { + // Moby is not installable in Debian 11 + "moby": false + } + }, + + "containerEnv": { + "TINYTDS_UNIT_HOST": "sqlserver", + "TOXIPROXY_HOST": "toxiproxy" + }, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // This can be used to network with other containers or the host. + // "forwardPorts": [3000, 5432], + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": ".devcontainer/boot.sh", + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} \ No newline at end of file diff --git a/README.md b/README.md index a5e83b2f..c3847f52 100644 --- a/README.md +++ b/README.md @@ -401,42 +401,26 @@ First, clone the repo using the command line or your Git GUI of choice. $ git clone git@github.com:rails-sqlserver/tiny_tds.git ``` -After that, the quickest way to get setup for development is to use [Docker](https://www.docker.com/). Assuming you have [downloaded docker](https://www.docker.com/products/docker) for your platform, you can use [docker-compose](https://docs.docker.com/compose/install/) to run the necessary containers for testing. +After that, the quickest way to get setup for development is to use the provided devcontainers setup. ```shell -$ docker-compose up -d +npm install -g @devcontainers/cli +devcontainer up --workspace-folder . +devcontainer exec --workspace-folder . bash ``` -This will download the official SQL Server for Linux Docker image from [Microsoft](https://hub.docker.com/r/microsoft/mssql-server-linux/). This will also download a [toxiproxy](https://github.com/shopify/toxiproxy) Docker image which we can use to simulate network failures for tests. Basically, it does the following: +From within the container, you can run the tests using the following command: ```shell -$ docker network create main-network -$ docker pull mcr.microsoft.com/mssql/server:2017-latest -$ docker run -p 1433:1433 -d --name sqlserver --network main-network mcr.microsoft.com/mssql/server:2017-latest -$ docker pull shopify/toxiproxy -$ docker run -p 8474:8474 -p 1234:1234 -d --name toxiproxy --network main-network shopify/toxiproxy +bundle install +bundle exec rake test ``` -Make sure to run these SQL scripts as SA to get the test database and user installed. If needed, install [sqlcmd as described by Microsoft for your platform](https://learn.microsoft.com/en-us/sql/tools/sqlcmd/sqlcmd-utility?view=sql-server-ver16&tabs=go%2Clinux&pivots=cs1-bash). +You can customize the environment variables to run the tests against a different environment -```shell -/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P super01S3cUr3 -i ./test/sql/db-create.sql -/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P super01S3cUr3 -i ./test/sql/db-login.sql -``` - -From here you can build and run tests against an installed version of FreeTDS. - -```shell -$ bundle install -$ bundle exec rake -``` - -Examples us using enviornment variables to customize the test task. - -``` -$ rake TINYTDS_UNIT_DATASERVER=mydbserver -$ rake TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2017 -$ rake TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure +```shell +rake test TINYTDS_UNIT_DATASERVER=mydbserver TINYTDS_SCHEMA=sqlserver_2017 +rake test TINYTDS_UNIT_HOST=mydb.host.net TINYTDS_SCHEMA=sqlserver_azure ``` ### Code formatting @@ -450,7 +434,7 @@ We are using `standardrb` to format the Ruby code and Artistic Style for the C c For the convenience, TinyTDS ships pre-compiled gems for supported versions of Ruby on Windows and Linux. In order to generate these gems, [rake-compiler-dock](https://github.com/rake-compiler/rake-compiler-dock) is used. -Run the following rake task to compile the gems. This will check the availability of [Docker](https://www.docker.com/) and will give some advice for download and installation. When docker is running, it will download the docker image (once-only) and start the build: +Run the following rake task to compile the gems. You can run these commands from inside the devcontainers setup, or outside if neeed. The command will check the availability of [Docker](https://www.docker.com/) and will give some advice for download and installation. When docker is running, it will download the docker image (once-only) and start the build: ```shell bundle exec rake gem:native diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 292f36ea..00000000 --- a/docker-compose.yml +++ /dev/null @@ -1,34 +0,0 @@ -version: '3' - -networks: - main-network: - -services: - mssql: - image: mcr.microsoft.com/mssql/server:${MSSQL_VERSION:-2017}-latest - container_name: sqlserver - environment: - ACCEPT_EULA: Y - MSSQL_SA_PASSWORD: super01S3cUr3 - ports: - - "1433:1433" - network_mode: "host" - - toxiproxy: - image: shopify/toxiproxy - container_name: toxiproxy - command: '/toxiproxy -host=127.0.0.1' - network_mode: "host" - - cimgruby: - image: "cimg/ruby:${RUBY_VERSION:-2.7}" - container_name: cimg_ruby - environment: - TESTOPTS: '-v' - TINYTDS_UNIT_HOST: '127.0.0.1' - SA_PASSWORD: super01S3cUr3 - TOXIPROXY_HOST: '127.0.0.1' - command: tail -F anything - volumes: - - .:/home/circleci/project - network_mode: "host" diff --git a/setup_cimgruby_dev.sh b/setup_cimgruby_dev.sh deleted file mode 100755 index 5964bacf..00000000 --- a/setup_cimgruby_dev.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env bash - -set -x -set -e - -# this should mirror the steps outlined in the circleci yml -echo "Installing mssql-tools..." -sleep 5 -sudo -E ./test/bin/install-mssqltools.sh - -echo "Configurating tinytds test database..." -sleep 5 -./test/bin/setup_tinytds_db.sh - -echo "Building openssl library..." -sleep 5 -sudo -E ./test/bin/install-openssl.sh - -echo "Building freetds library..." -sleep 5 -sudo -E ./test/bin/install-freetds.sh - -echo "Installing gems..." -sleep 5 -bundle install diff --git a/start_dev.sh b/start_dev.sh deleted file mode 100755 index 724fc72c..00000000 --- a/start_dev.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash - -set -x -set -e - -# set volume read/write permissions to work both outside and inside container -sudo ./test/bin/setup_volume_permissions.sh - -docker-compose up -d -echo "Waiting for containers to start..." -sleep 10 - -# setup circleci ruby container for development -docker exec cimg_ruby bash -c './setup_cimgruby_dev.sh' - -# enter container -set +x -echo "cimg/ruby container is ready for tiny_tds development.........." -echo "To enter container run: docker exec -it cimg_ruby /bin/bash" -echo "To build solution run: docker exec cimg_ruby bash -c 'bundle exec rake build'" -echo "To test solution run: docker exec cimg_ruby bash -c 'bundle exec rake test'" diff --git a/test/bin/install-mssql.ps1 b/test/bin/install-mssql.ps1 deleted file mode 100644 index 2629c3c3..00000000 --- a/test/bin/install-mssql.ps1 +++ /dev/null @@ -1,42 +0,0 @@ -param ([int] $Version) - -$ProgressPreference = 'SilentlyContinue' - -$DownloadLinkTable = @{ - 2017 = "https://go.microsoft.com/fwlink/?linkid=829176"; - 2019 = "https://download.microsoft.com/download/7/c/1/7c14e92e-bdcb-4f89-b7cf-93543e7112d1/SQLEXPR_x64_ENU.exe"; - 2022 = "https://download.microsoft.com/download/3/8/d/38de7036-2433-4207-8eae-06e247e17b25/SQLEXPR_x64_ENU.exe"; -} - -$MajorVersionTable = @{ - 2017 = 14; - 2019 = 15; - 2022 = 16; -} - -if (-not(Test-path "C:\Downloads")) { - mkdir "C:\Downloads" -} - -$sqlInstallationFile = "C:\Downloads\sqlexpress.exe" -if (-not(Test-path $sqlInstallationFile -PathType leaf)) { - Write-Host "Downloading SQL Express ..." - Invoke-WebRequest -Uri $DownloadLinkTable[$Version] -OutFile "C:\Downloads\sqlexpress.exe" -} - -Write-Host "Installing SQL Express ..." -Start-Process -Wait -FilePath "C:\Downloads\sqlexpress.exe" -ArgumentList /qs, /x:"C:\Downloads\setup" -C:\Downloads\setup\setup.exe /q /ACTION=Install /INSTANCENAME=SQLEXPRESS /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT='NT AUTHORITY\System' /SQLSYSADMINACCOUNTS='BUILTIN\ADMINISTRATORS' /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS - -Write-Host "Configuring SQL Express ..." -stop-service MSSQL`$SQLEXPRESS -set-itemproperty -path "HKLM:\software\microsoft\microsoft sql server\mssql$($MajorVersionTable[$Version]).SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall" -name tcpdynamicports -value '' -set-itemproperty -path "HKLM:\software\microsoft\microsoft sql server\mssql$($MajorVersionTable[$Version]).SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall" -name tcpport -value 1433 -set-itemproperty -path "HKLM:\software\microsoft\microsoft sql server\mssql$($MajorVersionTable[$Version]).SQLEXPRESS\mssqlserver\" -name LoginMode -value 2 - -Write-Host "Starting SQL Express ..." -start-service MSSQL`$SQLEXPRESS - -Write-Host "Configuring MSSQL for TinyTDS ..." -& sqlcmd -i './test/sql/db-create.sql' -& sqlcmd -i './test/sql/db-login.sql' diff --git a/test/bin/install-mssqltools.sh b/test/bin/install-mssqltools.sh deleted file mode 100755 index eafa3307..00000000 --- a/test/bin/install-mssqltools.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -set -x -set -e - -curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - -curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list -sudo apt-get update -sudo ACCEPT_EULA=Y apt-get -y install mssql-tools unixodbc-dev diff --git a/test/bin/install-openssl.sh b/test/bin/install-openssl.sh deleted file mode 100755 index 7effb31f..00000000 --- a/test/bin/install-openssl.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash - -set -x -set -e - -if [ -z "$OPENSSL_VERSION" ]; then - OPENSSL_VERSION=$(ruby -r "./ext/tiny_tds/extconsts.rb" -e "puts OPENSSL_VERSION") -fi - -wget https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz -tar -xzf openssl-$OPENSSL_VERSION.tar.gz -cd openssl-$OPENSSL_VERSION -./config --prefix=/opt/local --openssldir=/opt/local -make -make install_sw install_ssldirs -cd .. -rm -rf openssl-$OPENSSL_VERSION -rm openssl-$OPENSSL_VERSION.tar.gz diff --git a/test/bin/setup_tinytds_db.sh b/test/bin/setup_tinytds_db.sh deleted file mode 100755 index 42f5e544..00000000 --- a/test/bin/setup_tinytds_db.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -set -x -set -e - -/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -i ./test/sql/db-create.sql -/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -i ./test/sql/db-login.sql diff --git a/test/bin/setup_volume_permissions.sh b/test/bin/setup_volume_permissions.sh deleted file mode 100755 index bb1e246d..00000000 --- a/test/bin/setup_volume_permissions.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash - -set -x - -sudo groupadd -g 3434 circleci_tinytds -sudo usermod -a -G circleci_tinytds $USER -sudo useradd circleci_tinytds -u 3434 -g 3434 -sudo usermod -a -G circleci_tinytds circleci_tinytds -sudo chgrp -R circleci_tinytds . -sudo chmod -R g+rwx . diff --git a/tiny_tds.gemspec b/tiny_tds.gemspec index 6f05d80e..9ec0a05d 100644 --- a/tiny_tds.gemspec +++ b/tiny_tds.gemspec @@ -1,9 +1,9 @@ $LOAD_PATH.push File.expand_path("../lib", __FILE__) -require "tiny_tds/version" +version = File.read(File.expand_path("VERSION", __dir__)).strip Gem::Specification.new do |s| s.name = "tiny_tds" - s.version = TinyTds::VERSION + s.version = version s.platform = Gem::Platform::RUBY s.authors = ["Ken Collins", "Erik Bryn", "Will Bond"] s.email = ["ken@metaskills.net", "will@wbond.net"]