summaryrefslogtreecommitdiffstats
path: root/spec/unit/file_bucket/dipper_spec.rb
blob: 08ca79cfde10514896888fff1adb53b438fc0c6c (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/file_bucket/dipper'
describe Puppet::FileBucket::Dipper do
    before do
        ['/my/file'].each do |x|
            Puppet::FileBucket::Dipper.any_instance.stubs(:absolutize_path).with(x).returns(x)
        end
    end

    it "should fail in an informative way when there are failures backing up to the server" do
        File.stubs(:exist?).returns true
        File.stubs(:read).returns "content"

        @dipper = Puppet::FileBucket::Dipper.new(:Path => "/my/bucket")

        filemock = stub "bucketfile"
        Puppet::FileBucket::File.stubs(:new).returns(filemock)
        filemock.expects(:name).returns "name"
        filemock.expects(:save).raises ArgumentError

        lambda { @dipper.backup("/my/file") }.should raise_error(Puppet::Error)
    end

    it "should backup files to a local bucket" do
        @dipper = Puppet::FileBucket::Dipper.new(
            :Path => "/my/bucket"
        )

        File.stubs(:exist?).returns true
        File.stubs(:read).with("/my/file").returns "my contents"

        bucketfile = stub "bucketfile"
        bucketfile.stubs(:name).returns('md5/DIGEST123')
        bucketfile.stubs(:checksum_data).returns("DIGEST123")
        bucketfile.expects(:save).with('md5/DIGEST123')


                    Puppet::FileBucket::File.stubs(:new).with(
                
            "my contents",
            :bucket_path => '/my/bucket',
        
            :path => '/my/file'
        ).returns(bucketfile)

        @dipper.backup("/my/file").should == "DIGEST123"
    end

    it "should retrieve files from a local bucket" do
        @dipper = Puppet::FileBucket::Dipper.new(
            :Path => "/my/bucket"
        )

        File.stubs(:exist?).returns true
        File.stubs(:read).with("/my/file").returns "my contents"

        bucketfile = stub "bucketfile"
        bucketfile.stubs(:to_s).returns "Content"

        Puppet::FileBucket::File.expects(:find).with{|x,opts|
            x == 'md5/DIGEST123'
        }.returns(bucketfile)

        @dipper.getfile("DIGEST123").should == "Content"
    end

    it "should backup files to a remote server" do

                    @dipper = Puppet::FileBucket::Dipper.new(
                
            :Server => "puppetmaster",
        
            :Port   => "31337"
        )

        File.stubs(:exist?).returns true
        File.stubs(:read).with("/my/file").returns "my contents"

        bucketfile = stub "bucketfile"
        bucketfile.stubs(:name).returns('md5/DIGEST123')
        bucketfile.stubs(:checksum_data).returns("DIGEST123")
        bucketfile.expects(:save).with('https://puppetmaster:31337/production/file_bucket_file/md5/DIGEST123')


                    Puppet::FileBucket::File.stubs(:new).with(
                
            "my contents",
            :bucket_path => nil,
        
            :path => '/my/file'
        ).returns(bucketfile)

        @dipper.backup("/my/file").should == "DIGEST123"
    end

    it "should retrieve files from a remote server" do

                    @dipper = Puppet::FileBucket::Dipper.new(
                
            :Server => "puppetmaster",
        
            :Port   => "31337"
        )

        File.stubs(:exist?).returns true
        File.stubs(:read).with("/my/file").returns "my contents"

        bucketfile = stub "bucketfile"
        bucketfile.stubs(:to_s).returns "Content"

        Puppet::FileBucket::File.expects(:find).with{|x,opts|
            x == 'https://puppetmaster:31337/production/file_bucket_file/md5/DIGEST123'
        }.returns(bucketfile)

        @dipper.getfile("DIGEST123").should == "Content"
    end


end