File: network_spec.rb

package info (click to toggle)
ruby-docker-api 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: ruby: 4,044; sh: 138; makefile: 5
file content (160 lines) | stat: -rw-r--r-- 4,091 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require 'spec_helper'

unless ::Docker.podman?
  SingleCov.covered! uncovered: 2

  describe Docker::Network, docker_1_9: true do
    let(:name) do |example|
      example.description.downcase.gsub(/\s/, '-')
    end

    describe '#to_s' do
      subject { described_class.new(Docker.connection, info) }
      let(:connection) { Docker.connection }

      let(:id) do
        'a6c5ffd25e07a6c906accf804174b5eb6a9d2f9e07bccb8f5aa4f4de5be6d01d'
      end

      let(:info) do
        {
          'Name' => 'bridge',
          'Scope' => 'local',
          'Driver' => 'bridge',
          'IPAM' => {
            'Driver' => 'default',
            'Config' => [{ 'Subnet' => '172.17.0.0/16' }]
          },
          'Containers' => {},
          'Options' => {
            'com.docker.network.bridge.default_bridge' => 'true',
            'com.docker.network.bridge.enable_icc' => 'true',
            'com.docker.network.bridge.enable_ip_masquerade' => 'true',
            'com.docker.network.bridge.host_binding_ipv4' => '0.0.0.0',
            'com.docker.network.bridge.name' => 'docker0',
            'com.docker.network.driver.mtu' => '1500'
          },
          'id' => id
        }
      end

      let(:expected_string) do
        "Docker::Network { :id => #{id}, :info => #{info.inspect}, "\
          ":connection => #{connection} }"
      end

      its(:to_s) { should == expected_string }
    end

    describe '.create' do
      let!(:id) { subject.id }
      subject { described_class.create(name) }
      after { described_class.remove(id) }

      it 'creates a Network' do
        expect(Docker::Network.all.map(&:id)).to include(id)
      end
    end

    describe '.remove' do
      let(:id) { subject.id }
      subject { described_class.create(name) }

      it 'removes the Network' do
        described_class.remove(id)
        expect(Docker::Network.all.map(&:id)).to_not include(id)
      end
    end

    describe '.get' do
      after do
        described_class.remove(name)
      end

      let!(:network) { described_class.create(name) }

      it 'returns a network' do
        expect(Docker::Network.get(name).id).to eq(network.id)
      end
    end

    describe '.all' do
      let!(:networks) do
        5.times.map { |i| described_class.create("#{name}-#{i}") }
      end

      after do
        networks.each(&:remove)
      end

      it 'should return all networks' do
        expect(Docker::Network.all.map(&:id)).to include(*networks.map(&:id))
      end
    end

    describe '.prune', :docker_17_03 => true do
      it 'prune networks' do
        expect { Docker::Network.prune }.not_to raise_error
      end
    end

    describe '#connect' do
      let!(:container) do
        Docker::Container.create(
          'Cmd' => %w(sleep 10),
          'Image' => 'debian:stable'
        )
      end
      subject { described_class.create(name) }

      before(:each) { container.start }
      after(:each) do
        container.kill!.remove
        subject.remove
      end

      it 'connects a container to a network' do
        subject.connect(container.id)
        expect(subject.info['Containers']).to include(container.id)
      end
    end

    describe '#disconnect' do
      let!(:container) do
        Docker::Container.create(
          'Cmd' => %w(sleep 10),
          'Image' => 'debian:stable'
        )
      end

      subject { described_class.create(name) }

      before(:each) do
        container.start
        sleep 1
        subject.connect(container.id)
      end

      after(:each) do
        container.kill!.remove
        subject.remove
      end

      it 'connects a container to a network' do
        subject.disconnect(container.id)
        expect(subject.info['Containers']).not_to include(container.id)
      end
    end

    describe '#remove' do
      let(:id) { subject.id }
      let(:name) { 'test-network-remove' }
      subject { described_class.create(name) }

      it 'removes the Network' do
        subject.remove
        expect(Docker::Network.all.map(&:id)).to_not include(id)
      end
    end
  end
end