Skip to content

Commit

Permalink
fix integration tests (#3311)
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmcosta authored Feb 12, 2025
1 parent b41de7f commit d48edb9
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 195 deletions.
29 changes: 17 additions & 12 deletions management/client/rest/accounts_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package rest
//go:build integration
// +build integration

package rest_test

import (
"context"
Expand All @@ -7,10 +10,12 @@ import (
"net/http"
"testing"

"github.com/netbirdio/netbird/management/server/http/api"
"github.com/netbirdio/netbird/management/server/http/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/netbirdio/netbird/management/client/rest"
"github.com/netbirdio/netbird/management/server/http/api"
"github.com/netbirdio/netbird/management/server/http/util"
)

var (
Expand All @@ -33,7 +38,7 @@ var (
)

func TestAccounts_List_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/accounts", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal([]api.Account{testAccount})
_, err := w.Write(retBytes)
Expand All @@ -47,7 +52,7 @@ func TestAccounts_List_200(t *testing.T) {
}

func TestAccounts_List_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/accounts", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "No", Code: 400})
w.WriteHeader(400)
Expand All @@ -62,7 +67,7 @@ func TestAccounts_List_Err(t *testing.T) {
}

func TestAccounts_Update_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/accounts/Test", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "PUT", r.Method)
reqBytes, err := io.ReadAll(r.Body)
Expand All @@ -87,7 +92,7 @@ func TestAccounts_Update_200(t *testing.T) {
}

func TestAccounts_Update_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/accounts/Test", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "No", Code: 400})
w.WriteHeader(400)
Expand All @@ -106,7 +111,7 @@ func TestAccounts_Update_Err(t *testing.T) {
}

func TestAccounts_Delete_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/accounts/Test", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "DELETE", r.Method)
w.WriteHeader(200)
Expand All @@ -117,7 +122,7 @@ func TestAccounts_Delete_200(t *testing.T) {
}

func TestAccounts_Delete_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/accounts/Test", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "Not found", Code: 404})
w.WriteHeader(404)
Expand All @@ -131,7 +136,7 @@ func TestAccounts_Delete_Err(t *testing.T) {
}

func TestAccounts_Integration_List(t *testing.T) {
withBlackBoxServer(t, func(c *Client) {
withBlackBoxServer(t, func(c *rest.Client) {
accounts, err := c.Accounts.List(context.Background())
require.NoError(t, err)
assert.Len(t, accounts, 1)
Expand All @@ -141,7 +146,7 @@ func TestAccounts_Integration_List(t *testing.T) {
}

func TestAccounts_Integration_Update(t *testing.T) {
withBlackBoxServer(t, func(c *Client) {
withBlackBoxServer(t, func(c *rest.Client) {
accounts, err := c.Accounts.List(context.Background())
require.NoError(t, err)
assert.Len(t, accounts, 1)
Expand All @@ -157,7 +162,7 @@ func TestAccounts_Integration_Update(t *testing.T) {

// Account deletion on MySQL and PostgreSQL databases causes unknown errors
// func TestAccounts_Integration_Delete(t *testing.T) {
// withBlackBoxServer(t, func(c *Client) {
// withBlackBoxServer(t, func(c *rest.Client) {
// accounts, err := c.Accounts.List(context.Background())
// require.NoError(t, err)
// assert.Len(t, accounts, 1)
Expand Down
14 changes: 9 additions & 5 deletions management/client/rest/client_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
package rest
//go:build integration
// +build integration

package rest_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/netbirdio/netbird/management/client/rest"
"github.com/netbirdio/netbird/management/server/http/testing/testing_tools"
)

func withMockClient(callback func(*Client, *http.ServeMux)) {
func withMockClient(callback func(*rest.Client, *http.ServeMux)) {
mux := &http.ServeMux{}
server := httptest.NewServer(mux)
defer server.Close()
c := New(server.URL, "ABC")
c := rest.New(server.URL, "ABC")
callback(c, mux)
}

func ptr[T any, PT *T](x T) PT {
return &x
}

func withBlackBoxServer(t *testing.T, callback func(*Client)) {
func withBlackBoxServer(t *testing.T, callback func(*rest.Client)) {
t.Helper()
handler, _, _ := testing_tools.BuildApiBlackBoxWithDBState(t, "../../server/testdata/store.sql", nil, false)
server := httptest.NewServer(handler)
defer server.Close()
c := New(server.URL, "nbp_apTmlmUXHSC4PKmHwtIZNaGr8eqcVI2gMURp")
c := rest.New(server.URL, "nbp_apTmlmUXHSC4PKmHwtIZNaGr8eqcVI2gMURp")
callback(c)
}
41 changes: 23 additions & 18 deletions management/client/rest/dns_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package rest
//go:build integration
// +build integration

package rest_test

import (
"context"
Expand All @@ -7,10 +10,12 @@ import (
"net/http"
"testing"

"github.com/netbirdio/netbird/management/server/http/api"
"github.com/netbirdio/netbird/management/server/http/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/netbirdio/netbird/management/client/rest"
"github.com/netbirdio/netbird/management/server/http/api"
"github.com/netbirdio/netbird/management/server/http/util"
)

var (
Expand All @@ -25,7 +30,7 @@ var (
)

func TestDNSNameserverGroup_List_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal([]api.NameserverGroup{testNameserverGroup})
_, err := w.Write(retBytes)
Expand All @@ -39,7 +44,7 @@ func TestDNSNameserverGroup_List_200(t *testing.T) {
}

func TestDNSNameserverGroup_List_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "No", Code: 400})
w.WriteHeader(400)
Expand All @@ -54,7 +59,7 @@ func TestDNSNameserverGroup_List_Err(t *testing.T) {
}

func TestDNSNameserverGroup_Get_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers/Test", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(testNameserverGroup)
_, err := w.Write(retBytes)
Expand All @@ -67,7 +72,7 @@ func TestDNSNameserverGroup_Get_200(t *testing.T) {
}

func TestDNSNameserverGroup_Get_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers/Test", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "No", Code: 400})
w.WriteHeader(400)
Expand All @@ -82,7 +87,7 @@ func TestDNSNameserverGroup_Get_Err(t *testing.T) {
}

func TestDNSNameserverGroup_Create_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
reqBytes, err := io.ReadAll(r.Body)
Expand All @@ -104,7 +109,7 @@ func TestDNSNameserverGroup_Create_200(t *testing.T) {
}

func TestDNSNameserverGroup_Create_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "No", Code: 400})
w.WriteHeader(400)
Expand All @@ -121,7 +126,7 @@ func TestDNSNameserverGroup_Create_Err(t *testing.T) {
}

func TestDNSNameserverGroup_Update_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers/Test", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "PUT", r.Method)
reqBytes, err := io.ReadAll(r.Body)
Expand All @@ -143,7 +148,7 @@ func TestDNSNameserverGroup_Update_200(t *testing.T) {
}

func TestDNSNameserverGroup_Update_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers/Test", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "No", Code: 400})
w.WriteHeader(400)
Expand All @@ -160,7 +165,7 @@ func TestDNSNameserverGroup_Update_Err(t *testing.T) {
}

func TestDNSNameserverGroup_Delete_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers/Test", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "DELETE", r.Method)
w.WriteHeader(200)
Expand All @@ -171,7 +176,7 @@ func TestDNSNameserverGroup_Delete_200(t *testing.T) {
}

func TestDNSNameserverGroup_Delete_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/nameservers/Test", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "Not found", Code: 404})
w.WriteHeader(404)
Expand All @@ -185,7 +190,7 @@ func TestDNSNameserverGroup_Delete_Err(t *testing.T) {
}

func TestDNSSettings_Get_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/settings", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(testSettings)
_, err := w.Write(retBytes)
Expand All @@ -198,7 +203,7 @@ func TestDNSSettings_Get_200(t *testing.T) {
}

func TestDNSSettings_Get_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/settings", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "No", Code: 400})
w.WriteHeader(400)
Expand All @@ -213,7 +218,7 @@ func TestDNSSettings_Get_Err(t *testing.T) {
}

func TestDNSSettings_Update_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/settings", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "PUT", r.Method)
reqBytes, err := io.ReadAll(r.Body)
Expand All @@ -235,7 +240,7 @@ func TestDNSSettings_Update_200(t *testing.T) {
}

func TestDNSSettings_Update_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/dns/settings", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "No", Code: 400})
w.WriteHeader(400)
Expand Down Expand Up @@ -267,7 +272,7 @@ func TestDNS_Integration(t *testing.T) {
Primary: true,
SearchDomainsEnabled: false,
}
withBlackBoxServer(t, func(c *Client) {
withBlackBoxServer(t, func(c *rest.Client) {
// Create
nsGroup, err := c.DNS.CreateNameserverGroup(context.Background(), nsGroupReq)
require.NoError(t, err)
Expand Down
17 changes: 11 additions & 6 deletions management/client/rest/events_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package rest
//go:build integration
// +build integration

package rest_test

import (
"context"
"encoding/json"
"net/http"
"testing"

"github.com/netbirdio/netbird/management/server/http/api"
"github.com/netbirdio/netbird/management/server/http/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/netbirdio/netbird/management/client/rest"
"github.com/netbirdio/netbird/management/server/http/api"
"github.com/netbirdio/netbird/management/server/http/util"
)

var (
Expand All @@ -20,7 +25,7 @@ var (
)

func TestEvents_List_200(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/events", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal([]api.Event{testEvent})
_, err := w.Write(retBytes)
Expand All @@ -34,7 +39,7 @@ func TestEvents_List_200(t *testing.T) {
}

func TestEvents_List_Err(t *testing.T) {
withMockClient(func(c *Client, mux *http.ServeMux) {
withMockClient(func(c *rest.Client, mux *http.ServeMux) {
mux.HandleFunc("/api/events", func(w http.ResponseWriter, r *http.Request) {
retBytes, _ := json.Marshal(util.ErrorResponse{Message: "No", Code: 400})
w.WriteHeader(400)
Expand All @@ -49,7 +54,7 @@ func TestEvents_List_Err(t *testing.T) {
}

func TestEvents_Integration(t *testing.T) {
withBlackBoxServer(t, func(c *Client) {
withBlackBoxServer(t, func(c *rest.Client) {
// Do something that would trigger any event
_, err := c.SetupKeys.Create(context.Background(), api.CreateSetupKeyRequest{
Ephemeral: ptr(true),
Expand Down
Loading

0 comments on commit d48edb9

Please sign in to comment.