|
| 1 | +// Copyright Mondoo, Inc. 2024, 2026 |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package cloud_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.mondoo.com/mql/v13/providers-sdk/v1/inventory" |
| 12 | + "go.mondoo.com/mql/v13/providers/os/connection/mock" |
| 13 | + "go.mondoo.com/mql/v13/providers/os/detector" |
| 14 | + "go.mondoo.com/mql/v13/providers/os/resources/cloud" |
| 15 | +) |
| 16 | + |
| 17 | +func newHetznerCloud(t *testing.T, path string) cloud.OSCloud { |
| 18 | + t.Helper() |
| 19 | + conn, err := mock.New(0, &inventory.Asset{}, mock.WithPath(path)) |
| 20 | + require.NoError(t, err) |
| 21 | + |
| 22 | + platform, ok := detector.DetectOS(conn) |
| 23 | + require.True(t, ok) |
| 24 | + conn.Asset().Platform = platform |
| 25 | + |
| 26 | + hc, err := cloud.NewHetznerCloud(conn) |
| 27 | + require.NoError(t, err) |
| 28 | + return hc |
| 29 | +} |
| 30 | + |
| 31 | +func TestHetznerInstance(t *testing.T) { |
| 32 | + hc := newHetznerCloud(t, "../../id/hetznercloud/testdata/metadata_linux.toml") |
| 33 | + |
| 34 | + md, err := hc.Instance() |
| 35 | + require.NoError(t, err) |
| 36 | + |
| 37 | + assert.Equal(t, "ubuntu-8gb-hil-1", md.PrivateHostname) |
| 38 | + assert.Equal(t, "5.78.107.208", md.PublicIP()) |
| 39 | + |
| 40 | + require.Len(t, md.PublicIpv4, 1) |
| 41 | + assert.Equal(t, "5.78.107.208", md.PublicIpv4[0].IP) |
| 42 | + |
| 43 | + // local-ipv4 is empty in test data, so PrivateIpv4 should be empty |
| 44 | + assert.Empty(t, md.PrivateIpv4) |
| 45 | +} |
| 46 | + |
| 47 | +func TestHetznerInstanceWithLocalIP(t *testing.T) { |
| 48 | + hc := newHetznerCloud(t, "./testdata/hetzner_with_local_ip.toml") |
| 49 | + |
| 50 | + md, err := hc.Instance() |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + assert.Equal(t, "ubuntu-8gb-hil-1", md.PrivateHostname) |
| 54 | + assert.Equal(t, "5.78.107.208", md.PublicIP()) |
| 55 | + assert.Equal(t, "10.0.0.5", md.PrivateIP()) |
| 56 | + |
| 57 | + require.Len(t, md.PublicIpv4, 1) |
| 58 | + assert.Equal(t, "5.78.107.208", md.PublicIpv4[0].IP) |
| 59 | + |
| 60 | + require.Len(t, md.PrivateIpv4, 1) |
| 61 | + assert.Equal(t, "10.0.0.5", md.PrivateIpv4[0].IP) |
| 62 | +} |
0 commit comments