-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
136 lines (123 loc) · 4.32 KB
/
Copy pathmain_test.go
File metadata and controls
136 lines (123 loc) · 4.32 KB
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
package main
import (
"context"
"testing"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func strPtr(s string) *string { return &s }
// makeProviderConfig builds a tfsdk.Config matching the provider schema. Pass nil
// for an attribute to make it null (exercising the env-fallback path).
func makeProviderConfig(t *testing.T, p *runpodProvider, apiKey, baseURL *string) tfsdk.Config {
t.Helper()
ctx := context.Background()
sr := &provider.SchemaResponse{}
p.Schema(ctx, provider.SchemaRequest{}, sr)
objType := tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"api_key": tftypes.String,
"base_url": tftypes.String,
}}
val := func(s *string) tftypes.Value {
if s == nil {
return tftypes.NewValue(tftypes.String, nil)
}
return tftypes.NewValue(tftypes.String, *s)
}
raw := tftypes.NewValue(objType, map[string]tftypes.Value{
"api_key": val(apiKey),
"base_url": val(baseURL),
})
return tfsdk.Config{Schema: sr.Schema, Raw: raw}
}
func configureProvider(t *testing.T, apiKey, baseURL *string) (*runpodProvider, *provider.ConfigureResponse) {
t.Helper()
p := &runpodProvider{}
resp := &provider.ConfigureResponse{}
p.Configure(context.Background(), provider.ConfigureRequest{Config: makeProviderConfig(t, p, apiKey, baseURL)}, resp)
return p, resp
}
func TestConfigure_ConfigApiKeyWins(t *testing.T) {
t.Setenv("RUNPOD_API_KEY", "envkey123")
p, resp := configureProvider(t, strPtr("cfgkey123"), nil)
if resp.Diagnostics.HasError() {
t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics)
}
if p.apiKey != "cfgkey123" {
t.Errorf("apiKey = %q, want config value to override env", p.apiKey)
}
}
func TestConfigure_EnvApiKeyFallback(t *testing.T) {
t.Setenv("RUNPOD_API_KEY", "envkey123")
p, resp := configureProvider(t, nil, nil)
if resp.Diagnostics.HasError() {
t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics)
}
if p.apiKey != "envkey123" {
t.Errorf("apiKey = %q, want env fallback", p.apiKey)
}
}
func TestConfigure_ConfigBaseURLWins(t *testing.T) {
t.Setenv("RUNPOD_API_KEY", "testkey123")
t.Setenv("RUNPOD_BASE_URL", "https://env.runpod.io/v1")
p, resp := configureProvider(t, nil, strPtr("https://cfg.runpod.io/v1"))
if resp.Diagnostics.HasError() {
t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics)
}
if p.baseUrl != "https://cfg.runpod.io/v1" {
t.Errorf("baseUrl = %q, want config value to override env", p.baseUrl)
}
}
func TestConfigure_BaseURLDefault(t *testing.T) {
t.Setenv("RUNPOD_API_KEY", "envkey123")
t.Setenv("RUNPOD_BASE_URL", "")
p, resp := configureProvider(t, nil, nil)
if resp.Diagnostics.HasError() {
t.Fatalf("unexpected diagnostics: %v", resp.Diagnostics)
}
if p.baseUrl != "https://api.runpod.io/v2" {
t.Errorf("baseUrl = %q, want default", p.baseUrl)
}
}
func TestConfigure_MissingApiKeyErrors(t *testing.T) {
t.Setenv("RUNPOD_API_KEY", "")
_, resp := configureProvider(t, nil, nil)
if !resp.Diagnostics.HasError() {
t.Fatal("expected a 'Missing API Key' error diagnostic, got none")
}
}
// TestResourceSchemas is a smoke check: every registered resource builds its
// schema without diagnostics and exposes at least one attribute. It does not
// run full framework schema validation — it catches gross breakage / gen drift.
func TestResourceSchemas(t *testing.T) {
ctx := context.Background()
p := &runpodProvider{}
for i, ctor := range p.Resources(ctx) {
r := ctor()
resp := &resource.SchemaResponse{}
r.Schema(ctx, resource.SchemaRequest{}, resp)
if resp.Diagnostics.HasError() {
t.Errorf("resource[%d] schema diagnostics: %v", i, resp.Diagnostics)
}
if len(resp.Schema.Attributes) == 0 {
t.Errorf("resource[%d] schema has no attributes", i)
}
}
}
func TestDataSourceSchemas(t *testing.T) {
ctx := context.Background()
p := &runpodProvider{}
for i, ctor := range p.DataSources(ctx) {
d := ctor()
resp := &datasource.SchemaResponse{}
d.Schema(ctx, datasource.SchemaRequest{}, resp)
if resp.Diagnostics.HasError() {
t.Errorf("datasource[%d] schema diagnostics: %v", i, resp.Diagnostics)
}
if len(resp.Schema.Attributes) == 0 {
t.Errorf("datasource[%d] schema has no attributes", i)
}
}
}