forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
packngo_test.go
101 lines (88 loc) · 2.24 KB
/
packngo_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
package packngo
import (
"fmt"
"math/rand"
"os"
"regexp"
"strings"
"testing"
"time"
)
const (
packetTokenEnvVar = "PACKET_AUTH_TOKEN"
packngoAccTestVar = "PACKNGO_TEST_ACTUAL_API"
testProjectPrefix = "PACKNGO_TEST_DELME_2d768716_"
testFacilityVar = "PACKNGO_TEST_FACILITY"
)
func testFacility() string {
envFac := os.Getenv(testFacilityVar)
if envFac != "" {
return envFac
}
return "ewr1"
}
func randString8() string {
n := 8
rand.Seed(time.Now().UnixNano())
letterRunes := []rune("acdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func setupWithProject(t *testing.T) (*Client, string, func()) {
c := setup(t)
p, _, err := c.Projects.Create(&ProjectCreateRequest{Name: testProjectPrefix + randString8()})
if err != nil {
t.Fatal(err)
}
return c, p.ID, func() {
_, err := c.Projects.Delete(p.ID)
if err != nil {
panic(fmt.Errorf("while deleting %s: %s", p, err))
}
}
}
func skipUnlessAcceptanceTestsAllowed(t *testing.T) {
if os.Getenv(packngoAccTestVar) == "" {
t.Skipf("%s is not set", packngoAccTestVar)
}
}
func setup(t *testing.T) *Client {
apiToken := os.Getenv(packetTokenEnvVar)
if apiToken == "" {
t.Fatalf("If you want to run packngo test, you must export %s.", packetTokenEnvVar)
}
c := NewClient("packngo test", apiToken, nil)
return c
}
func projectTeardown(c *Client) {
ps, _, err := c.Projects.List()
if err != nil {
panic(fmt.Errorf("while teardown: %s", err))
}
for _, p := range ps {
if strings.HasPrefix(p.Name, testProjectPrefix) {
_, err := c.Projects.Delete(p.ID)
if err != nil {
panic(fmt.Errorf("while deleting %s: %s", p, err))
}
}
}
}
func TestAccInvalidCredentials(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
c := NewClient("packngo test", "wrongApiToken", nil)
_, r, expectedErr := c.Projects.List()
matched, err := regexp.MatchString(".*Invalid.*", expectedErr.Error())
if err != nil {
t.Fatalf("Err while matching err string from response err %s: %s", expectedErr, err)
}
if r.StatusCode != 401 {
t.Fatalf("Expected 401 as response code, got: %d", r.StatusCode)
}
if !matched {
t.Fatalf("Unexpected error string: %s", expectedErr)
}
}