File: main_test.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (114 lines) | stat: -rw-r--r-- 2,706 bytes parent folder | download
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
package integration

import (
	"context"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"testing"
	"time"

	"github.com/linode/linodego"
)

var (
	client       *linodego.Client
	cleanup      func()
	firewallID   int
	fixturesYaml = "fixtures/TestLinodeCloudFirewall"
)

func TestMain(m *testing.M) {
	envFixtureMode, _ := os.LookupEnv("LINODE_FIXTURE_MODE")
	enableCloudFW := os.Getenv("ENABLE_CLOUD_FW") != "false" // default true unless explicitly "false"

	if envFixtureMode == "record" && enableCloudFW {
		setupCloudFirewall(nil)
	} else if envFixtureMode == "record" {
		log.Printf("[INFO] ENABLE_CLOUD_FW is false - skipping Cloud Firewall setup")
	} else if envFixtureMode == "play" {
		log.Printf("[INFO] Fixture mode play - Test Linode Cloud Firewall not created")
	}

	code := m.Run()

	if envFixtureMode == "record" && enableCloudFW {
		deleteCloudFirewall()
	}

	os.Exit(code)
}

func setupCloudFirewall(t *testing.T) {
	client, cleanup = createTestClient(t, fixturesYaml)

	publicIPv4, err := getPublicIPv4()
	if err != nil {
		t.Fatalf("[ERROR] Failed to retrieve public IPv4: %v", err)
	}

	firewallRuleSet := getDefaultFirewallRuleSet(publicIPv4)
	firewallLabel := fmt.Sprintf("cloudfw-%d", time.Now().UnixNano())

	firewall, err := client.CreateFirewall(context.Background(), linodego.FirewallCreateOptions{
		Label: firewallLabel,
		Rules: firewallRuleSet,
	})
	if err != nil {
		log.Printf("[ERROR] Error creating firewall: %v\n", err)
		os.Exit(1)
	}

	firewallID = firewall.ID
	log.Printf("[INFO] Created Test Linode Cloud Firewall with ID: %d\n", firewallID)
}

func deleteCloudFirewall() {
	if firewallID != 0 {
		err := client.DeleteFirewall(context.Background(), firewallID)
		if err != nil {
			log.Printf("[ERROR] Error deleting Cloud Firewall: %v\n", err)
			os.Exit(1)
		}
		log.Printf("[INFO] Deleted Test Linode Cloud Firewall with ID: %d\n", firewallID)
	}
}

func getDefaultFirewallRuleSet(publicIPv4 string) linodego.FirewallRuleSet {
	cloudFirewallRule := linodego.FirewallRule{
		Label:     "ssh-inbound-accept-local",
		Action:    "ACCEPT",
		Ports:     "22",
		Protocol:  "TCP",
		Addresses: linodego.NetworkAddresses{IPv4: &[]string{publicIPv4}},
	}

	return linodego.FirewallRuleSet{
		Inbound:        []linodego.FirewallRule{cloudFirewallRule},
		InboundPolicy:  "DROP",
		Outbound:       []linodego.FirewallRule{},
		OutboundPolicy: "ACCEPT",
	}
}

func getPublicIPv4() (string, error) {
	resp, err := http.Get("https://api.ipify.org?format=text")
	if err != nil {
		return "", err
	}

	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	return string(body) + "/32", nil
}

func GetFirewallID() int {
	return firewallID
}