summaryrefslogtreecommitdiffstats
path: root/test/ral/type/yumrepo.rb
blob: f3221fb84deea53c93adafc674c25c3d4759d8cf (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../lib/puppettest'

require 'puppettest'
require 'fileutils'

class TestYumRepo < Test::Unit::TestCase
    include PuppetTest

    def setup
        super
        @yumdir = tempfile()
        Dir.mkdir(@yumdir)
        @yumconf = File.join(@yumdir, "yum.conf")
        File.open(@yumconf, "w") do |f|
            f.print "[main]\nreposdir=#{@yumdir} /no/such/dir\n"
        end
        Puppet::Type.type(:yumrepo).yumconf = @yumconf

        # It needs to be reset each time, otherwise the cache is used.
        Puppet::Type.type(:yumrepo).inifile = nil
    end

    # Modify one existing section
    def test_modify
        copy_datafiles
        devel = make_repo("development", { :descr => "New description" })
        current_values = devel.retrieve

        assert_equal("development", devel[:name])
        assert_equal('Fedora Core $releasever - Development Tree', current_values[devel.property(:descr)])
        assert_equal('New description', devel.property(:descr).should)
        assert_apply(devel)

        inifile = Puppet::Type.type(:yumrepo).read()
        assert_equal('New description', inifile['development']['name'])
        assert_equal('Fedora Core $releasever - $basearch - Base', inifile['base']['name'])
        assert_equal("foo\n  bar\n  baz", inifile['base']['exclude'])
        assert_equal(['base', 'development', 'main'], all_sections(inifile))
    end

    # Create a new section
    def test_create
        values = {
            :descr => "Fedora Core $releasever - $basearch - Base",
            :baseurl => "http://example.com/yum/$releasever/$basearch/os/",
            :enabled => "1",
            :gpgcheck => "1",
            :includepkgs => "absent",
            :gpgkey => "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora",
            :proxy => "http://proxy.example.com:80/",
            :proxy_username => "username",
            :proxy_password => "password"
        }
        repo = make_repo("base", values)

        assert_apply(repo)
        inifile = Puppet::Type.type(:yumrepo).read()
        sections = all_sections(inifile)
        assert_equal(['base', 'main'], sections)
        text = inifile["base"].format
        assert_equal(CREATE_EXP, text)
    end

    # Delete mirrorlist by setting it to :absent and enable baseurl
    def test_absent
        copy_datafiles
        baseurl = 'http://example.com/'
        devel = make_repo("development", 
                          { :mirrorlist => 'absent',
                            :baseurl => baseurl })
        devel.retrieve
        assert_apply(devel)
        inifile = Puppet::Type.type(:yumrepo).read()
        sec = inifile["development"]
        assert_nil(sec["mirrorlist"])
        assert_equal(baseurl, sec["baseurl"])
    end

    def make_repo(name, hash={})
        hash[:name] = name
        Puppet::Type.type(:yumrepo).new(hash)
    end

    def all_sections(inifile)
        sections = []
        inifile.each_section { |section| sections << section.name }
        return sections.sort
    end

    def copy_datafiles
        fakedata("data/types/yumrepos").select { |file|
            file =~ /\.repo$/
        }.each { |src|
            dst = File::join(@yumdir, File::basename(src))
            FileUtils::copy(src, dst)
        }
    end
    
    CREATE_EXP = <<'EOF'
[base]
name=Fedora Core $releasever - $basearch - Base
baseurl=http://example.com/yum/$releasever/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora
proxy=http://proxy.example.com:80/
proxy_username=username
proxy_password=password
EOF

end