-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
93 lines (80 loc) · 2.36 KB
/
client_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
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-Present Datadog, Inc.
package cloudcraft_test
import (
"errors"
"testing"
"github.com/DataDog/cloudcraft-go"
)
func TestNewClient(t *testing.T) {
t.Parallel()
tests := []struct {
name string
give *cloudcraft.Config
want error
}{
{
name: "Valid configuration",
give: cloudcraft.NewConfig("not-a-real-key-oRbwhd5RTvWsPJ89ZkASHU13qcyd="),
want: nil,
},
{
name: "Invalid configuration, missing API key",
give: &cloudcraft.Config{
Scheme: cloudcraft.DefaultScheme,
Host: cloudcraft.DefaultHost,
Port: cloudcraft.DefaultPort,
Path: cloudcraft.DefaultPath,
Timeout: cloudcraft.DefaultTimeout,
},
want: cloudcraft.ErrMissingKey,
},
{
name: "Invalid configuration, invalid API key length",
give: cloudcraft.NewConfig("short_key"),
want: cloudcraft.ErrInvalidKey,
},
{
name: "Invalid configuration, invalid endpoint",
give: &cloudcraft.Config{
Scheme: "ftp",
Host: cloudcraft.DefaultHost,
Port: cloudcraft.DefaultPort,
Path: cloudcraft.DefaultPath,
Timeout: cloudcraft.DefaultTimeout,
Key: "not-a-real-key-oRbwhd5RTvWsPJ89ZkASHU13qcyd=",
},
want: cloudcraft.ErrInvalidConfig,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
client, err := cloudcraft.NewClient(tt.give)
if !errors.Is(err, tt.want) {
t.Fatalf("NewClient() error = %v, want %v", err, tt.want)
}
if tt.want == nil && client == nil {
t.Error("Expected non-nil client, got nil")
}
})
}
}
func TestNewClientWithNilConfig(t *testing.T) {
// Setting environment variables required for NewConfigFromEnv.
t.Setenv("CLOUDCRAFT_PROTOCOL", cloudcraft.DefaultScheme)
t.Setenv("CLOUDCRAFT_HOST", cloudcraft.DefaultHost)
t.Setenv("CLOUDCRAFT_PORT", cloudcraft.DefaultPort)
t.Setenv("CLOUDCRAFT_PATH", cloudcraft.DefaultPath)
t.Setenv("CLOUDCRAFT_API_KEY", "not-a-real-key-oRbwhd5RTvWsPJ89ZkASHU13qcyd=")
t.Setenv("CLOUDCRAFT_TIMEOUT", "80s")
client, err := cloudcraft.NewClient(nil)
if err != nil {
t.Fatalf("Unexpected error for nil config: %v", err)
}
if client == nil {
t.Error("Expected non-nil client, got nil")
}
}