-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil_test.go
84 lines (69 loc) · 2.51 KB
/
util_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
package platform_test
import (
"os"
"sync"
"testing"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/jfrog/terraform-provider-platform/v2/pkg/platform"
"github.com/jfrog/terraform-provider-shared/client"
)
// TestProvider PreCheck(t) must be called before using this provider instance.
var TestProvider provider.Provider
// testAccProviderConfigure ensures Provider is only configured once
//
// The PreCheck(t) function is invoked for every test and this prevents
// extraneous reconfiguration to the same values each time. However, this does
// not prevent reconfiguration that may happen should the address of
// Provider be errantly reused in ProviderFactories.
var testAccProviderConfigure sync.Once
// testAccPreCheck This function should be present in every acceptance test.
func testAccPreCheck(t *testing.T) {
// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderConfigure.Do(func() {
restyClient := getTestResty(t)
platformUrl := getPlatformUrl(t)
// Set custom base URL so repos that relies on it will work
// https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API#ArtifactoryRESTAPI-UpdateCustomURLBase
_, err := restyClient.R().
SetBody(platformUrl).
SetHeader("Content-Type", "text/plain").
Put("/artifactory/api/system/configuration/baseUrl")
if err != nil {
t.Fatalf("failed to set custom base URL: %v", err)
}
})
}
func getTestResty(t *testing.T) *resty.Client {
var ok bool
platformUrl := getPlatformUrl(t)
restyClient, err := client.Build(platformUrl, "")
if err != nil {
t.Fatal(err)
}
var accessToken string
if accessToken, ok = os.LookupEnv("JFROG_ACCESS_TOKEN"); !ok {
t.Fatal("JFROG_ACCESS_TOKEN must be set for acceptance tests")
}
restyClient, err = client.AddAuth(restyClient, "", accessToken)
if err != nil {
t.Fatal(err)
}
return restyClient
}
func getPlatformUrl(t *testing.T) string {
platformUrl, ok := os.LookupEnv("JFROG_URL")
if !ok {
t.Fatal("JFROG_URL must be set for acceptance tests")
}
return platformUrl
}
func testAccProviders() map[string]func() (tfprotov6.ProviderServer, error) {
TestProvider = platform.NewProvider()()
return map[string]func() (tfprotov6.ProviderServer, error){
"platform": providerserver.NewProtocol6WithError(TestProvider),
}
}