|
| 1 | +package hcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + hrobot "github.com/syself/hrobot-go" |
| 13 | + |
| 14 | + "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/testsupport" |
| 15 | + "github.com/hetznercloud/hcloud-go/v2/hcloud" |
| 16 | + "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" |
| 17 | +) |
| 18 | + |
| 19 | +func TestHCloudClientReloadsTokenFromFile(t *testing.T) { |
| 20 | + defer unsetEnv(t, "HCLOUD_TOKEN")() |
| 21 | + |
| 22 | + var authorizations []string |
| 23 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 24 | + authorizations = append(authorizations, r.Header.Get("Authorization")) |
| 25 | + assert.NoError(t, json.NewEncoder(w).Encode(schema.LocationListResponse{Locations: []schema.Location{}})) |
| 26 | + })) |
| 27 | + defer server.Close() |
| 28 | + |
| 29 | + tokenFile := filepath.Join(t.TempDir(), "hcloud-token") |
| 30 | + assert.NoError(t, os.WriteFile(tokenFile, []byte("token-1"), 0o600)) |
| 31 | + |
| 32 | + resetEnv := testsupport.Setenv(t, "HCLOUD_TOKEN_FILE", tokenFile) |
| 33 | + defer resetEnv() |
| 34 | + |
| 35 | + client := hcloud.NewClient( |
| 36 | + hcloud.WithEndpoint(server.URL), |
| 37 | + hcloud.WithHTTPClient(newHCloudHTTPClient(0)), |
| 38 | + hcloud.WithPollOpts(hcloud.PollOpts{BackoffFunc: hcloud.ConstantBackoff(0)}), |
| 39 | + hcloud.WithRetryOpts(hcloud.RetryOpts{BackoffFunc: hcloud.ConstantBackoff(0)}), |
| 40 | + ) |
| 41 | + |
| 42 | + _, _, err := client.Location.List(t.Context(), hcloud.LocationListOpts{}) |
| 43 | + assert.NoError(t, err) |
| 44 | + |
| 45 | + assert.NoError(t, os.WriteFile(tokenFile, []byte("token-2"), 0o600)) |
| 46 | + |
| 47 | + _, _, err = client.Location.List(t.Context(), hcloud.LocationListOpts{}) |
| 48 | + assert.NoError(t, err) |
| 49 | + |
| 50 | + assert.Equal(t, []string{"Bearer token-1", "Bearer token-2"}, authorizations) |
| 51 | +} |
| 52 | + |
| 53 | +func TestRobotClientReloadsCredentialsFromFile(t *testing.T) { |
| 54 | + defer unsetEnv(t, "ROBOT_USER")() |
| 55 | + defer unsetEnv(t, "ROBOT_PASSWORD")() |
| 56 | + |
| 57 | + var users []string |
| 58 | + var passwords []string |
| 59 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 | + user, password, ok := r.BasicAuth() |
| 61 | + assert.True(t, ok) |
| 62 | + users = append(users, user) |
| 63 | + passwords = append(passwords, password) |
| 64 | + assert.NoError(t, json.NewEncoder(w).Encode([]map[string]any{ |
| 65 | + { |
| 66 | + "server": map[string]any{ |
| 67 | + "server_number": 1, |
| 68 | + "server_name": "node-1", |
| 69 | + "server_ip": "192.0.2.1", |
| 70 | + }, |
| 71 | + }, |
| 72 | + })) |
| 73 | + })) |
| 74 | + defer server.Close() |
| 75 | + |
| 76 | + dir := t.TempDir() |
| 77 | + userFile := filepath.Join(dir, "robot-user") |
| 78 | + passwordFile := filepath.Join(dir, "robot-password") |
| 79 | + assert.NoError(t, os.WriteFile(userFile, []byte("robot-user-1"), 0o600)) |
| 80 | + assert.NoError(t, os.WriteFile(passwordFile, []byte("robot-password-1"), 0o600)) |
| 81 | + |
| 82 | + resetEnv := testsupport.Setenv(t, |
| 83 | + "ROBOT_USER_FILE", userFile, |
| 84 | + "ROBOT_PASSWORD_FILE", passwordFile, |
| 85 | + ) |
| 86 | + defer resetEnv() |
| 87 | + |
| 88 | + client := hrobot.NewBasicAuthClientWithCustomHttpClient("stale-user", "stale-password", newRobotHTTPClient(0)) |
| 89 | + client.SetBaseURL(server.URL) |
| 90 | + |
| 91 | + _, err := client.ServerGetList() |
| 92 | + assert.NoError(t, err) |
| 93 | + |
| 94 | + assert.NoError(t, os.WriteFile(userFile, []byte("robot-user-2"), 0o600)) |
| 95 | + assert.NoError(t, os.WriteFile(passwordFile, []byte("robot-password-2"), 0o600)) |
| 96 | + |
| 97 | + _, err = client.ServerGetList() |
| 98 | + assert.NoError(t, err) |
| 99 | + |
| 100 | + assert.Equal(t, []string{"robot-user-1", "robot-user-2"}, users) |
| 101 | + assert.Equal(t, []string{"robot-password-1", "robot-password-2"}, passwords) |
| 102 | +} |
| 103 | + |
| 104 | +func unsetEnv(t *testing.T, key string) func() { |
| 105 | + t.Helper() |
| 106 | + |
| 107 | + value, ok := os.LookupEnv(key) |
| 108 | + assert.NoError(t, os.Unsetenv(key)) |
| 109 | + |
| 110 | + return func() { |
| 111 | + if !ok { |
| 112 | + assert.NoError(t, os.Unsetenv(key)) |
| 113 | + return |
| 114 | + } |
| 115 | + assert.NoError(t, os.Setenv(key, value)) |
| 116 | + } |
| 117 | +} |
0 commit comments