-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodule_test.go
143 lines (125 loc) · 4 KB
/
module_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
package scalr
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestModulesList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
m, err := client.Modules.Read(ctx, defaultModuleID)
require.NoError(t, err)
assert.Equal(t, m.ID, defaultModuleID)
t.Run("without list options", func(t *testing.T) {
ml, err := client.Modules.List(ctx, ModuleListOptions{})
require.NoError(t, err)
mlIDs := make([]string, len(ml.Items))
for _, ws := range ml.Items {
mlIDs = append(mlIDs, ws.ID)
}
assert.Contains(t, mlIDs, defaultModuleID)
})
t.Run("with list options", func(t *testing.T) {
ml, err := client.Modules.List(ctx, ModuleListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, ml.Items)
assert.Equal(t, 999, ml.CurrentPage)
})
filters := []ModuleListOptions{
{Name: &m.Name},
{Status: &m.Status, Name: &m.Name},
{Provider: &m.Provider, Name: &m.Name},
{Account: &m.Account.ID, Environment: &m.Environment.ID},
{Environment: &m.Environment.ID},
}
for _, mlo := range filters {
t.Run("with a valid option", func(t *testing.T) {
wl, err := client.Modules.List(ctx, mlo)
assert.Len(t, wl.Items, 1, mlo)
assert.NoError(t, err)
assert.Equal(t, m.ID, wl.Items[0].ID)
})
}
}
func TestModulesCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("when empty options", func(t *testing.T) {
w, err := client.Modules.Create(ctx, ModuleCreateOptions{})
assert.Nil(t, w)
assert.EqualError(t, err, "vcs repo is required")
})
t.Run("when options has invalid vcs repo identifier", func(t *testing.T) {
w, err := client.Modules.Create(ctx, ModuleCreateOptions{
VCSRepo: &ModuleVCSRepo{Identifier: "foo/bar"},
VcsProvider: &VcsProvider{ID: *String(badIdentifier)},
})
assert.Nil(t, w)
assert.EqualError(t, err, ResourceNotFoundError{
Message: fmt.Sprintf("Invalid Relationship\n\nVcsProvider with ID '%s' not found or user unauthorized.", badIdentifier),
}.Error())
})
t.Run("when an error is returned from the api", func(t *testing.T) {
ws, err := client.Modules.Create(ctx, ModuleCreateOptions{
Environment: &Environment{ID: *String(badIdentifier)},
VCSRepo: &ModuleVCSRepo{Identifier: "foo/bar"},
VcsProvider: &VcsProvider{ID: "vcs-test"},
})
assert.Nil(t, ws)
assert.Error(t, err)
})
}
func TestModulesRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("when the module exists", func(t *testing.T) {
m, err := client.Modules.Read(ctx, defaultModuleID)
require.NoError(t, err)
assert.Equal(t, defaultModuleID, m.ID)
})
t.Run("when the module does not exist", func(t *testing.T) {
_, err := client.Modules.Read(ctx, "nonexisting")
assert.Error(t, err)
})
t.Run("without a valid identifier", func(t *testing.T) {
_, err := client.Modules.Read(ctx, badIdentifier)
assert.Error(t, err)
assert.EqualError(t, err, "invalid value for module ID")
})
}
func TestModulesReadBySource(t *testing.T) {
client := testClient(t)
ctx := context.Background()
module, err := client.Modules.Read(ctx, defaultModuleID)
require.NoError(t, err)
t.Run("with valid source", func(t *testing.T) {
m, err := client.Modules.ReadBySource(ctx, module.Source)
require.NoError(t, err)
assert.NotEmpty(t, m.Source)
assert.Equal(t, module.ID, m.ID)
assert.Equal(t, module.Source, m.Source)
})
t.Run("Invalid source", func(t *testing.T) {
ms := "invalidSource"
_, err := client.Modules.ReadBySource(ctx, "invalidSource")
require.Error(t, err)
assert.EqualError(t, err, ResourceNotFoundError{
Message: fmt.Sprintf("Module with source '%s' not found.", ms),
}.Error())
})
}
func TestModulesDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("without a valid module ID", func(t *testing.T) {
err := client.Modules.Delete(ctx, badIdentifier)
assert.EqualError(t, err, "invalid value for module ID")
})
}