|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "os" |
| 10 | + "strconv" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/hetznercloud/csi-driver/internal/app" |
| 14 | + "github.com/hetznercloud/hcloud-go/v2/hcloud" |
| 15 | + "github.com/hetznercloud/hcloud-go/v2/hcloud/exp/kit/randutil" |
| 16 | + "github.com/hetznercloud/hcloud-go/v2/hcloud/metadata" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +func TestLocationFromEnv(t *testing.T) { |
| 22 | + token := os.Getenv("HCLOUD_TOKEN") |
| 23 | + require.NotEmpty(t, token, "HCLOUD_TOKEN is not set") |
| 24 | + |
| 25 | + client := hcloud.NewClient(hcloud.WithToken(token)) |
| 26 | + |
| 27 | + metadataService := metadataServiceMux() |
| 28 | + t.Cleanup(metadataService.Close) |
| 29 | + |
| 30 | + metadataClient := metadata.NewClient( |
| 31 | + metadata.WithHTTPClient(metadataService.Client()), |
| 32 | + metadata.WithEndpoint(fmt.Sprintf("%s/hetzner/v1/metadata", metadataService.URL)), |
| 33 | + ) |
| 34 | + |
| 35 | + serverName := fmt.Sprintf("csi-integration-%s", randutil.GenerateID()) |
| 36 | + |
| 37 | + result, _, err := client.Server.Create(t.Context(), hcloud.ServerCreateOpts{ |
| 38 | + Name: serverName, |
| 39 | + ServerType: &hcloud.ServerType{Name: "cpx22"}, |
| 40 | + Location: &hcloud.Location{Name: "hel1"}, |
| 41 | + Image: &hcloud.Image{Name: "ubuntu-24.04"}, |
| 42 | + }) |
| 43 | + require.NoError(t, err) |
| 44 | + t.Cleanup(func() { |
| 45 | + ctx := context.Background() // t.Context() is already canceled at this point |
| 46 | + delResult, _, err := client.Server.DeleteWithResult(ctx, result.Server) |
| 47 | + require.NoError(t, err) |
| 48 | + require.NoError(t, client.Action.WaitFor(ctx, delResult.Action)) |
| 49 | + }) |
| 50 | + |
| 51 | + require.NoError(t, client.Action.WaitFor(t.Context(), result.Action)) |
| 52 | + |
| 53 | + t.Run("location env", func(t *testing.T) { |
| 54 | + t.Setenv("HCLOUD_VOLUME_DEFAULT_LOCATION", "hel1") |
| 55 | + loc, err := app.GetServerLocation(slog.New(slog.DiscardHandler), metadataClient, client, false) |
| 56 | + require.NoError(t, err) |
| 57 | + assert.Equal(t, "hel1", loc) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("server ID env", func(t *testing.T) { |
| 61 | + t.Setenv("HCLOUD_SERVER_ID", strconv.FormatInt(result.Server.ID, 10)) |
| 62 | + loc, err := app.GetServerLocation(slog.New(slog.DiscardHandler), metadataClient, client, true) |
| 63 | + require.NoError(t, err) |
| 64 | + assert.Equal(t, "hel1", loc) |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("node name env", func(t *testing.T) { |
| 68 | + t.Setenv("KUBE_NODE_NAME", serverName) |
| 69 | + loc, err := app.GetServerLocation(slog.New(slog.DiscardHandler), metadataClient, client, true) |
| 70 | + require.NoError(t, err) |
| 71 | + assert.Equal(t, "hel1", loc) |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("metadata service", func(t *testing.T) { |
| 75 | + loc, err := app.GetServerLocation(slog.New(slog.DiscardHandler), metadataClient, client, false) |
| 76 | + require.NoError(t, err) |
| 77 | + assert.Equal(t, "hel1", loc) |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +func metadataServiceMux() *httptest.Server { |
| 82 | + mux := http.NewServeMux() |
| 83 | + |
| 84 | + mux.HandleFunc("/hetzner/v1/metadata/availability-zone", func(w http.ResponseWriter, r *http.Request) { |
| 85 | + if r.Method != http.MethodGet { |
| 86 | + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) |
| 87 | + return |
| 88 | + } |
| 89 | + w.Header().Set("Content-Type", "text/plain") |
| 90 | + fmt.Fprint(w, "hel1-dc2") |
| 91 | + }) |
| 92 | + |
| 93 | + return httptest.NewServer(mux) |
| 94 | +} |
0 commit comments