From 23902e31c4f2dff212962e6651366f458f498353 Mon Sep 17 00:00:00 2001 From: Zhiwei Liang <121905282+zliang-akamai@users.noreply.github.com> Date: Wed, 21 Aug 2024 14:26:38 -0400 Subject: [PATCH] Bump Golang to v1.22 (#569) * Bump Golang to 1.22 * Apply for loop breaking changes for Go 1.22 * gofumpt --- client.go | 2 +- go.mod | 2 +- go.work | 2 +- internal/duration/duration.go | 2 +- k8s/go.mod | 2 +- pagination.go | 2 +- test/go.mod | 2 +- test/integration/account_invoice_test.go | 60 ++++---- test/integration/account_settings_test.go | 134 +++++++++--------- test/integration/account_transfer_test.go | 36 ++--- test/integration/integration_suite_test.go | 24 ++-- test/integration/network_pools_test.go | 30 ++-- .../profile_security_question_test.go | 20 +-- test/integration/support_ticket_test.go | 34 ++--- waitfor.go | 1 - 15 files changed, 176 insertions(+), 177 deletions(-) diff --git a/client.go b/client.go index 07f3f135d..348e71cc9 100644 --- a/client.go +++ b/client.go @@ -144,7 +144,7 @@ func (c *httpClient) doRequest(ctx context.Context, method, url string, params R err error ) - for attempt := 0; attempt < httpDefaultRetryCount; attempt++ { + for range httpDefaultRetryCount { req, bodyBuffer, err = c.createRequest(ctx, method, url, params) if err != nil { return err diff --git a/go.mod b/go.mod index 83ea46dee..2b4ef8318 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,6 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -go 1.21 +go 1.22 retract v1.0.0 // Accidental branch push diff --git a/go.work b/go.work index 193259886..9f6d7df5e 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.21 +go 1.22 use ( . diff --git a/internal/duration/duration.go b/internal/duration/duration.go index 14d4f1365..a63a11a95 100644 --- a/internal/duration/duration.go +++ b/internal/duration/duration.go @@ -51,7 +51,7 @@ func durationToSeconds(s string) (int, error) { l := len(segs) - for i := 0; i < l; i++ { + for i := range l { m, err := strconv.Atoi(segs[i]) if err != nil { return 0, err diff --git a/k8s/go.mod b/k8s/go.mod index da99557e6..b9e7aa28a 100644 --- a/k8s/go.mod +++ b/k8s/go.mod @@ -49,4 +49,4 @@ require ( replace github.com/linode/linodego => ../ -go 1.21 +go 1.22 diff --git a/pagination.go b/pagination.go index 3c1c5b602..3b3f50ac9 100644 --- a/pagination.go +++ b/pagination.go @@ -114,7 +114,7 @@ func flattenQueryStruct(val any) (map[string]string, error) { valType := reflectVal.Type() - for i := 0; i < valType.NumField(); i++ { + for i := range valType.NumField() { currentField := valType.Field(i) queryTag, ok := currentField.Tag.Lookup("query") diff --git a/test/go.mod b/test/go.mod index 27bbb0aa6..c3b6dedbc 100644 --- a/test/go.mod +++ b/test/go.mod @@ -53,7 +53,7 @@ require ( sigs.k8s.io/yaml v1.3.0 // indirect ) -go 1.21 +go 1.22 replace github.com/linode/linodego => ../ diff --git a/test/integration/account_invoice_test.go b/test/integration/account_invoice_test.go index 16c1f8751..a02b83ab1 100644 --- a/test/integration/account_invoice_test.go +++ b/test/integration/account_invoice_test.go @@ -1,44 +1,44 @@ package integration import ( - "context" - "testing" + "context" + "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/require" ) func TestInvoice_List(t *testing.T) { - warnSensitiveTest(t) - client, teardown := createTestClient(t, "fixtures/TestInvoice_List") - defer teardown() + warnSensitiveTest(t) + client, teardown := createTestClient(t, "fixtures/TestInvoice_List") + defer teardown() - invoices, err := client.ListInvoices(context.Background(), nil) - require.NoError(t, err, "Error getting Invoices, expected struct") - require.NotEmpty(t, invoices, "Expected to see invoices returned") + invoices, err := client.ListInvoices(context.Background(), nil) + require.NoError(t, err, "Error getting Invoices, expected struct") + require.NotEmpty(t, invoices, "Expected to see invoices returned") } func TestInvoice_Get(t *testing.T) { - warnSensitiveTest(t) - client, teardown := createTestClient(t, "fixtures/TestInvoice_Get") - defer teardown() - - invoice, err := client.GetInvoice(context.Background(), 123) - require.NoError(t, err, "Error getting Invoice, expected struct") - require.Equal(t, 123, invoice.ID, "Expected Invoice ID to be 123") - require.Equal(t, "Invoice", invoice.Label, "Expected Invoice Label to be 'Invoice'") - require.Equal(t, 132.5, float64(invoice.Total), "Expected Invoice Total to be 132.5") + warnSensitiveTest(t) + client, teardown := createTestClient(t, "fixtures/TestInvoice_Get") + defer teardown() + + invoice, err := client.GetInvoice(context.Background(), 123) + require.NoError(t, err, "Error getting Invoice, expected struct") + require.Equal(t, 123, invoice.ID, "Expected Invoice ID to be 123") + require.Equal(t, "Invoice", invoice.Label, "Expected Invoice Label to be 'Invoice'") + require.Equal(t, 132.5, float64(invoice.Total), "Expected Invoice Total to be 132.5") } func TestInvoiceItems_List(t *testing.T) { - warnSensitiveTest(t) - client, teardown := createTestClient(t, "fixtures/TestInvoiceItems_List") - defer teardown() - - items, err := client.ListInvoiceItems(context.Background(), 123, nil) - require.NoError(t, err, "Error getting Invoice Items, expected struct") - require.NotEmpty(t, items, "Expected to see invoice items returned") - - item := items[0] - require.Equal(t, "Linode 2GB", item.Label, "Expected item label to be 'Linode 2GB'") - require.Equal(t, 10.0, float64(item.Amount), "Expected item amount to be 10") -} \ No newline at end of file + warnSensitiveTest(t) + client, teardown := createTestClient(t, "fixtures/TestInvoiceItems_List") + defer teardown() + + items, err := client.ListInvoiceItems(context.Background(), 123, nil) + require.NoError(t, err, "Error getting Invoice Items, expected struct") + require.NotEmpty(t, items, "Expected to see invoice items returned") + + item := items[0] + require.Equal(t, "Linode 2GB", item.Label, "Expected item label to be 'Linode 2GB'") + require.Equal(t, 10.0, float64(item.Amount), "Expected item amount to be 10") +} diff --git a/test/integration/account_settings_test.go b/test/integration/account_settings_test.go index a14d86cd6..ecba9393d 100644 --- a/test/integration/account_settings_test.go +++ b/test/integration/account_settings_test.go @@ -1,79 +1,79 @@ package integration import ( - "context" - "encoding/json" - "testing" + "context" + "encoding/json" + "testing" - "github.com/jarcoal/httpmock" - "github.com/linode/linodego" - "github.com/stretchr/testify/require" + "github.com/jarcoal/httpmock" + "github.com/linode/linodego" + "github.com/stretchr/testify/require" ) func TestAccountSettings_Get(t *testing.T) { - client, teardown := createTestClient(t, "fixtures/TestAccountSettings") - defer teardown() - - // Mocking the API response - httpmock.Activate() - defer httpmock.DeactivateAndReset() - - mockSettings := linodego.AccountSettings{ - BackupsEnabled: true, - Managed: true, - NetworkHelper: true, - LongviewSubscription: String("longview-3"), - ObjectStorage: String("active"), - } - mockResponse, _ := json.Marshal(mockSettings) - - httpmock.RegisterResponder("GET", "https://api.linode.com/v4/account/settings", - httpmock.NewStringResponder(200, string(mockResponse))) - - settings, err := client.GetAccountSettings(context.Background()) - require.NoError(t, err, "Error getting Account Settings") - - require.True(t, settings.BackupsEnabled, "Expected BackupsEnabled to be true") - require.True(t, settings.Managed, "Expected Managed to be true") - require.True(t, settings.NetworkHelper, "Expected NetworkHelper to be true") - require.NotNil(t, settings.LongviewSubscription, "Expected LongviewSubscription to be non-nil") - require.Equal(t, "longview-3", *settings.LongviewSubscription, "Expected LongviewSubscription to be 'longview-3'") - require.NotNil(t, settings.ObjectStorage, "Expected ObjectStorage to be non-nil") - require.Equal(t, "active", *settings.ObjectStorage, "Expected ObjectStorage to be 'active'") + client, teardown := createTestClient(t, "fixtures/TestAccountSettings") + defer teardown() + + // Mocking the API response + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + mockSettings := linodego.AccountSettings{ + BackupsEnabled: true, + Managed: true, + NetworkHelper: true, + LongviewSubscription: String("longview-3"), + ObjectStorage: String("active"), + } + mockResponse, _ := json.Marshal(mockSettings) + + httpmock.RegisterResponder("GET", "https://api.linode.com/v4/account/settings", + httpmock.NewStringResponder(200, string(mockResponse))) + + settings, err := client.GetAccountSettings(context.Background()) + require.NoError(t, err, "Error getting Account Settings") + + require.True(t, settings.BackupsEnabled, "Expected BackupsEnabled to be true") + require.True(t, settings.Managed, "Expected Managed to be true") + require.True(t, settings.NetworkHelper, "Expected NetworkHelper to be true") + require.NotNil(t, settings.LongviewSubscription, "Expected LongviewSubscription to be non-nil") + require.Equal(t, "longview-3", *settings.LongviewSubscription, "Expected LongviewSubscription to be 'longview-3'") + require.NotNil(t, settings.ObjectStorage, "Expected ObjectStorage to be non-nil") + require.Equal(t, "active", *settings.ObjectStorage, "Expected ObjectStorage to be 'active'") } func TestAccountSettings_Update(t *testing.T) { - client, teardown := createTestClient(t, "fixtures/TestAccountSettings") - defer teardown() - - // Mocking the API response - httpmock.Activate() - defer httpmock.DeactivateAndReset() - - opts := linodego.AccountSettingsUpdateOptions{ - BackupsEnabled: Bool(false), - LongviewSubscription: String("longview-10"), - NetworkHelper: Bool(false), - } - - mockSettings := linodego.AccountSettings{ - BackupsEnabled: false, - NetworkHelper: false, - LongviewSubscription: String("longview-10"), - } - mockResponse, _ := json.Marshal(mockSettings) - - httpmock.RegisterResponder("PUT", "https://api.linode.com/v4/account/settings", - httpmock.NewStringResponder(200, string(mockResponse))) - - settings, err := client.UpdateAccountSettings(context.Background(), opts) - require.NoError(t, err, "Error updating Account Settings") - - require.False(t, settings.BackupsEnabled, "Expected BackupsEnabled to be false") - require.False(t, settings.NetworkHelper, "Expected NetworkHelper to be false") - require.NotNil(t, settings.LongviewSubscription, "Expected LongviewSubscription to be non-nil") - require.Equal(t, "longview-10", *settings.LongviewSubscription, "Expected LongviewSubscription to be 'longview-10'") + client, teardown := createTestClient(t, "fixtures/TestAccountSettings") + defer teardown() + + // Mocking the API response + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + opts := linodego.AccountSettingsUpdateOptions{ + BackupsEnabled: Bool(false), + LongviewSubscription: String("longview-10"), + NetworkHelper: Bool(false), + } + + mockSettings := linodego.AccountSettings{ + BackupsEnabled: false, + NetworkHelper: false, + LongviewSubscription: String("longview-10"), + } + mockResponse, _ := json.Marshal(mockSettings) + + httpmock.RegisterResponder("PUT", "https://api.linode.com/v4/account/settings", + httpmock.NewStringResponder(200, string(mockResponse))) + + settings, err := client.UpdateAccountSettings(context.Background(), opts) + require.NoError(t, err, "Error updating Account Settings") + + require.False(t, settings.BackupsEnabled, "Expected BackupsEnabled to be false") + require.False(t, settings.NetworkHelper, "Expected NetworkHelper to be false") + require.NotNil(t, settings.LongviewSubscription, "Expected LongviewSubscription to be non-nil") + require.Equal(t, "longview-10", *settings.LongviewSubscription, "Expected LongviewSubscription to be 'longview-10'") } -func Bool(v bool) *bool { return &v } -func String(v string) *string { return &v } \ No newline at end of file +func Bool(v bool) *bool { return &v } +func String(v string) *string { return &v } diff --git a/test/integration/account_transfer_test.go b/test/integration/account_transfer_test.go index 7490e39c7..dbad6d480 100644 --- a/test/integration/account_transfer_test.go +++ b/test/integration/account_transfer_test.go @@ -1,29 +1,29 @@ package integration import ( - "context" - "testing" + "context" + "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/require" ) func TestAccountTransfer_Get(t *testing.T) { - client, teardown := createTestClient(t, "fixtures/TestAccountTransfer_Get") - defer teardown() + client, teardown := createTestClient(t, "fixtures/TestAccountTransfer_Get") + defer teardown() - transfer, err := client.GetAccountTransfer(context.Background()) - require.NoError(t, err, "Error getting Account Transfer, expected struct") + transfer, err := client.GetAccountTransfer(context.Background()) + require.NoError(t, err, "Error getting Account Transfer, expected struct") - require.NotEqual(t, 0, transfer.Billable, "Expected non-zero value for Billable") - require.NotEqual(t, 0, transfer.Quota, "Expected non-zero value for Quota") - require.NotEqual(t, 0, transfer.Used, "Expected non-zero value for Used") + require.NotEqual(t, 0, transfer.Billable, "Expected non-zero value for Billable") + require.NotEqual(t, 0, transfer.Quota, "Expected non-zero value for Quota") + require.NotEqual(t, 0, transfer.Used, "Expected non-zero value for Used") - require.NotEmpty(t, transfer.RegionTransfers, "Expected to see region transfers") + require.NotEmpty(t, transfer.RegionTransfers, "Expected to see region transfers") - for _, regionTransfer := range transfer.RegionTransfers { - require.NotEmpty(t, regionTransfer.ID, "Expected region ID to be non-empty") - require.NotEqual(t, 0, regionTransfer.Billable, "Expected non-zero value for Billable in region %s", regionTransfer.ID) - require.NotEqual(t, 0, regionTransfer.Quota, "Expected non-zero value for Quota in region %s", regionTransfer.ID) - require.NotEqual(t, 0, regionTransfer.Used, "Expected non-zero value for Used in region %s", regionTransfer.ID) - } -} \ No newline at end of file + for _, regionTransfer := range transfer.RegionTransfers { + require.NotEmpty(t, regionTransfer.ID, "Expected region ID to be non-empty") + require.NotEqual(t, 0, regionTransfer.Billable, "Expected non-zero value for Billable in region %s", regionTransfer.ID) + require.NotEqual(t, 0, regionTransfer.Quota, "Expected non-zero value for Quota in region %s", regionTransfer.ID) + require.NotEqual(t, 0, regionTransfer.Used, "Expected non-zero value for Used in region %s", regionTransfer.ID) + } +} diff --git a/test/integration/integration_suite_test.go b/test/integration/integration_suite_test.go index 00ed65301..b55924c61 100644 --- a/test/integration/integration_suite_test.go +++ b/test/integration/integration_suite_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "log/slog" "net/http" "os" "regexp" @@ -18,7 +19,6 @@ import ( "github.com/dnaeon/go-vcr/recorder" "github.com/linode/linodego" "golang.org/x/oauth2" - "log/slog" "k8s.io/client-go/transport" ) @@ -63,17 +63,17 @@ func init() { } func warnSensitiveTest(t *testing.T) { - if testingMode == recorder.ModeReplaying { - return - } - - slog.Warn( - fmt.Sprintf( - "Test %s is a sensitive test. Ensure you validate and sanitize "+ - "its generated test fixtures before pushing.", - t.Name(), - ), - ) + if testingMode == recorder.ModeReplaying { + return + } + + slog.Warn( + fmt.Sprintf( + "Test %s is a sensitive test. Ensure you validate and sanitize "+ + "its generated test fixtures before pushing.", + t.Name(), + ), + ) } // testRecorder returns a go-vcr recorder and an associated function that the caller must defer diff --git a/test/integration/network_pools_test.go b/test/integration/network_pools_test.go index 335348e35..dc6a0daee 100644 --- a/test/integration/network_pools_test.go +++ b/test/integration/network_pools_test.go @@ -1,31 +1,31 @@ package integration import ( - "context" - "testing" + "context" + "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/require" ) func TestIPv6Pool_List(t *testing.T) { - client, teardown := createTestClient(t, "fixtures/TestIPv6Pool_List") - defer teardown() + client, teardown := createTestClient(t, "fixtures/TestIPv6Pool_List") + defer teardown() - ipv6Pools, err := client.ListIPv6Pools(context.Background(), nil) - require.NoError(t, err, "Error getting IPv6 Pools, expected struct") + ipv6Pools, err := client.ListIPv6Pools(context.Background(), nil) + require.NoError(t, err, "Error getting IPv6 Pools, expected struct") - require.NotEmpty(t, ipv6Pools, "Expected to see IPv6 pools returned") + require.NotEmpty(t, ipv6Pools, "Expected to see IPv6 pools returned") - require.Equal(t, "2600:3c00::/32", ipv6Pools[0].Range, "Expected IPv6 pool range '2600:3c00::/32'") + require.Equal(t, "2600:3c00::/32", ipv6Pools[0].Range, "Expected IPv6 pool range '2600:3c00::/32'") } func TestIPv6Pool_Get(t *testing.T) { - client, teardown := createTestClient(t, "fixtures/TestIPv6Pool_Get") - defer teardown() + client, teardown := createTestClient(t, "fixtures/TestIPv6Pool_Get") + defer teardown() - ipv6Pool, err := client.GetIPv6Pool(context.Background(), "2600:3c00::/32") - require.NoError(t, err, "Error getting IPv6 Pool, expected struct") + ipv6Pool, err := client.GetIPv6Pool(context.Background(), "2600:3c00::/32") + require.NoError(t, err, "Error getting IPv6 Pool, expected struct") - require.Equal(t, "2600:3c00::/32", ipv6Pool.Range, "Expected IPv6 pool range '2600:3c00::/32'") - require.Equal(t, "us-east", ipv6Pool.Region, "Expected IPv6 pool region 'us-east'") + require.Equal(t, "2600:3c00::/32", ipv6Pool.Range, "Expected IPv6 pool range '2600:3c00::/32'") + require.Equal(t, "us-east", ipv6Pool.Region, "Expected IPv6 pool region 'us-east'") } diff --git a/test/integration/profile_security_question_test.go b/test/integration/profile_security_question_test.go index 48c15adbd..30520eca3 100644 --- a/test/integration/profile_security_question_test.go +++ b/test/integration/profile_security_question_test.go @@ -1,22 +1,22 @@ package integration import ( - "context" - "testing" + "context" + "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/require" ) func TestSecurityQuestions_List(t *testing.T) { - warnSensitiveTest(t) + warnSensitiveTest(t) - client, teardown := createTestClient(t, "fixtures/TestSecurityQuestions_List") - defer teardown() + client, teardown := createTestClient(t, "fixtures/TestSecurityQuestions_List") + defer teardown() - questions, err := client.SecurityQuestionsList(context.Background()) - require.NoError(t, err, "Error getting security questions, expected struct") + questions, err := client.SecurityQuestionsList(context.Background()) + require.NoError(t, err, "Error getting security questions, expected struct") - require.NotEmpty(t, questions.SecurityQuestions, "Expected to see security questions returned") + require.NotEmpty(t, questions.SecurityQuestions, "Expected to see security questions returned") - require.Equal(t, "What was the name of your first pet?", questions.SecurityQuestions[0].Question, "Expected question 'What was the name of your first pet?'") + require.Equal(t, "What was the name of your first pet?", questions.SecurityQuestions[0].Question, "Expected question 'What was the name of your first pet?'") } diff --git a/test/integration/support_ticket_test.go b/test/integration/support_ticket_test.go index 31fd0457d..0857af261 100644 --- a/test/integration/support_ticket_test.go +++ b/test/integration/support_ticket_test.go @@ -1,33 +1,33 @@ package integration import ( - "context" - "testing" + "context" + "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/require" ) func TestTicket_List(t *testing.T) { - warnSensitiveTest(t) - client, teardown := createTestClient(t, "fixtures/TestTicket_List") - defer teardown() + warnSensitiveTest(t) + client, teardown := createTestClient(t, "fixtures/TestTicket_List") + defer teardown() - tickets, err := client.ListTickets(context.Background(), nil) - require.NoError(t, err, "Error getting Tickets, expected struct") + tickets, err := client.ListTickets(context.Background(), nil) + require.NoError(t, err, "Error getting Tickets, expected struct") - require.NotEmpty(t, tickets, "Expected to see tickets returned") + require.NotEmpty(t, tickets, "Expected to see tickets returned") - require.Equal(t, 123, tickets[0].ID, "Expected ticket ID 123") + require.Equal(t, 123, tickets[0].ID, "Expected ticket ID 123") } func TestTicket_Get(t *testing.T) { - warnSensitiveTest(t) - client, teardown := createTestClient(t, "fixtures/TestTicket_Get") - defer teardown() + warnSensitiveTest(t) + client, teardown := createTestClient(t, "fixtures/TestTicket_Get") + defer teardown() - ticket, err := client.GetTicket(context.Background(), 123) - require.NoError(t, err, "Error getting Ticket, expected struct") + ticket, err := client.GetTicket(context.Background(), 123) + require.NoError(t, err, "Error getting Ticket, expected struct") - require.Equal(t, 123, ticket.ID, "Expected ticket ID 123") - require.Equal(t, "Test description", ticket.Description, "Expected ticket description 'Test description'") + require.Equal(t, 123, ticket.ID, "Expected ticket ID 123") + require.Equal(t, "Test description", ticket.Description, "Expected ticket description 'Test description'") } diff --git a/waitfor.go b/waitfor.go index df2f484f7..e65e491a9 100644 --- a/waitfor.go +++ b/waitfor.go @@ -76,7 +76,6 @@ func (client Client) WaitForInstanceDiskStatus(ctx context.Context, instanceID i } for _, disk := range disks { - disk := disk if disk.ID == diskID { complete := (disk.Status == status) if complete {