File: tc_augeas.rb

package info (click to toggle)
ruby-augeas 1%3A0.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 180 kB
  • ctags: 97
  • sloc: ruby: 309; ansic: 304; makefile: 7
file content (286 lines) | stat: -rw-r--r-- 9,225 bytes parent folder | download | duplicates (4)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
require 'test/unit'

TOPDIR = File::expand_path(File::join(File::dirname(__FILE__), ".."))

require 'augeas'
require 'fileutils'

class TestAugeas < Test::Unit::TestCase

    SRC_ROOT = File::expand_path(File::join(TOPDIR, "tests", "root")) + "/."
    TST_ROOT = File::expand_path(File::join(TOPDIR, "build", "root")) + "/"

    def test_basics
        aug = aug_open(Augeas::SAVE_NEWFILE)
        assert_equal("newfile", aug.get("/augeas/save"))
        assert_equal(TST_ROOT, aug.get("/augeas/root"))

        assert(aug.exists("/augeas/root"))
        assert_not_nil(aug.get("/augeas/root"))
        node = "/ruby/test/node"
        assert_nothing_raised {
            aug.set(node, "value")
        }
        assert_equal("value", aug.get(node))
        assert_nothing_raised {
            aug.clear(node)
        }
        assert_equal(nil, aug.get(node))
        m = aug.match("/*")
        ["/augeas", "/ruby"].each do |p|
            assert(m.include?(p))
            assert(aug.exists(p))
        end
    end

    def test_no_new
        assert_raise NoMethodError do
            Augeas.new
        end
    end

    def test_close
        aug = Augeas::open("/tmp", nil, Augeas::SAVE_NEWFILE)
        assert_equal("newfile", aug.get("/augeas/save"))
        aug.close

        assert_raise(SystemCallError) {
            aug.get("/augeas/save")
        }

        assert_raise(SystemCallError) {
            aug.close
        }
    end

    def test_mv
        Augeas::open("/dev/null") do |aug|
            aug.set("/a/b", "value")
            aug.mv("/a/b", "/x/y")
            assert_equal("value", aug.get("/x/y"))
        end
    end

    def test_load
        aug = aug_open(Augeas::NO_LOAD)
        assert_equal([], aug.match("/files/etc/*"))
        aug.rm("/augeas/load/*");
        assert_nothing_raised {
            aug.load
        }
        assert_equal([], aug.match("/files/etc/*"))
    end

    def test_transform
        aug = aug_open(Augeas::NO_LOAD)
        aug.clear_transforms
        aug.transform(:lens => "Hosts.lns",
                      :incl => "/etc/hosts")
        assert_raise(ArgumentError) {
            aug.transform(:name => "Fstab",
                          :incl => [ "/etc/fstab" ],
                          :excl => [ "*~", "*.rpmnew" ])
        }
        aug.transform(:lens => "Inittab",
                      :incl => "/etc/inittab")
        aug.transform(:lens => "Fstab.lns",
                      :incl => "/etc/fstab*",
                      :excl => "*~")
        assert_equal(["/augeas/load/Fstab", "/augeas/load/Fstab/excl",
                      "/augeas/load/Fstab/incl", "/augeas/load/Fstab/lens",
                      "/augeas/load/Hosts", "/augeas/load/Hosts/incl",
                      "/augeas/load/Hosts/lens", "/augeas/load/Inittab",
                      "/augeas/load/Inittab/incl",
                      "/augeas/load/Inittab/lens"],
                     aug.match("/augeas/load//*").sort)
        aug.load
        assert_equal(["/files/etc/hosts", "/files/etc/inittab"],
                     aug.match("/files/etc/*").sort)
    end

    def test_defvar
        Augeas::open("/dev/null") do |aug|
            aug.set("/a/b", "bval")
            aug.set("/a/c", "cval")
            assert aug.defvar("var", "/a/b")
            assert_equal(["/a/b"], aug.match("$var"))
            assert aug.defvar("var", nil)
            assert_raises(SystemCallError) {
                aug.match("$var")
            }
            assert ! aug.defvar("var", "/foo/")
        end
    end

    def test_defnode
        aug = aug_open
        assert aug.defnode("x", "/files/etc/hosts/*[ipaddr = '127.0.0.1']", nil)
        assert_equal(["/files/etc/hosts/1"], aug.match("$x"))
    end

    def test_save!
        aug = aug_open
        aug.set("/files/etc/hosts/1/garbage", "trash")
        assert_raises(Augeas::Error) { aug.save! }
    end

    def test_set!
        aug = aug_open
        assert_raises(Augeas::Error) { aug.set!("files/etc/hosts/*", nil) }
    end

    def test_set
       aug = aug_open
       aug.set("/files/etc/group/disk/user[last()+1]",["user1","user2"])
       assert_equal( aug.get("/files/etc/group/disk/user[1]"),"root" )
       assert_equal( aug.get("/files/etc/group/disk/user[2]"),"user1" )
       assert_equal( aug.get("/files/etc/group/disk/user[3]"),"user2" )

       aug.set("/files/etc/group/new_group/user[last()+1]",
	       "nuser1",["nuser2","nuser3"])
       assert_equal( aug.get("/files/etc/group/new_group/user[1]"),"nuser1")
       assert_equal( aug.get("/files/etc/group/new_group/user[2]"),"nuser2" )
       assert_equal( aug.get("/files/etc/group/new_group/user[3]"),"nuser3" )

       aug.rm("/files/etc/group/disk/user")
       aug.set("/files/etc/group/disk/user[last()+1]","testuser")
       assert_equal( aug.get("/files/etc/group/disk/user"),"testuser")

       aug.rm("/files/etc/group/disk/user")
       aug.set("/files/etc/group/disk/user[last()+1]", nil)
       assert_equal( aug.get("/files/etc/group/disk/user"), nil)
    end

    def test_setm
        aug = aug_open

        aug.setm("/files/etc/group/*[label() =~ regexp(\"rpc.*\")]","users", "testuser1")
        assert_equal( aug.get("/files/etc/group/rpc/users"), "testuser1")
        assert_equal( aug.get("/files/etc/group/rpcuser/users"), "testuser1")

        aug.setm("/files/etc/group/*[label() =~ regexp(\"rpc.*\")]/users",nil, "testuser2")
        assert_equal( aug.get("/files/etc/group/rpc/users"), "testuser2")
        assert_equal( aug.get("/files/etc/group/rpcuser/users"), "testuser2")
    end

    def test_error
        aug = aug_open

        # Cause an error
        aug.get("/files/etc/hosts/*")
        err = aug.error
        assert_equal(Augeas::EMMATCH, err[:code])
        assert err[:message]
        assert err[:details]
        assert err[:minor].nil?
    end

    def test_span
        aug = aug_open

        span = aug.span("/files/etc/ssh/sshd_config/Protocol")
        assert_equal({}, span)

        aug.set("/augeas/span", "enable")
        aug.rm("/files/etc")
        aug.load

        span = aug.span("/files/etc/ssh/sshd_config/Protocol")
        assert_not_nil(span[:filename])
        assert_equal(29..37, span[:label])
        assert_equal(38..39, span[:value])
        assert_equal(29..40, span[:span])
    end

    def test_srun
        aug = aug_open

        path = "/files/etc/hosts/*[canonical='localhost.localdomain']/ipaddr"
        r, out = aug.srun("get #{path}\n")
        assert_equal(1, r)
        assert_equal("#{path} = 127.0.0.1\n", out)

        assert_equal(0, aug.srun(" ")[0])
        assert_equal(-1, aug.srun("foo")[0])
        assert_equal(-1, aug.srun("set")[0])
        assert_equal(-2, aug.srun("quit")[0])
    end

    def test_label
        Augeas::open("/dev/null") do |aug|
            assert_equal 'augeas', aug.label('/augeas')
            assert_equal 'files', aug.label('/files')
        end
    end

    def test_rename
        Augeas::open("/dev/null") do |aug|
            assert_equal false, aug.rename('/files', 'invalid/label')
            assert_equal 0, aug.rename('/nonexistent', 'label')
            assert_equal ['/files'], aug.match('/files')
            assert_equal 1, aug.rename('/files', 'label')
        end
    end

    def test_text_store_retrieve
        Augeas::open("/dev/null") do |aug|
            # text_store errors
            assert_equal false, aug.text_store('Simplelines.lns', '/input', '/store')

            # text_store
            aug.set('/input', "line1\nline2\n")
            assert aug.text_store('Simplelines.lns', '/input', '/store')
            assert_equal 'line2', aug.get('/store/2')

            # text_retrieve errors
            assert_equal false, aug.text_retrieve('Simplelines.lns', '/unknown', '/store', '/output')

            # text_retrieve
            aug.set('/store/3', 'line3')
            assert aug.text_retrieve('Simplelines.lns', '/input', '/store', '/output')
            assert_equal "line1\nline2\nline3\n", aug.get('/output')
        end
    end

    def test_context
        Augeas::open("/dev/null") do |aug|
            aug.context = '/augeas'
            assert_equal '/augeas', aug.get('/augeas/context')
            assert_equal '/augeas', aug.get('context')
            assert_equal '/augeas', aug.context
        end
    end

    def test_touch
        Augeas::open("/dev/null") do |aug|
            assert_equal [], aug.match('/foo')
            aug.touch '/foo'
            assert_equal ['/foo'], aug.match('/foo')

            aug.set '/foo', 'bar'
            aug.touch '/foo'
            assert_equal 'bar', aug.get('/foo')
        end
    end

    def test_clearm
        Augeas::open("/dev/null") do |aug|
            aug.set('/foo/a', '1')
            aug.set('/foo/b', '2')
            aug.clearm('/foo', '*')
            assert_nil aug.get('/foo/a')
            assert_nil aug.get('/foo/b')
        end
    end

    private
    def aug_open(flags = Augeas::NONE)
        if File::directory?(TST_ROOT)
            FileUtils::rm_rf(TST_ROOT)
        end
        FileUtils::mkdir_p(TST_ROOT)
        FileUtils::cp_r(SRC_ROOT, TST_ROOT)

        Augeas::open(TST_ROOT, nil, flags)
    end
end