forked from jhaals/puppet-anvil
/
component_test.go
139 lines (110 loc) · 3.7 KB
/
component_test.go
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
package main
import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"github.com/jhaals/puppet-anvil/api"
"github.com/jhaals/puppet-anvil/service"
. "gopkg.in/check.v1"
)
var _ = Suite(&ComponentTestSuite{})
type ComponentTestSuite struct {
svc *service.AnvilService
client *api.AnvilClient
path string
port int
}
// create and start a service, create a client
// download fixture modules if they aren't present
func (s *ComponentTestSuite) SetUpSuite(c *C) {
s.path = "./tmp-test"
os.MkdirAll(s.path+"/modules", 0755)
if _, err := os.Stat(s.path + "/puppetlabs-apache-1.5.0.tar.gz"); err != nil {
dl("https://forgeapi.puppetlabs.com/v3/files/puppetlabs-apache-1.5.0.tar.gz", s.path+"/puppetlabs-apache-1.5.0.tar.gz")
dl("https://forgeapi.puppetlabs.com/v3/files/puppetlabs-concat-1.2.3.tar.gz", s.path+"/puppetlabs-concat-1.2.3.tar.gz")
dl("https://forgeapi.puppetlabs.com/v3/files/puppetlabs-stdlib-4.6.0.tar.gz", s.path+"/puppetlabs-stdlib-4.6.0.tar.gz")
}
s.port = Port()
s.svc = service.New(strconv.Itoa(s.port), s.path+"/modules")
s.client = &api.AnvilClient{Address: fmt.Sprintf("localhost:%d", s.port)}
go s.svc.Run()
}
// tear down installed files
// (leave fixture downloads for future tests)
func (s *ComponentTestSuite) TearDownTest(c *C) {
os.RemoveAll(s.path + "/modules")
}
// client should publish module to filesystem
func (s *ComponentTestSuite) TestPublish(c *C) {
// given
file := s.path + "/puppetlabs-apache-1.5.0.tar.gz"
f, _ := os.Open(file)
defer f.Close()
// when
loc, err := s.client.PublishModule(f, "puppetlabs-apache-1.5.0.tar.gz")
// then
c.Assert(err, Equals, nil)
c.Assert(loc, Equals, "/v3/files/puppetlabs/apache/puppetlabs-apache-1.5.0.tar.gz")
}
// client should error out with invlid module fileName
func (s *ComponentTestSuite) TestPublishError(c *C) {
// given
file := s.path + "/puppetlabs-apache-1.5.0.tar.gz"
f, _ := os.Open(file)
defer f.Close()
// when
_, err := s.client.PublishModule(f, "puppetlabs-apache.tar.gz")
// then
c.Assert(err, Not(Equals), nil)
}
// client should return list releases for a given module
func (s *ComponentTestSuite) TestGetReleaseByModule(c *C) {
// given
file := s.path + "/puppetlabs-apache-1.5.0.tar.gz"
f, _ := os.Open(file)
defer f.Close()
loc, _ := s.client.PublishModule(f, "puppetlabs-apache-1.5.0.tar.gz")
// when
resp, err := s.client.GetReleaseByModule("puppetlabs", "apache")
// then
c.Assert(err, Equals, nil)
c.Assert(resp.Results[0].FileUri, Equals, loc)
c.Assert(resp.Results[0].Version, Equals, "1.5.0")
}
// client should return empty array when module not found
func (s *ComponentTestSuite) TestGetReleaseByModuleNotFound(c *C) {
// when
resp, err := s.client.GetReleaseByModule("puppetlabs", "apache")
// then
c.Assert(err, Equals, nil)
c.Assert(len(resp.Results), Equals, 0)
}
// client should return error when searching with invalid module name
func (s *ComponentTestSuite) TestGetReleaseByModuleInvalidModule(c *C) {
// when
_, err := s.client.GetReleaseByModule("puppetlabs", "apache-foo")
// then
c.Assert(err, Not(Equals), nil)
}
// file should be accessible from location-header address
func (s *ComponentTestSuite) TestDownloadFile(c *C) {
// given
file := s.path + "/puppetlabs-apache-1.5.0.tar.gz"
expected, _ := service.Checksum(file)
f, _ := os.Open(file)
defer f.Close()
loc, _ := s.client.PublishModule(f, "puppetlabs-apache-1.5.0.tar.gz")
outPath := s.path + "/out.tar.gz"
// when
output, _ := os.Create(outPath)
defer output.Close()
resp, _ := http.Get(fmt.Sprintf("http://localhost:%d/%s", s.port, loc))
defer resp.Body.Close()
io.Copy(output, resp.Body)
// then
found, err := service.Checksum(outPath)
c.Assert(err, Equals, nil)
c.Assert(found, Equals, expected)
}