summaryrefslogtreecommitdiff
path: root/spec/acceptance/git_clone_protocols_spec.rb
blob: 4d9129e6e5061b2c4583695f96215805d67df383 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
require 'spec_helper_acceptance'

hosts.each do |host|

  describe 'clones a repo with git' do
    tmpdir =  host.tmpdir('vcsrepo')

    before(:all) do
      # {{{ setup
      apply_manifest_on(host, "user{'testuser': ensure => present, managehome => true }")
      apply_manifest_on(host, "user{'vagrant': ensure => present, }")
      # install git
      install_package(host, 'git')
      install_package(host, 'git-daemon')
      # create ssh keys
      on(host, 'mkdir -p /home/testuser/.ssh')
      on(host, 'ssh-keygen -q -t rsa -f /root/.ssh/id_rsa -N ""')

      # copy public key to authorized_keys
      on(host, 'cat /root/.ssh/id_rsa.pub >> /home/testuser/.ssh/authorized_keys')
      on(host, 'echo -e "Host *\n\tStrictHostKeyChecking no\n" >> /home/testuser/.ssh/config')
      on(host, 'echo -e "Host *\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config')
      on(host, 'chown -R testuser:testuser /home/testuser/.ssh')
      on(host, 'chown -R root:root /root/.ssh')

      # create git repo
      my_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
      scp_to(host, "#{my_root}/acceptance/files/create_git_repo.sh", tmpdir)
      on(host, "cd #{tmpdir} && ./create_git_repo.sh")

      # copy ssl keys
      scp_to(host, "#{my_root}/acceptance/files/server.crt", tmpdir)
      scp_to(host, "#{my_root}/acceptance/files/server.key", tmpdir)
      # }}}
    end

    after(:all) do
      # {{{ teardown
      apply_manifest_on(host, "user{'testuser': ensure => absent, managehome => true }")
      apply_manifest_on(host, "file{'/root/.ssh/id_rsa': ensure => absent, force => true }")
      apply_manifest_on(host, "file{'/root/.ssh/id_rsa.pub': ensure => absent, force => true }")
      # }}}
    end


    #---------------  TESTS ----------------------#

    context 'using local protocol (file URL)' do
      before(:all) do
        apply_manifest_on(host, "file {'#{tmpdir}/testrepo': ensure => directory, purge => true, recurse => true, recurselimit => 1, force => true; }")
      end

      it 'should have HEAD pointing to master' do
        pp = <<-EOS
        vcsrepo { "#{tmpdir}/testrepo":
          ensure => present,
          provider => git,
          source => "file://#{tmpdir}/testrepo.git",
        }
        EOS

        # Run it twice and test for idempotency
        apply_manifest_on(host, pp, :catch_failures => true)
        apply_manifest_on(host, pp, :catch_changes => true)
      end

      describe file("#{tmpdir}/testrepo/.git/HEAD") do
        it { should contain 'ref: refs/heads/master' }
      end

    end

    context 'using local protocol (file path)' do
      before(:all) do
        apply_manifest_on(host, "file {'#{tmpdir}/testrepo': ensure => directory, purge => true, recurse => true, recurselimit => 1, force => true; }")
      end

      it 'should have HEAD pointing to master' do
        pp = <<-EOS
        vcsrepo { "#{tmpdir}/testrepo":
          ensure => present,
          provider => git,
          source => "#{tmpdir}/testrepo.git",
        }
        EOS

        # Run it twice and test for idempotency
        apply_manifest_on(host, pp, :catch_failures => true)
        apply_manifest_on(host, pp, :catch_changes => true)
      end

      describe file("#{tmpdir}/testrepo/.git/HEAD") do
        it { should contain 'ref: refs/heads/master' }
      end

    end

    context 'using git protocol' do
      before(:all) do
        apply_manifest_on(host, "file {'#{tmpdir}/testrepo': ensure => directory, purge => true, recurse => true, recurselimit => 1, force => true; }")
        on(host, "nohup git daemon  --detach --base-path=/#{tmpdir}")
      end

      it 'should have HEAD pointing to master' do
        pp = <<-EOS
        vcsrepo { "#{tmpdir}/testrepo":
          ensure => present,
          provider => git,
          source => "git://#{host}/testrepo.git",
        }
        EOS

        # Run it twice and test for idempotency
        apply_manifest_on(host, pp, :catch_failures => true)
        apply_manifest_on(host, pp, :catch_changes => true)
      end
      describe file("#{tmpdir}/testrepo/.git/HEAD") do
        it { should contain 'ref: refs/heads/master' }
      end

      after(:all) do
        on(host, 'pkill -9 git')
      end
    end

    context 'using http protocol' do
      before(:all) do
        apply_manifest_on(host, "file {'#{tmpdir}/testrepo': ensure => directory, purge => true, recurse => true, recurselimit => 1, force => true; }")
        daemon =<<-EOF
        require 'webrick'
        server = WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => "#{tmpdir}")
        WEBrick::Daemon.start
        server.start
        EOF
        create_remote_file(host, '/tmp/daemon.rb', daemon)
        on(host, "ruby /tmp/daemon.rb")
      end

      it 'should have HEAD pointing to master' do
        pp = <<-EOS
        vcsrepo { "#{tmpdir}/testrepo":
          ensure => present,
          provider => git,
          source => "http://#{host}:8000/testrepo.git",
        }
        EOS

        # Run it twice and test for idempotency
        apply_manifest_on(host, pp, :catch_failures => true)
        apply_manifest_on(host, pp, :catch_changes => true)
      end
      describe file("#{tmpdir}/testrepo/.git/HEAD") do
        it { should contain 'ref: refs/heads/master' }
      end

      after(:all) do
        on(host, 'pkill -9 ruby')
      end
    end

    context 'using https protocol' do
      before(:all) do
        apply_manifest_on(host, "file {'#{tmpdir}/testrepo': ensure => directory, purge => true, recurse => true, recurselimit => 1, force => true; }")
        daemon =<<-EOF
        require 'webrick'
        require 'webrick/https'
        server = WEBrick::HTTPServer.new(
        :Port               => 8443,
        :DocumentRoot       => "#{tmpdir}",
        :SSLEnable          => true,
        :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
        :SSLCertificate     => OpenSSL::X509::Certificate.new(  File.open("#{tmpdir}/server.crt").read),
        :SSLPrivateKey      => OpenSSL::PKey::RSA.new(          File.open("#{tmpdir}/server.key").read),
        :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ])
        WEBrick::Daemon.start
        server.start
        EOF
        create_remote_file(host, '/tmp/daemon.rb', daemon)
        on(host, "ruby /tmp/daemon.rb")
      end

      it 'should have HEAD pointing to master' do
        # howto whitelist ssl cert
        pp = <<-EOS
        vcsrepo { "#{tmpdir}/testrepo":
          ensure => present,
          provider => git,
          source => "https://#{host}:8443/testrepo.git",
        }
        EOS

        # Run it twice and test for idempotency
        apply_manifest_on(host, pp, :catch_failures => true)
        apply_manifest_on(host, pp, :catch_changes => true)
      end

      describe file("#{tmpdir}/testrepo/.git/HEAD") do
        it { should contain 'ref: refs/heads/master' }
      end

      after(:all) do
        on(host, 'pkill -9 ruby')
      end
    end

    context 'using ssh protocol' do
      before(:all) do
        apply_manifest_on(host, "file {'#{tmpdir}/testrepo': ensure => directory, purge => true, recurse => true, recurselimit => 1, force => true; }")
      end
      it 'should have HEAD pointing to master' do
        pp = <<-EOS
        vcsrepo { "#{tmpdir}/testrepo":
          ensure => present,
          provider => git,
          source => "ssh://root@#{host}#{tmpdir}/testrepo.git",
        }
        EOS

        # Run it twice and test for idempotency
        apply_manifest_on(host, pp, :catch_failures => true)
        apply_manifest_on(host, pp, :catch_changes => true)
      end

      describe file("#{tmpdir}/testrepo/.git/HEAD") do
        it { should contain 'ref: refs/heads/master' }
      end
    end

  end
end