forked from dikhan/terraform-provider-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
155 lines (148 loc) · 5.96 KB
/
main_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"io/ioutil"
"log"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestInitProvider(t *testing.T) {
Convey("Given a valid binary name and the OTF_VAR pointing at a valid OpenAPI document", t, func() {
binaryName := "terraform-provider-openapi"
file, err := ioutil.TempFile("", "openapi.yaml")
if err != nil {
log.Fatal(err)
}
file.Write([]byte(`swagger: "2.0"
paths:
/v1/cdns:`))
os.Setenv("OTF_VAR_openapi_SWAGGER_URL", file.Name())
Convey("When initProvider method is called", func() {
providerName, err := initProvider(binaryName)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And then the provider returned should not be nil", func() {
So(providerName, ShouldNotBeNil)
})
})
os.Unsetenv("OTF_VAR_openapi_SWAGGER_URL")
})
Convey("Given an invalid binary name", t, func() {
binaryName := "some-invalid-binary-name"
Convey("When initProvider method is called", func() {
providerName, err := initProvider(binaryName)
Convey("Then the error returned should be the expected one", func() {
So(err.Error(), ShouldEqual, "error getting the provider's name from the binary 'some-invalid-binary-name': provider binary name (some-invalid-binary-name) does not match terraform naming convention 'terraform-provider-{name}', please rename the provider binary")
})
Convey("And the plugin returned should be nil", func() {
So(providerName, ShouldBeNil)
})
})
})
Convey("Given an invalid OpenAPI document", t, func() {
binaryName := "terraform-provider-openapi"
file, err := ioutil.TempFile("", "invalid_openapi_document.yaml")
if err != nil {
log.Fatal(err)
}
file.Write([]byte(`some non valid open api document`))
os.Setenv("OTF_VAR_openapi_SWAGGER_URL", file.Name())
Convey("When initProvider method is called", func() {
providerName, err := initProvider(binaryName)
Convey("Then the error returned should be the expected one", func() {
So(err.Error(), ShouldContainSubstring, "error initialising the terraform provider: plugin OpenAPI spec analyser error: failed to retrieve the OpenAPI document")
So(err.Error(), ShouldContainSubstring, "error = analyzed: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `some no...` into map[interface {}]interface {}")
})
Convey("And the plugin returned should be nil", func() {
So(providerName, ShouldBeNil)
})
})
})
}
func TestGetProviderName(t *testing.T) {
Convey("Given a valid pluginName with no version", t, func() {
binaryName := "terraform-provider-goa"
Convey("When getProviderName method is called", func() {
providerName, err := getProviderName(binaryName)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the error message returned should be", func() {
So(providerName, ShouldEqual, "goa")
})
})
})
Convey("Given a valid pluginName with correct version", t, func() {
binaryName := "terraform-provider-goa_v1.0.0"
Convey("When getProviderName method is called", func() {
providerName, err := getProviderName(binaryName)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the error message returned should be", func() {
So(providerName, ShouldEqual, "goa")
})
})
})
Convey("Given a valid pluginName with correct version using multiple digits", t, func() {
binaryName := "terraform-provider-goa_v10.34.1"
Convey("When getProviderName method is called", func() {
providerName, err := getProviderName(binaryName)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the error message returned should be", func() {
So(providerName, ShouldEqual, "goa")
})
})
})
Convey("Given a valid pluginName with numbers in the name", t, func() {
binaryName := "terraform-provider-goa1234"
Convey("When getProviderName method is called", func() {
providerName, err := getProviderName(binaryName)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the error message returned should be", func() {
So(providerName, ShouldEqual, "goa1234")
})
})
})
Convey("Given a valid binary path containing multiple matches for terraform-provider-", t, func() {
binaryName := "/Users/user/dev/src/github.com/terraform-provider-openapi/examples/swaggercodegen/.terraform/providers/terraform.example.com/examplecorp/swaggercodegen/1.0.0/darwin_amd64/terraform-provider-swaggercodegen"
Convey("When getProviderName method is called", func() {
providerName, err := getProviderName(binaryName)
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the error message returned should be", func() {
So(providerName, ShouldEqual, "swaggercodegen")
})
})
})
Convey("Given a valid pluginName with incorrect version format", t, func() {
binaryName := "terraform-provider-goa_v1"
Convey("When getProviderName method is called", func() {
_, err := getProviderName(binaryName)
Convey("Then the error returned should be nil", func() {
So(err, ShouldNotBeNil)
})
Convey("And the error message returned should be", func() {
So(err.Error(), ShouldEqual, "provider binary name (terraform-provider-goa_v1) does not match terraform naming convention 'terraform-provider-{name}', please rename the provider binary")
})
})
})
Convey("Given a NON valid pluginName", t, func() {
binaryName := "terraform-providerWrongName-goa"
Convey("When getProviderName method is called", func() {
_, err := getProviderName(binaryName)
Convey("Then the error returned should be nil", func() {
So(err, ShouldNotBeNil)
})
Convey("And the error message returned should be", func() {
So(err.Error(), ShouldEqual, "provider binary name (terraform-providerWrongName-goa) does not match terraform naming convention 'terraform-provider-{name}', please rename the provider binary")
})
})
})
}