dockerspec icon indicating copy to clipboard operation
dockerspec copied to clipboard

execut tests to a running container with specific options

Open Azaroths opened this issue 7 years ago • 6 comments

Dockerspec Version

Dockerspec (0.4.1)

Ruby Version

Ruby 2.4.0

Platform Details

CentOS Linux release 7.3.1611

Scenario

Hello,

I would like execute scripts for test in containers with specific options.

For this, containers are executed with specific options like this:

docker run --name httpd-prod-opts --health-cmd "echo" --memory = 512M --cpu-period = 100000 --cpu-quota = 75000 Pids-limit = 100 --network = my_bridge -d my_image I would like to connect to a running container in order to run serverspec tests on it such as , is it possible ? In my script I can't use "describe docker_run('')" cause I need to have my specific options formy containers.

I tried several things without success, I don't know how to proceed to connect to a running container Is there a method for this?

Sorry for my english, it is not really good

Regards,

Azaroths

Azaroths avatar Apr 21 '17 08:04 Azaroths

Hello,

Finally I use this method to use tests on a container :

describe "test on container" do

        describe "container" do
                @container = Docker::Container.get('httpd_container')
                set :backend, :docker
                set :docker_container, @container.id

                describe port(8080) do
                        it { should be_listening }
                end

                describe port(8443) do
                        it { should be_listening }
                end

                describe command('curl -s localhost:8080') do
                        its(:stdout) { should match /Hello!/ }
                end

        end


end

However I encounter another problem, I want to know if my container is healthy with the following command :

describe command('docker ps -f name=httpd_container') do
            its(:stdout) { should match /healthy/ }
end

The problem is that I already use a backend for the container then the test doesn't work.

Is it possible to use 2 backend in a rspec script ?

Regards,

Azaroths

Azaroths avatar Apr 24 '17 13:04 Azaroths

Hi @Azaroths,

I'm not sure if I'm understanding your problem correctly. I don't quite understand what you mean by "use 2 backend". The command instruction will be run inside the container, so running your docker ps should not work as far as I know.

Anyway, for your case I recommend you to use docker_run with the :id option. Something like the following:

        describe docker_run(id: 'httpd_container') do

                describe port(8080) do
                        it { should be_listening }
                end

                describe port(8443) do
                        it { should be_listening }
                end

                describe command('curl -s localhost:8080') do
                        its(:stdout) { should match /Hello!/ }
                end
        end

I have not tested it, but should work in theory.

zuazo avatar Apr 24 '17 16:04 zuazo

Hi @zuazo

I need to start a container with specific options (--memory = 512M --cpu-period = 100000 --cpu-quota = 75000 Pids-limit = 100 --network = my_bridge) but I think docker_run just create and start a container without the specific options so I need to start my container in command line before (with specific options) then start tests on container but I don't know if it's possible...

I try to start tests in a running container or maybe exists a serverspec command for execute a container with my specific options but I didn't find it :(

Thanks for your help

Azaroths avatar Apr 25 '17 09:04 Azaroths

Hi @zuazo

I tried this

PRODUCTION_OPTIONS = "--cpu-period=100000 --cpu-quota=75000 --security-opt=no-new-privileges --pids-limit=100 --network=wl_bridge"
WL_HTTPD_IMAGE = "my_image"
WL_HTTPD_TEST="httpd_test"

@container1 = %x[docker run --name httpd-prod --health-cmd "echo" #{PRODUCTION_OPTIONS} -d #{WL_HTTPD_IMAGE}]

@container2 = %x[docker run --name httpd-middle --health-cmd "echo" #{PRODUCTION_OPTIONS} -d #{WL_HTTPD_TEST}]

describe "test on containers" do

        describe "container 1" do

                describe docker_run(id: @container1, tag: "#{WL_HTTPD_IMAGE}") do
                        describe file('/etc') do
                                it { should exist }
                        end

                        describe port(8080) do
                                it { should be_listening }
                        end

                        describe port(8443) do
                                it { should be_listening }
                        end

                        describe file('/var/www/html/index.html') do
                                it { should exist }
                                its(:content) { should match /Hello!/ }
                        end
                end
        end

        describe "container 2" do

                describe docker_run(id: @container2, tag: "#{WL_HTTPD_TEST}") do
                        describe file('/etc') do
                                it { should exist }
                        end

                        describe port(8080) do
                                it { should be_listening }
                        end

                        describe port(8443) do
                                it { should be_listening }
                        end

                        describe file('/var/www/html/index.html') do
                                it { should exist }
                                its(:content) { should match /Hello!/ }
                        end

                end
        end

end

But when I do docker inspect on my container, the production options are not in the logs. Is it normal ?

Regards,

Azaroths

Azaroths avatar Apr 26 '17 12:04 Azaroths

Finally, I will try to modify your gem code to have more options when I start a container.

Regards,

Azaroths

Azaroths avatar Apr 27 '17 09:04 Azaroths

OK, sorry @Azaroths. I did not respond because to do what you intend, as you have realized, would require implementing changes in the library and I have not had time to look at it carefully.

Either way, let me know if you need help or you find any problem. A PR would be greatly appreciated :wink:

zuazo avatar Apr 27 '17 20:04 zuazo