forked from concourse/registry-image-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
119 lines (96 loc) · 3.3 KB
/
types_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
package resource_test
import (
"encoding/json"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/ecr/ecriface"
resource "github.com/concourse/registry-image-resource"
)
var _ = Describe("Source", func() {
It("should unmarshal tag float value into a string", func() {
var source resource.Source
raw := []byte(`{ "tag": 42.1 }`)
err := json.Unmarshal(raw, &source)
Expect(err).ToNot(HaveOccurred())
Expect(source.Tag()).To(Equal("42.1"))
})
It("should unmarshal tag int value into a string", func() {
var source resource.Source
raw := []byte(`{ "tag": 42 }`)
err := json.Unmarshal(raw, &source)
Expect(err).ToNot(HaveOccurred())
Expect(source.Tag()).To(Equal("42"))
})
It("should unmarshal tag string value into a string", func() {
var source resource.Source
raw := []byte(`{ "tag": "foo" }`)
err := json.Unmarshal(raw, &source)
Expect(err).ToNot(HaveOccurred())
Expect(source.Tag()).To(Equal("foo"))
})
It("should unmarshal tag '' value to latest", func() {
var source resource.Source
raw := []byte(`{ "tag": "" }`)
err := json.Unmarshal(raw, &source)
Expect(err).ToNot(HaveOccurred())
Expect(source.Tag()).To(Equal("latest"))
})
It("should default unspecified tag to latest", func() {
var source resource.Source
raw := []byte(`{}`)
err := json.Unmarshal(raw, &source)
Expect(err).ToNot(HaveOccurred())
Expect(source.Tag()).To(Equal("latest"))
})
It("should marshal a tag back out to a string", func() {
source := resource.Source{Repository: "foo", RawTag: "0"}
json, err := json.Marshal(source)
Expect(err).ToNot(HaveOccurred())
Expect(json).To(MatchJSON(`{"repository":"foo","tag":"0"}`))
})
Describe("ecr", func() {
It("should exclude a registry id as part of the request for an authorization token when omitted", func() {
source := resource.Source{
Repository: "foo",
RawTag: "0",
AwsCredentials: resource.AwsCredentials{
AwsAccessKeyId: "foo",
AwsSecretAccessKey: "bar",
AwsRegion: "us-east-1",
},
}
m := &mockECR{}
_, err := source.GetECRAuthorizationToken(m)
Expect(err).ToNot(HaveOccurred())
Expect(len(m.getAuthorizationInput.RegistryIds)).To(Equal(0))
})
It("should include a registry id as part of the request for an authorization token when specified", func() {
source := resource.Source{
Repository: "foo",
RawTag: "0",
AwsCredentials: resource.AwsCredentials{
AwsAccessKeyId: "foo",
AwsSecretAccessKey: "bar",
AwsRegion: "us-east-1",
AWSECRRegistryId: "012345678901",
},
}
m := &mockECR{}
_, err := source.GetECRAuthorizationToken(m)
Expect(err).ToNot(HaveOccurred())
Expect(len(m.getAuthorizationInput.RegistryIds)).To(Equal(1))
Expect(*m.getAuthorizationInput.RegistryIds[0]).To(Equal(source.AwsCredentials.AWSECRRegistryId))
})
})
})
type mockECR struct {
ecriface.ECRAPI
getAuthorizationInput *ecr.GetAuthorizationTokenInput
getAuthorizationOutput *ecr.GetAuthorizationTokenOutput
getAuthorizationError error
}
func (m *mockECR) GetAuthorizationToken(input *ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error) {
m.getAuthorizationInput = input
return m.getAuthorizationOutput, m.getAuthorizationError
}